Mysqld的关键优化参数

skip-name-resolve

现象

mysql连接很慢,登陆到服务器上查看服务器日志都是正常的,无可疑记录,登陆到mysql服务器上,查看下进程,发现有很多这样的连接:
218 | unauthenticated user | 192.168.10.6:44500 | NULL  | Connect | NULL | login | NULL
219 | unauthenticated user | 192.168.10.6:44501 | NULL  | Connect | NULL | login | NULL  
 

原因是由于mysql对连接的客户端进行DNS反向解析。

有2种解决办法:
1,把client的ip写在mysql服务器的/etc/hosts文件里,随便给个名字就可以了。
2,在 my.cnf 中加入 skip_name_resolve=on

mysql是这样来处理客户端解析过程

Mysqld的关键优化参数_第1张图片

  • mysql-client向mysqld发起连接请求;
  • mysqld创建子线程来处理该连接请求;
  • 子线程首先去检查主机名是否存在于performance_schema.host_cache表中;
  • 如果是,则进入权限验证阶段;
  • 如果否,那么会尝试解析将IP地址解析为主机名;
  • 然后再将主机名解析为IP地址;
  • 将解析到的结果和原始IP地址进行比较;
  • 如果相同则添加到host_cache中然后进行权限验证;
  • 如果不相同,那么返回报错
  • 注:只有非本地主机的 TCP 连接会使用主机缓存;使用网络回环地址或 socket 建立的连接不会使用主机缓存
     

所谓反向解析是这样的

mysql接收到连接请求后,获得的是客户端的ip,为了更好的匹配mysql.user里的权限记录(某些是用hostname定义的)。
如果mysql服务器设置了dns服务器,并且客户端ip在dns上并没有相应的hostname,那么这个过程很慢,导致连接等待。

添加skip-name-resolve以后就跳过着一个过程了。

How MySQL
uses DNS When a new thread connects to mysqld, mysqld will
spawn a new thread to handle the request. This thread will first check
if the hostname is in the hostname cache. If not the thread will call
gethostbyaddr_r() and gethostbyname_r() to resolve the hostname. If
the operating system doesn’t support the above thread-safe calls, the
thread will lock a mutex and call gethostbyaddr() and gethostbyname()
instead. Note that in this case no other thread can resolve other
hostnames that is not in the hostname cache until the first thread is
ready. You can disable DNS host lookup by starting mysqld with
–skip-name-resolve. In this case you can however only use IP names in
the MySQL privilege tables. If you have a very slow DNS and many
hosts, you can get more performance by either disabling DNS lookop
with –skip-name-resolve or by increasing the HOST_CACHE_SIZE define
(default: 128) and recompile mysqld. You can disable the hostname
cache with –skip-host-cache. You can clear the hostname cache with
FLUSH HOSTS or mysqladmin flush-hosts. If you don’t want to allow
connections over TCP/IP, you can do this by starting mysqld with
–skip-networking.
 

根据文档说明,如果你的mysql主机查询DNS很慢或是有很多客户端主机时会导致连接很慢,由于我们的开发机器是不能够连接外网的,所以DNS解析是不可能完成的,从而也就明白了为什么连接那么慢了。同时,请注意在增加该配置参数后,mysql的授权表中的host字段就不能够使用域名而只能够使用 ip地址了,因为这是禁止了域名解析的结果。

skip-host-cache

主机缓存的作用

  • 通过缓存 IP 到主机名查找的结果,可以避免每次客户端进行连接时都去寻找DNS进行解析,所能缓存条目的数量受 host_cache_size 控制。
  • 缓存中包含客户端连接过程中发生的错误信息。如果同一主机连续多次连接错误,那么将阻止该主机的进一步连接,该行为受 max_connect_errors 控制。
     
参数 host_cache_size与skip-host-cache和skip_name_resolve 的作用
  • host_cache_size:控制主机缓存的数量,设置为0时,禁用主机缓存,每次服务器连接时,服务器都会进行DNS查找,相比skip-host-cache来说,host_cache_size更加灵活,可以随时变更或禁用。
  • skip-host-cache:作用类似于host_cache_size,但mysql在运行时无法对该参数进行变更,并且skip-host-cache在之后的版本中已弃用。
  • skip_name_resolve:检查客户端连接时是否解析主机名,变量为off,则mysqld 会在检查客户端连接时解析主机名。若变量为on,mysqld 只使用 IP ;在这种情况下,授权表中的所有列值都必须是 IP地址
     

read_buffer_size

read_buffer_size参数用于控制每次从MySQL服务器读取数据时使用的缓冲区大小,单位为字节,默认值为 1MB。

线上配置参考

[client]
port = 3306
socket=/var/lib/mysql/mysql.sock

[mysqld]
########basic settings########
server-id = 11 
port = 3306
user = mysql
bind_address = 0.0.0.0
autocommit = 1
character_set_server=utf8
skip_name_resolve = 1
log_bin_trust_function_creators = 1
max_connections = 800
max_connect_errors = 1000
datadir = /data/mysql/data
transaction_isolation = READ-COMMITTED
explicit_defaults_for_timestamp = 1
join_buffer_size = 134217728
tmp_table_size = 67108864
tmpdir = /data/mysql/tmp/
max_allowed_packet = 16777216
sql_mode = "STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION,NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER"
interactive_timeout = 1800
wait_timeout = 1800
read_buffer_size = 16777216
read_rnd_buffer_size = 33554432
sort_buffer_size = 33554432
########log settings########
log_error = error.log
slow_query_log = 1
slow_query_log_file = /data/mysql/logs/slow/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
########replication settings########
master_info_repository = TABLE
relay_log_info_repository = TABLE
log_bin = /data/mysql/logs/bin_log/bin.log
sync_binlog = 1
gtid_mode = on
enforce_gtid_consistency = 1
log_slave_updates
binlog_format = row 
relay_log = relay.log
relay_log_recovery = 1
binlog_gtid_simple_recovery = 1
slave_skip_errors = ddl_exist_errors
########innodb settings########
innodb_page_size = 8192
innodb_buffer_pool_size = 12G
innodb_buffer_pool_instances = 8
innodb_buffer_pool_load_at_startup = 1
innodb_buffer_pool_dump_at_shutdown = 1
innodb_lru_scan_depth = 2000
innodb_lock_wait_timeout = 5
innodb_io_capacity = 4000
innodb_io_capacity_max = 8000
innodb_flush_method = O_DIRECT
innodb_file_format = Barracuda
innodb_file_format_max = Barracuda
#innodb_log_group_home_dir = /redolog/
#innodb_undo_directory = /undolog/
innodb_undo_logs = 128
innodb_undo_tablespaces = 3
innodb_flush_neighbors = 1
innodb_log_file_size = 4G
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 
########semi sync replication settings########
plugin_load = "rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so"
loose_rpl_semi_sync_master_enabled = 1
loose_rpl_semi_sync_slave_enabled = 1
loose_rpl_semi_sync_master_timeout = 5000

[mysqld-5.7]
innodb_buffer_pool_dump_pct = 40
innodb_page_cleaners = 4
innodb_undo_log_truncate = 1
innodb_max_undo_log_size = 2G
innodb_purge_rseg_truncate_frequency = 128
binlog_gtid_simple_recovery=1
log_timestamps=system
transaction_write_set_extraction=MURMUR32
show_compatibility_56=on

你可能感兴趣的:(#,Mysql,mysql)