我在 Debian(Jessie)服务器上使用 MySQL 时遇到了一个奇怪的问题。
当服务器重新启动时,systemd 将尝试启动 mysqld,但会失败(看起来端口 3306 已被使用,但我不明白为什么会这样)。
但是,如果我几分钟后通过 SSH 连接到服务器并运行,sudo systemctl start mysql
它就会成功。
每次我重新启动服务器时都会重现该问题。
有没有人遇到过这个问题,或者猜猜是什么原因导致 MySQL 无法正常启动?我可以在重启后启动 MySQL 之前添加一个延迟,但我想了解发生了什么,实际上我甚至不确定这是否能解决问题。
sudo systemctl status mysql -l
重启后的结果如下:
● mysql.service - LSB: Start and stop the mysql database server daemon
Loaded: loaded (/etc/init.d/mysql)
Active: failed (Result: exit-code) since Fri 2017-09-01 21:54:44 CEST; 1min 41s ago
Process: 581 ExecStart=/etc/init.d/mysql start (code=exited, status=1/FAILURE)
Sep 01 21:54:06 gimli systemd[1]: Starting LSB: Start and stop the mysql database server daemon...
Sep 01 21:54:44 gimli mysql[581]: Starting MySQL database server: mysqld . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . failed!
Sep 01 21:54:44 gimli systemd[1]: mysql.service: control process exited, code=exited status=1
Sep 01 21:54:44 gimli systemd[1]: Failed to start LSB: Start and stop the mysql database server daemon.
Sep 01 21:54:44 gimli systemd[1]: Unit mysql.service entered failed state.
以及来自的相应日志/var/log/mysql/error.log
170901 21:54:16 [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.
170901 21:54:16 [Note] Plugin 'FEDERATED' is disabled.
170901 21:54:17 InnoDB: The InnoDB memory heap is disabled
170901 21:54:17 InnoDB: Mutexes and rw_locks use GCC atomic builtins
170901 21:54:17 InnoDB: Compressed tables use zlib 1.2.8
170901 21:54:17 InnoDB: Using Linux native AIO
170901 21:54:17 InnoDB: Initializing buffer pool, size = 128.0M
170901 21:54:17 InnoDB: Completed initialization of buffer pool
170901 21:54:17 InnoDB: highest supported file format is Barracuda.
170901 21:54:22 InnoDB: Waiting for the background threads to start
170901 21:54:23 InnoDB: 5.5.57 started; log sequence number 351234412
170901 21:54:23 [Note] Server hostname (bind-address): '192.168.1.14'; port: 3306
170901 21:54:23 [Note] - '192.168.1.14' resolves to '192.168.1.14';
170901 21:54:23 [Note] Server socket created on IP: '192.168.1.14'.
170901 21:54:23 [ERROR] Can't start server: Bind on TCP/IP port: Cannot assign requested address
170901 21:54:23 [ERROR] Do you already have another mysqld server running on port: 3306 ?
170901 21:54:23 [ERROR] Aborting
170901 21:54:23 InnoDB: Starting shutdown...
170901 21:54:24 InnoDB: Shutdown completed; log sequence number 351234412
170901 21:54:24 [Note] /usr/sbin/mysqld: Shutdown complete
但是,如果我sudo systemctl start mysql
手动运行,它可以正常工作:
170901 22:01:36 [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.
170901 22:01:36 [Note] Plugin 'FEDERATED' is disabled.
170901 22:01:36 InnoDB: The InnoDB memory heap is disabled
170901 22:01:36 InnoDB: Mutexes and rw_locks use GCC atomic builtins
170901 22:01:36 InnoDB: Compressed tables use zlib 1.2.8
170901 22:01:36 InnoDB: Using Linux native AIO
170901 22:01:36 InnoDB: Initializing buffer pool, size = 128.0M
170901 22:01:36 InnoDB: Completed initialization of buffer pool
170901 22:01:36 InnoDB: highest supported file format is Barracuda.
170901 22:01:36 InnoDB: Waiting for the background threads to start
170901 22:01:37 InnoDB: 5.5.57 started; log sequence number 351234412
170901 22:01:37 [Note] Server hostname (bind-address): '192.168.1.14'; port: 3306
170901 22:01:37 [Note] - '192.168.1.14' resolves to '192.168.1.14';
170901 22:01:37 [Note] Server socket created on IP: '192.168.1.14'.
170901 22:01:37 [Note] Event Scheduler: Loaded 0 events
170901 22:01:37 [Note] /usr/sbin/mysqld: ready for connections.
Version: '5.5.57-0+deb8u1' socket: '/var/run/mysqld/mysqld.sock' port: 3306 (Debian)
据我所知,systemd 脚本尚未被修改(它是 Debian mysql 软件包附带的脚本),并且据我所知,它从未正常工作过。3306 端口不应被其他进程使用。
答案1
尝试在重启并以 root 身份运行后立即通过 SSH 进入服务器,netstat -lp | grep 3306
以检查哪个程序实际上在监听该端口,以便您可以进一步调试该问题。
答案2
显然,问题在于 mysql 启动时网络接口未启动。这可能与 DHCP 花费一些时间有关。
至少,现在我将接口设置为静态 IP,我不再观察到 mysql 问题(再次,每次启动时都可以重现),所以问题解决了。
以下是在 Debian 上设置静态 IP 的步骤:
- 在尝试干扰网络之前,请确保您可以物理访问您的服务器(电源按钮、键盘、屏幕),因为您可能会失去 ssh 功能
- 编辑 /etc/network/interfaces
替换
iface eth0 inet dhcp
为:iface eth0 inet static address 192.168.1.14 network 192.168.1.0 netmask 255.255.255.0 broadcast 192.168.1.255 gateway 192.168.1.1
- 重启(注意:重启网络就足够了,例如
systemctl restart networking
,但就我而言,我失去了连接,但如果不使用电源按钮重启服务器就无法重新连接)
在此示例中,我分配了静态 IP 192.168.1.14 和网关 192.168.1.1
当然,这只是一种解决方法,真正的问题是 mysql 应该等待网络接口启动,但我对 systemd 不太了解,无法做到这一点。