my.cnf 文件
[mysqld]
# 基础设置
user=mysql
pid-file=/var/run/mysqld/mysqld.pid
socket=/var/run/mysqld/mysqld.sock
datadir=/var/lib/mysql
# 配合logrotate定期轮转,避免日志文件过大
log_error=/var/log/mysql/error.log
# 确保MySQL用户有读写权限
tmpdir=/tmp
# 字符集
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
# 连接与线程
# 根据实际负载调整
max_connections=500
# 限制单个用户连接数
max_user_connections=400
# 减少线程创建开销
thread_cache_size=13
max_allowed_packet=64M
table_open_cache=2000
open_files_limit=65535
wait_timeout=28800
interactive_timeout=28800
# 内存优化
# 建议设置为物理内存的50-70%
innodb_buffer_pool_size=8G
# 每个实例至少1G,避免单实例争用
innodb_buffer_pool_instances=4
sort_buffer_size=2M
join_buffer_size=256K
# 排序和临时表
tmp_table_size=256M
max_heap_table_size=256M
# InnoDB日志与I/O
innodb_io_capacity=2000
# 突发I/O上限
innodb_io_capacity_max=4000
innodb_flush_neighbors=0
innodb_log_buffer_size=16M
innodb_flush_log_at_trx_commit=1
innodb_flush_method=O_DIRECT
innodb_file_per_table=1
# 日志与监控
# 记录详细错误(1=错误, 2=错误+警告, 3=所有)
log_error_verbosity=3
slow_query_log=1
slow_query_log_file=/var/log/mysql/slow.log
long_query_time=1
log_queries_not_using_indexes=1
# 启用监控
performance_schema=ON
# 主从复制
gtid_mode=ON
enforce_gtid_consistency=ON
# 避免MINIMAL导致复制问题
binlog_row_image=FULL
# 默认1G,小实例可减小
max_binlog_size=512M
server-id=1
log_bin=mysql-bin
binlog_format=ROW
binlog_expire_logs_seconds=604800
sync_binlog=1
# 安全加固
safe_user_create=1
secure_file_priv=/var/lib/mysql-files
bind-address=0.0.0.0
skip-name-resolve
skip_show_database
local_infile=0
# 备注:以上参数数值以实际情况为准,不可盲目照搬