MySQL高级配置

参考文章:http://www.jb51.net/article/47419.htm 

https://blog.csdn.net/waneto2008/article/details/52502208

1、配置MySQL的原则

(1)一次只改变一个设置!这是测试改变是否有益的唯一方法。

大多数配置能在运行时使用SET GLOBAL改变。这是非常便捷的方法它能使你在出问题后快速撤销变更。但是,要永久生效你需要在配置文件里做出改动。

(2)一个变更即使重启了MySQL也没起作用?请确定你使用了正确的配置文件。请确定你把配置放在了正确的区域内(所有这篇文章提到的配置都属于 [mysqld])

服务器在改动一个配置后启不来了:请确定你使用了正确的单位。例如,innodb_buffer_pool_size的单位是MB而max_connection是没有单位的。

不要在一个配置文件里出现重复的配置项。如果你想追踪改动,请使用版本控制。

(3)不要用天真的计算方法,例如”现在我的服务器的内存是之前的2倍,所以我得把所有数值都改成之前的2倍“。
2、基本配置

[mysqld]

########basic settings########

server-id = 11

#表示是本机的序号为11,一般来讲就是master的意思

port = 3306

#MySQL服务端口

#重要

bind_address = 10.166.224.32

#绑定IP,增加远程访问IP地址或者禁掉可以让远程机登陆访问了

autocommit = 0

#是否开启自动提交

#重要

character_set_server=utf8

#数据库默认的字符集

#旧的写法为,从5.1之后,不建议使用这个配置

#default-character-set=UTF8

#重要

max_connections = 800

# MySQL的最大连接数,如果服务器的并发连接请求量比较大,建议调高此值,以增加并行连接数量,

当然这建立在机器能支撑的情况下,因为如果连接数越多,介于MySQL会为每个连接提供连接缓冲区,

就会开销越多的内存,所以要适当调整该值,不能盲目提高设值。

可以过'conn%'通配符查看当前状态的连接数量,以定夺该值的大小。

查看当前的Max_connections参数值:

复制代码代码如下:

show variables like "max_connections";

或者

mysql> SELECT @@MAX_CONNECTIONS AS 'Max Connections';


设置该参数的值:

复制代码代码如下:

mysql> set GLOBAL max_connections=1000;



max_connect_errors = 1000

# 对于同一主机,如果有超出该参数值个数的中断错误连接,则该主机将被禁止连接。如需对该主机进行解禁,执行:FLUSH HOST。

datadir = /data/mysql_data

#数据库文件存储目录

transaction_isolation = READ-COMMITTED

# MySQL支持4种事务隔离级别,他们分别是:

# READ-UNCOMMITTED, READ-COMMITTED, REPEATABLE-READ, SERIALIZABLE.

# 如没有指定,MySQL默认采用的是REPEATABLE-READ,ORACLE默认的是READ-COMMITTED

explicit_defaults_for_timestamp = 1

#重要

join_buffer_size = 12M

# 联合查询操作所能使用的缓冲区大小,和sort_buffer_size一样,该参数对应的分配内存也是每连接独享

tmp_table_size = 64M

#临时HEAP数据表的最大长度

tmpdir = /tmp

#重要

max_allowed_packet = 16M

# 接受的数据包大小;增加该变量的值十分安全,这是因为仅当需要时才会分配额外内存。

# 例如,仅当你发出长查询或MySQLd必须返回大的结果行时MySQLd才会分配更多内存。

# 该变量之所以取较小默认值是一种预防措施,以捕获客户端和服务器之间的错误信息包,并确保不会因偶然使用大的信息包而导致内存溢出

sql_mode = "STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION,NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,

NO_AUTO_CREATE_USER"

# SQL模式

wait_timeout = 1800

# 服务器关闭非交互连接之前等待活动的秒数。参数默认值:28800秒(8小时)

#重要

read_buffer_size = 16M

# MySQL读入缓冲区大小。对表进行顺序扫描的请求将分配一个读入缓冲区,MySQL会为它分配一段内存缓冲区。read_buffer_size变量控制这一缓冲区的大小。

# 如果对表的顺序扫描请求非常频繁,并且你认为频繁扫描进行得太慢,可以通过增加该变量值以及内存缓冲区大小提高其性能

read_rnd_buffer_size = 32M

# MySQL的随机读缓冲区大小。当按任意顺序读取行时(例如,按照排序顺序),将分配一个随机读缓存区。进行排序查询时,

# MySQL会首先扫描一遍该缓冲,以避免磁盘搜索,提高查询速度,如果需要排序大量数据,可适当调高该值。

MySQL会为每个客户连接发放该缓冲空间,所以应尽量适当设置该值,以避免内存开销过大

#重要

sort_buffer_size = 33554432

# MySQL执行排序使用的缓冲大小。如果想要增加ORDER BY的速度,首先看是否可以让MySQL使用索引而不是额外的排序阶段。

# 如果不能,可以尝试增加sort_buffer_size变量的大小

#重要

lower_case_table_names = 1

#是否区分大小写

################   log settings   ################

#重要

log-bin = bin.log

#把对数据进行修改的所有SQL命令(也就是INSERT、UPDATE和DELETE命令)以二进制格式记入日志(二进制变更日志,binary update log)

这种日志的文件名是filename.n或默认的hostname.n,其中n是一个6位数字的整数(日志文件按顺序编号)。

binlog_format = mixed

expire_logs_days = 90

#bin log 自动删除/过期的天数。默认值为0,表示“没有自动删除

log_error = error.log

#错误日志路径

#重要

slow_query_log = 1

#开启慢查日志

slow_query_log_file = slow.log

#慢查日志记录文件

long_query_time = 2

#慢查询时间 超过2秒则为慢查询

log_queries_not_using_indexes = 1

#开启未使用索引查询

log_slow_admin_statements = 1

log_slow_slave_statements = 1

log_throttle_queries_not_using_indexes = 10

#参数用来做日志记录的流量控制,一分钟可以记录多少条,默认0是表示不限制。

min_examined_row_limit = 100

#类似于SELECT ... FROM TBL LIMIT N这样的全表扫描的查询因为用不到索引将要报告为慢查询,如果--log-queries-not-using-indexes被开启的话;

#可以在配置文件中使用min-examined-row-limit=Num of Rows来设置,如果要检查的行数大于等于这个量的查询,才会被报告为慢查询。

################      innodb settings     ################

innodb_page_size = 8192

#重要

#每个数据页大小

innodb_buffer_pool_size = 6G

# InnoDB使用一个缓冲池来保存索引和原始数据,不像MyISAM.

# 这里你设置越大,你在存取表里面数据时所需要的磁盘I/O越少.

# 在一个独立使用的数据库服务器上,你可以设置这个变量到服务器物理内存大小的80%

# 不要设置过大,否则,由于物理内存的竞争可能导致操作系统的换页颠簸.

# 注意在32位系统上你每个进程可能被限制在 2-3.5G用户层面内存限制,

# 所以不要设置的太高.

#重要

innodb_buffer_pool_instances = 8

#innodb_buffer_pool_instances可以开启多个内存缓冲池,上面配置分成8个区域,

把需要缓冲的数据hash到不同的缓冲池中,这样可以并行的内存读写。

#可以提升InnoDB的并发性能。如果InnoDB缓存池被划分成多个区域,建议每个区域不小于1GB的空间。

innodb_buffer_pool_load_at_startup = 1

#解释:在启动时把热数据加载到内存。

innodb_buffer_pool_dump_at_shutdown = 1

#解释:在关闭时把热数据dump到本地磁盘。

#以上两个参数配合使用,MySQL5.6之后提供

#在之前的版本里,如果一台高负荷的机器重启后,内存中大量的热数据被清空,此时就会重新从磁盘加载到Buffer_Pool缓冲池里,

#这样当高峰期间,性能就会变得很差,连接数就会很高。

innodb_lock_wait_timeout = 5

#MySQL可以自动地监测行锁导致的死锁并进行相应的处理,但是对于表锁导致的死锁不能自动的监测,

#所以该参数主要被用于在出现类似情况的时候等待指定的时间后回滚。系统默认值是50秒

