您的位置:首页 > 数据库 > MySQL

mysql配置

2015-11-10 04:26 447 查看
前言:《mysql配置》,在centOS上安装完成mysql后,自然是要对mysql进行配置,对于mysql来说,my.cnf可是很重要的,相当于画龙点睛的作用呢。

经常不经意间就发现文章被扣到各式各样的网站上面,甚是可恶!

那么人生何处不爬虫,爬虫请标http://blog.csdn.net/qing_gee

见贤思齐焉,见不贤而内自省也!

特此说明,我这个配置文件内容是结合我项目实战经验多次总结出来的王道,对提升mysql性能有着关键性的作用,当然了,这要看你的项目是否需要这样做喽。

这个文件里面的配置项目很多,我就一一说明了(主要是俺其实有一些也不懂,千万要笑啊,我是真不懂,不过我有问过度娘!),
<code class="language-shell hljs makefile has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">[client]
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#no-beep</span>
<span class="hljs-constant" style="box-sizing: border-box;">port</span>=3306

[mysql]
default-character-set=utf8
<span class="hljs-constant" style="box-sizing: border-box;">socket</span>          = /var/lib/mysql/mysql.sock

[mysqld]
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># The TCP/IP Port the MySQL Server will listen on</span>
<span class="hljs-constant" style="box-sizing: border-box;">port</span>=3306
<span class="hljs-constant" style="box-sizing: border-box;">socket</span>          = /var/lib/mysql/mysql.sock

character-set-server=utf8

<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#默认引擎设置为INNODB,这要看你的数据库是做什么用的</span>
default-storage-engine=INNODB
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#最大连接数,这个说实话,我没有测出来最合理的数值</span>
<span class="hljs-constant" style="box-sizing: border-box;">max_connections</span> = 500
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#下面这两个参数就是禁用缓存查询,主要是因为我的数据库大量的写操作,所以设置了cache,反而会影响性能,也是基于理论上的,所以你大可不必相信。</span>
<span class="hljs-constant" style="box-sizing: border-box;">query_cache_size</span>=0
<span class="hljs-constant" style="box-sizing: border-box;">query_cache_type</span>=0

<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#这几个数值,你千万要找度娘理论一下啊,我是说不清楚了</span>
<span class="hljs-constant" style="box-sizing: border-box;">table_open_cache</span>=2000
<span class="hljs-constant" style="box-sizing: border-box;">tmp_table_size</span>=19M
<span class="hljs-constant" style="box-sizing: border-box;">thread_cache_size</span> = 18
<span class="hljs-constant" style="box-sizing: border-box;">myisam_max_sort_file_size</span> = 1G
<span class="hljs-constant" style="box-sizing: border-box;">myisam_sort_buffer_size</span>=30M
<span class="hljs-constant" style="box-sizing: border-box;">key_buffer_size</span>=8M
<span class="hljs-constant" style="box-sizing: border-box;">read_buffer_size</span> = 512K
<span class="hljs-constant" style="box-sizing: border-box;">read_rnd_buffer_size</span> = 1M
<span class="hljs-constant" style="box-sizing: border-box;">sort_buffer_size</span> = 512k

<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#这个很重要了,对性能有着很大的影响,我会告诉你的。</span>
<span class="hljs-constant" style="box-sizing: border-box;">innodb_flush_log_at_trx_commit</span>=2

<span class="hljs-constant" style="box-sizing: border-box;">innodb_log_buffer_size</span>=1M

<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># Remove leading # and set to the amount of RAM for the most important data</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.</span>
<span class="hljs-constant" style="box-sizing: border-box;">innodb_buffer_pool_size</span>=2G
<span class="hljs-constant" style="box-sizing: border-box;">innodb_buffer_pool_instances</span>=1
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#上面这两个参数对性能的作用我会论证给你的。</span>

<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#这一块参数的作用我也忘的差不多了,所以度娘吧</span>
<span class="hljs-constant" style="box-sizing: border-box;">innodb_log_file_size</span>=48M
<span class="hljs-constant" style="box-sizing: border-box;">innodb_thread_concurrency</span>=9
<span class="hljs-constant" style="box-sizing: border-box;">innodb_autoextend_increment</span>=64
<span class="hljs-constant" style="box-sizing: border-box;">innodb_buffer_pool_instances</span>=8
<span class="hljs-constant" style="box-sizing: border-box;">innodb_concurrency_tickets</span>=5000
<span class="hljs-constant" style="box-sizing: border-box;">innodb_old_blocks_time</span>=1000
<span class="hljs-constant" style="box-sizing: border-box;">innodb_open_files</span>=300
<span class="hljs-constant" style="box-sizing: border-box;">innodb_stats_on_metadata</span>=0
<span class="hljs-constant" style="box-sizing: border-box;">innodb_file_per_table</span>=1
<span class="hljs-constant" style="box-sizing: border-box;">innodb_checksum_algorithm</span>=0
<span class="hljs-constant" style="box-sizing: border-box;">flush_time</span>=0
<span class="hljs-constant" style="box-sizing: border-box;">join_buffer_size</span>=256K
<span class="hljs-constant" style="box-sizing: border-box;">max_connect_errors</span>=100
<span class="hljs-constant" style="box-sizing: border-box;">max_allowed_packet</span> = 16M
<span class="hljs-constant" style="box-sizing: border-box;">open_files_limit</span>=4161
<span class="hljs-constant" style="box-sizing: border-box;">table_definition_cache</span>=1400
<span class="hljs-constant" style="box-sizing: border-box;">binlog_row_event_max_size</span>=8K

