生产环境:

操作系统:CentOS release 6.8

数据库系统:MySQL 5.5.52

分析问题:

一、客户端访问出现以下错误:

错误1:浏览器访问Cacti监控系统

Linux服务器上的MySQL数据库宕掉的原因_第1张图片

FATAL: Cannotconnect to MySQL server on 'localhost'. Please make sure you have specified avalid MySQL database name in 'include/config.php'

错误2:浏览器访问Discuz系统

Discuz! Database Error

The database has encountered a problem. Need Help?

   

Error messages:

  • [Type] 无法连接到数据库服务器

  • [2002] Can't connect to       local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)


Program messages:

  • [Line:       0050]forum.php(discuz_core->init)

  • [Line:       0065]source/class/class_core.php(discuz_core->_init_db)

  • [Line:       0327]source/class/class_core.php(db_mysql->connect)

  • [Line:       0621]source/class/class_core.php(db_mysql->_dbconnect)

错误3:浏览器访问BBS论坛

MYSQL 连接数据库失败,请确定数据库用户名,密码设置正确
Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock'(2)
2002

错误4:由于mysql连接数太多,导致Toomany connections的问题

Linux服务器上的MySQL数据库宕掉的原因_第2张图片

通过以上错误,基本上确定是数据库出了问题。

二、进一步通过服务器来分析问题:

通过SecureCRT远程登录 Linux服务器,通过服务器来分析数据库运行情况:

[root@sky9896/]# service mysqld status

mysqld (pid18965) 正在运行...    #实际上是一个假像,没有真正启动MySQL服务

[root@sky9896/]# lsof -i:3306     #没有监测到3306端口

[root@sky9896/]# mysql -uroot –p     #无法登录数据库服务

ERROR 2002(HY000): Can't connect to local MySQL server through socket '/var

[root@sky9896 ~]# mysql -uroot -p

    Enter password:

    ERROR 1040 (HY000): Too many connections

[root@sky9896 ~]# service mysqld start

Timeout erroroccurred trying to start MySQL Daemon.   #启动 mysqld 时经过很长时间显示

正在启动 mysqld                                          [失败]

[root@sky9896httpd]# service mysqld start

MySQL Daemonfailed to start.

正在启动 mysqld                                          [失败]

[root@sky9896httpd]# service mysqld status

mysqld 已停

三、通过分析日志来找到问题所在

[root@sky9896httpd]# tail  -100 /var/log/mysqld.log  170906 20:27:41 [Note] Plugin 'FEDERATED' isdisabled.

170906 20:27:41InnoDB: The InnoDB memory heap is disabled

170906 20:27:41InnoDB: Mutexes and rw_locks use GCC atomic builtins

170906 20:27:41InnoDB: Compressed tables use zlib 1.2.3

170906 20:27:41InnoDB: Using Linux native AIO

170906 20:27:41InnoDB: Initializing buffer pool, size = 128.0M

170906 20:27:41InnoDB: Completed initialization of buffer pool

170906 20:27:41InnoDB: highest supported file format is Barracuda.

17090620:27:41  InnoDB: Waiting for thebackground threads to start

170906 20:27:42InnoDB: 5.5.52 started; log sequence number 1648953943

170906 20:27:42[Note] Server hostname (bind-address): '0.0.0.0'; port: 3306

170906 20:27:42 [Note]   - '0.0.0.0' resolves to '0.0.0.0';

170906 20:27:42[Note] Server socket created on IP: '0.0.0.0'.

170906 20:27:42 [ERROR] /usr/libexec/mysqld: Error writingfile '/var/run/mysqld/mysqld.pid' (Errcode: 28)   #无法写入文件到磁盘

170906 20:27:42 [ERROR] Can't start server: can't create PIDfile: No space left on device                            #不能创建PID文件

170906 20:27:42mysqld_safe Number of processes running now: 0

170906 20:27:42mysqld_safe mysqld restarted

170906 20:27:42[Note] libgovernor.so not found

170906 20:27:42[Note] /usr/libexec/mysqld (mysqld 5.5.52-cll-lve) starting as process 28237...

170906 20:27:42[Note] Plugin 'FEDERATED' is disabled.