innodb_undo_directory = /undolog/

innodb_undo_logs = 128

innodb_undo_tablespaces = 3

#在MySQL5.6中开始支持把undo log分离到独立的表空间,并放到单独的文件目录下

Undo log是InnoDB MVCC事务特性的重要组成部分。当我们对记录做了变更操作时就会产生undo记录,Undo记录默认被记录到系统表空间(ibdata)中,但从5.6开始,也可以使用独立的Undo表空间。

Undo记录中存储的是老版本数据,当一个旧的事务需要读取数据时,为了能读取到老版本的数据,需要顺着undo链找到满足其可见性的记录。当版本链很长时,通常可以认为这是个比较耗时的操作

innodb_log_file_size = 4G

有很高写入吞吐量的系统需要增加该值以允许后台检查点活动在更长的时间周期内平滑写入,得以改进性能.将此值设置为4G以下是很安全的. 过去的实践表明,日志文件太大的缺点是增加了崩溃时所需的修复时间,但这在5.5和5.6中已得到重大改进.

innodb_large_prefix = 1

innodb_thread_concurrency = 64

innodb_print_all_deadlocks = 1

innodb_strict_mode = 1

innodb_sort_buffer_size = 67108864

#重要

innodb_file_per_table = 1

# InnoDB为独立表空间模式,每个数据库的每个表都会生成一个数据空间

# 独立表空间优点:

# 1.每个表都有自已独立的表空间。

# 2.每个表的数据和索引都会存在自已的表空间中。

# 3.可以实现单表在不同的数据库中移动。

# 4.空间可以回收(除drop table操作处,表空间不能自已回收)

# 缺点:

# 单表增加过大,如超过100G

# 结论:

# 共享表空间在Insert操作上少有优势。其它都没独立表空间表现好。当启用独立表空间时,请合理调整:innodb_open_files

[mysqld-5.7]

innodb_buffer_pool_dump_pct = 40

InnoDB_Buffer_Pool缓冲区有两个区域,一个是sublist of new blocks区域(经常被访问的数据——热数据),一个是sublist of old blocks区域(不经常访问的数据)。当用户访问数据时,如果缓冲区里有相应的数据则直接返回,否则会从磁盘读入缓冲区的sublist of old blocks区域,然后再移动到sublist of new blocks区域,并通过LRU最近最少使用算法来踢出旧数据页。

但是这其中也许会存在一个问题,假如有些sql语句做统计用全表扫描,例如select * from t1,或者做一次MySQLdump,这时就会进入sublist of new blocks区域,把一些真正的热数据“踢走”,这样就会造成缓冲区的数据进进出出,导致磁盘I/O频繁。

所以从MySQL5.5.X版本开始,innodb_old_blocks_pct参数可以控制进入缓冲区sublist of old blocks区域的数量,默认是37,占整个缓冲池的比例为3/8。当全表扫描一个大表,或者做MySQLdump时,就可以将innodb_old_blocks_pct设置得小些,例如,设置innodb_old_blocks_pct=5,使数据块进入少量sublist of old blocks区域,并移动到sublist of new blocks区域,从而让更多的热数据不被踢出。当你访问一个小表,或者select查询结果很少时,则可以保持默认的innodb_old_blocks_pct=37,或者设置得更大,比如innodb_old_blocks_pct=50

3、高级配置
你需要经常察看以下3个配置项。不然,可能很快就会出问题。

innodb_buffer_pool_size:这是你安装完InnoDB后第一个应该设置的选项。缓冲池是数据和索引缓存的地方:这个值越大越好,这能保证你在大多数的读取操作时使用的是内存而不是硬盘。典型的值是5-6GB(8GB内存),20-25GB(32GB内存),100-120GB(128GB内存)。

innodb_log_file_size:这是redo日志的大小。redo日志被用于确保写操作快速而可靠并且在崩溃时恢复。一直到MySQL 5.1,它都难于调整,因为一方面你想让它更大来提高性能,另一方面你想让它更小来使得崩溃后更快恢复。幸运的是从MySQL 5.5之后,崩溃恢复的性能的到了很大提升,这样你就可以同时拥有较高的写入性能和崩溃恢复性能了。一直到MySQL 5.5,redo日志的总尺寸被限定在4GB(默认可以有2个log文件)。这在MySQL 5.6里被提高。

一开始就把innodb_log_file_size设置成512M(这样有1GB的redo日志)会使你有充裕的写操作空间。如果你知道你的应用程序需要频繁的写入数据并且你使用的时MySQL 5.6,你可以一开始就把它这是成4G。

max_connections:如果你经常看到‘Too many connections'错误,是因为max_connections的值太低了。这非常常见因为应用程序没有正确的关闭数据库连接,你需要比默认的151连接数更大的值。max_connection值被设高了(例如1000或更高)之后一个主要缺陷是当服务器运行1000个或更高的活动事务时会变的没有响应。在应用程序里使用连接池或者在MySQL里使用进程池有助于解决这一问题。

3、InnoDB配置
从MySQL 5.5版本开始,InnoDB就是默认的存储引擎并且它比任何其他存储引擎的使用都要多得多。那也是为什么它需要小心配置的原因。

innodb_file_per_table:这项设置告知InnoDB是否需要将所有表的数据和索引存放在共享表空间里(innodb_file_per_table = OFF) 或者为每张表的数据单独放在一个.ibd文件(innodb_file_per_table = ON)。每张表一个文件允许你在drop、truncate或者rebuild表时回收磁盘空间。这对于一些高级特性也是有必要的,比如数据压缩。但是它不会带来任何性能收益。你不想让每张表一个文件的主要场景是:有非常多的表(比如10k+)。

MySQL 5.6中,这个属性默认值是ON,因此大部分情况下你什么都不需要做。对于之前的版本你必需在加载数据之前将这个属性设置为ON,因为它只对新创建的表有影响。

innodb_flush_log_at_trx_commit:默认值为1,表示InnoDB完全支持ACID特性。当你的主要关注点是数据安全的时候这个值是最合适的,比如在一个主节点上。但是对于磁盘(读写)速度较慢的系统,它会带来很巨大的开销,因为每次将改变flush到redo日志都需要额外的fsyncs。将它的值设置为2会导致不太可靠(reliable)因为提交的事务仅仅每秒才flush一次到redo日志,但对于一些场景是可以接受的,比如对于主节点的备份节点这个值是可以接受的。如果值为0速度就更快了,但在系统崩溃时可能丢失一些数据:只适用于备份节点。

innodb_flush_method: 这项配置决定了数据和日志写入硬盘的方式。一般来说,如果你有硬件RAID控制器,并且其独立缓存采用write-back机制,并有着电池断电保护,那么应该设置配置为O_DIRECT;否则,大多数情况下应将其设为fdatasync(默认值)。sysbench是一个可以帮助你决定这个选项的好工具。

innodb_log_buffer_size: 这项配置决定了为尚未执行的事务分配的缓存。其默认值(1MB)一般来说已经够用了,但是如果你的事务中包含有二进制大对象或者大文本字段的话,这点缓存很快就会被填满并触发额外的I/O操作。看看Innodb_log_waits状态变量,如果它不是0,增加innodb_log_buffer_size。

其他设置

query_cache_size: query cache(查询缓存)是一个众所周知的瓶颈,甚至在并发并不多的时候也是如此。最佳选项是将其从一开始就停用,设置query_cache_size = 0(现在MySQL 5.6的默认值)并利用其他方法加速查询:优化索引、增加拷贝分散负载或者启用额外的缓存(比如memcache或redis)。如果你已经为你的应用启用了query cache并且还没有发现任何问题,query cache可能对你有用。这是如果你想停用它,那就得小心了。

log_bin:如果你想让数据库服务器充当主节点的备份节点,那么开启二进制日志是必须的。如果这么做了之后,还别忘了设置server_id为一个唯一的值。就算只有一个服务器,如果你想做基于时间点的数据恢复,这(开启二进制日志)也是很有用的:从你最近的备份中恢复(全量备份),并应用二进制日志中的修改(增量备份)。二进制日志一旦创建就将永久保存。所以如果你不想让磁盘空间耗尽,你可以用 PURGE BINARY LOGS 来清除旧文件,或者设置 expire_logs_days 来指定过多少天日志将被自动清除。

