如何减少 innodb 数据库关闭的时间?

来源:互联网 发布:自拍 婚纱照 知乎 编辑:程序博客网 时间:2024/05/01 06:50

来自:http://blog.chinaunix.net/uid-10449864-id-2956856.html


     大家在使用innodb数据库的时候,会发现有这么一个现象,当我们需要关闭或者重新启动mysql服务的时候,会需要相当一段长的时间,如果是数据库进程异常关闭,打开数据库又会花很长的时间来作恢复。
     开始还以为是数据库太大了引起的,后来看到一篇文章讲的是 “innodb_max_dirty_pages_pct与检查点的关系” ,才明白其中的道理。通常的原因是来自 buffer pool 的脏页数据交换,mysql会通知OS,把一些脏页交换出去,而交换是在内存中进行修改,而不是在硬盘上。


如果你需要快速关闭 innodb 数据库,那么有一个办法,如下:


1,设置全局变量,设置 innodb_max_dirty_pages_pct 参数的值。
mysql> set global innodb_max_dirty_pages_pct = 0;


2,执行如下命令,查看被修改的数据页面。
/usr/local/mysql/bin/mysqladmin -uroot -p  ext -i10 | grep dirty
Enter password:
| Innodb_buffer_pool_pages_dirty    | 4530        |
| Innodb_buffer_pool_pages_dirty    | 4576        |
| Innodb_buffer_pool_pages_dirty    | 4475        |
| Innodb_buffer_pool_pages_dirty    | 4496        |
| Innodb_buffer_pool_pages_dirty    | 4411        |


当这个参数变为 0,或者接近 0(如果你的服务器在使用,是不会变成 0 的)。
此时,这个数值相当的低时,你就可以重新启动你的数据库服务,这个时候,你会发现速度相当的快。


注意:
    这个数值和 innodb buffer 有很大的关系,一般不建议更改这个数值,除非你的 innodb buffer 很大。我这里有3台数据库服务器,一个设置了3G的 innodb buffer 脏页数据在1000左右,其他的是1G的 innodb buffer 脏页在4000左右,,看来还是 innodb buffer 越大越好呀!
   
    也许你会说可以通过 innodb_fast_shutdown 这个选项来调整关闭速度,系统默认为 1,但是如果你将此项的值修改为 0 或者 2,,这样的mysql就比较危险。
   
   
官方描述如下:

The InnoDB shutdown mode. By default, the value is 1, which causes a “fast” shutdown (the normal type of shutdown).

If the value is 0, InnoDB does a full purge and an insert buffer merge before a shutdown. These operations can take minutes, or even hours in extreme cases.

If the value is 1, InnoDB skips these operations at shutdown.

If the value is 2, InnoDB will just flush its logs and then shut down cold, as if MySQL had crashed; no committed transaction will be lost, but crash recovery will be done at the next startup. A value of 2 cannot be used on NetWare.

原创粉丝点击