首页 > 技术文章 > Mysql 配置参数性能调优

h-gallop 2020-02-16 16:25 原文

mysql 服务器 性能调优

mysql数据库性能优化,除了从sql 语句,服务器资源等其他方面外,mysql 服务器本身也需要进行参数优化,才能让mysql 服务性能达到最优使用
mysql 服务参数优化后,性能较之前能达到翻倍的效果

服务器资源情况:
cpu : 4核
memory : 16G

mysql 版本: 5.6

my.cnf:

[client]
port=3306
socket = /var/lib/mysql/mysql.sock
#default-character-set=utf8

[mysqld]
# root 用户忘记密码参数
#skip-grant-tables 
datadir = /var/lib/mysql
basedir = /usr
socket = /var/lib/mysql/mysql.sock
plugin_dir = /usr/lib64/mysql/plugin
#tmpdir = /var/lib/mysql/tmp
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
character-set-server=utf8
user=mysql
transaction_isolation = READ-COMMITTED
default-storage-engine = InnoDB
explicit_defaults_for_timestamp = 1
lower_case_table_names = 1
event_scheduler = 1
skip-external-locking
#sql_mode = 'NO_AUTO_VALUE_ON_ZERO,STRICT_TRANS_TABLES,NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION,PIPES_AS_CONCAT,ANSI_QUOTES'
sql_mode = "STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"

#secure-file-priv = /data/lib/

#  ---------------- Connection/File/Table ----------------
interactive_timeout = 1800
wait_timeout = 1800
lock_wait_timeout = 1800
skip_name_resolve = 1
max_connections = 1000
max_user_connections = 500
max_connect_errors = 20000
back_log = 3000
open_files_limit = 65535
table_open_cache = 1024
table_definition_cache = 1024
table_open_cache_instances = 32


#  ---------------- Session Buffer Cache ----------------
query_cache_size = 0
query_cache_type = 0
max_allowed_packet = 64M
join_buffer_size = 64M
# 根据内存修改1G 8,2 16,>3G 64
thread_cache_size = 64
thread_stack = 256K
# key_buffer_size默认为8M
key_buffer_size=256M
read_buffer_size=256M
read_rnd_buffer_size=256M
# 设置了后性能变慢
#sort_buffer_size = 256M
tmp_table_size = 32M




#  ---------------- Logfile ----------------
slow_query_log = 1
#slow_query_log_file = /var/lib/mysql/log/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
min_examined_row_limit = 100
binlog-rows-query-log-events = 1
log-bin-trust-function-creators = 1
long_query_time = 1


#  ---------------- Binlog ----------------
#log-bin = /var/lib/mysql/log/binlog/mysql-bin
#log-bin-index = /var/lib/mysql/log/binlog/mysql-bin.index
binlog_format = row
expire_logs_days = 7
sync_binlog = 1
binlog_cache_size = 200M
max_binlog_cache_size = 2048M
max_binlog_size = 1024M
binlog_rows_query_log_events = 1

#  ---------------- InnoDB ----------------
innodb_buffer_pool_instances = 1
innodb_online_alter_log_max_size = 1024M
# thread_concurrency对MySQL的性能影响很大,应设为CPU核数的2倍
innodb_thread_concurrency = 8
innodb_io_capacity = 4000
innodb_io_capacity_max = 8000
innodb_sort_buffer_size = 256M
innodb_page_size = 16K
innodb_open_files = 1024
innodb_flush_log_at_trx_commit = 1
innodb_read_io_threads = 8
innodb_write_io_threads = 8
innodb_purge_threads = 4
innodb_max_dirty_pages_pct = 80
innodb_buffer_pool_load_at_startup = 1
innodb_buffer_pool_dump_at_shutdown = 1
innodb_lock_wait_timeout = 30
innodb_rollback_on_timeout = 1
innodb_lru_scan_depth = 1024
innodb_flush_method = O_DIRECT
innodb_checksum_algorithm = CRC32
innodb_file_per_table = 1
innodb_file_format = Barracuda
innodb_file_format_max = Barracuda
innodb_flush_neighbors = 0
innodb_large_prefix = 0
innodb_print_all_deadlocks = 1
innodb_stats_persistent_sample_pages = 64
innodb_autoinc_lock_mode = 2
innodb_sync_spin_loops = 100
innodb_spin_wait_delay = 30
innodb_stats_on_metadata = 0
innodb_buffer_pool_size=10G
# 默认8M 推荐设置为 0.25 * innodb_buffer_pool_size,值越大,宕机恢复时间越长
innodb_log_file_size=2048M
#innodb_autoextend_increment

#  ---------------- Redo/Undo ----------------
#innodb_log_file_size = 1024M
#innodb_log_files_in_group = 3
#innodb_log_group_home_dir = /var/lib/mysql/log/redo
#innodb_undo_logs = 128
#innodb_undo_tablespaces = 3
#innodb_undo_directory = /var/lib/mysql/log/undo


#  ---------------- Replication ----------------
server-id = 101
master_info_repository = TABLE
relay_log_info_repository = TABLE
gtid_mode = on
enforce_gtid_consistency = 1
log_slave_updates = 1
relay_log = /var/lib/mysql/log/relay/relay.log
relay_log_recovery = 1
slave_skip_errors = ddl_exist_errors
slave-rows-search-algorithms = 'INDEX_SCAN,HASH_SCAN'

#  ---------------- perforamnce_schema ----------------
# 注释为mysql 5.6版本不支持的参数
performance-schema-instrument = 'memory/%=COUNTED'
performance_schema_digests_size = 40000
performance_schema_max_table_instances = 40000
#performance_schema_max_sql_text_length = 4096
performance_schema_max_digest_length = 4096

loose_innodb_numa_interleave = 1
#innodb_buffer_pool_dump_pct = 75
#innodb_page_cleaners = 4
#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 = system
#show_compatibility_56 = 1


[mysql]
no-auto-rehash
default-character-set=utf8

[mysqld_safe]
log-error = /var/log/mysqld.log
pid-file = /var/run/mysqld/mysqld.pid


使用测试工具为 sysbench
安装

yum install -y sysbench

测试效果中 查询性能显示不明显,读写混合模式、只写模式效果明显

推荐阅读