<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#二进制的类型,这个有很大学问,稍候我也会告诉你的。</span>
binlog-format = MIXED

<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#事务锁时间,这个同样学问很大。</span>
<span class="hljs-constant" style="box-sizing: border-box;">innodb_lock_wait_timeout</span> = 20

<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#事务锁级别,这个学问同样很大很大啊</span>
transaction-isolation = REPEATABLE-READ

<span class="hljs-constant" style="box-sizing: border-box;">binlog_cache_size</span> = 1M

<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># Remove leading # to turn on a very important data integrity option: logging</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># changes to the binary log between backups.</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#这个参数就是设置二进制文件的路径的,注意啊,注意啊!</span>
<span class="hljs-constant" style="box-sizing: border-box;">log_bin</span>=mysql-bin

<span class="hljs-constant" style="box-sizing: border-box;">server_id</span> = 1

[mysqldump]
<span class="hljs-constant" style="box-sizing: border-box;">max_allowed_packet</span> = 16M
</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li><li style="box-sizing: border-box; padding: 0px 5px;">39</li><li style="box-sizing: border-box; padding: 0px 5px;">40</li><li style="box-sizing: border-box; padding: 0px 5px;">41</li><li style="box-sizing: border-box; padding: 0px 5px;">42</li><li style="box-sizing: border-box; padding: 0px 5px;">43</li><li style="box-sizing: border-box; padding: 0px 5px;">44</li><li style="box-sizing: border-box; padding: 0px 5px;">45</li><li style="box-sizing: border-box; padding: 0px 5px;">46</li><li style="box-sizing: border-box; padding: 0px 5px;">47</li><li style="box-sizing: border-box; padding: 0px 5px;">48</li><li style="box-sizing: border-box; padding: 0px 5px;">49</li><li style="box-sizing: border-box; padding: 0px 5px;">50</li><li style="box-sizing: border-box; padding: 0px 5px;">51</li><li style="box-sizing: border-box; padding: 0px 5px;">52</li><li style="box-sizing: border-box; padding: 0px 5px;">53</li><li style="box-sizing: border-box; padding: 0px 5px;">54</li><li style="box-sizing: border-box; padding: 0px 5px;">55</li><li style="box-sizing: border-box; padding: 0px 5px;">56</li><li style="box-sizing: border-box; padding: 0px 5px;">57</li><li style="box-sizing: border-box; padding: 0px 5px;">58</li><li style="box-sizing: border-box; padding: 0px 5px;">59</li><li style="box-sizing: border-box; padding: 0px 5px;">60</li><li style="box-sizing: border-box; padding: 0px 5px;">61</li><li style="box-sizing: border-box; padding: 0px 5px;">62</li><li style="box-sizing: border-box; padding: 0px 5px;">63</li><li style="box-sizing: border-box; padding: 0px 5px;">64</li><li style="box-sizing: border-box; padding: 0px 5px;">65</li><li style="box-sizing: border-box; padding: 0px 5px;">66</li><li style="box-sizing: border-box; padding: 0px 5px;">67</li><li style="box-sizing: border-box; padding: 0px 5px;">68</li><li style="box-sizing: border-box; padding: 0px 5px;">69</li><li style="box-sizing: border-box; padding: 0px 5px;">70</li><li style="box-sizing: border-box; padding: 0px 5px;">71</li><li style="box-sizing: border-box; padding: 0px 5px;">72</li><li style="box-sizing: border-box; padding: 0px 5px;">73</li><li style="box-sizing: border-box; padding: 0px 5px;">74</li><li style="box-sizing: border-box; padding: 0px 5px;">75</li><li style="box-sizing: border-box; padding: 0px 5px;">76</li><li style="box-sizing: border-box; padding: 0px 5px;">77</li><li style="box-sizing: border-box; padding: 0px 5px;">78</li><li style="box-sizing: border-box; padding: 0px 5px;">79</li><li style="box-sizing: border-box; padding: 0px 5px;">80</li><li style="box-sizing: border-box; padding: 0px 5px;">81</li><li style="box-sizing: border-box; padding: 0px 5px;">82</li><li style="box-sizing: border-box; padding: 0px 5px;">83</li><li style="box-sizing: border-box; padding: 0px 5px;">84</li><li style="box-sizing: border-box; padding: 0px 5px;">85</li><li style="box-sizing: border-box; padding: 0px 5px;">86</li></ul>


