当前位置: 代码网 > it编程>数据库>Mysql > MySQL 配置参数全解之参数含义、作用域与运维建议(完整配置文件)

MySQL 配置参数全解之参数含义、作用域与运维建议(完整配置文件)

2026年01月25日 Mysql 我要评论
一、mysql 配置文件参数解析表配置段参数名含义说明当前值默认值[client]loose-default-character-set客户端连接默认字符集;loose- 前缀表示若当前 mysql

一、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 传输。64m64m
[mysqld]user启动 mysqld 进程的操作系统用户,必须对 datadir 有读写权限。mysqlmysql(linux 包安装)
[mysqld]portmysql 监听的 tcp 端口。33063306
[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)。onoff
[mysqld]basedirmysql 安装根目录。/usr依赖安装方式
[mysqld]datadir数据库数据文件存储目录。/var/lib/mysql/usr/local/mysql/data(源码)或 /var/lib/mysql(包管理)
[mysqld]tmpdir临时文件(如排序、临时表)存储目录。/tmp/tmp
[mysqld]log_error错误日志文件路径。/var/log/mysql/error.logstderr 或 syslog(未指定时)
[mysqld]pid-file记录 mysql 主进程 id 的文件路径。/var/run/mysqld/mysqld.pid$ {datadir}/host_name.pid
[mysqld]max_connections允许的最大并发连接数。512151
[mysqld]max_connect_errors同一主机连接失败超此后被屏蔽,需 flush hosts 解除。100000100
[mysqld]back_log操作系统在 mysql 处理前可排队的连接请求数(tcp listen backlog)。51250 + (max_connections / 5),上限 900
[mysqld]wait_timeout非交互连接空闲超时时间(秒),超时后断开。2880028800
[mysqld]interactive_timeout交互式连接(如 cli)空闲超时时间(秒)。180028800
[mysqld]key_buffer_sizemyisam 引擎索引缓存大小;即使不用 myisam 也建议保留少量。32m8m
[mysqld]sort_buffer_size每个连接用于 order bygroup by 的排序内存,按需分配。4m256k
[mysqld]join_buffer_size用于无索引 join 的连接缓冲区大小,每个 join 分配一份。4m256k
[mysqld]read_buffer_size顺序扫描(如全表扫描)时的缓冲区大小。1m128k
[mysqld]read_rnd_buffer_size随机读取(如按排序结果回表)的缓冲区大小。2m256k
[mysqld]max_allowed_packet服务器允许的最大数据包大小,需 ≥ 客户端设置。64m64m
[mysqld]thread_cache_size线程缓存大小,缓存已断开连接的线程以加速新连接复用。64-1(自动)或 0(旧版本)
[mysqld]table_open_cache表描述符缓存数量,影响同时打开表的能力。40964000(8.0+),2000(5.7)
[mysqld]table_definition_cache表定义(.frm 等元数据)缓存数量,减少磁盘 io。40962000(或自动计算)
[mysqld]open_files_limit操作系统允许 mysql 打开的最大文件数(需系统 ulimit 支持)。65535min(5000, ulimit -n)
[mysqld]character-set-server服务端默认字符集。utf8mb4utf8mb4
[mysqld]collation-server服务端默认排序规则。utf8mb4_unicode_ciutf8mb4_0900_ai_ci(8.0)
[mysqld]log_error_verbosity错误日志详细级别(1=error, 2=+warning, 3=+note)。23(8.0+),2(5.7)
[mysqld]slow_query_log开启慢查询日志。onoff
[mysqld]slow_query_log_file慢查询日志文件路径。/var/log/mysql/mysql-slow.loghost_name-slow.log
[mysqld]long_query_timesql 执行时间超过此值(秒)记为慢查询。110.0
[mysqld]log_queries_not_using_indexes是否记录未使用索引的查询到慢日志。offoff
[mysqld]general_log是否开启通用查询日志(记录所有 sql)。offoff
[mysqld]general_log_file通用查询日志文件路径。/var/log/mysql/mysql-general.loghost_name.log
[mysqld]server-id实例唯一标识,主从复制必需,集群内必须唯一。10(禁用复制)
[mysqld]log_bin启用二进制日志并指定路径前缀。/var/log/mysql/mysql-bin.logoff(未设置)
[mysqld]binlog_format二进制日志格式,row 模式最安全,推荐用于复制。rowrow(8.0+),statement(5.7)
[mysqld]expire_logs_days自动删除超过 n 天的 binlog(⚠️ 8.0.35+ 已废弃)。140(不自动清理)
[mysqld]max_binlog_size单个 binlog 文件最大大小,达到后轮转新文件。1g1g
[mysqld]sync_binlog每 n 次事务提交同步 binlog 到磁盘;1=每次提交都刷盘(最安全)。11(8.0+),0(5.7)
[mysqld]binlog_cache_size每个连接事务中 binlog 的内存缓存大小。128k32k
[mysqld]binlog_rows_query_log_eventsrow 格式下是否在 binlog 中记录原始 sql(用于调试)。offoff
[mysqld]innodb_buffer_pool_sizeinnodb 缓冲池大小,缓存数据和索引,是最重要的性能参数。2g128m
[mysqld]innodb_buffer_pool_instances将缓冲池划分为多个实例,减少内部争用;建议每 1gb 缓冲池设 1 个实例。21(<1gb 时),8(≥1gb 且 ≥8.0.29)
[mysqld]innodb_max_dirty_pages_pct脏页占缓冲池比例超过此值时,后台线程加速刷新。7590
[mysqld]default_storage_engine默认存储引擎。innodbinnodb
[mysqld]innodb_file_per_table每张表使用独立 .ibd 文件,便于管理与空间回收。onon
[mysqld]innodb_flush_methodinnodb 数据文件 i/o 方式,o_direct 绕过 os 缓存,避免双缓冲。o_directfsync(linux)
[mysqld]innodb_flush_log_at_trx_commit事务提交时 redo log 刷盘策略;1=最安全(acid),0/2=更高性能但可能丢数据。11
[mysqld]innodb_data_file_pathinnodb 系统表空间初始大小及自动扩展策略。ibdata1:64m:autoextendibdata1:12m:autoextend
[mysqld]innodb_log_file_size单个 redo log 文件大小。2g48m(8.0.30 前),96m(8.0.30+)
[mysqld]innodb_log_files_in_groupredo log 文件数量(通常为 2)。22
[mysqld]innodb_log_buffer_sizeredo log 内存缓冲区大小,大事务可适当增大。16m16m
[mysqld]innodb_lock_wait_timeoutinnodb 行锁等待超时时间(秒),超时后报错。5050
[mysqld]innodb_io_capacity后台任务(刷脏页等)的 i/o 能力基准值。200200
[mysqld]innodb_io_capacity_max后台任务突发 i/o 上限。20002000
[mysqld]innodb_buffer_pool_dump_at_shutdown关闭时将热数据页列表 dump 到磁盘,加速下次启动预热。onon
[mysqld]innodb_buffer_pool_load_at_startup启动时加载之前 dump 的热数据页列表。onon
[mysqld]innodb_print_all_deadlocks将所有死锁信息记录到错误日志,便于分析。onoff
[mysqld]default_authentication_plugin新建用户的默认认证插件,兼容旧客户端。mysql_native_passwordcaching_sha2_password(8.0+)
[mysqld]performance_schema启用性能模式,用于监控内部运行状态。onon
[mysqld]performance_schema_max_table_instances限制 performance_schema 中表实例数量,控制内存使用。5000-1(自动)
[mysqld]gtid_mode启用 gtid(全局事务 id)。onoff
[mysqld]enforce_gtid_consistency强制 gtid 安全语句(禁止非事务性操作)。onoff
[mysqld]log_slave_updates从库是否将 relay log 写入自己的 binlog(级联复制必需)。1off
[mysqld]read_only是否只读模式;从库应设为 1。0off
[mysqld]relay-log从库 relay log 文件路径前缀。/var/log/mysql/slave-relay-binhost_name-relay-bin
[mysqld]relay-log-indexrelay log 索引文件路径/var/log/mysql/slave-relay-bin.indexhost_name-relay-bin.index
[mysqld]master_info_repository主库连接信息存储方式(table 表示存入 mysql.slave_master_info 表)。tablefile(5.7),table(8.0+)
[mysqld]relay_log_info_repositoryrelay log 位置信息存储方式(table 表示存入 mysql.slave_relay_log_info 表)。tablefile(5.7),table(8.0+)
[mysqld]relay_log_recovery启动时自动恢复 relay log,避免数据不一致。onoff
[mysqld]slave_parallel_type并行复制分组策略,logical_clock 基于事务 commit_parent 并行。logical_clockdatabase(5.7),logical_clock(8.0+)
[mysqld]slave_parallel_workers从库并行应用 relay log 的工作线程数。80(串行)
[mysqld_safe]log-errormysqld_safe 启动脚本使用的错误日志路径。/var/log/mysql/error.log同 mysqld
[mysqld_safe]socketmysqld_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_packetmysqldump 客户端允许的最大包大小。256m24m
[mysqldump]single-transaction对 innodb 表使用一致性快照导出(不加锁),推荐用于备份。trueoff
[mysqladmin]character-set-clientmysqladmin 工具连接时的客户端字符集。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配置参数内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2026  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com