记录二进制日志不是没有开销的,所以如果你在一个非主节点的复制节点上不需要它的话,那么建议关闭这个选项。

skip_name_resolve:当客户端连接数据库服务器时,服务器会进行主机名解析,并且当DNS很慢时,建立连接也会很慢。因此建议在启动服务器时关闭skip_name_resolve选项而不进行DNS查找。唯一的局限是之后GRANT语句中只能使用IP地址了,因此在添加这项设置到一个已有系统中必须格外小心。

延伸阅读

http://my.oschina.net/liting/blog/387489

基本是通过内存大小来选择mysql的配置文件的,那有博友会说了,现在的服务器动不动就是32G内存或者64G内存,甚至更大的内存,你那个配置文件最大只支持4G内存是不是有点小了,确认会有这样的问题,从mysql5.6以后,为了更大的发挥mysql的性能,已经去除了配置文件选择,只有一个默认的配置文件,里面只有一些基本配置,所有设置管理员都可以根据自己实际的需求进行自行设置,好了说了这么多,我们就来说一说,在企业的用的最多的my-innodb-heavy-4G.cnf配置文件!

二、详解 my-innodb-heavy-4G.cnf

1.详细说明

注:下面是my-innodb-heavy-4G.cnf默认配置我没有做任何修改,下面我们就来详细的说一说!