重点来了,下面这些内容,如果你没有看到,我觉得你错过了精彩,精彩啊,如果你错过了,我强烈抗议的,虽然抗议无效!

1.innodb_flush_log_at_trx_commit=2

Controls the balance between strict ACID compliance for commit operations, and higher performance

that is possible when commit-related I/O operations are rearranged and done in batches. You can

achieve better performance by changing the default value, but then you can lose up to a second of

transactions in a crash.

• The default value of 1 is required for full ACID compliance. With this value, the contents of the InnoDB

log buffer are written out to the log file at each transaction commit and the log file is flushed to disk.

• With a value of 0, the contents of the InnoDB log buffer are written to the log file approximately once

per second and the log file is flushed to disk. No writes from the log buffer to the log file are performed

at transaction commit. Once-per-second flushing is not 100% guaranteed to happen every second,

due to process scheduling issues. Because the flush to disk operation only occurs approximately once

per second, you can lose up to a second of transactions with any mysqld process crash.

• With a value of 2, the contents of the InnoDB log buffer are written to the log file after each transaction

commit and the log file is flushed to disk approximately once per second. Once-per-second flushing

is not 100% guaranteed to happen every second, due to process scheduling issues. Because the

flush to disk operation only occurs approximately once per second.

大致的意思是将该属性主要是为数据库的ACID原则进行服务的,并且默认为1,但是实际情况下(我们项目是结合spring和mybatis,可能是某一方面设置不当),设置为2会提高很多的事务性能,从文档中可以看得出来,“1的时候,innodb的缓存会在事务提交或者每秒钟时都会进行磁盘的刷新操作,2的时候,innodb缓存会在提交事务时写入到事务日志但不会刷新磁盘,然后在每秒钟时进行磁盘刷新操作”,2要比1提高很多性能,但是对于隐患来说,我没有太好的理解,按照文档中给出的结果好像是“在操作系统崩溃的时候,2的情况下,会丢失1秒的数据”,但是仔细想想发生的时间节点,1.事务没有commit时,断电了,此时肯定数据是没有更新成功的,因为都还没有来得及写入事务日志,2.事务提交后,在写入事务日志的时候,发生断电,此时无论是参数的值是1还是2,都应该恢复不了数据了,3.每秒钟刷新磁盘时,发生断电,按照《高性能mysql》的字面意思,此时既然事务日志已经持久化了,那么重启后,数据是会自动恢复的。那么疑问来了,2和1的隐患到底在什么情况下会发生。

我在/article/1463044.html,这篇文章中有介绍。

2.innodb_buffer_pool_size=2G

innodb_buffer_pool_instances=1

这两个参数,你必须得看看这个mysql:提升性能的最关键参数

3.binlog-format = MIXED

binlog_format=mixed:二进制日志的格式为mixed,该中模式是statement和row模式的结合体,注意查看我同事写的http://www.xx566.com/detail/177.html这篇文章,里面讲解了我们项目在二进制日志设置上遇到的问题和解决办法,如果遇到类似的问题后,会有所帮助。

In MySQL 5.7, the default format is STATEMENT.

You must have the SUPER privilege to set either the global or session binlog_format value.

The rules governing when changes to this variable take effect and how long the effect lasts are the same

as for other MySQL server system variables. See Section 13.7.4, “SET Syntax”, for more information.

When MIXED is specified, statement-based replication is used, except for cases where only row-based

replication is guaranteed to lead to proper results. For example, this happens when statements contain

user-defined functions (UDF) or the UUID() function. An exception to this rule is that MIXED always

uses statement-based replication for stored functions and triggers.

4.innodb_lock_wait_timeout = 20

你可以看看这个Transactional和mysql究竟有什么关系,你会明白的,我相信!

5.transaction-isolation = REPEATABLE-READ

高性能mysql札记:事务,这里面,我有大量的论证。

当然了,我之前也从各地摘录了一些关于参数介绍的,如果你觉得需要的话,我会给你地址的。mysql:配置参数优化建议

写到这,我觉得我的套路就要结束了,这些经验,我真想不说出来的!

最后啊,记得要重启mysql的不然,肯定是没有效果的。
<code class="language-shell hljs  has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">service mysql restart</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>


结语:分享知识是快乐的,我只好这样安慰自己吧,哈哈,其实我心态是很宽的,所谓“人逢知己千杯少”,我主要是想结交朋友的,哈哈。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: