環(huán)境為CentOS系統(tǒng),1G內(nèi)存,Mysql5.5.30,在/etc/my.cnf內(nèi)添加如下代碼:
- skip-external-locking
- skip-name-resolve
- max_connections = 1024
- query_cache_size = 16M
- sort_buffer_size = 1M
- table_cache = 256
- innodb_buffer_pool_size = 128M
- innodb_additional_mem_pool_size = 4M
- innodb_log_file_size = 128M
- innodb_log_files_in_group = 2
- innodb_log_buffer_size = 2M
- innodb_flush_log_at_trx_commit = 1
修改前先將mysql服務(wù)停止:service mysqld restart
由于修改了日志大小重啟會(huì)報(bào)錯(cuò),如下錯(cuò)誤:
- 130319 04:45:14 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended
- 130319 05:02:43 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
- 130319 5:02:54 [Note] Plugin 'FEDERATED' is disabled.
- 130319 5:02:59 InnoDB: The InnoDB memory heap is disabled
- 130319 5:02:59 InnoDB: Mutexes and rw_locks use InnoDB's own implementation
- 130319 5:02:59 InnoDB: Compressed tables use zlib 1.2.3
- 130319 5:02:59 InnoDB: Using Linux native AIO
- 130319 5:03:01 InnoDB: Initializing buffer pool, size = 128.0M
- 130319 5:03:02 InnoDB: Completed initialization of buffer pool
- InnoDB: Error: log file ./ib_logfile0 is of different size 0 5242880 bytes
- InnoDB: than specified in the .cnf file 0 134217728 bytes!
- 130319 5:03:02 [ERROR] Plugin 'InnoDB' init function returned error.
- 130319 5:03:02 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
- 130319 5:03:02 [ERROR] Aborting
將/var/lib/mysql下原先的ib_logfile0、ib_logfile1刪除再重啟mysql.
service mysqld start
正常啟動(dòng):
- 130319 05:20:59 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended
- 130319 05:22:53 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
- 130319 5:22:56 [Note] Plugin 'FEDERATED' is disabled.
- 130319 5:22:57 InnoDB: The InnoDB memory heap is disabled
- 130319 5:22:57 InnoDB: Mutexes and rw_locks use InnoDB's own implementation
- 130319 5:22:57 InnoDB: Compressed tables use zlib 1.2.3
- 130319 5:22:57 InnoDB: Using Linux native AIO
- 130319 5:22:57 InnoDB: Initializing buffer pool, size = 128.0M
- 130319 5:22:57 InnoDB: Completed initialization of buffer pool
- 130319 5:22:57 InnoDB: highest supported file format is Barracuda.
- 130319 5:22:58 InnoDB: Waiting for the background threads to start
- 130319 5:22:59 InnoDB: 5.5.30 started; log sequence number 2621452
- 130319 5:22:59 [Note] Server hostname (bind-address): '0.0.0.0'; port: 3306
- 130319 5:22:59 [Note] - '0.0.0.0' resolves to '0.0.0.0';
- 130319 5:22:59 [Note] Server socket created on IP: '0.0.0.0'.
- 130319 5:23:00 [Warning] 'proxies_priv' entry '@ root@server08' ignored in --skip-name-resolve mode. //Vevb.com
- 130319 5:23:04 [Note] Event Scheduler: Loaded 0 events
- 130319 5:23:04 [Note] /usr/libexec/mysqld: ready for connections.
- Version: '5.5.30' socket: '/var/lib/mysql/mysql.sock' port: 3306 MySQL Community Server (GPL) by Remi
使用mysqladmin variables命令可查看所有參數(shù)變量.
InnoDB引擎配置優(yōu)化
innodb_buffer_pool_size=1G //一個(gè)Innodb最重要的參數(shù),這個(gè)參數(shù)和MyISAM的key_buffer_size有相似之處,但也是有差別的,這個(gè)參數(shù)主要緩存innodb表的索引,數(shù)據(jù),插入數(shù)據(jù)時(shí)的緩沖,設(shè)置得越大,存取表里面數(shù)據(jù)時(shí)所需要的磁盤(pán)I/O越少,一般是內(nèi)存的一半,不超過(guò)2G,否則系統(tǒng)會(huì)崩潰。
為Innodb加速優(yōu)化首要參數(shù),該參數(shù)分配內(nèi)存的原則:這個(gè)參數(shù)默認(rèn)分配只有8M,可以說(shuō)是非常小的一個(gè)值,如果是一個(gè)專(zhuān)用DB服務(wù)器,那么他可以占到內(nèi)存的70%-80%,這個(gè)參數(shù)不能動(dòng)態(tài)更改,所以分配需多考慮,分配過(guò)大,會(huì)使Swap占用過(guò)多,致使Mysql的查詢特慢,如果你的數(shù)據(jù)比較小,那么可分配是你的數(shù)據(jù)大?。保埃プ笥易鰹檫@個(gè)參數(shù)的值.
例如:數(shù)據(jù)大小為50M,那么給這個(gè)值分配innodb_buffer_pool_size=64M
innodb_additional_mem_pool_size=16M //用來(lái)存放Innodb的內(nèi)部目錄這個(gè)值不用分配太大,系統(tǒng)可以自動(dòng)調(diào)。不用設(shè)置太高,通常比較大數(shù)據(jù)設(shè)置16M夠用了,如果表比較多,可以適當(dāng)?shù)脑龃?如果這個(gè)值自動(dòng)增加,會(huì)在error log有中顯示的.
innodb_log_file_size=256M //在日志組中每個(gè)日志文件的大小,一般是innodb_buffer_pool_size的25%,官方推薦是innodb_buffer_pool_size的40-50%,一般控制在幾個(gè)LOG文件相加大小在2G以內(nèi)為佳,具體情況還需要看你的事務(wù)大小,數(shù)據(jù)大小為依據(jù),說(shuō)明,這個(gè)值分配的大小和數(shù)據(jù)庫(kù)的寫(xiě)入速度,事務(wù)大小,異常重啟后的恢復(fù)有很大的關(guān)系.
innodb_log_files_in_group=2 //指定你有幾個(gè)日志組,分配原則,一般我們可以用2-3個(gè)日值組,默認(rèn)為兩個(gè).
innodb_log_buffer_size=3M //事務(wù)在內(nèi)存中的緩沖。分配原則:控制在2-8M.這個(gè)值不用太多的。他里面的內(nèi)存一般一秒鐘寫(xiě)到磁盤(pán)一次。具體寫(xiě)入方式和你的事務(wù)提交方式有關(guān)。在oracle等數(shù)據(jù)庫(kù)了解這個(gè),一般最大指定為3M比較合適。
innodb_flush_logs_at_trx_commit=0 //控制事務(wù)的提交方式分配原則:這個(gè)參數(shù)只有3個(gè)值,0,1,2請(qǐng)確認(rèn)一下自已能接受的級(jí)別,默認(rèn)為1,主庫(kù)請(qǐng)不要更改了,性能更高的可以設(shè)置為0或是2,但會(huì)丟失一秒鐘的事務(wù),說(shuō)明,這個(gè)參數(shù)的設(shè)置對(duì)Innodb的性能有很大的影響,所以在這里給多說(shuō)明一下,當(dāng)這個(gè)值為1時(shí),innodb 的事務(wù)LOG在每次提交后寫(xiě)入日值文件,并對(duì)日值做刷新到磁盤(pán),這個(gè)可以做到不丟任何一個(gè)事務(wù).
當(dāng)這個(gè)值為2時(shí),在每個(gè)提交,日志緩沖被寫(xiě)到文件,但不對(duì)日志文件做到磁盤(pán)操作的刷新,在對(duì)日志文件的刷新在值為2的情況也每秒發(fā)生一次,但需要注意的是,由于進(jìn)程調(diào)用方面的問(wèn)題,并不能保證每秒100%的發(fā)生,從而在性能上是最快的,但操作系統(tǒng)崩潰或掉電才會(huì)刪除最后一秒的事務(wù),當(dāng)這個(gè)值為0時(shí),日志緩沖每秒一次地被寫(xiě)到日志文件,并且對(duì)日志文件做到磁盤(pán)操作的刷新,但是在一個(gè)事務(wù)提交不做任何操作,mysqld進(jìn)程的崩潰會(huì)刪除崩潰前最后一秒的事務(wù).
新聞熱點(diǎn)
疑難解答
圖片精選