[root@mysql support-files]# vim my-innodb-heavy-4G.cnf
#BEGIN CONFIG INFO 
#DESCR: 4GB RAM, InnoDB only, ACID, few connections, heavy queries  
#TYPE: SYSTEM  
#END CONFIG INFO
# 
# This is a MySQL example config file for systems with 4GB of memory  
# running mostly MySQL using InnoDB only tables and performing complex  
# queries with few connections.  
#  
# MySQL programs look for option files in a set of  
# locations which depend on the deployment platform.  
# You can copy this option file to one of those  
# locations. For information about these locations, see:  
# http://dev.mysql.com/doc/mysql/en/option-files.html
#  
# 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.  
#  
# More detailed information about the individual options can also be  
# found in the manual.  
#
# 
# The following options will be read by MySQL client applications.  
# Note that only client applications shipped by MySQL are guaranteed  
# to read this section. If you want your own MySQL client program to  
# honor these values, you need to specify it as an option during the  
# MySQL client library initialization.  
#
# 以下选项会被MySQL客户端应用读取, 注意只有MySQL附带的客户端应用程序保证可以读取这段内容,如果你想你自己的MySQL应用程序获取这些值,需要在MySQL客户端库初始化的时候指定这些选项
[client] 
#password    = [your_password] #mysql客户端连接mysql时的密码  
port        = 3306 #mysql客户端连接时的默认端口  
socket        = /tmp/mysql.sock #与mysql服务器本地通信所使用的socket文件路径
# *** Application-specific options follow here ***
# 
# The MySQL server  
#  
[mysqld]
# generic configuration options #一般配置选项 
port        = 3306 #mysql服务器监听的默认端口  
socket        = /tmp/mysql.sock #socket本地通信文件路径
# back_log is the number of connections the operating system can keep in 
# the listen queue, before the MySQL connection manager thread has  
# processed them. If you have a very high connection rate and experience  
# "connection refused" errors, you might need to increase this value.  
# Check your OS documentation for the maximum value of this parameter.  
# Attempting to set back_log higher than your operating system limit  
# will have no effect.
# back_log 是操作系统在监听队列中所能保持的连接数, 
# 队列保存了在MySQL连接管理器线程处理之前的连接.  
# 如果你有非常高的连接率并且出现“connection refused”报错,  
# 你就应该增加此处的值.  
# 检查你的操作系统能打开文件数来获取这个变量的最大值.  
# 如果将back_log设定到比你操作系统限制更高的值,将会没有效果
back_log = 50
# Don't listen on a TCP/IP port at all. This can be a security 
# enhancement, if all processes that need to connect to mysqld run  
# on the same host.  All interaction with mysqld must be made via Unix  
# sockets or named pipes.  
# Note that using this option without enabling named pipes on Windows  
# (via the "enable-named-pipe" option) will render mysqld useless!
# 不在TCP/IP端口上进行监听. 
# 如果所有的进程都是在同一台服务器连接到本地的mysqld,  
# 这样设置将是增强安全的方法  
# 所有mysqld的连接都是通过Unix sockets 或者命名管道进行的.  
# 注意在windows下如果没有打开命名管道选项而只是用此项  
# (通过 “enable-named-pipe” 选项) 将会导致mysql服务没有任何作用!
#skip-networking #默认是没有开启的
# The maximum amount of concurrent sessions the MySQL server will 
# allow. One of these connections will be reserved for a user with  
# SUPER privileges to allow the administrator to login even if the  
# connection limit has been reached.
# MySQL 服务器所允许的同时会话数的上限 
# 其中一个连接将被SUPER权限保留作为管理员登录.  
# 即便已经达到了连接数的上限.  
max_connections = 100
# Maximum amount of errors allowed per host. If this limit is reached, 
# the host will be blocked from connecting to the MySQL server until  
# "FLUSH HOSTS" has been run or the server was restarted. Invalid  
# passwords and other errors during the connect phase result in  
# increasing this value. See the "Aborted_connects" status variable for  
# global counter.
# 每个客户端连接最大的错误允许数量,如果达到了此限制. 
# 这个客户端将会被MySQL服务阻止直到执行了”FLUSH HOSTS” 或者服务重启  
# 非法的密码以及其他在链接时的错误会增加此值.  
# 查看 “Aborted_connects” 状态来获取全局计数器.  
max_connect_errors = 10
# The number of open tables for all threads. Increasing this value 
# increases the number of file descriptors that mysqld requires.  
# Therefore you have to make sure to set the amount of open files  
# allowed to at least 4096 in the variable "open-files-limit" in  
# section [mysqld_safe]
# 所有线程所打开表的数量. 
# 增加此值就增加了mysqld所需要的文件描述符的数量  
# 这样你需要确认在[mysqld_safe]中 “open-files-limit” 变量设置打开文件数量允许至少2048
table_open_cache = 2048
# Enable external file level locking. Enabled file locking will have a 
# negative impact on performance, so only use it in case you have  
# multiple database instances running on the same files (note some  
# restrictions still apply!) or if you use other software relying on  
# locking MyISAM tables on file level.
# 允许外部文件级别的锁. 打开文件锁会对性能造成负面影响 
# 所以只有在你在同样的文件上运行多个数据库实例时才使用此选项(注意仍会有其他约束!)  
# 或者你在文件层面上使用了其他一些软件依赖来锁定MyISAM表
#external-locking #默认是没有开启的
# The maximum size of a query packet the server can handle as well as 
# maximum query size server can process (Important when working with  
# large BLOBs).  enlarged dynamically, for each connection.
# 服务所能处理的请求包的最大大小以及服务所能处理的最大的请求大小(当与大的BLOB字段一起工作时相当必要) 
# 每个连接独立的大小.大小动态增加  
max_allowed_packet = 16M
# The size of the cache to hold the SQL statements for the binary log 
# during a transaction. If you often use big, multi-statement  
# transactions you can increase this value to get more performance. All  
# statements from transactions are buffered in the binary log cache and  
# are being written to the binary log at once after the COMMIT.  If the  
# transaction is larger than this value, temporary file on disk is used  
# instead.  This buffer is allocated per connection on first update  
# statement in transaction
# 在一个事务中binlog为了记录SQL状态所持有的cache大小 
# 如果你经常使用大的,多声明的事务,你可以增加此值来获取更大的性能.  
# 所有从事务来的状态都将被缓冲在binlog缓冲中然后在提交后一次性写入到binlog中  
# 如果事务比此值大, 会使用磁盘上的临时文件来替代.  
# 此缓冲在每个连接的事务第一次更新状态时被创建  
binlog_cache_size = 1M
# Maximum allowed size for a single HEAP (in memory) table. This option 
# is a protection against the accidential creation of a very large HEAP  
# table which could otherwise use up all memory resources.
# 独立的内存表所允许的最大容量. 
# 此选项为了防止意外创建一个超大的内存表导致永尽所有的内存资源.  
max_heap_table_size = 64M
# Size of the buffer used for doing full table scans. 
# Allocated per thread, if a full scan is needed.
#MySql读入缓冲区大小。对表进行顺序扫描的请求将分配一个读入缓冲区,MySql会为它分#配一段内存缓冲区。read_buffer_size变量控制这一缓冲区的大小。如果对表的顺序扫描请求非常频繁,#并且你认为频繁扫描进行得太慢,可以通过增加该变量值以及内存缓冲区大小提高其性能。 
read_buffer_size = 2M
# When reading rows in sorted order after a sort, the rows are read 
# through this buffer to avoid disk seeks. You can improve ORDER BY  
# performance a lot, if set this to a high value.  
# Allocated per thread, when needed.
#是MySql的随机读缓冲区大小。当按任意顺序读取行时(例如,按照排序顺序),将分配一个随机读缓存区。进行排序查询时,MySql会首先扫描一遍该缓冲,以避免磁盘搜索,提高查询速度,如果需#要排序大量数据,可适当调高该值。但MySql会为每个客户连接发放该缓冲空间,所以应尽量适当设置该值,以避免内存开销过大。 
read_rnd_buffer_size = 16M
# Sort buffer is used to perform sorts for some ORDER BY and GROUP BY 
# queries. If sorted data does not fit into the sort buffer, a disk  
# based merge sort is used instead - See the "Sort_merge_passes"  
# status variable. Allocated per thread if sort is needed.
# 排序缓冲被用来处理类似ORDER BY以及GROUP BY队列所引起的排序 
# 如果排序后的数据无法放入排序缓冲,  
# 一个用来替代的基于磁盘的合并分类会被使用  
# 查看 “Sort_merge_passes” 状态变量.  
# 在排序发生时由每个线程分配  
sort_buffer_size = 8M
# This buffer is used for the optimization of full JOINs (JOINs without 
# indexes). Such JOINs are very bad for performance in most cases  
# anyway, but setting this variable to a large value reduces the  
# performance impact. See the "Select_full_join" status variable for a  
# count of full JOINs. Allocated per thread if full join is found
# 此缓冲被使用来优化全联合(full JOINs 不带索引的联合). 
# 类似的联合在极大多数情况下有非常糟糕的性能表现,  
# 但是将此值设大能够减轻性能影响.  
# 通过 “Select_full_join” 状态变量查看全联合的数量  
# 当全联合发生时,在每个线程中分配  
join_buffer_size = 8M
# How many threads we should keep in a cache for reuse. When a client 
# disconnects, the client's threads are put in the cache if there aren't  
# more than thread_cache_size threads from before.  This greatly reduces  
# the amount of thread creations needed if you have a lot of new  
# connections. (Normally this doesn't give a notable performance  
# improvement if you have a good thread implementation.)
# 我们在cache中保留多少线程用于重用 
# 当一个客户端断开连接后,如果cache中的线程还少于thread_cache_size,  
# 则客户端线程被放入cache中.  
# 这可以在你需要大量新连接的时候极大的减少线程创建的开销  
# (一般来说如果你有好的线程模型的话,这不会有明显的性能提升.)  
thread_cache_size = 8
# This permits the application to give the threads system a hint for the 
# desired number of threads that should be run at the same time.  This  
# value only makes sense on systems that support the thread_concurrency()  
# function call (Sun Solaris, for example).  
# You should try [number of CPUs]*(2..4) for thread_concurrency
# 此允许应用程序给予线程系统一个提示在同一时间给予渴望被运行的线程的数量. 
# 此值只对于支持 thread_concurrency() 函数的系统有意义( 例如Sun Solaris).  
# 你可可以尝试使用 [CPU数量]*(2..4) 来作为thread_concurrency的值  
thread_concurrency = 8
# Query cache is used to cache SELECT results and later return them 
# without actual executing the same query once again. Having the query  
# cache enabled may result in significant speed improvements, if your  
# have a lot of identical queries and rarely changing tables. See the  
# "Qcache_lowmem_prunes" status variable to check if the current value  
# is high enough for your load.  
# Note: In case your tables change very often or if your queries are  
# textually different every time, the query cache may result in a  
# slowdown instead of a performance improvement.
# 查询缓冲常被用来缓冲 SELECT 的结果并且在下一次同样查询的时候不再执行直接返回结果. 
# 打开查询缓冲可以极大的提高服务器速度, 如果你有大量的相同的查询并且很少修改表.  
# 查看 “Qcache_lowmem_prunes” 状态变量来检查是否当前值对于你的负载来说是否足够高.  
# 注意: 在你表经常变化的情况下或者如果你的查询原文每次都不同,  
# 查询缓冲也许引起性能下降而不是性能提升.  
query_cache_size = 64M
# Only cache result sets that are smaller than this limit. This is to 
# protect the query cache of a very large result set overwriting all  
# other query results.
# 只有小于此设定值的结果才会被缓冲 
# 此设置用来保护查询缓冲,防止一个极大的结果集将其他所有的查询结果都覆盖.
query_cache_limit = 2M
# Minimum word length to be indexed by the full text search index. 
# You might wish to decrease it if you need to search for shorter words.  
# Note that you need to rebuild your FULLTEXT index, after you have  
# modified this value.
# 被全文检索索引的最小的字长. 
# 你也许希望减少它,如果你需要搜索更短字的时候.  
# 注意在你修改此值之后,  
# 你需要重建你的 FULLTEXT 索引  
ft_min_word_len = 4
# If your system supports the memlock() function call, you might want to 
# enable this option while running MySQL to keep it locked in memory and  
# to avoid potential swapping out in case of high memory pressure. Good  
# for performance.
# 如果你的系统支持 memlock() 函数,你也许希望打开此选项用以让运行中的mysql在在内存高度紧张的时候,数据在内存中保持锁定并且防止可能被swapping out 
# 此选项对于性能有益  
#memlock
# Table type which is used by default when creating new tables, if not 
# specified differently during the CREATE TABLE statement.
# 当创建新表时作为默认使用的表类型, 
# 如果在创建表示没有特别执行表类型,将会使用此值  
default-storage-engine = MYISAM
# Thread stack size to use. This amount of memory is always reserved at 
# connection time. MySQL itself usually needs no more than 64K of  
# memory, while if you use your own stack hungry UDF functions or your  
# OS requires more stack for some operations, you might need to set this  
# to a higher value.
# 线程使用的堆大小. 此容量的内存在每次连接时被预留. 
# MySQL 本身常不会需要超过64K的内存  
# 如果你使用你自己的需要大量堆的UDF函数  
# 或者你的操作系统对于某些操作需要更多的堆,  
# 你也许需要将其设置的更高一点.  
thread_stack = 192K
# Set the default transaction isolation level. Levels available are:
# 设定默认的事务隔离级别.可用的级别如下: 
# READ-UNCOMMITTED, READ-COMMITTED, REPEATABLE-READ, SERIALIZABLE  
transaction_isolation = REPEATABLE-READ
# Maximum size for internal (in-memory) temporary tables. If a table 
# grows larger than this value, it is automatically converted to disk  
# based table This limitation is for a single table. There can be many  
# of them.
# 内部(内存中)临时表的最大大小 
# 如果一个表增长到比此值更大,将会自动转换为基于磁盘的表.  
# 此限制是针对单个表的,而不是总和.  
tmp_table_size = 64M
# Enable binary logging. This is required for acting as a MASTER in a 
# replication configuration. You also need the binary log if you need  
# the ability to do point in time recovery from your latest backup.
# 打开二进制日志功能. 
# 在复制(replication)配置中,作为MASTER主服务器必须打开此项  
# 如果你需要从你最后的备份中做基于时间点的恢复,你也同样需要二进制日志.  
log-bin=mysql-bin
# binary logging format - mixed recommended
#设定记录二进制日志的格式,有三种格式,基于语句 statement、 基于行 row、 混合方式 mixed 
binlog_format=mixed
# If you're using replication with chained slaves (A->B->C), you need to 
# enable this option on server B. It enables logging of updates done by  
# the slave thread into the slave's binary log.
# 如果你在使用链式从服务器结构的复制模式 (A->B->C), 
# 你需要在服务器B上打开此项.  
# 此选项打开在从线程上重做过的更新的日志,  
# 并将其写入从服务器的二进制日志.  
#log_slave_updates
# Enable the full query log. Every query (even ones with incorrect 
# syntax) that the server receives will be logged. This is useful for  
# debugging, it is usually disabled in production use.
# 打开查询日志. 所有的由服务器接收到的查询 (甚至对于一个错误语法的查询) 
# 都会被记录下来. 这对于调试非常有用, 在生产环境中常常关闭此项.  
#log #默认是没有开启的,会影响服务器性能
# Print warnings to the error log file.  If you have any problem with 
# MySQL you should enable logging of warnings and examine the error log  
# for possible explanations.
# 将警告打印输出到错误log文件. 如果你对于MySQL有任何问题 
# 你应该打开警告log并且仔细审查错误日志,查出可能的原因.  
#log_warnings
# Log slow queries. Slow queries are queries which take more than the 
# amount of time defined in "long_query_time" or which do not use  
# indexes well, if log_short_format is not enabled. It is normally good idea  
# to have this turned on if you frequently add new queries to the  
# system.
# 记录慢速查询. 慢速查询是指消耗了比 “long_query_time” 定义的更多时间的查询. 
# 如果 log_long_format 被打开,那些没有使用索引的查询也会被记录.  
# 如果你经常增加新查询到已有的系统内的话. 一般来说这是一个好主意  
slow_query_log
# All queries taking more than this amount of time (in seconds) will be 
# trated as slow. Do not use "1" as a value here, as this will result in  
# even very fast queries being logged from time to time (as MySQL  
# currently measures time with second accuracy only).  
# 所有的使用了比这个时间(以秒为单位)更多的查询会被认为是慢速查询.  
# 不要在这里使用”1″, 否则会导致所有的查询,甚至非常快的查询页被记录下来(由于MySQL 目前时间的精确度只能达到秒的级别).
long_query_time = 2
# ***  Replication related settings  # *** 主从复制相关的设置
# Unique server identification number between 1 and 2^32-1. This value 
# is required for both master and slave hosts. It defaults to 1 if  
# "master-host" is not set, but will MySQL will not function as a master  
# if it is omitted.
# 唯一的服务辨识号,数值位于 1 到 2^32-1之间. 
# 此值在master和slave上都需要设置.  
# 如果 “master-host” 没有被设置,则默认为1, 但是如果忽略此选项,MySQL不会作为master生效.  
server-id = 1
# Replication Slave (comment out master section to use this) #复制的Slave (去掉master段的注释来使其生效) 
#  
# To configure this host as a replication slave, you can choose between  
# two methods : #为了配置此主机作为复制的slave服务器,你可以选择两种方法:  
#  
# 1) Use the CHANGE MASTER TO command (fully described in our manual) -  
#    the syntax is: #使用 CHANGE MASTER TO 命令 (在我们的手册中有完整描述) -  
# 语法如下:
# 
#    CHANGE MASTER TO MASTER_HOST=<host>, MASTER_PORT=<port>,  
#    MASTER_USER=<user>, MASTER_PASSWORD=<password> ;  
#  
#    where you replace <host>, <user>, <password> by quoted strings and  
#    <port> by the master's port number (3306 by default).  
#    你需要替换掉 , , 等被尖括号包围的字段以及使用master的端口号替换 (默认3306).  
#    Example: 案例  
#  
#    CHANGE MASTER TO MASTER_HOST='125.564.12.1', MASTER_PORT=3306,  
#    MASTER_USER='joe', MASTER_PASSWORD='secret';  
#  
# OR 或者  
#  
# 2) Set the variables below. However, in case you choose this method, then  
#    start replication for the first time (even unsuccessfully, for example  
#    if you mistyped the password in master-password and the slave fails to  
#    connect), the slave will create a master.info file, and any later  
#    changes in this file to the variable values below will be ignored and  
#    overridden by the content of the master.info file, unless you shutdown  
#    the slave server, delete master.info and restart the slaver server.  
#    For that reason, you may want to leave the lines below untouched  
#    (commented) and instead use CHANGE MASTER TO (see above)  
#
#设置以下的变量. 不论如何, 在你选择这种方法的情况下, 然后第一次启动复制(甚至不成功的情况下, 
# 例如如果你输入错密码在master-password字段并且slave无法连接),  
# slave会创建一个 master.info 文件,并且之后任何对于包含在此文件内的参数的变化都会被忽略  
# 并且由 master.info 文件内的内容覆盖, 除非你关闭slave服务, 删除 master.info 并且重启slave 服务.  
# 由于这个原因,你也许不想碰一下的配置(注释掉的) 并且使用 CHANGE MASTER TO (查看上面) 来代替
# 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
# 所需要的唯一id号位于 2 和 2^32 – 1之间 
# (并且和master不同)  
# 如果master-host被设置了.则默认值是2  
# 但是如果省略,则不会生效  
#server-id = 2  
#  
# The replication master for this slave – required
# 复制结构中的master – 必须 
#master-host = <hostname>  
#  
# The username the slave will use for authentication when connecting  
# to the master – required
# 当连接到master上时slave所用来认证的用户名 – 必须
#master-user = <username>  
#  
# The password the slave will authenticate with when connecting to
# the master – required
# 当连接到master上时slave所用来认证的密码 – 必须
#master-password = <password> 
#  
# The port the master is listening on.  
# optional - defaults to 3306
# master监听的端口. 
# 可选 – 默认是3306  
#master-port = <port>
# Make the slave read-only. Only users with the SUPER privilege and the 
# replication slave thread will be able to modify data on it. You can  
# use this to ensure that no applications will accidently modify data on  
# the slave instead of the master
# 使得slave只读.只有用户拥有SUPER权限和在上面的slave线程能够修改数据. 
# 你可以使用此项去保证没有应用程序会意外的修改slave而不是master上的数据
#read_only
#*** MyISAM Specific options
#*** MyISAM 相关选项 
# Size of the Key Buffer, used to cache index blocks for MyISAM tables.  
# Do not set it larger than 30% of your available memory, as some memory  
# is also required by the OS to cache rows. Even if you're not using  
# MyISAM tables, you should still set it to 8-64M as it will also be  
# used for internal temporary disk tables.
# 关键词缓冲的大小, 一般用来缓冲MyISAM表的索引块. 
# 不要将其设置大于你可用内存的30%,  
# 因为一部分内存同样被OS用来缓冲行数据  
# 甚至在你并不使用MyISAM 表的情况下, 你也需要仍旧设置起 8-64M 内存由于它同样会被内部临时磁盘表使用.  
key_buffer_size = 32M
# MyISAM uses special tree-like cache to make bulk inserts (that is, 
# INSERT ... SELECT, INSERT ... VALUES (...), (...), ..., and LOAD DATA  
# INFILE) faster. This variable limits the size of the cache tree in  
# bytes per thread. Setting it to 0 will disable this optimisation.  Do  
# not set it larger than "key_buffer_size" for optimal performance.  
# This buffer is allocated when a bulk insert is detected.
# MyISAM 使用特殊的类似树的cache来使得突发插入 
# (这些插入是,INSERT … SELECT, INSERT … VALUES (…), (…), …, 以及 LOAD DATA  
# INFILE) 更快. 此变量限制每个进程中缓冲树的字节数.  
# 设置为 0 会关闭此优化.  
# 为了最优化不要将此值设置大于 “key_buffer_size”.  
# 当突发插入被检测到时此缓冲将被分配.  
bulk_insert_buffer_size = 64M
# This buffer is allocated when MySQL needs to rebuild the index in 
# REPAIR, OPTIMIZE, ALTER table statements as well as in LOAD DATA INFILE  
# into an empty table. It is allocated per thread so be careful with  
# large settings.
# 此缓冲当MySQL需要在 REPAIR, OPTIMIZE, ALTER 以及 LOAD DATA INFILE 到一个空表中引起重建索引时被分配. 
# 这在每个线程中被分配.所以在设置大值时需要小心.  
myisam_sort_buffer_size = 128M
# The maximum size of the temporary file MySQL is allowed to use while 
# recreating the index (during REPAIR, ALTER TABLE or LOAD DATA INFILE.  
# If the file-size would be bigger than this, the index will be created  
# through the key cache (which is slower).
# MySQL重建索引时所允许的最大临时文件的大小 (当 REPAIR, ALTER TABLE 或者 LOAD DATA INFILE). 
# 如果文件大小比此值更大,索引会通过键值缓冲创建(更慢)  
myisam_max_sort_file_size = 10G
# If a table has more than one index, MyISAM can use more than one 
# thread to repair them by sorting in parallel. This makes sense if you  
# have multiple CPUs and plenty of memory.
# 如果一个表拥有超过一个索引, MyISAM 可以通过并行排序使用超过一个线程去修复他们. 
# 这对于拥有多个CPU以及大量内存情况的用户,是一个很好的选择.  
myisam_repair_threads = 1
# Automatically check and repair not properly closed MyISAM tables.
# 自动检查和修复没有适当关闭的 MyISAM 表. 
myisam_recover
                          
