阿里云服务器 MySQL 经常自动停止、挂掉、重启。打开 MySQL 的 error.log 错误信息,在阿里云 CentOS 的路径为 /alidata/log/mysql/error.log,如下:
2016-03-13 00:16:37 0[Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use--explicit_defaults_for_timestampserver option(see documentationformore details).
2016-03-13 00:16:37 30576[Note] Plugin'FEDERATED'is disabled.
2016-03-13 00:16:37 30576[Note] InnoDB: Using atomics to ref count buffer pool pages
2016-03-13 00:16:37 30576[Note] InnoDB: The InnoDB memory heap is disabled
2016-03-13 00:16:37 30576[Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2016-03-13 00:16:37 30576[Note] InnoDB: Memory barrier is not used
2016-03-13 00:16:37 30576[Note] InnoDB: Compressed tables use zlib 1.2.3
2016-03-13 00:16:37 30576[Note] InnoDB: Using Linux native AIO
2016-03-13 00:16:37 30576[Note] InnoDB: Using CPU crc32 instructions
2016-03-13 00:16:37 30576[Note] InnoDB: Initializing buffer pool, size=64.0M
2016-03-13 00:16:37 30576[Note] InnoDB: Completed initialization of buffer pool
2016-03-13 00:16:37 30576[Note] InnoDB: Highest supported file format is Barracuda.
2016-03-13 00:16:37 30576[Note] InnoDB: The log sequence numbers 52296883 and 52296883inibdata filesdonot match the log sequence number 93636989inthe ib_logfiles!
2016-03-13 00:16:37 30576[Note] InnoDB: Database was not shutdown normally!
2016-03-13 00:16:37 30576[Note] InnoDB: Starting crash recovery.
2016-03-13 00:16:37 30576[Note] InnoDB: Reading tablespace information from the .ibd files...
2016-03-13 00:16:37 30576[Note] InnoDB: Restoring possible half-written data pages
2016-03-13 00:16:37 30576[Note] InnoDB: from the doublewrite buffer...InnoDB: Last MySQL binlog file position 0 9828, file name mysql-bin.000335
2016-03-13 00:16:37 30576[Note] InnoDB: 128 rollback segment(s)are active.
2016-03-13 00:16:37 30576[Note] InnoDB: Waitingforpurge to start
2016-03-13 00:16:37 30576[Note] InnoDB: 5.6.21 started;log sequence number 93636989
2016-03-13 00:16:38 30576[Note] Recovering after a crash using mysql-bin
2016-03-13 00:16:38 30576[Note] Starting crash recovery...
2016-03-13 00:16:38 30576[Note] Crash recovery finished.
2016-03-13 00:16:40 30576[Note] Server hostname(bind-address):'*';port: 3306
2016-03-13 00:16:40 30576[Note] IPv6 is not available.
2016-03-13 00:16:40 30576[Note] -'0.0.0.0'resolves to'0.0.0.0';
2016-03-13 00:16:40 30576[Note] Server socket created on IP:'0.0.0.0'.
2016-03-13 00:16:41 30576[Note] Event Scheduler: Loaded 0 events
2016-03-13 00:16:41 30576[Note] /alidata/server/mysql/bin/mysqld: readyforconnections.Version:'5.6.21-log'socket:'/tmp/mysql.sock'port: 3306 MySQL Community Server(GPL)160313 00:16:41 mysqld_safe Number of processes running now: 0160313 00:16:41 mysqld_safe mysqld restarted
MySQL 的 error.log 中,我们很难找出出是哪里出了问题。去 Google 了一下,给出的答案基本是修改”/alidata/server/mysql/my.cnf”:
innodb_buffer_pool_size=64M# 默认为128M,修改成64M、32M或者8M,但实际上重启 MySQL 之后以为能运行,实际上过了一会 MySQL 还是会出现不知原因地自动关闭。
注意去看其中的2016-03-13 00:16:37 30576[Note] InnoDB: Database was not shutdown normally!说明了 MySQL 非正常关闭,MySQL 也不知道自己为什么被关闭掉了。。。于是, MySQL 觉得真奇怪,自己又重启了自己的进程开始 Crash Recoverying,如下:
接着,大家又猜想这条2016-03-13 00:16:37 30576[Note] InnoDB: The log sequence numbers 52296883 and 52296883inibdata filesdonot match the log sequence number 93636989inthe ib_logfiles!Google,发现原来就是这里捣鬼,才出现了 MySQL 再重启之后,又继续自动关闭,再重启。于是,尝试按照 StackOverflow 上面的 suggestions 去做:
重启,又可以了。。。没一会,嚓,MySQL 又宕了!这次不仅宕,而且 error.log 又多了因 * innodb_force_recovery = 6 * 而引起的错误。
怎么办?现在我们回到下面这条日志:
2016-03-13 00:16:37 30576[Note] InnoDB: Database was not shutdown normally!
现在我们要看看,为什么 MySQL 会不正常关闭呢!?现在我们打开top看看。下面使用htop进行查看:
一看 mysql 进程占用的 VIRT 怎么这么多!这时,你要是多刷新几下你服务器上搭建的 WordPress 网站的主页的话,会出现 mysql 内存占用率超过 50% 的情况,这时候 mysql 进程就会被 linux 内核杀死。也就出现了我们在 MySQL 的 error.log 中看到的“[Note] InnoDB: Database was not shutdown normally!” 日志了。
为了确定这个猜测是否真实,我们去看看 kernel 日志。
接着你会看到:
看到最后的两条:
好了,终于找出原因了,内存不够,杀死了 mysqld 进程。
下面是我的优化方案:
1.创建 SWAP 分区:
检查系统Swap文件信息
sudo swapon -s
free -m
检查硬盘可用空间
df -h
创建Swap文件
sudo /bin/dd if=/dev/zero of=/var/swap.1 bs=1M count=1024
激活Swap
sudo /sbin/mkswap /var/swap.1
sudo /sbin/swapon /var/swap.1
使Swap在主机重启后依然生效
sudo nano /etc/fstab
加入以下内容
/var/swap.1 swap swap defaults 0 0
All done!
2.降低数据库 InnoDB 引擎的缓冲区大小,以及限制 MySQL 的最大连接数(max_connections):
降低 InnoDB 缓冲区大小为 64M 或者 32M(到mysql配置文件中修改)
innodb_buffer_pool_size=64M
限制最大连接数为100,在服务器配置很低时可以继续降低
max_connections=100
修改完重启 MySQL:
service mysqld restart
注释:max_connections 的默认值是 151,可以动态更改这个值。参见:max_connections
如有必要,可以重启服务器: reboot