关于ubuntu环境下的mysql错误:
OperationalError at /admin/article/body/
(2003, "Can't connect to MySQL server on '127.0.0.1' ([Errno 111] Connection refused)")
重启mysql后,大概半小时后,又不断报错,再次重启mysql报错;
$ /etc/init.d/mysqld start
Starting mysqld (via systemctl): Job for mysqld.service failed because the control process exited with error code. See "systemctl status mysqld.service" and "journalctl -xe" for details. [FAILED]
根据以上错误提示,分别使用systemctl status mysqld.service和journalctl -xe查看服务启动失败的原因:
[mysql]
Mar 25 12:51:10 ip-172-31-17-237 audit[99286]: AVC apparmor="DENIED" operation="open" profile="/usr/sbin/mysqld" name="/proc/99286/status" pid
Mar 25 12:51:10 ip-172-31-17-237 audit[99286]: AVC apparmor="DENIED" operation="open" profile="/usr/sbin/mysqld" name="/sys/devices/system/nod
Mar 25 12:51:10 ip-172-31-17-237 audit[99286]: AVC apparmor="DENIED" operation="open" profile="/usr/sbin/mysqld" name="/proc/99286/status" pid
Mar 25 12:51:11 ip-172-31-17-237 systemd[1]: mysql.service: Main process exited, code=exited, status=1/FAILURE
通过上面也没发现导致mysql启动错误的关键原因:
然后查看开MySQL的错误日志,我的错误日志在/var/log/mysql
目录下
$ cat /var/log/mysq/error.log
2018-03-25T12:54:12.565469Z 0 [Note] InnoDB: Using Linux native AIO
2018-03-25T12:54:12.565700Z 0 [Note] InnoDB: Number of pools: 1
2018-03-25T12:54:12.565808Z 0 [Note] InnoDB: Using CPU crc32 instructions
2018-03-25T12:54:12.567294Z 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M
2018-03-25T12:54:12.567327Z 0 [ERROR] InnoDB: mmap(137428992 bytes) failed; errno 12
2018-03-25T12:54:12.567334Z 0 [ERROR] InnoDB: Cannot allocate memory for the buffer pool
2018-03-25T12:54:12.567338Z 0 [ERROR] InnoDB: Plugin initialization aborted with error Generic error
2018-03-25T12:54:12.567343Z 0 [ERROR] Plugin 'InnoDB' init function returned error.
2018-03-25T12:54:12.567346Z 0 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
2018-03-25T12:54:12.567350Z 0 [ERROR] Failed to initialize builtin plugins.
2018-03-25T12:54:12.567353Z 0 [ERROR] Aborting
通过以上日志,可以看到Cannot allocate memory for the buffer pool,这是因为MySQL内存不足导致启动失败,
查询内存的命令:
$ free -h
解决方法:
增加swap交换空间解决问题:
dd if=/dev/zero of=1.txt bs=1M count=2 生成一个指定大小的空文件
if=文件名:输入文件名
of=文件名:输出文件名
bs=字节大小
count=个数
$ dd if=/dev/zero of=/swapfile bs=1M count=1024
$ mkswap /swapfile
$ swapon /swapfile
增加自动挂载:
在文件/etc/fstab中加入 /swapfile swap swap defaults 0 0
$ sudo vim /etc/fstab
在下面添加/swapfile swap swap defaults 0 0
重启mysql
sudo /etc/init.d/mysql restart
重启后问题解决。