# *** INNODB Specific options ***
# *** INNODB 相关选项 ***
# Use this option if you have a MySQL server with InnoDB support enabled 
# but you do not plan to use it. This will save memory and disk space  
# and speed up some things.
# 如果你的MySQL服务包含InnoDB支持但是并不打算使用的话, 
# 使用此选项会节省内存以及磁盘空间,并且加速某些部分  
#skip-innodb
# Additional memory pool that is used by InnoDB to store metadata 
# information.  If InnoDB requires more memory for this purpose it will  
# start to allocate it from the OS.  As this is fast enough on most  
# recent operating systems, you normally do not need to change this  
# value. SHOW INNODB STATUS will display the current amount used.
# 附加的内存池被InnoDB用来保存 metadata 信息 
# 如果InnoDB为此目的需要更多的内存,它会开始从OS这里申请内存.  
# 由于这个操作在大多数现代操作系统上已经足够快, 你一般不需要修改此值.  
# SHOW INNODB STATUS 命令会显示当先使用的数量.  
innodb_additional_mem_pool_size = 16M
# InnoDB, unlike MyISAM, uses a buffer pool to cache both indexes and 
# row data. The bigger you set this the less disk I/O is needed to  
# access data in tables. On a dedicated database server you may set this  
# parameter up to 80% of the machine physical memory size. Do not set it  
# too large, though, because competition of the physical memory may  
# cause paging in the operating system.  Note that on 32bit systems you  
# might be limited to 2-3.5G of user level memory per process, so do not  
# set it too high.  
# InnoDB使用一个缓冲池来保存索引和原始数据, 不像 MyISAM.  
# 这里你设置越大,你在存取表里面数据时所需要的磁盘I/O越少.  
# 在一个独立使用的数据库服务器上,你可以设置这个变量到服务器物理内存大小的80%  
# 不要设置过大,否则,由于物理内存的竞争可能导致操作系统的换页颠簸.  
# 注意在32位系统上你每个进程可能被限制在 2-3.5G 用户层面内存限制,  
# 所以不要设置的太高.
innodb_buffer_pool_size = 2G
# InnoDB stores data in one or more data files forming the tablespace. 
# If you have a single logical drive for your data, a single  
# autoextending file would be good enough. In other cases, a single file  
# per device is often a good choice. You can configure InnoDB to use raw  
# disk partitions as well - please refer to the manual for more info  
# about this.
# InnoDB 将数据保存在一个或者多个数据文件中成为表空间. 
# 如果你只有单个逻辑驱动保存你的数据,一个单个的自增文件就足够好了.  
# 其他情况下.每个设备一个文件一般都是个好的选择.  
# 你也可以配置InnoDB来使用裸盘分区 – 请参考手册来获取更多相关内容  
innodb_data_file_path = ibdata1:10M:autoextend
# Set this option if you would like the InnoDB tablespace files to be 
# stored in another location. By default this is the MySQL datadir.
# 设置此选项如果你希望InnoDB表空间文件被保存在其他分区. 
# 默认保存在MySQL的datadir中.  
#innodb_data_home_dir = <directory>
# Number of IO threads to use for async IO operations. This value is 
# hardcoded to 8 on Unix, but on Windows disk I/O may benefit from a  
# larger number.
# 用来同步IO操作的IO线程的数量. This value is 
# 此值在Unix下被硬编码为8,但是在Windows磁盘I/O可能在一个大数值下表现的更好.  
innodb_write_io_threads = 8  
innodb_read_io_threads = 8
# If you run into InnoDB tablespace corruption, setting this to a nonzero 
# value will likely help you to dump your tables. Start from value 1 and  
# increase it until you're able to dump the table successfully.
# 如果你发现InnoDB表空间损坏, 设置此值为一个非零值可能帮助你导出你的表. 
# 从1开始并且增加此值知道你能够成功的导出表.  
#innodb_force_recovery=1
# Number of threads allowed inside the InnoDB kernel. The optimal value 
# depends highly on the application, hardware as well as the OS  
# scheduler properties. A too high value may lead to thread thrashing.
# 在InnoDb核心内的允许线程数量. 
# 最优值依赖于应用程序,硬件以及操作系统的调度方式.  
# 过高的值可能导致线程的互斥颠簸.  
innodb_thread_concurrency = 16
# If set to 1, InnoDB will flush (fsync) the transaction logs to the 
# disk at each commit, which offers full ACID behavior. If you are  
# willing to compromise this safety, and you are running small  
# transactions, you may set this to 0 or 2 to reduce disk I/O to the  
# logs. Value 0 means that the log is only written to the log file and  
# the log file flushed to disk approximately once per second. Value 2  
# means the log is written to the log file at each commit, but the log  
# file is only flushed to disk approximately once per second.
# 如果设置为1 ,InnoDB会在每次提交后刷新(fsync)事务日志到磁盘上, 
# 这提供了完整的ACID行为.  
# 如果你愿意对事务安全折衷, 并且你正在运行一个小的食物, 你可以设置此值到0或者2来减少由事务日志引起的磁盘I/O  
# 0代表日志只大约每秒写入日志文件并且日志文件刷新到磁盘.  
# 2代表日志写入日志文件在每次提交后,但是日志文件只有大约每秒才会刷新到磁盘上.  
innodb_flush_log_at_trx_commit = 1
# Speed up InnoDB shutdown. This will disable InnoDB to do a full purge 
# and insert buffer merge on shutdown. It may increase shutdown time a  
# lot, but InnoDB will have to do it on the next startup instead.
# 加速InnoDB的关闭. 这会阻止InnoDB在关闭时做全清除以及插入缓冲合并. 
# 这可能极大增加关机时间, 但是取而代之的是InnoDB可能在下次启动时做这些操作.  
#innodb_fast_shutdown
# The size of the buffer InnoDB uses for buffering log data. As soon as 
# it is full, InnoDB will have to flush it to disk. As it is flushed  
# once per second anyway, it does not make sense to have it very large  
# (even with long transactions).
# 用来缓冲日志数据的缓冲区的大小. 
# 当此值快满时, InnoDB将必须刷新数据到磁盘上.  
# 由于基本上每秒都会刷新一次,所以没有必要将此值设置的太大(甚至对于长事务而言)  
innodb_log_buffer_size = 8M
# Size of each log file in a log group. You should set the combined size 
# of log files to about 25%-100% of your buffer pool size to avoid  
# unneeded buffer pool flush activity on log file overwrite. However,  
# note that a larger logfile size will increase the time needed for the  
# recovery process.
# 在日志组中每个日志文件的大小. 
# 你应该设置日志文件总合大小到你缓冲池大小的25%~100%  
# 来避免在日志文件覆写上不必要的缓冲池刷新行为.  
# 不论如何, 请注意一个大的日志文件大小会增加恢复进程所需要的时间.  
innodb_log_file_size = 256M
# Total number of files in the log group. A value of 2-3 is usually good 
# enough.
# 在日志组中的文件总数. 
# 通常来说2~3是比较好的.  
innodb_log_files_in_group = 3
# Location of the InnoDB log files. Default is the MySQL datadir. You 
# may wish to point it to a dedicated hard drive or a RAID1 volume for  
# improved performance
# InnoDB的日志文件所在位置. 默认是MySQL的datadir. 
# 你可以将其指定到一个独立的硬盘上或者一个RAID1卷上来提高其性能  
#innodb_log_group_home_dir
# Maximum allowed percentage of dirty pages in the InnoDB buffer pool. 
# If it is reached, InnoDB will start flushing them out agressively to  
# not run out of clean pages at all. This is a soft limit, not  
# guaranteed to be held.
# 在InnoDB缓冲池中最大允许的脏页面的比例. 
# 如果达到限额, InnoDB会开始刷新他们防止他们妨碍到干净数据页面.  
# 这是一个软限制,不被保证绝对执行.  
innodb_max_dirty_pages_pct = 90
# The flush method InnoDB will use for Log. The tablespace always uses 
# doublewrite flush logic. The default value is "fdatasync", another  
# option is "O_DSYNC".
# InnoDB用来刷新日志的方法. 
# 表空间总是使用双重写入刷新方法  
# 默认值是 “fdatasync”, 另一个是 “O_DSYNC”.  
#innodb_flush_method=O_DSYNC
# How long an InnoDB transaction should wait for a lock to be granted 
# before being rolled back. InnoDB automatically detects transaction  
# deadlocks in its own lock table and rolls back the transaction. If you  
# use the LOCK TABLES command, or other transaction-safe storage engines  
# than InnoDB in the same transaction, then a deadlock may arise which  
# InnoDB cannot notice. In cases like this the timeout is useful to  
# resolve the situation.
# 在被回滚前,一个InnoDB的事务应该等待一个锁被批准多久. 
# InnoDB在其拥有的锁表中自动检测事务死锁并且回滚事务.  
# 如果你使用 LOCK TABLES 指令, 或者在同样事务中使用除了InnoDB以外的其他事务安全的存储引擎  
# 那么一个死锁可能发生而InnoDB无法注意到.  
# 这种情况下这个timeout值对于解决这种问题就非常有帮助.  
innodb_lock_wait_timeout = 120
[mysqldump]  
# Do not buffer the whole result set in memory before writing it to  
# file. Required for dumping very large tables
# 不要在将内存中的整个结果写入磁盘之前缓存. 在导出非常巨大的表时需要此项 
quick
max_allowed_packet = 16M
[mysql] 
no-auto-rehash
# Only allow UPDATEs and DELETEs that use keys.
# 仅仅允许使用键值的 UPDATEs 和 DELETEs . 
#safe-updates
[myisamchk] 
key_buffer_size = 512M  
sort_buffer_size = 512M  
read_buffer = 8M  
write_buffer = 8M
[mysqlhotcopy] 
interactive-timeout
[mysqld_safe] 
# Increase the amount of open files allowed per process. Warning: Make  
# sure you have set the global system limit high enough! The high value  
# is required for a large number of opened tables
# 增加每个进程的可打开文件数量. 
# 警告: 确认你已经将全系统限制设定的足够高!  
# 打开大量表需要将此值设大  
open-files-limit = 8192

