欢迎来到福编程网,本站提供各种互联网专业知识!
您的位置:网站首页 > 数据库 > Mysql

MySQL 5.5.49 大内存优化配置文件优化详解

发布时间:2016-05-16 作者:陈明乾 来源:转载
最近mysql服务器升级到了MySQL5.5.49版本,性能比mysql5.0.**肯定效率高了不少,但mysql的默认配置文件不合理,这里是针对大内存访问量大的机器的配置方案,需要的朋友可以参考下

一、配置文件说明

my-small.cnf

my-medium.cnf

my-large.cnf

my-huge.cnf

my-innodb-heavy-4G.cnf

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

三、配置文件优化

注:环境说明,CentO5.5 x86_64+MySQL-5.5.32 相关软件下载:http://yunpan.cn/QtaCuLHLRKzRq

一、配置文件说明

Mysql-5.5.49是Mysql5.5系列中最后一个版本,也是最后一个有配置文件的版本,为什么这么说呢,用过5.6的博友都知道,在mysql5.6中已经不提供配置文件选择,只有一个默认的配置文件,好了,我们今天说的是5.5.49这个版,就不和大家说5.6了,下面我们来具体说一下,mysql5.5.49中,提供可选的几个配置文件,

my-small.cnf

my-medium.cnf

my-large.cnf

my-huge.cnf

my-innodb-heavy-4G.cnf

下面我们就来分别的看一下^_^……

1.my-small.cnf

代码
  1. [root@mysql support-files]# vim my-small.cnf
  2. # Example MySQL config file for small systems.
  3. # This is for a system with little memory (<= 64M) where MySQL is only used
  4. # from time to time and it's important that the mysqld daemon
  5. # doesn't use much resources.

这是my-small.cnf配置文件中开头的简介,它说明了,这个配置文件是在内存小于等于64M时使用的,小型数据库系统,目的是不占更多的系统资源!

2.my-medium.cnf

代码
  1. [root@mysql support-files]# vim my-medium.cnf
  2. # Example MySQL config file for medium systems.
  3. # This is for a system with little memory (32M - 64M) where MySQL plays
  4. # an important part, or systems up to 128M where MySQL is used together with
  5. # other programs (such as a web server)

这个配置文件是中型数据系统使用,内存在128M左右!

3.my-large.cnf

代码
  1. [root@mysql support-files]# vim my-large.cnf
  2. # Example MySQL config file for large systems.
  3. # This is for a large system with memory = 512M where the system runs mainly
  4. # MySQL.

这个配置文件是大型数据库系统使用,内存在512M左右!

4.my-huge.cnf

代码
  1. [root@mysql support-files]# vim my-huge.cnf
  2. # Example MySQL config file for very large systems.
  3. # This is for a large system with memory of 1G-2G where the system runs mainly
  4. # MySQL.

这个配置文件是巨型数据库系统使用,内存在1G-2G左右!

5.my-innodb-heavy-4G.cnf

代码
  1. [root@mysql support-files]# vim my-innodb-heavy-4G.cnf
  2. #BEGIN CONFIG INFO
  3. #DESCR: 4GB RAM, InnoDB only, ACID, few connections, heavy queries
  4. #TYPE: SYSTEM
  5. #END CONFIG INFO
  6. # This is a MySQL example config file for systems with 4GB of memory
  7. # running mostly MySQL using InnoDB only tables and performing complex
  8. # queries with few connections.

这个配置文件主要作用是,支持4G内存,支持InnoDB引擎,支持事务(ACID)等特性所使用!

说明:ACID,指数据库事务正确执行的四个基本要素的缩写。包含:原子性(Atomicity)、一致性(Consistency)、隔离性(Isolation)、持久性(Durability)!

6.总结

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

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

1.详细说明

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

