一、mysql 配置文件参数解析表
| 配置段 | 参数名 | 含义说明 | 当前值 | 默认值 |
|---|---|---|---|---|
| [client] | loose-default-character-set | 客户端连接默认字符集;loose- 前缀表示若当前 mysql 版本不支持该参数则忽略而不报错。 | utf8mb4 | 无(继承系统 locale) |
| [mysql] | no-auto-rehash | 禁用命令行 tab 自动补全(如表名、列名),避免连接时拉取全量元数据,加快启动速度。 | (启用) | off(即 auto-rehash 默认开启; use --skip-auto-rehash to disable) |
| [mysql] | prompt | 自定义 mysql 命令行提示符格式:\u=用户,\h=主机,\d=数据库,\r:\m:\s=时:分:秒。 | \u@\h:\d\r:\m:\s> | mysql> |
| [mysql] | max_allowed_packet | 客户端与服务器间单个数据包最大大小,影响大结果集或 blob 传输。 | 64m | 64m |
| [mysqld] | user | 启动 mysqld 进程的操作系统用户,必须对 datadir 有读写权限。 | mysql | mysql(linux 包安装) |
| [mysqld] | port | mysql 监听的 tcp 端口。 | 3306 | 3306 |
| [mysqld] | socket | 本地 unix 域套接字路径,用于本地高效连接。 | /var/lib/mysql/mysql.sock | /tmp/mysql.sock(源码编译)或 /var/run/mysqld/mysqld.sock(debian) |
| [mysqld] | bind-address | 监听的网络接口 ip;0.0.0.0 表示监听所有 ipv4 接口。 | 0.0.0.0 | *(所有地址) |
| [mysqld] | event_scheduler | 启用事件调度器,可定期执行 sql 任务(类似 cron)。 | on | off |
| [mysqld] | basedir | mysql 安装根目录。 | /usr | 依赖安装方式 |
| [mysqld] | datadir | 数据库数据文件存储目录。 | /var/lib/mysql | /usr/local/mysql/data(源码)或 /var/lib/mysql(包管理) |
| [mysqld] | tmpdir | 临时文件(如排序、临时表)存储目录。 | /tmp | /tmp |
| [mysqld] | log_error | 错误日志文件路径。 | /var/log/mysql/error.log | stderr 或 syslog(未指定时) |
| [mysqld] | pid-file | 记录 mysql 主进程 id 的文件路径。 | /var/run/mysqld/mysqld.pid | $ {datadir}/host_name.pid |
| [mysqld] | max_connections | 允许的最大并发连接数。 | 512 | 151 |
| [mysqld] | max_connect_errors | 同一主机连接失败超此后被屏蔽,需 flush hosts 解除。 | 100000 | 100 |
| [mysqld] | back_log | 操作系统在 mysql 处理前可排队的连接请求数(tcp listen backlog)。 | 512 | 50 + (max_connections / 5),上限 900 |
| [mysqld] | wait_timeout | 非交互连接空闲超时时间(秒),超时后断开。 | 28800 | 28800 |
| [mysqld] | interactive_timeout | 交互式连接(如 cli)空闲超时时间(秒)。 | 1800 | 28800 |
| [mysqld] | key_buffer_size | myisam 引擎索引缓存大小;即使不用 myisam 也建议保留少量。 | 32m | 8m |
| [mysqld] | sort_buffer_size | 每个连接用于 order by 或 group by 的排序内存,按需分配。 | 4m | 256k |
| [mysqld] | join_buffer_size | 用于无索引 join 的连接缓冲区大小,每个 join 分配一份。 | 4m | 256k |
| [mysqld] | read_buffer_size | 顺序扫描(如全表扫描)时的缓冲区大小。 | 1m | 128k |
| [mysqld] | read_rnd_buffer_size | 随机读取(如按排序结果回表)的缓冲区大小。 | 2m | 256k |
| [mysqld] | max_allowed_packet | 服务器允许的最大数据包大小,需 ≥ 客户端设置。 | 64m | 64m |
| [mysqld] | thread_cache_size | 线程缓存大小,缓存已断开连接的线程以加速新连接复用。 | 64 | -1(自动)或 0(旧版本) |
| [mysqld] | table_open_cache | 表描述符缓存数量,影响同时打开表的能力。 | 4096 | 4000(8.0+),2000(5.7) |
| [mysqld] | table_definition_cache | 表定义(.frm 等元数据)缓存数量,减少磁盘 io。 | 4096 | 2000(或自动计算) |
| [mysqld] | open_files_limit | 操作系统允许 mysql 打开的最大文件数(需系统 ulimit 支持)。 | 65535 | min(5000, ulimit -n) |
| [mysqld] | character-set-server | 服务端默认字符集。 | utf8mb4 | utf8mb4 |
| [mysqld] | collation-server | 服务端默认排序规则。 | utf8mb4_unicode_ci | utf8mb4_0900_ai_ci(8.0) |
| [mysqld] | log_error_verbosity | 错误日志详细级别(1=error, 2=+warning, 3=+note)。 | 2 | 3(8.0+),2(5.7) |
| [mysqld] | slow_query_log | 开启慢查询日志。 | on | off |
| [mysqld] | slow_query_log_file | 慢查询日志文件路径。 | /var/log/mysql/mysql-slow.log | host_name-slow.log |
| [mysqld] | long_query_time | sql 执行时间超过此值(秒)记为慢查询。 | 1 | 10.0 |
| [mysqld] | log_queries_not_using_indexes | 是否记录未使用索引的查询到慢日志。 | off | off |
| [mysqld] | general_log | 是否开启通用查询日志(记录所有 sql)。 | off | off |
| [mysqld] | general_log_file | 通用查询日志文件路径。 | /var/log/mysql/mysql-general.log | host_name.log |
| [mysqld] | server-id | 实例唯一标识,主从复制必需,集群内必须唯一。 | 1 | 0(禁用复制) |
| [mysqld] | log_bin | 启用二进制日志并指定路径前缀。 | /var/log/mysql/mysql-bin.log | off(未设置) |
| [mysqld] | binlog_format | 二进制日志格式,row 模式最安全,推荐用于复制。 | row | row(8.0+),statement(5.7) |
| [mysqld] | expire_logs_days | 自动删除超过 n 天的 binlog(⚠️ 8.0.35+ 已废弃)。 | 14 | 0(不自动清理) |
| [mysqld] | max_binlog_size | 单个 binlog 文件最大大小,达到后轮转新文件。 | 1g | 1g |
| [mysqld] | sync_binlog | 每 n 次事务提交同步 binlog 到磁盘;1=每次提交都刷盘(最安全)。 | 1 | 1(8.0+),0(5.7) |
| [mysqld] | binlog_cache_size | 每个连接事务中 binlog 的内存缓存大小。 | 128k | 32k |
| [mysqld] | binlog_rows_query_log_events | row 格式下是否在 binlog 中记录原始 sql(用于调试)。 | off | off |
| [mysqld] | innodb_buffer_pool_size | innodb 缓冲池大小,缓存数据和索引,是最重要的性能参数。 | 2g | 128m |
| [mysqld] | innodb_buffer_pool_instances | 将缓冲池划分为多个实例,减少内部争用;建议每 1gb 缓冲池设 1 个实例。 | 2 | 1(<1gb 时),8(≥1gb 且 ≥8.0.29) |
| [mysqld] | innodb_max_dirty_pages_pct | 脏页占缓冲池比例超过此值时,后台线程加速刷新。 | 75 | 90 |
| [mysqld] | default_storage_engine | 默认存储引擎。 | innodb | innodb |
| [mysqld] | innodb_file_per_table | 每张表使用独立 .ibd 文件,便于管理与空间回收。 | on | on |
| [mysqld] | innodb_flush_method | innodb 数据文件 i/o 方式,o_direct 绕过 os 缓存,避免双缓冲。 | o_direct | fsync(linux) |
| [mysqld] | innodb_flush_log_at_trx_commit | 事务提交时 redo log 刷盘策略;1=最安全(acid),0/2=更高性能但可能丢数据。 | 1 | 1 |
| [mysqld] | innodb_data_file_path | innodb 系统表空间初始大小及自动扩展策略。 | ibdata1:64m:autoextend | ibdata1:12m:autoextend |
| [mysqld] | innodb_log_file_size | 单个 redo log 文件大小。 | 2g | 48m(8.0.30 前),96m(8.0.30+) |
| [mysqld] | innodb_log_files_in_group | redo log 文件数量(通常为 2)。 | 2 | 2 |
| [mysqld] | innodb_log_buffer_size | redo log 内存缓冲区大小,大事务可适当增大。 | 16m | 16m |
| [mysqld] | innodb_lock_wait_timeout | innodb 行锁等待超时时间(秒),超时后报错。 | 50 | 50 |
| [mysqld] | innodb_io_capacity | 后台任务(刷脏页等)的 i/o 能力基准值。 | 200 | 200 |
| [mysqld] | innodb_io_capacity_max | 后台任务突发 i/o 上限。 | 2000 | 2000 |
| [mysqld] | innodb_buffer_pool_dump_at_shutdown | 关闭时将热数据页列表 dump 到磁盘,加速下次启动预热。 | on | on |
| [mysqld] | innodb_buffer_pool_load_at_startup | 启动时加载之前 dump 的热数据页列表。 | on | on |
| [mysqld] | innodb_print_all_deadlocks | 将所有死锁信息记录到错误日志,便于分析。 | on | off |
| [mysqld] | default_authentication_plugin | 新建用户的默认认证插件,兼容旧客户端。 | mysql_native_password | caching_sha2_password(8.0+) |
| [mysqld] | performance_schema | 启用性能模式,用于监控内部运行状态。 | on | on |
| [mysqld] | performance_schema_max_table_instances | 限制 performance_schema 中表实例数量,控制内存使用。 | 5000 | -1(自动) |
| [mysqld] | gtid_mode | 启用 gtid(全局事务 id)。 | on | off |
| [mysqld] | enforce_gtid_consistency | 强制 gtid 安全语句(禁止非事务性操作)。 | on | off |
| [mysqld] | log_slave_updates | 从库是否将 relay log 写入自己的 binlog(级联复制必需)。 | 1 | off |
| [mysqld] | read_only | 是否只读模式;从库应设为 1。 | 0 | off |
| [mysqld] | relay-log | 从库 relay log 文件路径前缀。 | /var/log/mysql/slave-relay-bin | host_name-relay-bin |
| [mysqld] | relay-log-index | relay log 索引文件路径 | /var/log/mysql/slave-relay-bin.index | host_name-relay-bin.index |
| [mysqld] | master_info_repository | 主库连接信息存储方式(table 表示存入 mysql.slave_master_info 表)。 | table | file(5.7),table(8.0+) |
| [mysqld] | relay_log_info_repository | relay log 位置信息存储方式(table 表示存入 mysql.slave_relay_log_info 表)。 | table | file(5.7),table(8.0+) |
| [mysqld] | relay_log_recovery | 启动时自动恢复 relay log,避免数据不一致。 | on | off |
| [mysqld] | slave_parallel_type | 并行复制分组策略,logical_clock 基于事务 commit_parent 并行。 | logical_clock | database(5.7),logical_clock(8.0+) |
| [mysqld] | slave_parallel_workers | 从库并行应用 relay log 的工作线程数。 | 8 | 0(串行) |
| [mysqld_safe] | log-error | mysqld_safe 启动脚本使用的错误日志路径。 | /var/log/mysql/error.log | 同 mysqld |
| [mysqld_safe] | socket | mysqld_safe 使用的 socket 路径。 | /var/lib/mysql/mysql.sock | 同 mysqld |
| [mysqld_safe] | open-files-limit | 通过 mysqld_safe 启动时设置的系统 open files 限制。 | 65535 | 无(继承 shell) |
| [mysqldump] | quick | 逐行检索结果而非缓存到内存,避免大表导出 oom。 | (启用) | on |
| [mysqldump] | max_allowed_packet | mysqldump 客户端允许的最大包大小。 | 256m | 24m |
| [mysqldump] | single-transaction | 对 innodb 表使用一致性快照导出(不加锁),推荐用于备份。 | true | off |
| [mysqladmin] | character-set-client | mysqladmin 工具连接时的客户端字符集。 | utf8mb4 | 无 |
特别说明
1.loose前缀的作用是允许配置文件中包含对当前 mysql 版本不可用的参数时,mysql 会忽略这些带有loose前缀的未知参数,而不会导致服务器启动失败
2.server-id的值在配置文件中不能使用变量,需要在实际使用手动修改或者配合脚本进行变更
3. expire_logs_days 废弃警告:
若使用 mysql ≥8.0.35,应替换为:
binlog_expire_logs_seconds = 1209600 # 14 * 24 * 3600
binlog_expire_logs_seconds参数是在mysql8.0中新增的参数binlog_expire_logs_seconds参数和expire_logs_days 参数是互相排斥的,在mysql服务器中只能选择其中一个参数使用
4.no-auto-rehash参数解析
- 启用
--auto-rehash的问题:
对于大型数据库实例(成千上万张表),客户端连接后会执行大量 show databases、show tables、describe table 等查询,耗时数秒甚至数十秒
在命令行临时启用: 临时想用 tab 补全命令
mysql --auto-rehash -u user -p
--auto-rehash参数的主要功能就是自动补全
- 使用
no-auto-rehash的好处:
连接瞬间完成,无额外查询,体验更流畅,可以避免无意义的元数据拉取,提升效率
5.配置文件 [mysql] 段设置的参数,只影响使用 mysql 命令行工具的行为,无法用 show variables like ‘xxx’ 命令查到
因为这些参数是客户端选项,不是服务端系统变量
二、完整版配置文件
[client] # client settings - applied to all client programs loose-default-character-set = utf8mb4 # default character set for clients [mysql] # mysql client specific settings no-auto-rehash # disable automatic tab completion for faster connection prompt = "\\u@\\h : \\d \\r:\\m:\\s> " # custom prompt format (user@host:database time) max_allowed_packet = 64m #define the maximum number of bytes for a single data packet transfer between the mysql server and the client [mysqld] # core server settings user = mysql port = 3306 socket = /var/lib/mysql/mysql.sock bind-address = 0.0.0.0 # ip address to bind to (use internal ip in production) event_scheduler = on # enable the event scheduler # directories basedir = /usr datadir = /var/lib/mysql tmpdir = /tmp log_error = /var/log/mysql/error.log pid-file = /var/run/mysqld/mysqld.pid # connections max_connections = 512 # maximum number of simultaneous connections max_connect_errors = 100000 # maximum connection errors before host blocking back_log = 512 # connection backlog queue size wait_timeout = 600 # timeout for non-interactive connections (seconds) interactive_timeout = 1800 # timeout for interactive connections (seconds) wait_timeout = 28800 # memory & cache (adjust based on your ram!) # -- global cache -- key_buffer_size = 32m # key buffer size for myisam (recommended even if not using myisam) # -- per-connection memory -- sort_buffer_size = 4m # buffer size for order by and group by operations join_buffer_size = 4m # buffer size for joins without indexes read_buffer_size = 1m # buffer size for sequential table scans read_rnd_buffer_size = 2m # buffer size for random-read buffer max_allowed_packet = 64m # maximum packet size thread_cache_size = 64 # thread cache size for faster connection handling # table definition table_open_cache = 4096 # number of open tables cache table_definition_cache = 4096 # number of table definition cache open_files_limit = 65535 # maximum number of open files # character set & collation character-set-server = utf8mb4 # default server character set collation-server = utf8mb4_unicode_ci # default server collation # logging # error log log_error = /var/log/mysql/error.log log_error_verbosity = 2 # error log verbosity level # slow query log slow_query_log = on # enable slow query log slow_query_log_file = /var/log/mysql/mysql-slow.log long_query_time = 1 # threshold for slow queries (seconds) log_queries_not_using_indexes = off # don't log queries not using indexes # general query log (usually disabled in production) general_log = off general_log_file = /var/log/mysql/mysql-general.log # binary logging (for replication & point-in-time recovery) server-id = 1 # unique server id for replication log_bin = /var/log/mysql/mysql-bin.log # binary log path binlog_format = row # binary log format (row recommended) expire_logs_days = 14 # binary log expiration period (days) max_binlog_size = 1g # maximum binary log file size sync_binlog = 1 # sync binary log to disk (1=safest) binlog_cache_size = 128k # binary log cache size binlog_rows_query_log_events = off # log original sql in row format (for debugging) # -- innodb buffer pool -- innodb_buffer_pool_size = 2g # most important cache (50-80% of system ram) innodb_buffer_pool_instances = 2 # number of buffer pool instances (1 per gb recommended) innodb_max_dirty_pages_pct = 75 #control the threshold for refreshing dirty pages # file format default_storage_engine = innodb # default storage engine innodb_file_per_table = on # use separate tablespace for each innodb table innodb_flush_method = o_direct # innodb flush method (direct i/o) innodb_flush_log_at_trx_commit = 1 # flush log to disk on transaction commit (1=safest) # innodb specific innodb_data_file_path = ibdata1:64m:autoextend # innodb system tablespace configuration innodb_log_file_size = 2g # innodb redo log file size (recommended 1-2g) innodb_log_files_in_group = 2 # number of innodb redo log files innodb_log_buffer_size = 16m # innodb log buffer size innodb_lock_wait_timeout = 50 # innodb lock wait timeout (seconds) innodb_io_capacity = 200 # innodb i/o capacity (adjust based on storage performance) innodb_io_capacity_max = 2000 # maximum innodb i/o capacity innodb_buffer_pool_dump_at_shutdown = on # dump buffer pool at shutdown innodb_buffer_pool_load_at_startup = on # load buffer pool at startup innodb_print_all_deadlocks = on # log all deadlocks to error log # mysql 8.0 authentication plugin settings default_authentication_plugin = mysql_native_password # maintain compatibility with older clients # performance schema (adjust based on monitoring needs) performance_schema = on # enable performance schema performance_schema_max_table_instances = 5000 # limit performance schema memory usage #gtid gtid_mode=on enforce_gtid_consistency=on # slave variables log_slave_updates =1 #control whether the slave library writes the received changes of the master library to its own binary log read_only = 0 relay-log = /var/log/mysql/slave-relay-bin relay-log-index = /var/log/mysqlslave-relay-bin.index master_info_repository=table # control the storage method of replication status information relay_log_info_repository=table #control the storage method of replication status information relay_log_recovery=on # trigger the automatic recovery process slave_parallel_type=logical_clock #define the transaction grouping policy for parallel replication slave_parallel_workers=8 # set the number of parallel execution threads [mysqld_safe] # mysql safe startup settings log-error = /var/log/mysql/error.log socket = /var/lib/mysql/mysql.sock open-files-limit = 65535 [mysqldump] # mysqldump settings quick max_allowed_packet = 256m single-transaction = true # recommended for innodb backups [mysqladmin] # mysqladmin settings character-set-client = utf8mb4
到此这篇关于mysql 配置参数全解之参数含义、作用域与运维建议(完整配置文件)的文章就介绍到这了,更多相关mysql配置参数内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论