三、配置文件优化(根据实际情况优化)

说明,上文中我对my-innodb-heavy-4G.cnf中默认的所有选项进行了说明,下面我就根据我们公司的实际情况进行优化!

1.服务器的运行环境

  • 硬件服务器:Dell R710,双至强E5620 CPU、16G内存、6*500G硬盘

  • 操作系统:CentOS5.5 X86_64 系统

  • Mysql版本:MySQL 5.5.32

  • 适用于:日IP 100-200W ,日PV 200-500W 的站点

2.具体优化配置如下

[client] 
port        = 3306  
socket        = /tmp/mysql.sock
default-character-set = utf8 #设置客户端的字符编码
[mysqld]
# generic configuration options 
port        = 3306  
socket        = /tmp/mysql.sock
#*** char set ***
character-set-server = utf8 #设置服务器端的字符编码
                       
#*** network ***
back_log = 512
#skip-networking #默认没有开启
max_connections = 3000
max_connect_errors = 30
table_open_cache = 4096
#external-locking #默认没有开启
max_allowed_packet = 32M
max_heap_table_size = 128M
                       
# *** global cache ***
read_buffer_size = 8M
read_rnd_buffer_size = 64M
sort_buffer_size = 16M
join_buffer_size = 16M
                       
# *** thread ***
thread_cache_size = 16
thread_concurrency = 8
thread_stack = 512K
                       
# *** query  cache ***
query_cache_size = 128M
query_cache_limit = 4M
                       
# *** index ***
ft_min_word_len = 8
                       
#memlock #默认没有开启
default-storage-engine = INNODB
transaction_isolation = REPEATABLE-READ
                       
# *** tmp table ***
tmp_table_size = 64M
                       
# *** bin log ***
log-bin=mysql-bin
binlog_cache_size = 4M
binlog_format=mixed
#log_slave_updates #默认没有开启
#log #默认没有开启,此处是查询日志,开启会影响服务器性能
log_warnings #开启警告日志
                       
# *** slow query log ***
slow_query_log
long_query_time = 10
# ***  Replication related settings
server-id = 1
#server-id = 2 
#master-host = <hostname>  
#master-user = <username>  
#master-password = <password>  
#master-port = <port>
#read_only
#*** MyISAM Specific options
key_buffer_size = 128M
bulk_insert_buffer_size = 256M
myisam_sort_buffer_size = 256M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
myisam_recover
                       
# *** INNODB Specific options ***
#skip-innodb #默认没有开启
innodb_additional_mem_pool_size = 64M
innodb_buffer_pool_size = 6G #注意在32位系统上你每个进程可能被限制在 2-3.5G 用户层面内存限制, 所以不要设置的太高.
innodb_data_file_path = ibdata1:10M:autoextend
#innodb_data_home_dir = <directory>
innodb_write_io_threads = 8 
innodb_read_io_threads = 8
#innodb_force_recovery=1
innodb_thread_concurrency = 16
innodb_flush_log_at_trx_commit = 2
#说明:innodb_flush_log_at_trx_commit = 2 如果是游戏服务器,建议此值设置为2;如果是对数据安全要求极高的应用,建议设置为1;设置为0性能最高,但如果发生故障,数据可能会有丢失的危险!默认值1的意思是每一次事务提交或事务外的指令都需要把日志写入(flush)硬盘,这是很费时的。特别是使用电池供电缓存(Battery backed up cache)时。设成2对于很多运用,特别是从MyISAM表转过来的是可以的,它的意思是不写入硬盘而是写入系统缓存。日志仍然会每秒flush到硬盘,所以你一般不会丢失超过1-2秒的更新。设成0会更快一点,但安全方面比较差,即使MySQL挂了也可能会丢失事务的数据。而值2只会在整个操作系统挂了时才可能丢数据。
#innodb_fast_shutdown
innodb_log_buffer_size = 16M
innodb_log_file_size = 512M
innodb_log_files_in_group = 3
#innodb_log_group_home_dir
innodb_max_dirty_pages_pct = 90
#innodb_flush_method=O_DSYNC
innodb_lock_wait_timeout = 120
[mysqldump]  
quick
max_allowed_packet = 32M
[mysql] 
no-auto-rehash
[myisamchk] 
key_buffer_size = 2048M  
sort_buffer_size = 2048M  
read_buffer = 32M  
write_buffer = 32M
[mysqlhotcopy] 
interactive-timeout
[mysqld_safe] 
open-files-limit = 10240

3.总结

