mysql配置

前言:《mysql配置》,在centOS上安裝完成mysql後,自然是要對mysql進行配置,對於mysql來說,my.cnf可是很重要的,相當於畫龍點睛的作用呢。

經常不經意間就發現文章被扣到各式各樣的網站上面,甚是可惡!
那麼人生何處不爬蟲,爬蟲請標https://blog.csdn.net/qing_gee
見賢思齊焉,見不賢而內自省也!

特此說明,我這個配置文件內容是結合我項目實戰經驗多次總結出來的王道,對提升mysql性能有著關鍵性的作用,當然瞭,這要看你的項目是否需要這樣做嘍。

這個文件裡面的配置項目很多,我就一一說明瞭(主要是俺其實有一些也不懂,千萬要笑啊,我是真不懂,不過我有問過度娘!),

[client]
#no-beep
port=3306

[mysql]
default-character-set=utf8
socket          = /var/lib/mysql/mysql.sock

[mysqld]
# The TCP/IP Port the MySQL Server will listen on
port=3306
socket          = /var/lib/mysql/mysql.sock

character-set-server=utf8

#默認引擎設置為INNODB,這要看你的資料庫是做什麼用的
default-storage-engine=INNODB
#最大連接數,這個說實話,我沒有測出來最合理的數值
max_connections = 500
#下面這兩個參數就是禁用緩存查詢,主要是因為我的資料庫大量的寫操作,所以設置瞭cache,反而會影響性能,也是基於理論上的,所以你大可不必相信。
query_cache_size=0
query_cache_type=0

#這幾個數值,你千萬要找度娘理論一下啊,我是說不清楚瞭
table_open_cache=2000
tmp_table_size=19M
thread_cache_size = 18
myisam_max_sort_file_size = 1G
myisam_sort_buffer_size=30M
key_buffer_size=8M
read_buffer_size = 512K
read_rnd_buffer_size = 1M
sort_buffer_size = 512k

#這個很重要瞭,對性能有著很大的影響,我會告訴你的。
innodb_flush_log_at_trx_commit=2

innodb_log_buffer_size=1M

# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
innodb_buffer_pool_size=2G
innodb_buffer_pool_instances=1
#上面這兩個參數對性能的作用我會論證給你的。

#這一塊參數的作用我也忘的差不多瞭,所以度娘吧
innodb_log_file_size=48M
innodb_thread_concurrency=9
innodb_autoextend_increment=64
innodb_buffer_pool_instances=8
innodb_concurrency_tickets=5000
innodb_old_blocks_time=1000
innodb_open_files=300
innodb_stats_on_metadata=0
innodb_file_per_table=1
innodb_checksum_algorithm=0
flush_time=0
join_buffer_size=256K
max_connect_errors=100
max_allowed_packet = 16M
open_files_limit=4161
table_definition_cache=1400
binlog_row_event_max_size=8K

#二進制的類型,這個有很大學問,稍候我也會告訴你的。
binlog-format = MIXED

#事務鎖時間,這個同樣學問很大。
innodb_lock_wait_timeout = 20

#事務鎖級別,這個學問同樣很大很大啊
transaction-isolation = REPEATABLE-READ


binlog_cache_size = 1M

# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
#這個參數就是設置二進制文件的路徑的,註意啊,註意啊!
log_bin=mysql-bin

server_id = 1

[mysqldump]
max_allowed_packet = 16M

重點來瞭,下面這些內容,如果你沒有看到,我覺得你錯過瞭精彩,精彩啊,如果你錯過瞭,我強烈抗議的,雖然抗議無效!

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的隱患到底在什麼情況下會發生。

我在https://blog.csdn.net/qing_gee/article/details/42551179,這篇文章中有介紹。

2.innodb_buffer_pool_size=2G
innodb_buffer_pool_instances=1

這兩個參數,你必須得看看這個mysql:提升性能的最關鍵參數

3.binlog-format = MIXED

binlog_format=mixed:二進制日志的格式為mixed,該中模式是statement和row模式的結合體,註意查看我同事寫的https://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的不然,肯定是沒有效果的。

service mysql restart

結語:分享知識是快樂的,我隻好這樣安慰自己吧,哈哈,其實我心態是很寬的,所謂“人逢知己千杯少”,我主要是想結交朋友的,哈哈。

You May Also Like