WordPressでError establishing a database connectionのエラーが出たときの対処手順
WordPressを使っていて「Error establishing a database connection」とエラーが表示されたときの対処方法を解説します。
エラーの原因
エラー原因はいくつか考えられるようですが、今回は、MySQLのひとつのテーブル「wp_options」が破損していたためでした。破損した状態でCentOSを再起動したら、このエラーが表示されるようになりました。
エラーの解消方法
MySQLのコマンドで、破損した「wp_options」テーブルを自動修復します。
エラーを解消するための手順
1)mysqlへログイン
# mysql -u root -p
このあとパスワードを訊かれるのでパスワードを入力
2)プロンプトが「mysql」に変わるとログイン完了
mysql>
3)WordPressのユーザへ切替
mysql> use wordpress;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
「Database changed」が出るとユーザ切替完了
4)「wp_options」のエラーをチェック
mysql> check table wp_options;
+———————-+——-+———-+———————————-+
| Table | Op | Msg_type | Msg_text |
+———————-+——-+———-+———————————-+
| wordpress.wp_options | check |warning | Table is marked as crashed |
| wordpress.wp_options | check | warning | 6 clients are using or haven’t closed the table properly |
| wordpress.wp_options | check | error | Record at pos: 393772 is not remove-marked|
| wordpress.wp_options | check | error | record delete-link-chain corrupted|
| wordpress.wp_options | check | error | Corrupt |
+———————-+——-+———-+———————————-+
5 rows in set (0.01 sec)
テーブルにエラーがあることを示す「warning」や「error」が表示されています。
このエラーをコマンドで修復する必要があります。
5)壊れたテーブルを修復
mysql> repair table wp_options;
+———————-+——–+———-+———————————+
| Table | Op | Msg_type | Msg_text |
+———————-+——–+———-+———————————+
| wordpress.wp_options | repair | warning | Duplicate key for record at 390084 against record at 162520 |
| wordpress.wp_options | repair | warning | Number of rows changed from 328 to 326 |
| wordpress.wp_options | repair | status | OK |
+———————-+——–+———-+———————————+
3 rows in set (0.04 sec)
「OK」が出ているので修復されたようです。
6)「wp_options」のエラーを再チェック
mysql> check table wp_options;
+———————-+——-+———-+———-+
| Table | Op | Msg_type | Msg_text |
+———————-+——-+———-+———-+
| wordpress.wp_options | check | status | OK |
+———————-+——-+———-+———-+
1 row in set (0.00 sec)
ステータスは「OK」なので、修復が完了しました。