MySQL 配置文件的优化是根据线上环境的实际需要进行优化,不能随便没有根据的进行优化,写这篇博文就是给博友们一些参考!

4.MySQL状态查看的常用命令



1
2
3
4
mysql> show status; #显示状态信息 
mysql> show variables; #显示系统变量  
mysql> show engines; #查看所有引擎
mysql> show engine innodb status; #显示InnoDB存储引擎的状态
a. show tables或show tables from database_name; -- 显示当前数据库中所有表的名称。
b. show databases; -- 显示mysql中所有数据库的名称。
c. show columns from table_name from database_name; 或show columns from database_name.table_name; -- 显示表中列名称。
d. show grants for user_name; -- 显示一个用户的权限,显示结果类似于grant 命令。
e. show index from table_name; -- 显示表的索引。
f. show status; -- 显示一些系统特定资源的信息,例如,正在运行的线程数量。
g. show variables; -- 显示系统变量的名称和值。
h. show processlist; -- 显示系统中正在运行的所有进程,也就是当前正在执行的查询。大多数用户可以查看他们自己的进程,但是如果他们拥有process权限,就可以查看所有人的进程,包括密码。
i. show table status; -- 显示当前使用或者指定的database中的每个表的信息。信息包括表类型和表的最新更新时间。
j. show privileges; -- 显示服务器所支持的不同权限。
k. show create database database_name; -- 显示create database 语句是否能够创建指定的数据库。
l. show create table table_name; -- 显示create database 语句是否能够创建指定的数据库。
m. show engies; -- 显示安装以后可用的存储引擎和默认引擎。
n. show innodb status; -- 显示innoDB存储引擎的状态。
o. show logs; -- 显示BDB存储引擎的日志。
p. show warnings; -- 显示最后一个执行的语句所产生的错误、警告和通知。
q. show errors; -- 只显示最后一个执行语句所产生的错误。
r. show [storage] engines; --显示安装后的可用存储引擎和默认引擎。



查看MySQL服务器配置信息:

1
show variables;

查看MySQL服务器运行的各种状态值:

1
show global status;

1. 慢查询

1
2
3
show variables like '%slow%';
show global status like '%slow%';
mysqldumpslow -s c -t 20 host -slow.log

分析慢查询日志,找出有问题的SQL语句,慢查询时间不宜设置过长,否则意义不大,最好在5秒以内,如果你需要微秒级别的慢查询,可以考虑给MySQL打补丁:http://www.percona.com/docs/wiki/release:start,记得找对应的版本。

2. 连接数

经常会遇见”MySQL: ERROR 1040: Too manyconnections” 的情况:

一种是访问量确实很高,MySQL服务器抗不住,这个时候就要考虑增加从服务器分散读压力。

一种情况是MySQL配置文件中max_connections值过小。

查看最大连接数:

1
show variables like 'max_connections';

查看mysql服务器过去的最大连接数:

1
2
show global status like 'max_used_connections';
理想值:max_used_connections / max_connections * 100% ≈ 85%

最大连接数占上限连接数的85%左右,如果发现比例在10%以下,MySQL服务器连接数上限设置的过高了。

3. key_buffer_size

key_buffer_size是对MyISAM表性能影响最大的一个参数,不过数据库中多为Innodb

查看key_buffer_size设置大小:

1
show variables like 'key_buffer_size';

查看key_buffer_size使用情况:

1
show global status like 'key_read%';

计算索引未命中缓存的概率:key_cache_miss_rate = Key_reads / Key_read_requests * 100%

key_cache_miss_rate在0.1%以下都很好(每1000个请求有一个直接读硬盘),如果key_cache_miss_rate在0.01%以下的话,key_buffer_size分配的过多,可以适当减少。

4. key_blocks_*参数

1
show global status like 'key_blocks_u%';

Key_blocks_unused:表示未使用的缓存簇(blocks)数

Key_blocks_used:表示曾经用到的最大的blocks数

理想值:Key_blocks_used / (Key_blocks_unused + Key_blocks_used) * 100% ≈ 80%

5. 临时表

当执行语句时,关于已经被创造了的隐含临时表的数量,查看命令:

1
show global status like 'created_tmp%';

每次创建表时Created_tmp_tables 都会增加,如果在磁盘上创建,Created_tmp_disk_tables也会增加,Created_tmp_files表示服务器创建的临时文件数

理想值:Created_tmp_disk_tables / Created_tmp_tables * 100% ≤25%

查看服务器对临时表的配置:

1
show variables where variable_name in('tmp_table_size','max_heap_table_size');

6.打开表的情况

1
show global status like 'open%tables%';

open_tables 表示打开表的数量,opened_tables表示打开过的表数量,如果opened_tables数量过大,说明配置中 table_cache(5.1.3之后这个值叫做table_open_cache)值可能太小

查询服务器table_cache值:

1
show variables like 'table_open_cache';

理想值:open_tables / opened_tables * 100% ≥ 85%

理想值:open_tables / table_cache * 100% ≤95%

7. 进程使用情况

如果我们在MySQL服务器配置文件中设置了thread_cache_size,当客户端断开之后,服务器处理此客户的线程将会缓存起来以响应下一个客户而不是销毁(前提是缓存数未达上限)。Threads_created表示创建过的线程数:

1
show global status like 'thread%';

如果发现Threads_created值过大的话,表明 MySQL服务器一直在创建线程,这也是比较耗资源,可以适当增加配置文件中thread_cache_size值,查询服务器 thread_cache_size配置:

1
show variables like 'thread_cache_size';

8. 查询缓存(query cache)

查看服务器query_cache配置情况:

1
show variables like 'query_cache%';

参数解释:

query_cache_limit:超过此大小的查询将不缓存。

query_cache_min_res_unit:缓存块的最小值。

query_cache_size:查询缓存大小。

query_cache_type:缓存类型,决定缓存什么样的查询。

query_cache_wlock_invalidate:表示当前客户端,正在对MyISAM表进行写操作时,读请求是要等SRITE LOCK释放资源后再查询,还是允许直接从Query Cache中读取结果,默认为OFF(可以直接从Query Cache 中读取结果)。

查看服务器query_cache使用情况:

1
show global status like 'qcache%';

参数解释:

Qcache_free_blocks:缓存中相邻内存块的个数。数目大说明可能有碎片。FLUSH QUERY CACHE会对缓存中的碎片进行整理,从而得到一个空闲块。

Qcache_free_memory:缓存中的空闲内存。

Qcache_hits:每次查询在缓存中命中时就增大

Qcache_inserts:每次插入一个查询时就增大。命中次数除以插入次数就是不中比率。

Qcache_lowmem_prunes:缓存出现内存不足并且必须要进行清理以便为更多查询提供空间的次数。这个数字最好长时间来看;如果这个数字不断增长,表示可能碎片非常严重,或内存很少。

Qcache_not_cached:不适合进行缓存的查询的数量,通常是由于这些查询不是 SELECT 语句或者用了now()之类的函数。

Qcache_queries_in_cache:当前缓存的查询(和响应)的数量。

Qcache_total_blocks:缓存中块的数量。

9. 排序使用情况

它表示系统中对数据进行排序时所使用Buffer,查看命令:

1
show global status like 'sort%';

增大sort_buffer_size 会减少Sort_merge_passes和创建临时文件的次数,但盲目增加并不一定能提高速度。

10. 文件打开数

当open_files大于open_files_limit值时,mysql数据库就会发生卡住的现象,导致web服务器打开不响应的页面。

查看open_files命令:

1
show global status like 'open_files';

查看open_files_limit命令:

1
show variables like 'open_files_limit';

理想值:open_files / open_files_limit *100% ≤ 75%

11.Innodb_buffer_pool_size 的合理设置

1
show status like 'Innodb_bufferpool_%';

根据实际运行场景进行调整

read 命中率:

(Innodb_buffer_pool_read_requests - Innodb_buffer_pool_reads) /  Innodb_buffer_pool_read_requests = ?

write 命中率:

Innodb_buffer_pool_pages_data / Innodb_buffer_pool_pages_total

原文地址:https://www.cnblogs.com/lxwphp/p/15454285.html