一、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的值在配置文件中不能使用变量,需要在实际使用手动修改或者配合脚本进行变更
expire_logs_days废弃警告 :
若使用 MySQL ≥8.0.35,应替换为:
ini
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 补全命令
bash
mysql --auto-rehash -u user -p
--auto-rehash参数的主要功能就是自动补全
- 使用
no-auto-rehash的好处:
连接瞬间完成,无额外查询,体验更流畅,可以避免无意义的元数据拉取,提升效率
5.配置文件 [mysql] 段设置的参数,只影响使用 mysql 命令行工具的行为,无法用 SHOW VARIABLES LIKE 'xxx' 命令查到
因为这些参数是客户端选项,不是服务端系统变量
二、完整版配置文件
bash
[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