快乐学习
前程无忧、中华英才非你莫属!

MySQL最佳配置文件

我假设MySQL服务器如下。你应该根据你的服务器调整变量。

  • 32个CPU核心
  • 256G内存
  • 在16K页面大小的情况下具有20000 IOPS的SSD存储

#作者:jiangchengyao@gmail.com

[MySQL的]
prompt = [\\ u @ \\ h] [\\ d]> \\ _

的[mysqld]
# 基本设置 #
user = mysql
sql_mode =“STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION,NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER”
autocommit = 1
被character_set_server = utf8mb4
transaction_isolation = READ-COMMITTED
explicit_defaults_for_timestamp = 1
max_allowed_packet = 16777216
event_scheduler = 1

#连接#
interactive_timeout = 1800
wait_timeout = 1800
lock_wait_timeout = 1800
skip_name_resolve = 1
max_connections = 512
max_connect_errors = 1000000

#表缓存性能设置
table_open_cache = 4096
table_definition_cache = 4096
table_open_cache_instances = 128

#会话记忆设置#
read_buffer_size = 16M
read_rnd_buffer_size = 32M
sort_buffer_size = 32M
tmp_table_size = 64M
join_buffer_size = 128M
thread_cache_size = 64

#日志设置#
log_error = error.log
slow_query_log = 1
slow_query_log_file = slow.log
log_queries_not_using_indexes = 1
log_slow_admin_statements = 1
log_slow_slave_statements = 1
log_throttle_queries_not_using_indexes = 10
expire_logs_days = 90
long_query_time = 2
min_examined_row_limit = 100
binlog-rows-query-log-events = 1
log-bin-trust-function-creators = 1
expire-logs-days = 90
log-slave-updates = 1

#innodb设置#
innodb_page_size = 16384
innodb_buffer_pool_size = 160G
innodb_buffer_pool_instances = 16
innodb_buffer_pool_load_at_startup = 1
innodb_buffer_pool_dump_at_shutdown = 1
innodb_lru_scan_depth = 4096
innodb_lock_wait_timeout = 5
innodb_io_capacity = 10000
innodb_io_capacity_max = 20000
innodb_flush_method = O_DIRECT
innodb_file_format =梭子鱼
innodb_file_format_max =梭子鱼
innodb_undo_logs = 128
innodb_undo_tablespaces = 3
innodb_flush_neighbors = 0
innodb_log_file_size = 17179869184
innodb_log_files_in_group = 2
innodb_log_buffer_size = 16777216
innodb_purge_threads = 4
innodb_large_prefix = 1
innodb_thread_concurrency = 64
innodb_print_all_deadlocks = 1
innodb_strict_mode = 1
innodb_sort_buffer_size = 67108864
innodb_write_io_threads = 16
innodb_read_io_threads = 16 
innodb_file_per_table = 1
innodb_stats_persistent_sample_pages = 64
innodb_autoinc_lock_mode = 2
innodb_online_alter_log_max_size = 1G
innodb_open_files = 4096

#复制设置#
master_info_repository = TABLE
relay_log_info_repository = TABLE
sync_binlog = 1
gtid_mode = on
enforce_gtid_consistency = 1
log_slave_updates
binlog_format = ROW
binlog_rows_query_log_events = 1
relay_log = relay.log
relay_log_recovery = 1
slave_skip_errors = ddl_exist_errors
slave-rows-search-algorithms ='INDEX_SCAN,HASH_SCAN'

#半同步复制设置#
plugin_load =“validate_password.so; rpl_semi_sync_master = semisync_master.so; rpl_semi_sync_slave = semisync_slave.so”
rpl_semi_sync_master_enabled = 1
rpl_semi_sync_master_timeout = 3000
rpl_semi_sync_slave_enabled = 1

#密码插件#
validate_password_policy =强
验证密码= FORCE_PLUS_PERMANENT

的[mysqld-5.6]
#metalock性能设置
metadata_locks_hash_instances = 64

的[mysqld-5.7]
#新的innodb设置#
loose_innodb_numa_interleave = 1
innodb_buffer_pool_dump_pct = 40
innodb_page_cleaners = 16
innodb_undo_log_truncate = 1
innodb_max_undo_log_size = 2G
innodb_purge_rseg_truncate_frequency = 128
#新的复制设置#
slave-parallel-type = LOGICAL_CLOCK
slave-parallel-workers = 16
slave_preserve_commit_order = 1
slave_transaction_retries = 128
#其他更改设置#
binlog_gtid_simple_recovery = 1
log_timestamps =系统
show_compatibility_56 =上

新增加的参数有:

  • metadata_locks_hash_instances = 64 -- 提升大并发下的元数据锁性能,注意该参数仅在MySQL 5.6版本下有效,5.7版本已经使用无锁结构进行优化
  • innodb_open_files = 4096 -- 增大InnoDB层缓存文件句柄的数量
  • table_open_cache = 4096 -- 增大缓存表的数量
  • table_definition_cache = 4096 -- 调大缓存frm表的数量,很多小伙伴遇到这个问题,导致show table status运行时等待非常长的时间
  • table_open_cache_instances = 128 -- 提升大并发下的性能
  • thread_cache_size = 64 -- 增大线程缓存,提升短连接的性能。通常来说都用连接池技术进行长连接操作,但是最近有一个项目用了短连接,遇到了坑。
  • innodb_online_alter_log_max_size=1G -- 增大内存,防止大表在DML比较多的场景下Online DDL操作失败

移除的参数有:

  • relay_log_recovery = 1 -- 若开启GTID功能,crash safe功能无需开启此参数。具体可见官方文档说明

最后,对上篇文章拿走不谢,Flashback for MySQL 5.7做一下说明,在编辑时漏了下面一段话,可能大家产生了误解:

Flashback最早由淘宝的彭立勋同学开发,后集成在InnoSQL中,并由网易的同学持续开发与维护。

因此官方的mysqlbinlog是不包含flashback功能,下载文章中指定mysqlbinlog才有。另外,彭立勋同学表示MariaDB 10.3将支持DDL的flashback功能,大家期待吧。

转载:IMG

作者:姜 承尧


打赏
赞(0) 打赏
未经允许不得转载:同乐学堂 » MySQL最佳配置文件

特别的技术,给特别的你!

联系QQ:1071235258QQ群:710045715

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续给力更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫打赏

微信扫一扫打赏

error: Sorry,暂时内容不可复制!