MySQL服务定期脱机并提供ERROR 2002(HY000):无法连接到本地MySQL服务器

时间:2014-12-19 17:53:46

标签: php mysql ubuntu vps

问题

我有一个使用Ubuntu 14.04,PHP 5.5.9和mysql Ver 12.12 Distrib 5.5.40在VPS上运行的网站。

大约90%的时间,一切似乎都在使用MySQL服务正常运行。然而,偶尔MySQL服务似乎会崩溃,并且它不会自行恢复在线状态。

2015年更新

由于升级虚拟专用服务器实例以拥有更多RAM,因此此问题尚未再次出现。在一个盒子上运行Apache和MySQL服务器需要超过512MB的内存才能获得稳定的网站。

我的调试步骤到目前为止

  1. 当我在Apache错误日志中看到以下错误时,我首先注意到了这个问题:

    PHP Warning: mysqli::mysqli(): (HY000/2002): Connection refused in [...]

  2. 我检查了日志文件/var/log/mysql.errvar/log/mysql.log/var/log/mysql/error.log,但它们都是空的。

  3. 我检查了日志文件/var/log/mysql/error.log.1.gz并找到了:

    141218 14:30:20 [Warning] Using unique option prefix myisam-recover instead of myisam-recover-options is deprecated and will be removed in a future release. Please use the full name instead. 141218 14:30:20 [Note] Plugin 'FEDERATED' is disabled. 141218 14:30:20 InnoDB: The InnoDB memory heap is disabled 141218 14:30:20 InnoDB: Mutexes and rw_locks use GCC atomic builtins 141218 14:30:20 InnoDB: Compressed tables use zlib 1.2.8 141218 14:30:20 InnoDB: Using Linux native AIO 141218 14:30:20 InnoDB: Initializing buffer pool, size = 128.0M InnoDB: mmap(137363456 bytes) failed; errno 12 141218 14:30:20 InnoDB: Completed initialization of buffer pool 141218 14:30:20 InnoDB: Fatal error: cannot allocate memory for the buffer pool 141218 14:30:20 [ERROR] Plugin 'InnoDB' init function returned error. 141218 14:30:20 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed. 141218 14:30:20 [ERROR] Unknown/unsupported storage engine: InnoDB 141218 14:30:20 [ERROR] Aborting

  4. 我现在从命令行检查与mysql的连接: mysql -u root -p并返回ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (111)

  5. 无赖。我尝试telnet 127.0.0.1 3306并获得telnet: Unable to connect to remote host: Connection refused

  6. 我还查看了netstat -a | grep mysql并且没有输出

  7. 运行sudo service mysql start后,一切恢复正常。 netstat显示正在运行的mysql服务,telnet连接,mysql命令行连接工作,并且不再有PHP警告。网站恢复正常功能。

  8. 我查看/var/log/mysql/error.log并看到以下内容:

    141219 12:26:31 [Warning] Using unique option prefix myisam-recover instead of myisam-recover-options is deprecated and will be removed in a future release. Please use the full name instead. 141219 12:26:31 [Note] Plugin 'FEDERATED' is disabled. 141219 12:26:31 InnoDB: The InnoDB memory heap is disabled 141219 12:26:31 InnoDB: Mutexes and rw_locks use GCC atomic builtins 141219 12:26:31 InnoDB: Compressed tables use zlib 1.2.8 141219 12:26:31 InnoDB: Using Linux native AIO 141219 12:26:31 InnoDB: Initializing buffer pool, size = 128.0M 141219 12:26:31 InnoDB: Completed initialization of buffer pool 141219 12:26:31 InnoDB: highest supported file format is Barracuda. InnoDB: The log sequence number in ibdata files does not match InnoDB: the log sequence number in the ib_logfiles! 141219 12:26:31 InnoDB: Database was not shut down normally! InnoDB: Starting crash recovery. InnoDB: Reading tablespace information from the .ibd files... InnoDB: Restoring possible half-written data pages from the doublewrite InnoDB: buffer... 141219 12:26:31 InnoDB: Waiting for the background threads to start 141219 12:26:32 InnoDB: 5.5.40 started; log sequence number 19000727 141219 12:26:32 [Note] Server hostname (bind-address): '127.0.0.1'; port: 3306 141219 12:26:32 [Note] - '127.0.0.1' resolves to '127.0.0.1'; 141219 12:26:32 [Note] Server socket created on IP: '127.0.0.1'. 141219 12:26:32 [Note] Event Scheduler: Loaded 0 events 141219 12:26:32 [Note] /usr/sbin/mysqld: ready for connections. Version: '5.5.40-0ubuntu0.14.04.1' socket: '/var/run/mysqld/mysqld.sock' port: 3306 (Ubuntu)

  9. 我期望的结果

    没有让MySQL服务失效显然是理想的,但至少我想要一些方法来检测它是否发生故障,然后在发生这种情况时自动重启它。我考虑过设置一个cron作业来运行一个尝试连接到本地mysql服务的脚本,然后在连接失败时重新启动服务。但是,如果我能够解决根本原因而不是坚持使用创可贴,那将会好得多。

    当前理论

    现在我发现的唯一线索是我的调试步骤中步骤3中日志文件/var/log/mysql/error.log.1.gz的最后7行。我的理论是我的VPS有时超载导致MySQL服务崩溃和烧毁,但我真的不想在不知道可以解决问题的情况下增加我的VPS大小。我希望有人遇到类似的问题,并且可以说明服务是否因为内存不足而导致服务中断,以及内存错误是否是一个不同的问题。

    其他StackOverflow问题

    我还查看了PHP MYSQL could not connect (HY000/2002)mysqli::mysqli(): (HY000/2002): Can't connect to local MySQL server through socket 'MySQL' (2)但这两个问题似乎只是解决了从PHP连接到MySQL的问题,而不是调试MySQL服务本身的问题。

0 个答案:

没有答案