1.4.1. bind-address
bind-address = 0.0.0.0
1.4.2. 禁用TCP/IP链接
与bind-address互斥,skip-networking 开启,只能通过UNIX SOCKET链接,而不能使用IP地址链接
[mysqld] skip-networking
1.4.3. 配置字符集
Configuring Database Character Encoding
mysql> SHOW VARIABLES LIKE 'character_set_%'; +--------------------------+----------------------------+ | Variable_name | Value | +--------------------------+----------------------------+ | character_set_client | latin1 | | character_set_connection | latin1 | | character_set_database | utf8 | | character_set_filesystem | binary | | character_set_results | latin1 | | character_set_server | latin1 | | character_set_system | utf8 | | character_sets_dir | /usr/share/mysql/charsets/ | +--------------------------+----------------------------+ 8 rows in set (0.00 sec)
Server Character Set and Collation
shell> mysqld --character-set-server=latin1 shell> mysqld --character-set-server=latin1 \ --collation-server=latin1_swedish_ci
$ vim /etc/mysql/my.cnf
[mysqld] character-set-server=utf8 collation_server=utf8_general_ci init_connect='SET NAMES utf8' [client] character_set_client=utf8
mysql --default-character-set=utf8 -u root -p
mysql> show variables like 'character%'; +--------------------------+----------------------------+ | Variable_name | Value | +--------------------------+----------------------------+ | character_set_client | utf8 | | character_set_connection | utf8 | | character_set_database | utf8 | | character_set_filesystem | binary | | character_set_results | utf8 | | character_set_server | utf8 | | character_set_system | utf8 | | character_sets_dir | /usr/share/mysql/charsets/ | +--------------------------+----------------------------+ 8 rows in set (0.00 sec)
1.4.4. 最大链接数 max_connections
[mysqld] max_connections=250
1.4.5. 默认引擎 storage-engine
[mysqld] default-storage-engine=INNODB
1.4.6. max_allowed_packet
max_allowed_packet=8M
1.4.7. skip-name-resolve
跳过域名解析
# vim /etc/mysql/my.cnf [mysqld] skip-name-resolve
MySQL 登录缓慢,大量用户排队等待
mysql> SHOW FULL PROCESSLIST; +-----+----------------------+------------------------+------+---------+------+-------+-----------------------+ | Id | User | Host | db | Command | Time | State | Info | +-----+----------------------+------------------------+------+---------+------+-------+-----------------------+ | 718 | unauthenticated user | 192.168.3.124:42075 | NULL | Connect | NULL | login | NULL | | 719 | unauthenticated user | 192.168.3.124:42073 | NULL | Connect | NULL | login | NULL | | 720 | unauthenticated user | 192.168.3.124:42074 | NULL | Connect | NULL | login | NULL | | 721 | unauthenticated user | 192.168.3.124:42077 | NULL | Connect | NULL | login | NULL | | 722 | unauthenticated user | 192.168.3.124:42076 | NULL | Connect | NULL | login | NULL | | 723 | unauthenticated user | 192.168.3.124:42079 | NULL | Connect | NULL | login | NULL | | 724 | unauthenticated user | 192.168.3.124:42078 | NULL | Connect | NULL | login | NULL | | 725 | unauthenticated user | 192.168.3.124:42081 | NULL | Connect | NULL | login | NULL | | 726 | unauthenticated user | 192.168.3.124:42080 | NULL | Connect | NULL | login | NULL | | 727 | unauthenticated user | 192.168.3.124:42082 | NULL | Connect | NULL | login | NULL | | 728 | unauthenticated user | 192.168.3.124:42083 | NULL | Connect | NULL | login | NULL | | 729 | unauthenticated user | 192.168.3.124:42085 | NULL | Connect | NULL | login | NULL | | 730 | unauthenticated user | 192.168.3.124:42084 | NULL | Connect | NULL | login | NULL | | 731 | unauthenticated user | 192.168.3.124:42086 | NULL | Connect | NULL | login | NULL | | 732 | unauthenticated user | 192.168.3.124:42087 | NULL | Connect | NULL | login | NULL | | 733 | unauthenticated user | 192.168.3.124:42088 | NULL | Connect | NULL | login | NULL | | 734 | unauthenticated user | 192.168.3.124:42089 | NULL | Connect | NULL | login | NULL | | 735 | unauthenticated user | 192.168.3.124:42090 | NULL | Connect | NULL | login | NULL | | 736 | unauthenticated user | 192.168.3.124:42091 | NULL | Connect | NULL | login | NULL | | 737 | unauthenticated user | 192.168.3.124:42092 | NULL | Connect | NULL | login | NULL | | 738 | unauthenticated user | 192.168.3.124:42093 | NULL | Connect | NULL | login | NULL | | 739 | unauthenticated user | 192.168.3.124:42094 | NULL | Connect | NULL | login | NULL | | 740 | unauthenticated user | 192.168.3.124:42095 | NULL | Connect | NULL | login | NULL | | 741 | unauthenticated user | 192.168.3.124:42096 | NULL | Connect | NULL | login | NULL | | 742 | unauthenticated user | 192.168.3.124:42097 | NULL | Connect | NULL | login | NULL | | 743 | unauthenticated user | 192.168.3.124:42098 | NULL | Connect | NULL | login | NULL | | 744 | unauthenticated user | 192.168.3.124:42099 | NULL | Connect | NULL | login | NULL | | 745 | unauthenticated user | 192.168.3.124:42100 | NULL | Connect | NULL | login | NULL | | 746 | unauthenticated user | 192.168.3.124:42101 | NULL | Connect | NULL | login | NULL | | 747 | unauthenticated user | 192.168.3.124:42102 | NULL | Connect | NULL | login | NULL | | 748 | unauthenticated user | 192.168.3.124:42103 | NULL | Connect | NULL | login | NULL | | 749 | unauthenticated user | 192.168.3.124:42104 | NULL | Connect | NULL | login | NULL | | 750 | unauthenticated user | 192.168.3.124:42068 | NULL | Connect | NULL | login | NULL | | 751 | unauthenticated user | 192.168.3.124:42064 | NULL | Connect | NULL | login | NULL | | 752 | unauthenticated user | 192.168.3.124:42071 | NULL | Connect | NULL | login | NULL | | 753 | unauthenticated user | 192.168.3.124:42072 | NULL | Connect | NULL | login | NULL | | 754 | unauthenticated user | 192.168.3.124:42067 | NULL | Connect | NULL | login | NULL | | 755 | unauthenticated user | 192.168.3.124:42070 | NULL | Connect | NULL | login | NULL | | 756 | unauthenticated user | 192.168.3.124:42069 | NULL | Connect | NULL | login | NULL | | 757 | unauthenticated user | 192.168.3.124:42065 | NULL | Connect | NULL | login | NULL | | 758 | unauthenticated user | 192.168.3.124:42112 | NULL | Connect | NULL | login | NULL | | 759 | unauthenticated user | 192.168.3.50:4872 | NULL | Connect | NULL | login | NULL | | 761 | unauthenticated user | 192.168.3.40:36363 | NULL | Connect | NULL | login | NULL | | 762 | neo | www.example.com:56200 | NULL | Query | 0 | NULL | SHOW FULL PROCESSLIST | +-----+----------------------+------------------------+------+---------+------+-------+-----------------------+ 44 rows in set (0.00 sec) mysql> SHOW FULL PROCESSLIST; +-----+----------------------+------------------------+------+---------+------+-------+-----------------------+ | Id | User | Host | db | Command | Time | State | Info | +-----+----------------------+------------------------+------+---------+------+-------+-----------------------+ | 718 | unauthenticated user | 192.168.3.124:42075 | NULL | Connect | NULL | login | NULL | | 719 | unauthenticated user | 192.168.3.124:42073 | NULL | Connect | NULL | login | NULL | | 720 | unauthenticated user | 192.168.3.124:42074 | NULL | Connect | NULL | login | NULL | | 721 | unauthenticated user | 192.168.3.124:42077 | NULL | Connect | NULL | login | NULL | | 722 | unauthenticated user | 192.168.3.124:42076 | NULL | Connect | NULL | login | NULL | | 723 | unauthenticated user | 192.168.3.124:42079 | NULL | Connect | NULL | login | NULL | | 724 | unauthenticated user | 192.168.3.124:42078 | NULL | Connect | NULL | login | NULL | | 725 | unauthenticated user | 192.168.3.124:42081 | NULL | Connect | NULL | login | NULL | | 726 | unauthenticated user | 192.168.3.124:42080 | NULL | Connect | NULL | login | NULL | | 727 | unauthenticated user | 192.168.3.124:42082 | NULL | Connect | NULL | login | NULL | | 728 | unauthenticated user | 192.168.3.124:42083 | NULL | Connect | NULL | login | NULL | | 729 | unauthenticated user | 192.168.3.124:42085 | NULL | Connect | NULL | login | NULL | | 730 | unauthenticated user | 192.168.3.124:42084 | NULL | Connect | NULL | login | NULL | | 731 | unauthenticated user | 192.168.3.124:42086 | NULL | Connect | NULL | login | NULL | | 732 | unauthenticated user | 192.168.3.124:42087 | NULL | Connect | NULL | login | NULL | | 733 | unauthenticated user | 192.168.3.124:42088 | NULL | Connect | NULL | login | NULL | | 734 | unauthenticated user | 192.168.3.124:42089 | NULL | Connect | NULL | login | NULL | | 735 | unauthenticated user | 192.168.3.124:42090 | NULL | Connect | NULL | login | NULL | | 736 | unauthenticated user | 192.168.3.124:42091 | NULL | Connect | NULL | login | NULL | | 737 | unauthenticated user | 192.168.3.124:42092 | NULL | Connect | NULL | login | NULL | | 738 | unauthenticated user | 192.168.3.124:42093 | NULL | Connect | NULL | login | NULL | | 739 | unauthenticated user | 192.168.3.124:42094 | NULL | Connect | NULL | login | NULL | | 740 | unauthenticated user | 192.168.3.124:42095 | NULL | Connect | NULL | login | NULL | | 741 | unauthenticated user | 192.168.3.124:42096 | NULL | Connect | NULL | login | NULL | | 742 | unauthenticated user | 192.168.3.124:42097 | NULL | Connect | NULL | login | NULL | | 743 | unauthenticated user | 192.168.3.124:42098 | NULL | Connect | NULL | login | NULL | | 744 | unauthenticated user | 192.168.3.124:42099 | NULL | Connect | NULL | login | NULL | | 745 | unauthenticated user | 192.168.3.124:42100 | NULL | Connect | NULL | login | NULL | | 746 | unauthenticated user | 192.168.3.124:42101 | NULL | Connect | NULL | login | NULL | | 747 | unauthenticated user | 192.168.3.124:42102 | NULL | Connect | NULL | login | NULL | | 748 | unauthenticated user | 192.168.3.124:42103 | NULL | Connect | NULL | login | NULL | | 749 | unauthenticated user | 192.168.3.124:42104 | NULL | Connect | NULL | login | NULL | | 750 | unauthenticated user | 192.168.3.124:42068 | NULL | Connect | NULL | login | NULL | | 751 | unauthenticated user | 192.168.3.124:42064 | NULL | Connect | NULL | login | NULL | | 752 | unauthenticated user | 192.168.3.124:42071 | NULL | Connect | NULL | login | NULL | | 753 | unauthenticated user | 192.168.3.124:42072 | NULL | Connect | NULL | login | NULL | | 754 | unauthenticated user | 192.168.3.124:42067 | NULL | Connect | NULL | login | NULL | | 755 | unauthenticated user | 192.168.3.124:42070 | NULL | Connect | NULL | login | NULL | | 756 | unauthenticated user | 192.168.3.124:42069 | NULL | Connect | NULL | login | NULL | | 757 | unauthenticated user | 192.168.3.124:42065 | NULL | Connect | NULL | login | NULL | | 758 | unauthenticated user | 192.168.3.124:42112 | NULL | Connect | NULL | login | NULL | | 759 | unauthenticated user | 192.168.3.50:4872 | NULL | Connect | NULL | login | NULL | | 761 | unauthenticated user | 192.168.3.40:36363 | NULL | Connect | NULL | login | NULL | | 762 | neo | www.example.com:56200 | NULL | Query | 0 | NULL | SHOW FULL PROCESSLIST | +-----+----------------------+------------------------+------+---------+------+-------+-----------------------+ 44 rows in set (0.00 sec)
解决方案 my.cnf 配置文件中加入skip-name-resolve
1.4.8. timeout
[mysqld] wait_timeout=30 interactive_timeout=30
如果你没有修改过MySQL的配置,缺省情况下,wait_timeout的初始值是28800。
wait_timeout过大有弊端,其体现就是MySQL里大量的SLEEP进程无法及时释放,拖累系统性能,不过也不能把这个指设置的过小,否则你可能会遭遇到“MySQL has gone away”之类的问题,通常来说,我觉得把wait_timeout设置为10是个不错的选择,但某些情况下可能也会出问题,比如说有一个CRON脚本,其中两次SQL查询的间隔时间大于10秒的话,那么这个设置就有问题了:
(1)interactive_timeout 参数含义:服务器关闭交互式连接前等待活动的秒数。 参数默认值:28800秒(8小时)
(2)wait_timeout 参数含义:服务器关闭非交互连接之前等待活动的秒数。
1.4.9. 与复制有关的参数
1.4.9.1. 用于主库的选项 Master
定义 log-bin 文件名
log-bin=mysql-bin
binlog 保留时间, 过期天数设置
expire-logs-days = 30
binlog-do-db=需要复制的数据库名 binlog-ignore-db=不需要复制的数据库
1.4.9.2. 用于从库的选项 Slave
replicate-do-db= 指定需要复制的数据库 replicate-ignore-db= 忽略复制的数据库
1.4.9.3. 逃过错误
主从复制经常遇到 Last_Errno: 1062 可以使用下面配置跳过
slave_skip_errors=1062
1.4.10. 与 InnoDB 有关的配置项
innodb_file_per_table
配置后重启mysql运行下面命令将ibdata1拆分到tbl_name.ibd
OPTIMIZE TABLE tbl_name;
ls /var/lib/mysql/中查看 tbl_name.ibd文件
临时开启
SET @@global.innodb_file_per_table = 1;
1.4.11. EVENT 设置
开启EVENT定时任务
event_scheduler=on
1.4.12. 日志
操作日志
log = mysql.log
慢查询日志
log-slow-queries = slow.log long_query_time = 5
错误日志
[mysqld_safe] log-error=/var/log/mysqld.log
1.4.13. MySQL 5.7 my.cnf 实例
例 1.1. my.cnf
[root@netkiller ~]# cat /etc/my.cnf # For advice on how to change settings please see # http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html [mysqld] # # Remove leading # and set to the amount of RAM for the most important data # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%. # innodb_buffer_pool_size = 128M # # Remove leading # to turn on a very important data integrity option: logging # changes to the binary log between backups. # log_bin # # Remove leading # to set options mainly useful for reporting servers. # The server defaults are faster for transactions and fast SELECTs. # Adjust sizes as needed, experiment to find the optimal values. # join_buffer_size = 128M # sort_buffer_size = 2M # read_rnd_buffer_size = 2M datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid !includedir /etc/my.cnf.d
例 1.2. my.cnf
[root@netkiller ~]# cat /etc/my.cnf.d/default.cnf [mysqld] skip-name-resolve max_connections=4096 default-storage-engine=INNODB #wait_timeout=300 #interactive_timeout=300 character-set-server=utf8 collation_server=utf8_general_ci init_connect='SET NAMES utf8' explicit_defaults_for_timestamp=true query_cache_type=1 query_cache_size=512M table-open-cache=2000 #validate-password=OFF sql_mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION [client] default-character-set=utf8 #character_set_client=utf8
1.4.14. Example for my.cnf
例 1.3. my.cnf
# Example MySQL config file for very large systems. # # This is for a large system with memory of 1G-2G where the system runs mainly # MySQL. # # In this file, you can use all long options that a program supports. # If you want to know which options a program supports, run the program # with the "--help" option. # The following options will be passed to all MySQL clients [client] #password = your_password port = 3306 socket = /var/lib/mysql/mysql.sock # Here follows entries for some specific programs character-set-server=utf8 # The MySQL server [mysqld] port = 3306 socket = /var/lib/mysql/mysql.sock skip-external-locking key_buffer_size = 384M max_allowed_packet = 1M table_open_cache = 512 sort_buffer_size = 2M read_buffer_size = 2M read_rnd_buffer_size = 8M myisam_sort_buffer_size = 64M thread_cache_size = 8 query_cache_size = 32M # Try number of CPU's*2 for thread_concurrency thread_concurrency = 8 #skip-networking # Replication Master Server (default) # binary logging is required for replication log-bin=mysql-bin # required unique id between 1 and 2^32 - 1 # defaults to 1 if master-host is not set # but will not function as a master if omitted server-id = 1 # Replication Slave (comment out master section to use this) # # # required unique id between 2 and 2^32 - 1 # (and different from the master) # defaults to 2 if master-host is set # but will not function as a slave if omitted #server-id = 2 # # The replication master for this slave - required #master-host = <hostname> # # The username the slave will use for authentication when connecting # to the master - required #master-user = <username> # # The password the slave will authenticate with when connecting to # the master - required #master-password = <password> # # The port the master is listening on. # optional - defaults to 3306 #master-port = <port> # # binary logging - not required for slaves, but recommended #log-bin=mysql-bin # # binary logging format - mixed recommended #binlog_format=mixed # Uncomment the following if you are using InnoDB tables #innodb_data_home_dir = /var/lib/mysql #innodb_data_file_path = ibdata1:2000M;ibdata2:10M:autoextend #innodb_log_group_home_dir = /var/lib/mysql # You can set .._buffer_pool_size up to 50 - 80 % # of RAM but beware of setting memory usage too high #innodb_buffer_pool_size = 384M #innodb_additional_mem_pool_size = 20M # Set .._log_file_size to 25 % of buffer pool size #innodb_log_file_size = 100M #innodb_log_buffer_size = 8M #innodb_flush_log_at_trx_commit = 1 #innodb_lock_wait_timeout = 50 # Here follows entries for some specific programs skip-name-resolve default-storage-engine = INNODB character-set-server=utf8 collation_server=utf8_general_ci init_connect='SET NAMES utf8' max_connections = 4096 max_connect_errors = 10 pid-file = mysql.pid log = mysql.log log-error = mysql_error.log log-slow-queries = slow.log long_query_time = 10 [mysqldump] quick max_allowed_packet = 16M [mysql] no-auto-rehash # Remove the next comment character if you are not familiar with SQL #safe-updates [myisamchk] key_buffer_size = 256M sort_buffer_size = 256M read_buffer = 2M write_buffer = 2M [mysqlhotcopy] interactive-timeout
原文出处:Netkiller 系列 手札
本文作者:陈景峯
转载请与作者联系,同时请务必标明文章原始出处和作者信息及本声明。