170906 20:27:42InnoDB: The InnoDB memory heap is disabled

170906 20:27:42InnoDB: Mutexes and rw_locks use GCC atomic builtins

170906 20:27:42InnoDB: Compressed tables use zlib 1.2.3

四、尝试启动数据库服务

[root@sky9896~]# /usr/bin/mysql_install_db

WARNING: Thehost 'sky9896' could not be looked up with resolveip.

This probablymeans that your libc libraries are not 100 % compatible

with this binaryMySQL version. The MySQL daemon, mysqld, should work

normally withthe exception that host name resolving will not work.

This means thatyou should use IP addresses instead of hostnames

when specifyingMySQL privileges !

Installing MySQLsystem tables...

170906 20:44:44[Note] libgovernor.so not found

170906 20:44:44[Note] /usr/libexec/mysqld (mysqld 5.5.52-cll-lve) starting as process 8472 ...

ERROR: 1030  Got error28 from storage engine   #28个表无法存储

170906 20:44:45 [ERROR] Aborting

170906 20:44:45[Note] /usr/libexec/mysqld: Shutdown complete

Installation ofsystem tables failed!  Examine the logsin

/var/lib/mysqlfor more information.

You can try tostart the mysqld daemon with:

    shell> /usr/libexec/mysqld --skip-grant&

and use thecommand line tool /usr/bin/mysql

to connect tothe mysql database and look at the grant tables:

    shell> /usr/bin/mysql -u root mysql

    mysql> show tables

……

 通过上面的分析,确定磁盘空间不够用。

[root@sky9896tmp]# df  -h

Filesystem      Size Used Avail Use% Mounted on

/dev/sda2       49G   47G     0   100%/    #49G-47G,还是无可用空间,

tmpfs           939M 100K  939M   1% /dev/shm

/dev/sda1        93M  54M   35M  61% /boot

/dev/sda5       179G 2.3G  168G   2% /home

 

[root@sky9896/]# du  -sh *   #查看文件大小

12K     a

152K    app

13G     application

6.7M    bin

52M     boot

4.0K    data

4.0K    data.php

340K    dev

326M    etc

2.2G    home

47M     html

318M    lib

20M     lib64

0       libmysqlclient_r.so

0       libmysqlclient.so

16K     lost+found

4.0K    media

8.0K    mnt

88M     oe

90M     opt

38M     root

53M     safedog_linux32

9.4M    sbin

376K    scripts

4.0K    selinux

42M     skyboy

4.0K    srv

14M     tmp

4.0K    usb

3.0G    usr

31G     var

[root@sky9896 www]# rm 5-*   #多删除一些文件,腾出可用空间

rm:是否删除普通文件"5-10第五课创业方式选择故事5专家点评.rar"y

rm:是否删除普通文件"5-1第五课创业方式选择故事1教学案例.rar"?y

rm:是否删除普通文件"5-2第五课创业方式选择故事1专家点评.rar"?y

rm:是否删除普通文件"5-3第五课创业方式选择故事2教学案例.rar"?y

rm:是否删除普通文件"5-4第五课创业方式选择故事2专家点评.rar"?y

rm:是否删除普通文件"5-5第五课创业方式选择故事3教学案例.rar"?y

rm:是否删除普通文件"5-6第五课创业方式选择故事3专家点评.rar"?y

rm:是否删除普通文件"5-7第五课创业方式选择故事4教学案例.rar"?^Z

[3]+  Stopped                 rm -i 5-*

[root@sky9896www]# df -h

Filesystem      Size Used Avail Use% Mounted on

/dev/sda2       49G   45G  1.2G 98% /   #可用空间1.2G

tmpfs           939M 100K  939M   1% /dev/shm

/dev/sda1        93M  54M   35M  61% /boot

/dev/sda5       179G 2.3G  168G   2% /home

[root@sky9896www]# service mysqld start

正在启动 mysqld:            [确定]    #数据库服务正常启动

小结,最终原因是Linux根目录”/”下可使用空间不足,导致数据库服务无法启动。网上有许多解决方法,并不能实际解决,关键还是要查看运行日志找问题。因作者是采用普通电脑架设Linux服务器,因上面跑了很多系统,导致空间骤增不够用。