代码
  1. [root@mysql support-files]# vim my-innodb-heavy-4G.cnf
  2. #BEGIN CONFIG INFO
  3. #DESCR: 4GB RAM, InnoDB only, ACID, few connections, heavy queries
  4. #TYPE: SYSTEM
  5. #END CONFIG INFO
  6. #
  7. # This is a MySQL example config file for systems with 4GB of memory
  8. # running mostly MySQL using InnoDB only tables and performing complex
  9. # queries with few connections.
  10. #
  11. # MySQL programs look for option files in a set of
  12. # locations which depend on the deployment platform.
  13. # You can copy this option file to one of those
  14. # locations. For information about these locations, see:
  15. # http://dev.mysql.com/doc/mysql/en/option-files.html
  16. #
  17. # In this file, you can use all long options that a program supports.
  18. # If you want to know which options a program supports, run the program
  19. # with the "--help" option.
  20. #
  21. # More detailed information about the individual options can also be
  22. # found in the manual.
  23. #
  24. #
  25. # The following options will be read by MySQL client applications.
  26. # Note that only client applications shipped by MySQL are guaranteed
  27. # to read this section. If you want your own MySQL client program to
  28. # honor these values, you need to specify it as an option during the
  29. # MySQL client library initialization.
  30. #
  31. # 以下选项会被MySQL客户端应用读取, 注意只有MySQL附带的客户端应用程序保证可以读取这段内容,如果你想你自己的MySQL应用程序获取这些值,需要在MySQL客户端库初始化的时候指定这些选项
  32. [client]
  33. #password = [your_password] #mysql客户端连接mysql时的密码
  34. port = 3306 #mysql客户端连接时的默认端口
  35. socket = /tmp/mysql.sock #与mysql服务器本地通信所使用的socket文件路径
  36. # *** Application-specific options follow here ***
  37. #
  38. # The MySQL server
  39. #
  40. [mysqld]
  41. # generic configuration options #一般配置选项
  42. port = 3306 #mysql服务器监听的默认端口
  43. socket = /tmp/mysql.sock #socket本地通信文件路径
  44. # back_log is the number of connections the operating system can keep in
  45. # the listen queue, before the MySQL connection manager thread has
  46. # processed them. If you have a very high connection rate and experience
  47. # "connection refused" errors, you might need to increase this value.
  48. # Check your OS documentation for the maximum value of this parameter.
  49. # Attempting to set back_log higher than your operating system limit
  50. # will have no effect.
  51. # back_log 是操作系统在监听队列中所能保持的连接数,
  52. # 队列保存了在MySQL连接管理器线程处理之前的连接.
  53. # 如果你有非常高的连接率并且出现“connection refused”报错,
  54. # 你就应该增加此处的值.
  55. # 检查你的操作系统能打开文件数来获取这个变量的最大值.
  56. # 如果将back_log设定到比你操作系统限制更高的值,将会没有效果
  57. back_log = 50
  58. # Don't listen on a TCP/IP port at all. This can be a security
  59. # enhancement, if all processes that need to connect to mysqld run
  60. # on the same host. All interaction with mysqld must be made via Unix
  61. # sockets or named pipes.
  62. # Note that using this option without enabling named pipes on Windows
  63. # (via the "enable-named-pipe" option) will render mysqld useless!
  64. # 不在TCP/IP端口上进行监听.
  65. # 如果所有的进程都是在同一台服务器连接到本地的mysqld,
  66. # 这样设置将是增强安全的方法
  67. # 所有mysqld的连接都是通过Unix sockets 或者命名管道进行的.
  68. # 注意在windows下如果没有打开命名管道选项而只是用此项
  69. # (通过 “enable-named-pipe” 选项) 将会导致mysql服务没有任何作用!
  70. #skip-networking #默认是没有开启的
  71. # The maximum amount of concurrent sessions the MySQL server will
  72. # allow. One of these connections will be reserved for a user with
  73. # SUPER privileges to allow the administrator to login even if the
  74. # connection limit has been reached.
  75. # MySQL 服务器所允许的同时会话数的上限
  76. # 其中一个连接将被SUPER权限保留作为管理员登录.
  77. # 即便已经达到了连接数的上限.
  78. max_connections = 100
  79. # Maximum amount of errors allowed per host. If this limit is reached,
  80. # the host will be blocked from connecting to the MySQL server until
  81. # "FLUSH HOSTS" has been run or the server was restarted. Invalid
  82. # passwords and other errors during the connect phase result in
  83. # increasing this value. See the "Aborted_connects" status variable for
  84. # global counter.
  85. # 每个客户端连接最大的错误允许数量,如果达到了此限制.
  86. # 这个客户端将会被MySQL服务阻止直到执行了”FLUSH HOSTS” 或者服务重启
  87. # 非法的密码以及其他在链接时的错误会增加此值.
  88. # 查看 “Aborted_connects” 状态来获取全局计数器.
  89. max_connect_errors = 10
  90. # The number of open tables for all threads. Increasing this value
  91. # increases the number of file descriptors that mysqld requires.
  92. # Therefore you have to make sure to set the amount of open files
  93. # allowed to at least 4096 in the variable "open-files-limit" in
  94. # section [mysqld_safe]
  95. # 所有线程所打开表的数量.
  96. # 增加此值就增加了mysqld所需要的文件描述符的数量
  97. # 这样你需要确认在[mysqld_safe]中 “open-files-limit” 变量设置打开文件数量允许至少2048
  98. table_open_cache = 2048
  99. # Enable external file level locking. Enabled file locking will have a
  100. # negative impact on performance, so only use it in case you have
  101. # multiple database instances running on the same files (note some
  102. # restrictions still apply!) or if you use other software relying on
  103. # locking MyISAM tables on file level.
  104. # 允许外部文件级别的锁. 打开文件锁会对性能造成负面影响
  105. # 所以只有在你在同样的文件上运行多个数据库实例时才使用此选项(注意仍会有其他约束!)
  106. # 或者你在文件层面上使用了其他一些软件依赖来锁定MyISAM表
  107. #external-locking #默认是没有开启的
  108. # The maximum size of a query packet the server can handle as well as
  109. # maximum query size server can process (Important when working with
  110. # large BLOBs). enlarged dynamically, for each connection.
  111. # 服务所能处理的请求包的最大大小以及服务所能处理的最大的请求大小(当与大的BLOB字段一起工作时相当必要)
  112. # 每个连接独立的大小.大小动态增加
  113. max_allowed_packet = 16M
  114. # The size of the cache to hold the SQL statements for the binary log
  115. # during a transaction. If you often use big, multi-statement
  116. # transactions you can increase this value to get more performance. All
  117. # statements from transactions are buffered in the binary log cache and
  118. # are being written to the binary log at once after the COMMIT. If the
  119. # transaction is larger than this value, temporary file on disk is used
  120. # instead. This buffer is allocated per connection on first update
  121. # statement in transaction
  122. # 在一个事务中binlog为了记录SQL状态所持有的cache大小
  123. # 如果你经常使用大的,多声明的事务,你可以增加此值来获取更大的性能.
  124. # 所有从事务来的状态都将被缓冲在binlog缓冲中然后在提交后一次性写入到binlog中
  125. # 如果事务比此值大, 会使用磁盘上的临时文件来替代.
  126. # 此缓冲在每个连接的事务第一次更新状态时被创建
  127. binlog_cache_size = 1M
  128. # Maximum allowed size for a single HEAP (in memory) table. This option
  129. # is a protection against the accidential creation of a very large HEAP
  130. # table which could otherwise use up all memory resources.
  131. # 独立的内存表所允许的最大容量.
  132. # 此选项为了防止意外创建一个超大的内存表导致永尽所有的内存资源.
  133. max_heap_table_size = 64M
  134. # Size of the buffer used for doing full table scans.
  135. # Allocated per thread, if a full scan is needed.
  136. #MySql读入缓冲区大校对表进行顺序扫描的请求将分配一个读入缓冲区,MySql会为它分#配一段内存缓冲区。read_buffer_size变量控制这一缓冲区的大校如果对表的顺序扫描请求非常频繁,#并且你认为频繁扫描进行得太慢,可以通过增加该变量值以及内存缓冲区大小提高其性能。
  137. read_buffer_size = 2M
  138. # When reading rows in sorted order after a sort, the rows are read
  139. # through this buffer to avoid disk seeks. You can improve ORDER BY
  140. # performance a lot, if set this to a high value.
  141. # Allocated per thread, when needed.
  142. #是MySql的随机读缓冲区大校当按任意顺序读取行时(例如,按照排序顺序),将分配一个随机读缓存区。进行排序查询时,MySql会首先扫描一遍该缓冲,以避免磁盘搜索,提高查询速度,如果需#要排序大量数据,可适当调高该值。但MySql会为每个客户连接发放该缓冲空间,所以应尽量适当设置该值,以避免内存开销过大。
  143. read_rnd_buffer_size = 16M
  144. # Sort buffer is used to perform sorts for some ORDER BY and GROUP BY
  145. # queries. If sorted data does not fit into the sort buffer, a disk
  146. # based merge sort is used instead - See the "Sort_merge_passes"
  147. # status variable. Allocated per thread if sort is needed.
  148. # 排序缓冲被用来处理类似ORDER BY以及GROUP BY队列所引起的排序
  149. # 如果排序后的数据无法放入排序缓冲,
  150. # 一个用来替代的基于磁盘的合并分类会被使用
  151. # 查看 “Sort_merge_passes” 状态变量.
  152. # 在排序发生时由每个线程分配
  153. sort_buffer_size = 8M
  154. # This buffer is used for the optimization of full JOINs (JOINs without
  155. # indexes). Such JOINs are very bad for performance in most cases
  156. # anyway, but setting this variable to a large value reduces the
  157. # performance impact. See the "Select_full_join" status variable for a
  158. # count of full JOINs. Allocated per thread if full join is found
  159. # 此缓冲被使用来优化全联合(full JOINs 不带索引的联合).
  160. # 类似的联合在极大多数情况下有非常糟糕的性能表现,
  161. # 但是将此值设大能够减轻性能影响.
  162. # 通过 “Select_full_join” 状态变量查看全联合的数量
  163. # 当全联合发生时,在每个线程中分配
  164. join_buffer_size = 8M
  165. # How many threads we should keep in a cache for reuse. When a client
  166. # disconnects, the client's threads are put in the cache if there aren't
  167. # more than thread_cache_size threads from before. This greatly reduces
  168. # the amount of thread creations needed if you have a lot of new
  169. # connections. (Normally this doesn't give a notable performance
  170. # improvement if you have a good thread implementation.)
  171. # 我们在cache中保留多少线程用于重用
  172. # 当一个客户端断开连接后,如果cache中的线程还少于thread_cache_size,
  173. # 则客户端线程被放入cache中.
  174. # 这可以在你需要大量新连接的时候极大的减少线程创建的开销
  175. # (一般来说如果你有好的线程模型的话,这不会有明显的性能提升.)
  176. thread_cache_size = 8
  177. # This permits the application to give the threads system a hint for the
  178. # desired number of threads that should be run at the same time. This
  179. # value only makes sense on systems that support the thread_concurrency()
  180. # function call (Sun Solaris, for example).
  181. # You should try [number of CPUs]*(2..4) for thread_concurrency
  182. # 此允许应用程序给予线程系统一个提示在同一时间给予渴望被运行的线程的数量.
  183. # 此值只对于支持 thread_concurrency() 函数的系统有意义( 例如Sun Solaris).
  184. # 你可可以尝试使用 [CPU数量]*(2..4) 来作为thread_concurrency的值
  185. thread_concurrency = 8
  186. # Query cache is used to cache SELECT results and later return them
  187. # without actual executing the same query once again. Having the query
  188. # cache enabled may result in significant speed improvements, if your
  189. # have a lot of identical queries and rarely changing tables. See the
  190. # "Qcache_lowmem_prunes" status variable to check if the current value
  191. # is high enough for your load.
  192. # Note: In case your tables change very often or if your queries are
  193. # textually different every time, the query cache may result in a
  194. # slowdown instead of a performance improvement.
  195. # 查询缓冲常被用来缓冲 SELECT 的结果并且在下一次同样查询的时候不再执行直接返回结果.
  196. # 打开查询缓冲可以极大的提高服务器速度, 如果你有大量的相同的查询并且很少修改表.
  197. # 查看 “Qcache_lowmem_prunes” 状态变量来检查是否当前值对于你的负载来说是否足够高.
  198. # 注意: 在你表经常变化的情况下或者如果你的查询原文每次都不同,
  199. # 查询缓冲也许引起性能下降而不是性能提升.
  200. query_cache_size = 64M
  201. # Only cache result sets that are smaller than this limit. This is to
  202. # protect the query cache of a very large result set overwriting all
  203. # other query results.
  204. # 只有小于此设定值的结果才会被缓冲
  205. # 此设置用来保护查询缓冲,防止一个极大的结果集将其他所有的查询结果都覆盖.
  206. query_cache_limit = 2M
  207. # Minimum word length to be indexed by the full text search index.
  208. # You might wish to decrease it if you need to search for shorter words.
  209. # Note that you need to rebuild your FULLTEXT index, after you have
  210. # modified this value.
  211. # 被全文检索索引的最小的字长.
  212. # 你也许希望减少它,如果你需要搜索更短字的时候.
  213. # 注意在你修改此值之后,
  214. # 你需要重建你的 FULLTEXT 索引
  215. ft_min_word_len = 4
  216. # If your system supports the memlock() function call, you might want to
  217. # enable this option while running MySQL to keep it locked in memory and
  218. # to avoid potential swapping out in case of high memory pressure. Good
  219. # for performance.
  220. # 如果你的系统支持 memlock() 函数,你也许希望打开此选项用以让运行中的mysql在在内存高度紧张的时候,数据在内存中保持锁定并且防止可能被swapping out
  221. # 此选项对于性能有益
  222. #memlock
  223. # Table type which is used by default when creating new tables, if not
  224. # specified differently during the CREATE TABLE statement.
  225. # 当创建新表时作为默认使用的表类型,
  226. # 如果在创建表示没有特别执行表类型,将会使用此值
  227. default-storage-engine = MYISAM
  228. # Thread stack size to use. This amount of memory is always reserved at
  229. # connection time. MySQL itself usually needs no more than 64K of
  230. # memory, while if you use your own stack hungry UDF functions or your
  231. # OS requires more stack for some operations, you might need to set this
  232. # to a higher value.
  233. # 线程使用的堆大小. 此容量的内存在每次连接时被预留.
  234. # MySQL 本身常不会需要超过64K的内存
  235. # 如果你使用你自己的需要大量堆的UDF函数
  236. # 或者你的操作系统对于某些操作需要更多的堆,
  237. # 你也许需要将其设置的更高一点.
  238. thread_stack = 192K
  239. # Set the default transaction isolation level. Levels available are:
  240. # 设定默认的事务隔离级别.可用的级别如下:
  241. # READ-UNCOMMITTED, READ-COMMITTED, REPEATABLE-READ, SERIALIZABLE
  242. transaction_isolation = REPEATABLE-READ
  243. # Maximum size for internal (in-memory) temporary tables. If a table
  244. # grows larger than this value, it is automatically converted to disk
  245. # based table This limitation is for a single table. There can be many
  246. # of them.
  247. # 内部(内存中)临时表的最大大小
  248. # 如果一个表增长到比此值更大,将会自动转换为基于磁盘的表.
  249. # 此限制是针对单个表的,而不是总和.
  250. tmp_table_size = 64M
  251. # Enable binary logging. This is required for acting as a MASTER in a
  252. # replication configuration. You also need the binary log if you need
  253. # the ability to do point in time recovery from your latest backup.
  254. # 打开二进制日志功能.
  255. # 在复制(replication)配置中,作为MASTER主服务器必须打开此项
  256. # 如果你需要从你最后的备份中做基于时间点的恢复,你也同样需要二进制日志.
  257. log-bin=mysql-bin
  258. # binary logging format - mixed recommended
  259. #设定记录二进制日志的格式,有三种格式,基于语句 statement、 基于行 row、 混合方式 mixed
  260. binlog_format=mixed
  261. # If you're using replication with chained slaves (A->B->C), you need to
  262. # enable this option on server B. It enables logging of updates done by
  263. # the slave thread into the slave's binary log.
  264. # 如果你在使用链式从服务器结构的复制模式 (A->B->C),
  265. # 你需要在服务器B上打开此项.
  266. # 此选项打开在从线程上重做过的更新的日志,
  267. # 并将其写入从服务器的二进制日志.
  268. #log_slave_updates
  269. # Enable the full query log. Every query (even ones with incorrect
  270. # syntax) that the server receives will be logged. This is useful for
  271. # debugging, it is usually disabled in production use.
  272. # 打开查询日志. 所有的由服务器接收到的查询 (甚至对于一个错误语法的查询)
  273. # 都会被记录下来. 这对于调试非常有用, 在生产环境中常常关闭此项.
  274. #log #默认是没有开启的,会影响服务器性能
  275. # Print warnings to the error log file. If you have any problem with
  276. # MySQL you should enable logging of warnings and examine the error log
  277. # for possible explanations.
  278. # 将警告打印输出到错误log文件. 如果你对于MySQL有任何问题
  279. # 你应该打开警告log并且仔细审查错误日志,查出可能的原因.
  280. #log_warnings
  281. # Log slow queries. Slow queries are queries which take more than the
  282. # amount of time defined in "long_query_time" or which do not use
  283. # indexes well, if log_short_format is not enabled. It is normally good idea
  284. # to have this turned on if you frequently add new queries to the
  285. # system.
  286. # 记录慢速查询. 慢速查询是指消耗了比 “long_query_time” 定义的更多时间的查询.
  287. # 如果 log_long_format 被打开,那些没有使用索引的查询也会被记录.
  288. # 如果你经常增加新查询到已有的系统内的话. 一般来说这是一个好主意
  289. slow_query_log
  290. # All queries taking more than this amount of time (in seconds) will be
  291. # trated as slow. Do not use "1" as a value here, as this will result in
  292. # even very fast queries being logged from time to time (as MySQL
  293. # currently measures time with second accuracy only).
  294. # 所有的使用了比这个时间(以秒为单位)更多的查询会被认为是慢速查询.
  295. # 不要在这里使用”1″, 否则会导致所有的查询,甚至非常快的查询页被记录下来(由于MySQL 目前时间的精确度只能达到秒的级别).
  296. long_query_time = 2
  297. # *** Replication related settings # *** 主从复制相关的设置
  298. # Unique server identification number between 1 and 2^32-1. This value
  299. # is required for both master and slave hosts. It defaults to 1 if
  300. # "master-host" is not set, but will MySQL will not function as a master
  301. # if it is omitted.
  302. # 唯一的服务辨识号,数值位于 1 到 2^32-1之间.
  303. # 此值在master和slave上都需要设置.
  304. # 如果 “master-host” 没有被设置,则默认为1, 但是如果忽略此选项,MySQL不会作为master生效.
  305. server-id = 1
  306. # Replication Slave (comment out master section to use this) #复制的Slave (去掉master段的注释来使其生效)
  307. #
  308. # To configure this host as a replication slave, you can choose between
  309. # two methods : #为了配置此主机作为复制的slave服务器,你可以选择两种方法:
  310. #
  311. # 1) Use the CHANGE MASTER TO command (fully described in our manual) -
  312. # the syntax is: #使用 CHANGE MASTER TO 命令 (在我们的手册中有完整描述) -
  313. # 语法如下:
  314. #
  315. # CHANGE MASTER TO MASTER_HOST=, MASTER_PORT=,
  316. # MASTER_USER=, MASTER_PASSWORD= ;
  317. #
  318. # where you replace , , by quoted strings and
  319. # by the master's port number (3306 by default).
  320. # 你需要替换掉 , , 等被尖括号包围的字段以及使用master的端口号替换 (默认3306).
  321. # Example: 案例
  322. #
  323. # CHANGE MASTER TO MASTER_HOST='125.564.12.1', MASTER_PORT=3306,
  324. # MASTER_USER='joe', MASTER_PASSWORD='secret';
  325. #
  326. # OR 或者
  327. #
  328. # 2) Set the variables below. However, in case you choose this method, then
  329. # start replication for the first time (even unsuccessfully, for example
  330. # if you mistyped the password in master-password and the slave fails to
  331. # connect), the slave will create a master.info file, and any later
  332. # changes in this file to the variable values below will be ignored and
  333. # overridden by the content of the master.info file, unless you shutdown
  334. # the slave server, delete master.info and restart the slaver server.
  335. # For that reason, you may want to leave the lines below untouched
  336. # (commented) and instead use CHANGE MASTER TO (see above)
  337. #
  338. #设置以下的变量. 不论如何, 在你选择这种方法的情况下, 然后第一次启动复制(甚至不成功的情况下,
  339. # 例如如果你输入错密码在master-password字段并且slave无法连接),
  340. # slave会创建一个 master.info 文件,并且之后任何对于包含在此文件内的参数的变化都会被忽略
  341. # 并且由 master.info 文件内的内容覆盖, 除非你关闭slave服务, 删除 master.info 并且重启slave 服务.
  342. # 由于这个原因,你也许不想碰一下的配置(注释掉的) 并且使用 CHANGE MASTER TO (查看上面) 来代替
  343. # required unique id between 2 and 2^32 - 1
  344. # (and different from the master)
  345. # defaults to 2 if master-host is set
  346. # but will not function as a slave if omitted
  347. # 所需要的唯一id号位于 2 和 2^32 – 1之间
  348. # (并且和master不同)
  349. # 如果master-host被设置了.则默认值是2
  350. # 但是如果省略,则不会生效
  351. #server-id = 2
  352. #
  353. # The replication master for this slave – required
  354. # 复制结构中的master – 必须
  355. #master-host =
  356. #
  357. # The username the slave will use for authentication when connecting
  358. # to the master – required
  359. # 当连接到master上时slave所用来认证的用户名 – 必须
  360. #master-user =
  361. #
  362. # The password the slave will authenticate with when connecting to
  363. # the master – required
  364. # 当连接到master上时slave所用来认证的密码 – 必须
  365. #master-password =
  366. #
  367. # The port the master is listening on.
  368. # optional - defaults to 3306
  369. # master监听的端口.
  370. # 可选 – 默认是3306
  371. #master-port =
  372. # Make the slave read-only. Only users with the SUPER privilege and the
  373. # replication slave thread will be able to modify data on it. You can
  374. # use this to ensure that no applications will accidently modify data on
  375. # the slave instead of the master
  376. # 使得slave只读.只有用户拥有SUPER权限和在上面的slave线程能够修改数据.
  377. # 你可以使用此项去保证没有应用程序会意外的修改slave而不是master上的数据
  378. #read_only
  379. #*** MyISAM Specific options
  380. #*** MyISAM 相关选项
  381. # Size of the Key Buffer, used to cache index blocks for MyISAM tables.
  382. # Do not set it larger than 30% of your available memory, as some memory
  383. # is also required by the OS to cache rows. Even if you're not using
  384. # MyISAM tables, you should still set it to 8-64M as it will also be
  385. # used for internal temporary disk tables.
  386. # 关键词缓冲的大小, 一般用来缓冲MyISAM表的索引块.
  387. # 不要将其设置大于你可用内存的30%,
  388. # 因为一部分内存同样被OS用来缓冲行数据
  389. # 甚至在你并不使用MyISAM 表的情况下, 你也需要仍旧设置起 8-64M 内存由于它同样会被内部临时磁盘表使用.
  390. key_buffer_size = 32M
  391. # MyISAM uses special tree-like cache to make bulk inserts (that is,
  392. # INSERT ... SELECT, INSERT ... VALUES (...), (...), ..., and LOAD DATA
  393. # INFILE) faster. This variable limits the size of the cache tree in
  394. # bytes per thread. Setting it to 0 will disable this optimisation. Do
  395. # not set it larger than "key_buffer_size" for optimal performance.
  396. # This buffer is allocated when a bulk insert is detected.
  397. # MyISAM 使用特殊的类似树的cache来使得突发插入
  398. # (这些插入是,INSERT … SELECT, INSERT … VALUES (…), (…), …, 以及 LOAD DATA
  399. # INFILE) 更快. 此变量限制每个进程中缓冲树的字节数.
  400. # 设置为 0 会关闭此优化.
  401. # 为了最优化不要将此值设置大于 “key_buffer_size”.
  402. # 当突发插入被检测到时此缓冲将被分配.
  403. bulk_insert_buffer_size = 64M
  404. # This buffer is allocated when MySQL needs to rebuild the index in
  405. # REPAIR, OPTIMIZE, ALTER table statements as well as in LOAD DATA INFILE
  406. # into an empty table. It is allocated per thread so be careful with
  407. # large settings.
  408. # 此缓冲当MySQL需要在 REPAIR, OPTIMIZE, ALTER 以及 LOAD DATA INFILE 到一个空表中引起重建索引时被分配.
  409. # 这在每个线程中被分配.所以在设置大值时需要小心.
  410. myisam_sort_buffer_size = 128M
  411. # The maximum size of the temporary file MySQL is allowed to use while
  412. # recreating the index (during REPAIR, ALTER TABLE or LOAD DATA INFILE.
  413. # If the file-size would be bigger than this, the index will be created
  414. # through the key cache (which is slower).
  415. # MySQL重建索引时所允许的最大临时文件的大小 (当 REPAIR, ALTER TABLE 或者 LOAD DATA INFILE).
  416. # 如果文件大小比此值更大,索引会通过键值缓冲创建(更慢)
  417. myisam_max_sort_file_size = 10G
  418. # If a table has more than one index, MyISAM can use more than one
  419. # thread to repair them by sorting in parallel. This makes sense if you
  420. # have multiple CPUs and plenty of memory.
  421. # 如果一个表拥有超过一个索引, MyISAM 可以通过并行排序使用超过一个线程去修复他们.
  422. # 这对于拥有多个CPU以及大量内存情况的用户,是一个很好的选择.
  423. myisam_repair_threads = 1
  424. # Automatically check and repair not properly closed MyISAM tables.
  425. # 自动检查和修复没有适当关闭的 MyISAM 表.
  426. myisam_recover
  427. # *** INNODB Specific options ***
  428. # *** INNODB 相关选项 ***
  429. # Use this option if you have a MySQL server with InnoDB support enabled
  430. # but you do not plan to use it. This will save memory and disk space
  431. # and speed up some things.
  432. # 如果你的MySQL服务包含InnoDB支持但是并不打算使用的话,
  433. # 使用此选项会节省内存以及磁盘空间,并且加速某些部分
  434. #skip-innodb
  435. # Additional memory pool that is used by InnoDB to store metadata
  436. # information. If InnoDB requires more memory for this purpose it will
  437. # start to allocate it from the OS. As this is fast enough on most
  438. # recent operating systems, you normally do not need to change this
  439. # value. SHOW INNODB STATUS will display the current amount used.
  440. # 附加的内存池被InnoDB用来保存 metadata 信息
  441. # 如果InnoDB为此目的需要更多的内存,它会开始从OS这里申请内存.
  442. # 由于这个操作在大多数现代操作系统上已经足够快, 你一般不需要修改此值.
  443. # SHOW INNODB STATUS 命令会显示当先使用的数量.
  444. innodb_additional_mem_pool_size = 16M
  445. # InnoDB, unlike MyISAM, uses a buffer pool to cache both indexes and
  446. # row data. The bigger you set this the less disk I/O is needed to
  447. # access data in tables. On a dedicated database server you may set this
  448. # parameter up to 80% of the machine physical memory size. Do not set it
  449. # too large, though, because competition of the physical memory may
  450. # cause paging in the operating system. Note that on 32bit systems you
  451. # might be limited to 2-3.5G of user level memory per process, so do not
  452. # set it too high.
  453. # InnoDB使用一个缓冲池来保存索引和原始数据, 不像 MyISAM.
  454. # 这里你设置越大,你在存取表里面数据时所需要的磁盘I/O越少.
  455. # 在一个独立使用的数据库服务器上,你可以设置这个变量到服务器物理内存大小的80%
  456. # 不要设置过大,否则,由于物理内存的竞争可能导致操作系统的换页颠簸.
  457. # 注意在32位系统上你每个进程可能被限制在 2-3.5G 用户层面内存限制,
  458. # 所以不要设置的太高.
  459. innodb_buffer_pool_size = 2G
  460. # InnoDB stores data in one or more data files forming the tablespace.
  461. # If you have a single logical drive for your data, a single
  462. # autoextending file would be good enough. In other cases, a single file
  463. # per device is often a good choice. You can configure InnoDB to use raw
  464. # disk partitions as well - please refer to the manual for more info
  465. # about this.
  466. # InnoDB 将数据保存在一个或者多个数据文件中成为表空间.
  467. # 如果你只有单个逻辑驱动保存你的数据,一个单个的自增文件就足够好了.
  468. # 其他情况下.每个设备一个文件一般都是个好的选择.
  469. # 你也可以配置InnoDB来使用裸盘分区 – 请参考手册来获取更多相关内容
  470. innodb_data_file_path = ibdata1:10M:autoextend
  471. # Set this option if you would like the InnoDB tablespace files to be
  472. # stored in another location. By default this is the MySQL datadir.
  473. # 设置此选项如果你希望InnoDB表空间文件被保存在其他分区.
  474. # 默认保存在MySQL的datadir中.
  475. #innodb_data_home_dir =
  476. # Number of IO threads to use for async IO operations. This value is
  477. # hardcoded to 8 on Unix, but on Windows disk I/O may benefit from a
  478. # larger number.
  479. # 用来同步IO操作的IO线程的数量. This value is
  480. # 此值在Unix下被硬编码为8,但是在Windows磁盘I/O可能在一个大数值下表现的更好.
  481. innodb_write_io_threads = 8
  482. innodb_read_io_threads = 8
  483. # If you run into InnoDB tablespace corruption, setting this to a nonzero
  484. # value will likely help you to dump your tables. Start from value 1 and
  485. # increase it until you're able to dump the table successfully.
  486. # 如果你发现InnoDB表空间损坏, 设置此值为一个非零值可能帮助你导出你的表.
  487. # 从1开始并且增加此值知道你能够成功的导出表.
  488. #innodb_force_recovery=1
  489. # Number of threads allowed inside the InnoDB kernel. The optimal value
  490. # depends highly on the application, hardware as well as the OS
  491. # scheduler properties. A too high value may lead to thread thrashing.
  492. # 在InnoDb核心内的允许线程数量.
  493. # 最优值依赖于应用程序,硬件以及操作系统的调度方式.
  494. # 过高的值可能导致线程的互斥颠簸.
  495. innodb_thread_concurrency = 16
  496. # If set to 1, InnoDB will flush (fsync) the transaction logs to the
  497. # disk at each commit, which offers full ACID behavior. If you are
  498. # willing to compromise this safety, and you are running small
  499. # transactions, you may set this to 0 or 2 to reduce disk I/O to the
  500. # logs. Value 0 means that the log is only written to the log file and
  501. # the log file flushed to disk approximately once per second. Value 2
  502. # means the log is written to the log file at each commit, but the log
  503. # file is only flushed to disk approximately once per second.
  504. # 如果设置为1 ,InnoDB会在每次提交后刷新(fsync)事务日志到磁盘上,
  505. # 这提供了完整的ACID行为.
  506. # 如果你愿意对事务安全折衷, 并且你正在运行一个小的食物, 你可以设置此值到0或者2来减少由事务日志引起的磁盘I/O
  507. # 0代表日志只大约每秒写入日志文件并且日志文件刷新到磁盘.
  508. # 2代表日志写入日志文件在每次提交后,但是日志文件只有大约每秒才会刷新到磁盘上.
  509. innodb_flush_log_at_trx_commit = 1
  510. # Speed up InnoDB shutdown. This will disable InnoDB to do a full purge
  511. # and insert buffer merge on shutdown. It may increase shutdown time a
  512. # lot, but InnoDB will have to do it on the next startup instead.
  513. # 加速InnoDB的关闭. 这会阻止InnoDB在关闭时做全清除以及插入缓冲合并.
  514. # 这可能极大增加关机时间, 但是取而代之的是InnoDB可能在下次启动时做这些操作.
  515. #innodb_fast_shutdown
  516. # The size of the buffer InnoDB uses for buffering log data. As soon as
  517. # it is full, InnoDB will have to flush it to disk. As it is flushed
  518. # once per second anyway, it does not make sense to have it very large
  519. # (even with long transactions).
  520. # 用来缓冲日志数据的缓冲区的大小.
  521. # 当此值快满时, InnoDB将必须刷新数据到磁盘上.
  522. # 由于基本上每秒都会刷新一次,所以没有必要将此值设置的太大(甚至对于长事务而言)
  523. innodb_log_buffer_size = 8M
  524. # Size of each log file in a log group. You should set the combined size
  525. # of log files to about 25%-100% of your buffer pool size to avoid
  526. # unneeded buffer pool flush activity on log file overwrite. However,
  527. # note that a larger logfile size will increase the time needed for the
  528. # recovery process.
  529. # 在日志组中每个日志文件的大小.
  530. # 你应该设置日志文件总合大小到你缓冲池大小的25%~100%
  531. # 来避免在日志文件覆写上不必要的缓冲池刷新行为.
  532. # 不论如何, 请注意一个大的日志文件大小会增加恢复进程所需要的时间.
  533. innodb_log_file_size = 256M
  534. # Total number of files in the log group. A value of 2-3 is usually good
  535. # enough.
  536. # 在日志组中的文件总数.
  537. # 通常来说2~3是比较好的.
  538. innodb_log_files_in_group = 3
  539. # Location of the InnoDB log files. Default is the MySQL datadir. You
  540. # may wish to point it to a dedicated hard drive or a RAID1 volume for
  541. # improved performance
  542. # InnoDB的日志文件所在位置. 默认是MySQL的datadir.
  543. # 你可以将其指定到一个独立的硬盘上或者一个RAID1卷上来提高其性能
  544. #innodb_log_group_home_dir
  545. # Maximum allowed percentage of dirty pages in the InnoDB buffer pool.
  546. # If it is reached, InnoDB will start flushing them out agressively to
  547. # not run out of clean pages at all. This is a soft limit, not
  548. # guaranteed to be held.
  549. # 在InnoDB缓冲池中最大允许的脏页面的比例.
  550. # 如果达到限额, InnoDB会开始刷新他们防止他们妨碍到干净数据页面.
  551. # 这是一个软限制,不被保证绝对执行.
  552. innodb_max_dirty_pages_pct = 90
  553. # The flush method InnoDB will use for Log. The tablespace always uses
  554. # doublewrite flush logic. The default value is "fdatasync", another
  555. # option is "O_DSYNC".
  556. # InnoDB用来刷新日志的方法.
  557. # 表空间总是使用双重写入刷新方法
  558. # 默认值是 “fdatasync”, 另一个是 “O_DSYNC”.
  559. #innodb_flush_method=O_DSYNC
  560. # How long an InnoDB transaction should wait for a lock to be granted
  561. # before being rolled back. InnoDB automatically detects transaction
  562. # deadlocks in its own lock table and rolls back the transaction. If you
  563. # use the LOCK TABLES command, or other transaction-safe storage engines
  564. # than InnoDB in the same transaction, then a deadlock may arise which
  565. # InnoDB cannot notice. In cases like this the timeout is useful to
  566. # resolve the situation.
  567. # 在被回滚前,一个InnoDB的事务应该等待一个锁被批准多久.
  568. # InnoDB在其拥有的锁表中自动检测事务死锁并且回滚事务.
  569. # 如果你使用 LOCK TABLES 指令, 或者在同样事务中使用除了InnoDB以外的其他事务安全的存储引擎
  570. # 那么一个死锁可能发生而InnoDB无法注意到.
  571. # 这种情况下这个timeout值对于解决这种问题就非常有帮助.
  572. innodb_lock_wait_timeout = 120
  573. [mysqldump]
  574. # Do not buffer the whole result set in memory before writing it to
  575. # file. Required for dumping very large tables
  576. # 不要在将内存中的整个结果写入磁盘之前缓存. 在导出非常巨大的表时需要此项
  577. quick
  578. max_allowed_packet = 16M
  579. [mysql]
  580. no-auto-rehash
  581. # Only allow UPDATEs and DELETEs that use keys.
  582. # 仅仅允许使用键值的 UPDATEs 和 DELETEs .
  583. #safe-updates
  584. [myisamchk]
  585. key_buffer_size = 512M
  586. sort_buffer_size = 512M
  587. read_buffer = 8M
  588. write_buffer = 8M
  589. [mysqlhotcopy]
  590. interactive-timeout
  591. [mysqld_safe]
  592. # Increase the amount of open files allowed per process. Warning: Make
  593. # sure you have set the global system limit high enough! The high value
  594. # is required for a large number of opened tables
  595. # 增加每个进程的可打开文件数量.
  596. # 警告: 确认你已经将全系统限制设定的足够高!
  597. # 打开大量表需要将此值设大
  598. 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.具体优化配置如下

代码
  1. [client]
  2. port = 3306
  3. socket = /tmp/mysql.sock
  4. default-character-set = utf8 #设置客户端的字符编码
  5. [mysqld]
  6. # generic configuration options
  7. port = 3306
  8. socket = /tmp/mysql.sock
  9. #*** char set ***
  10. character-set-server = utf8 #设置服务器端的字符编码
  11. #*** network ***
  12. back_log = 512
  13. #skip-networking #默认没有开启
  14. max_connections = 3000
  15. max_connect_errors = 30
  16. table_open_cache = 4096
  17. #external-locking #默认没有开启
  18. max_allowed_packet = 32M
  19. max_heap_table_size = 128M
  20. # *** global cache ***
  21. read_buffer_size = 8M
  22. read_rnd_buffer_size = 64M
  23. sort_buffer_size = 16M
  24. join_buffer_size = 16M
  25. # *** thread ***
  26. thread_cache_size = 16
  27. thread_concurrency = 8
  28. thread_stack = 512K
  29. # *** query cache ***
  30. query_cache_size = 128M
  31. query_cache_limit = 4M
  32. # *** index ***
  33. ft_min_word_len = 8
  34. #memlock #默认没有开启
  35. default-storage-engine = INNODB
  36. transaction_isolation = REPEATABLE-READ
  37. # *** tmp table ***
  38. tmp_table_size = 64M
  39. # *** bin log ***
  40. log-bin=mysql-bin
  41. binlog_cache_size = 4M
  42. binlog_format=mixed
  43. #log_slave_updates #默认没有开启
  44. #log #默认没有开启,此处是查询日志,开启会影响服务器性能
  45. log_warnings #开启警告日志
  46. # *** slow query log ***
  47. slow_query_log
  48. long_query_time = 10
  49. # *** Replication related settings
  50. server-id = 1
  51. #server-id = 2
  52. #master-host =
  53. #master-user =
  54. #master-password =
  55. #master-port =
  56. #read_only
  57. #*** MyISAM Specific options
  58. key_buffer_size = 128M
  59. bulk_insert_buffer_size = 256M
  60. myisam_sort_buffer_size = 256M
  61. myisam_max_sort_file_size = 10G
  62. myisam_repair_threads = 1
  63. myisam_recover
  64. # *** INNODB Specific options ***
  65. #skip-innodb #默认没有开启
  66. innodb_additional_mem_pool_size = 64M
  67. innodb_buffer_pool_size = 6G #注意在32位系统上你每个进程可能被限制在 2-3.5G 用户层面内存限制, 所以不要设置的太高.
  68. innodb_data_file_path = ibdata1:10M:autoextend
  69. #innodb_data_home_dir =
  70. innodb_write_io_threads = 8
  71. innodb_read_io_threads = 8
  72. #innodb_force_recovery=1
  73. innodb_thread_concurrency = 16
  74. innodb_flush_log_at_trx_commit = 2
  75. #说明:innodb_flush_log_at_trx_commit = 2 如果是游戏服务器,建议此值设置为2;如果是对数据安全要求极高的应用,建议设置为1;设置为0性能最高,但如果发生故障,数据可能会有丢失的危险!默认值1的意思是每一次事务提交或事务外的指令都需要把日志写入(flush)硬盘,这是很费时的。特别是使用电池供电缓存(Battery backed up cache)时。设成2对于很多运用,特别是从MyISAM表转过来的是可以的,它的意思是不写入硬盘而是写入系统缓存。日志仍然会每秒flush到硬盘,所以你一般不会丢失超过1-2秒的更新。设成0会更快一点,但安全方面比较差,即使MySQL挂了也可能会丢失事务的数据。而值2只会在整个操作系统挂了时才可能丢数据。
  76. #innodb_fast_shutdown
  77. innodb_log_buffer_size = 16M
  78. innodb_log_file_size = 512M
  79. innodb_log_files_in_group = 3
  80. #innodb_log_group_home_dir
  81. innodb_max_dirty_pages_pct = 90
  82. #innodb_flush_method=O_DSYNC
  83. innodb_lock_wait_timeout = 120
  84. [mysqldump]
  85. quick
  86. max_allowed_packet = 32M
  87. [mysql]
  88. no-auto-rehash
  89. [myisamchk]
  90. key_buffer_size = 2048M
  91. sort_buffer_size = 2048M
  92. read_buffer = 32M
  93. write_buffer = 32M
  94. [mysqlhotcopy]
  95. interactive-timeout
  96. [mysqld_safe]
  97. open-files-limit = 10240

3.总结

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

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

代码
  1. mysql> show status; #显示状态信息
  2. mysql> show variables; #显示系统变量
  3. mysql> show engines; #查看所有引擎
  4. mysql> show engine innodb status; #显示InnoDB存储引擎的状态

本文出自 “Share your knowledge …” 博客

经过全福编程网小编的测试,这样的效率很不错,直接拿来就可以使用。

相关推荐