mysql 在 debian 啟動(systemd)時無法啟動,但手動啟動可以

mysql 在 debian 啟動(systemd)時無法啟動,但手動啟動可以

我在 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 的過程:

  1. 在嘗試幹擾網路之前,請確保您可以實際存取伺服器(電源按鈕、鍵盤、螢幕),因為您可能會失去 ssh 功能
  2. 編輯/etc/network/interfaces
  3. 替換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
    
  4. 重新啟動(注意:重新啟動網路應該足夠了,例如使用systemctl restart networking,但在我的情況下,我失去了連接,但如果不使用電源按鈕重新啟動伺服器則無法重新連接)

在此範例中,我指派靜態 IP 192.168.1.14 和網關 192.168.1.1

當然,這只是一種解決方法,真正的問題是 mysql 應該等待網路介面啟動,但我對 systemd 的了解還不足以做到這一點。

相關內容