Trusted WordPress tutorials, when you need them most.
Beginner’s Guide to WordPress
WPBカップ
25 Million+
Websites using our plugins
16+
Years of WordPress experience
3000+
WordPress tutorials
by experts

WordPressでデータベース接続の確立エラーを修正する方法

WordPressで「Error establishing a database connection(データベース接続の確立エラー)」通知が表示されていませんか?

サイトにユーザーがアクセスできなくなる致命的なエラーです。WordPressがデータベースに接続できない場合に発生します。複数の要因がこの接続に影響する可能性があるため、初心者がこのエラーをトラブルシューティングするのは少し難しいかもしれません。

この投稿では、WordPressサイトの「Error establishing a database connection(データベース接続の確立エラー)」を簡単に修正する方法を順を追って説明します。

Fix the Error Establishing a Database Connection in WordPress

WordPressのデータベース接続確立エラーの原因は?

データベースとは、データを簡単に保存、整理し、他のソフトウェアから取り出すためのソフトウェアである。

コンテンツ管理システムであるWordPressは、すべてのコンテンツやその他のサイトデータを保存するためにデータベースを使用します。そして、誰かがあなたのサイトにアクセスするたびにデータベースに接続します。

WordPressはデータベースに接続するために以下の情報が必要です:

  • データベース名
  • データベースのユーザー名とパスワード
  • データベースサーバー

この情報は、wp-config.phpと呼ばれるWordPressの設定ファイルに保存されます。

これらの情報に誤りがある場合、WordPressはデータベースサーバーへの接続に失敗し、「Error establishing a database connection(データベース接続の確立エラー)」というエラーが表示されます。

Database connection error in WordPress

WordPressで最もよく見られるエラーのひとつです。不正なログイン情報以外にも、データベースサーバーがダウンしていたり、データベースファイルが破損している場合にもこのエラーが表示されることがあります。

それでは、WordPressの「Error establishing database connection(データベース接続の確立エラー)」問題を解決する方法を、ステップ・バイ・ステップのトラブルシューティングとともに見ていきましょう。

1.WordPressデータベースのログイン情報を確認する

データベース接続エラーの最も一般的な原因は、ログイン情報が正しくないことです。WordPressサイトを最近新しいホスティングサービスに移行した場合は、この問題が考えられます。

WordPressデータベースのログイン情報は、wp-config.phpファイルに保存されています。これはWordPressの設定ファイルで、データベース情報を含む重要な設定が含まれています。

wp-config.phpファイルを編集したことがない場合は、初心者向けのwp-config.phpファイルの編集方法をご覧ください。

wp-config.phpファイルで以下の行を探します:

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define( 'DB_NAME', 'database_name_here' );
/** MySQL database username */
define( 'DB_USER', 'username_here' );
/** MySQL database password */
define( 'DB_PASSWORD', 'password_here' );
/** MySQL hostname */
define( 'DB_HOST', 'localhost' );

ここで、データベース名、ユーザー名、パスワード、データベースホストの情報が正しいことを確認しなければなりません。

この情報は、WordPressホスティングアカウントのダッシュボードから確認できます。このチュートリアルでは、Bluehost を使用しますが、他のホスティング パネルでも同様のプロセスになります。

ログイン中、左カラムの「ウェブサイト」タブをクリックし、「詳細設定」タブに切り替えます。次に、「データベース」セクションまでスクロールダウンし、その横にある「管理」ボタンをクリックします。

Click the Manage button next to the Database section

これにより、cPanelのMySQL Databasesページが新しいウィンドウで開きます。

ここで、’Current Database’ セクションにデータベース名とユーザー名があります。これらの情報をコピー&ペーストして、wp-configファイルに追加してください。

Find your database name and username in the Current Databases section

次に、「現在のユーザー」セクションまでスクロールダウンし、データベース名の横にある「パスワードの変更」リンクをクリックします。

新しい画面が表示され、データベースのパスワードを変更することができます。

Change your database password

データベースの詳細をすべて確認したら、必要に応じてwp-config.phpファイルで情報を変更できます。

その後、もう一度サイトにアクセスしてみて、データベース接続エラーがなくなっているかどうかを確認してください。

それでもエラーが表示される場合は、何か他の問題があることを意味します。

2.データベースホスティングサービス情報の確認

データベース名、ユーザー名、パスワードの情報が正確であることを確信している場合は、正しいデータベースホスティング情報を使用していることを確認してください。

ほとんどのWordPressホスティング会社は、データベースのホスティングサービスとしてlocalhostを使用しています。しかし、一部のマネージドWordPressホスティング会社は、データベースのホスティングサービスに区切りのサーバーを使用しています。その場合、データベースのホスティングサービスはlocalhostではありません。

つまり、WordPressホスティングサービス会社に連絡して、これらの詳細を確認する必要があります。

3.WordPressデータベースの修復

現在、wp-adminダッシュボードで「1つまたは複数のデータベーステーブルが使用できません」または「データベースの修復が必要な場合があります」のような別のエラーが表示されている可能性があります。その場合は、データベースを修復する必要があります。

これは、wp-config.phpファイルに以下の行を追加することで可能です。本当に〜してもよいですか?Happy blogging’の行の直前に追加してください:

define('WP_ALLOW_REPAIR', true);

設定が完了したら、このページhttp://www.yoursite.com/wp-admin/maint/repair.php。yoursite.comを自分のドメイン名に置き換えてください。

ここで、「データベースの修復」ボタンをクリックしてプロセスを開始する必要があります。

Repair WordPress database

注:データベース修復ページにアクセスするために、ユーザーはログイン中である必要はありません。データベースの修復と最適化が完了したら、wp-config.phpからこの行を削除してください。

しかし、サイトにコードを追加したくない場合は、ホスティングアカウントのcPanelからデータベースを修復することもできます。

ホスティングサービスアカウントにアクセスし、左の列から「ウェブサイト」ページを開きます。次に、’Advanced’タブに切り替え、データベースセクションの’Manage’ボタンをクリックします。

Click the Manage button next to the Database section

新しいタブでcPanelが開きます。ここで、「データベースの修正」セクションまでスクロールダウンし、「データベースの修復」ドロップダウンメニューからデータベース名を選択します。

そうしたら、「データベースの修復」ボタンをクリックしてプロセスを開始する。

Click the Repair database button

ホスティングサービスパネルが自動的にデータベースを修復します。

プロセスが完了すると、成功のメッセージも表示されます。

Database repair success message

これでWordPressサイトにアクセスして、エラーが解決したかどうかを確認することができます。

4.データベースサーバーがダウンしていないか確認する

すべてが正しいように見えてもWordPressがデータベースに接続できない場合は、データベースサーバー(MySQLサーバー)がダウンしている可能性があります。

これは、サーバーのトラフィックが多いために起こる可能性があります。ホスティングサービスのサーバーが負荷に耐えられないのです(特に共有ホスティングの場合)。

このため、サイトが遅くなり、ユーザーによってはエラーが出力されることもあります。この場合、ホスティングサービスプロバイダーと電話またはライブチャットで、MySQLサーバーがレスポンシブかどうかを確認する必要があります。

さらに、同じサーバーで他のサイトを運営している場合は、それらのサイトをチェックして、SQLサーバーがダウンしていることを確認することができる。

同じホスティングアカウントに他のサイトがない場合は、ダッシュボードに移動し、[詳細設定]タブに切り替えるだけです。

その後、phpMyAdminセクションの横にある「管理」ボタンをクリックします。

Click the Manage button next to the PHPMyAdmin option

新しいウィンドウでphpMyAdminが開きますので、一番上の「データベース」設定をクリックしてください。

その後、データベースの名前をクリックして、その設定にアクセスしてください。それができたら、データベース・ユーザーに十分な権限があるかどうかをチェックしましょう。

Connect PHPMyAdmin with your database

そのためには、testconnection.phpというファイルを新規作成し、以下のコードを貼り付ける必要があります:

<?php
$link = mysqli_connect('localhost', 'username', 'password');
if (!$link) {
die('Could not connect: ' . mysqli_error());
}
echo 'Connected successfully';
mysqli_close($link);
?>

コードを貼り付けたら、ユーザー名とパスワードを自分のものに置き換えてください。このファイルをサイトにアップロードし、ウェブブラウザーからアクセスすることができます。

スクリプトが正常に接続された場合は、ユーザーに十分な権限があることを意味します。

次に、wp-configファイルに戻り、すべての詳細が正確で、タイプミスがないことを確認するためにスキャンする必要があります。

ユーザーにとって効果的だったその他のソリューション

上記のトラブルシューティングのヒントでサイト上のデータベース接続エラーを修正できない場合は、以下の追加手順をお試しください。

ユーザーからの報告によると、これらの手順でサイト上のデータベース接続エラーを解決したユーザーもいます。

1.WordPressサイトのURL更新

cPanelのphpMyAdminを使ってWordPressサイトのURLを更新してみてください。

ホスティングアカウントのダッシュボードからphpMyAdminにアクセスし、リストからWordPressデータベースを選択するだけです。

その後、上部のSQLメニューに切り替え、次のMySQLクエリーを入力する:

UPDATE wp_options SET option_value='YOUR_SITE_URL' WHERE option_name='siteurl'

このように見えるはずだ:

Change site URL

WordPressのテーブル接頭辞を変更している可能性があるので、自分のサイトURLをプロバイダーで指定し、wp_optionsを自分のテーブル名に変更することをお忘れなく。

2.Webサーバーの再起動

専用サーバー、ローカルサーバー、仮想専用サーバー(VPS)のユーザーは、サーバーの再起動を試すことができる。

これによりWebサーバーとデータベースサーバーが再起動され、エラーの原因となる一時的な不具合が修正される可能性があります。

3.助けを求める

他のすべてがうまくいかない場合は、ホスティングサービス会社に連絡する必要があるかもしれません。すべての優れたWordPressホスティングサービス会社は、トラブルシューティングを手伝い、正しい方向を示し、あるいは問題を解決してくれます。

WPBeginnerプロサービスのようなプラットフォームを使ってWordPress開発者を雇い、リーズナブルな料金でこの問題を解決することもできます。WPBeginner Pro Services には、WordPress の問題を解決するために信頼できる専門の開発者チームがあります。

WPBeginner Pro Services

さらに、サイトのメンテナンス、ハッキングされたサイトの修復、スピードの最適化、サイトの再構築、SEOの最適化にもご利用いただけます。

この投稿がWordPressの「Error establishing a database connection」問題の解決に役立てば幸いです。WordPressの問題を自分で解決するためのヒントについては、WordPressトラブルシューティングガイド、またはWordPressのフリーランサーや開発者のための最高のツールの専門家のピックアップも参照してください。

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

情報開示 私たちのコンテンツは読者支援型です。これは、あなたが私たちのリンクの一部をクリックした場合、私たちはコミッションを得ることができることを意味します。 WPBeginnerの資金源 をご覧ください。3$編集プロセスをご覧ください。

アバター

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi with over 16 years of experience in WordPress, Web Hosting, eCommerce, SEO, and Marketing. Started in 2009, WPBeginner is now the largest free WordPress resource site in the industry and is often referred to as the Wikipedia for WordPress.

究極のWordPressツールキット

ツールキットへの無料アクセス - すべてのプロフェッショナルが持つべきWordPress関連製品とリソースのコレクション!

Reader Interactions

663件のコメント返信を残す

  1. Syed Balkhi says

    Hey WPBeginner readers,
    Did you know you can win exciting prizes by commenting on WPBeginner?
    Every month, our top blog commenters will win HUGE rewards, including premium WordPress plugin licenses and cash prizes.
    You can get more details about the contest from here.
    Start sharing your thoughts below to stand a chance to win!

  2. Arafat says

    Sometimes when I click on the “Publish” button to post a new article on my WordPress blog, then this error message appears:

    “”Error establishing a database connection… This either means that the username and password information in your wp-config.php file is incorrect or we can’t contact the database server at localhost. This could mean your host’s database server is down…..””

    Despite the error message appears, my post got published and there is no problem with visiting my site. This error does not come after each post publication. Sometimes it occurs. (My hosting reseller provider changed my server some days ago and this problem began after coming to the new server) What is the problem and how to solve it, please tell …… Thanks.

  3. Brian Deyo says

    The quick snippet of PHP you provided was everything I needed to track down my issue.Thanks for the post!

  4. Mario Ben says

    I am very excited to find this solution. I got the exact problem with my website and after searching it, I found this amazing article.
    Thank you so much!

  5. HighHopes says

    i was using Xampp on local machine to host my wordpress site and got error “Error Establishing the Database Connection”. first time i got that .. not sure why. i noticed in Xampp setup that the user and their privileges was deleted somehow. so i made a new user, using same user name as in ws-config, using host “localhost” and password same as in ws-config. worked like a charm :)

  6. Karl says

    I got error but after couple minutes everything got back normal, any way to avoid getting this error in future?

  7. Demannu says

    THANK YOU AUTHOR!!! Your little TestConnection script has saved me some serious non-paid time.

    MUAH! / HUG / HIGH FIVE

  8. RaDLab says

    Great article by the way.
    My issue is somehow strange to me. In the last couple of days maybe weeks, every morning few of my sites on my server will get the Error establishing connection prompt. But after a few minutes, it starts to work without me doing anything. For example, I will get an email from a client that the Error is showing, but when I quickly check it on my end, it works fine, and The client will also notice it back up. So my problem is strange, I’m not sure what to check if everything seem to resolve itself. Is possible because of high traffic?

    • Editorial Staff says

      Your issue is happening due to server overload. While this can mean high traffic, but it doesn’t always mean that. Sometimes, you have medium traffic but a bad plugin that is doing it. Other times, it is just high traffic.

      管理者

      • RaDLab says

        Thanks for the response.
        This has never happened before. Just today, I’ve had to restart MySQL several times to keep many of my sites running, while I try to figure why it keeps losing connection. I’m really baffled by this. I’ve looked into the php.ini looking for where to increase any sort of limits, but I come up with nothing. It’s running right now, but soon it will fail to connect until I restart MySQL.

        Any ideas on how to adjust to the high traffic and keep the MySQL server running?

        • Editorial Staff says

          One thing you can do is increase your PHP memory limit. If you are running at 32MB, then that could be a reason. But you will get to a point when your current servers just can’t handle it. You would have to upgrade. Not sure what your server specs are, and how much load you are putting on it.

          Run your server logs and see which processes are causing the load. Then see if those can be eliminated or optimized.

    • XeNNo says

      I have seen that too myself.
      This is happening to me, not only with wordpress but also with joomla and piwik and everything that i use to work with and needs a database.
      My provider seems to have a different machine for the MySQL service and every now and then the connection between my sites and the MySQL server drops and give me failure.

      You should check that this is your case too… and if so… get another host service :D

      • RaDLab says

        Thanks for your input. I think I have narrowed it down to a db instance that was causing my MySQL to lose connection. It’s an instance running a ELGG system. Soon as I discovered how much load this instance was putting on the server, I deleted it. Since then, the Error has seem to have stopped. Who knew…

  9. aly says

    for me it was the sql password, which had a symbol ( ] ) at the end.
    After giving up on all other “fixes,” I edited the password and presto! All good ;)

  10. yogesh says

    a very big thanks from me to you because first i website show 500 internel error i fix it by read wpbeginner article now my site show error establishing a database connection i fix it by read your article….thankssssss

    • Raul says

      Which version of wordpress did you upgrade to? I was previously on 3.5.1 but read that the 3.5 version is bugged, so I downgraded to 3.4.2. It’s more stable now but I still see the error…

  11. Matt says

    This has been a question asked by many of our past clients and is such a pain to explain this. Pretty much, 100% of the time I end up having to go in and fix it myself.

  12. Fanto Chandra says

    I just do repairing the database via cpanel-mysql database and choosing repair database and turn out working and can open the site again now. Thanks for the clue from your explanation.

  13. Alex Patterson says

    Just went through this issue – and the problem turned out to be that my mysql database was by default using the old 16 byte hashes for the passwords, rather than the 41 byte hashes that the more recent versions of php scripts that WordPress uses requires.

    To see if this is the issue, you can look at your user table in mysql:

    select user, host, password from mysql.user;

    If the password column for your user is 16 characters rather than 41, that is your issue.

    You will need to temporarily set the hashing to 41 digits and then re-encode your password:

    set @@session.old_passwords = 0;
    set password for ‘wordpress_user’@’localhost’=PASSWORD(‘password’);

  14. TP says

    Great tip I was able to login through backend and reload my site with no more error messages. Took five minutes! Thanks.

  15. Markus says

    After editing the wp-config.php and updating with new credentials, the error was still present. I had also to clear my APC opcode cache!

  16. Evik says

    Hi,

    thank you for this article. I got this error twice and each time I tried to login into my cpanel it helped. Nothing else, but log into the cpanel resolved my problem.

    It does not make sense, and I have no idea what is going on.

    I got the message every time after I have made some changes on the website (the last time I was putting on a new email subscription).

  17. Rae says

    Aside from updating wp-config.php, I had to update the domain for the following tables:
    wp_blogs
    wp_site
    wp_options -> option_value (for option_name siteurl, home)
    If you use WPMU, you will need to update ALL wp_options tables.

    The error would NOT go away until all of this was done.

  18. sara says

    This happened to our site the other day, we got the dreaded error! We received a large amount of traffic but it was expected. We were using Quick Cache as the plugin and the hosts changed it for Super cache and also told us we had hit MySQL max_user_connections due to the queries on the wp_options table. They told us after installing super cache it’s taken us from 600+ database connections to under 5.

    After installing the super cache plugin everything worked great again. Now I am concerned about this happening in future, what can I do to try and prevent this? We will always have a large surge in tarffic and then very quiet moments. How can I be sure the database will be able to cope with such demands?

    Thanks

  19. smithers says

    i had “error connecting to database” problem twice in the past two days on a wordpress site on an amazon ec2 micro instance. it occurred after sending out a blast email regarding a fund raising campaign and asking people to check out the site, so i assume it was a load-related failure. anyway, the first time it occurred, i tried to restart the database (service mysqld restart), and it shutdown OK but wouldn’t restart. i then restarted apache (service httpd restart) and then restarted the database, and the database and site came back up.

    the problem occurred again today, and the database restart worked without having to restart apache.

    • Jon says

      The scenario you describe is almost certainly caused by your EC2 instance being out of available memory. When you restart Apache a huge chunk of memory is released; only the minimum resources required to get the server up and running are re-allocated when Apache comes back up, then mysqld has plenty of elbow room to start.

      A few good places to start fixing this:

      1. Check your php.ini memory limit
      2. Check your innodb_buffer_pool_size setting in my.cnf
      3. Check your swap space

    • Jamie says

      I’m running an Amazon EC2 instance also & restarting apache & then restarting mysqld worked for me too.

  20. Daniel says

    I was moving some webspace from an older to a newer server and tested some things, got stuck with this error. This helped, thanks a lot:
    update wp_options set option_value=’http://www.newsitename.de’ where option_name=’siteurl’;

  21. Shawn says

    Thanks a lot! I was waiting for support from my host for hours…Sundays…but they took too long. My problem was my database password in wp-config, which was different from the real password. I changed it and everything is fine again.

    *bookmarked

  22. tristan says

    Thank you so much! I had messed around in my functions php and lost the ability to open my website. in the process of trying to fix this issue I changed my password for database access and had no idea it was the cause for my error establishing database connection. I’m sure this seems fairly simple to someone who is familiar with this web stuff but for someone like me who is just a dabbler it was very frustrating. website works great now! thank you.

  23. XeNNo says

    Actually I’ve had a different problem and took me some time to resolve it. My wp after moving from one server to another, editing all files needed (wp-config) started loading on home page nicely. Also the wp-admin section worked like a charm … but still the problem apeared when I started to point on pages and articles … all of theme besides homepage get me the mysql error regarding connection to database….
    So…. looking on the internet I’ve found this article of yours and tried your advices … my problem not getting resolved on any of them.
    Somehow … don’t know why… my .htaccess file pointed on another default root document and after editing my .htaccess …. :D everything started to work …
    Thought somebody will need that …

  24. Anne says

    MANY many thanks for posting this. Thought I was going to have to start all over. Being one step above novice is not always sufficient! GAH!

  25. Sean Murphy says

    Thanks for this post. The adding that line of php code in the first para did the trick for my sites.

  26. Lucy Gage says

    Hi there,

    I am getting this error every day “Error establishing a network connection”. The site was working perfectly, then after the first day I got up in the morning and check the site and saw the error for the first time. I logged into the crazy domains control panel and the database was missing. I called support and it took them all day to fix the problem, but then the site was ok again.

    The next morning the same problem again. I called support again. After a couple of hours the site was back working again.

    The next morning – the same problem again. another hour speaking to support, but they don’t actually tell me what the problem is.

    I am so worried about this and jsut want the problem fixed. Do you think it could be anything I have done whilst setting up the website, or just a problem with the host?

  27. Damian says

    What happened with me is the php.config’s password didn’t match..downloading config.php and putting in the proper password fixed it!

  28. Guest says

    Hi, I run WordPress multisite and just went through an update. I have a blog that has a static front page and a separate page for blog entries. When I tried to open the blog page, I got the above message, Error Establishing a Database Connection.

    Turned out I needed to click the yellow bar that appears when you do a major WordPress update, to Update Network which synchs everything database-wise on all sites in your Multisite install. Blog page is viewable now and everything’s working fine!

  29. Rob Rawson says

    My solution was to install DBVisualizer and what it showed me was that while the dbname was right it was in the wrong case. So changing the db name from wp to WP in my wp-config.php resolved this issue. No idea how this worked in the first place since this was up and running.

  30. Anna Crowe says

    This was a well written and thorough article. Thank you. Not everything applied to me, but I was able to troubleshoot and identify the problem using some of these suggestions. I’m praising your name today! Thanks.

  31. Jacob Degeling says

    Hi,

    I moved my wp-config.php from the dir below the www root into the www root. That brought the site back up. I would prefer the wp-config.php to be outside the www root, so I’ll have to look into why it wasn’t working in the first place.

    Thanks for the help.

    Jacob

  32. chris says

    sql.safe_mode=On in php.ini hosed my multisite install this way. Change it back and the site came up again.

  33. blkbam says

    One thing to check on if you are moving from your system to the server and using MySQL is the case of the database name. In my situation the server is in all lowercase while my development is all uppercase. Iit’s a very easy over site.

  34. Laura says

    I had this error on my site – i called my host who told me that my username was being denied permission to the database.

    No idea how this happened as I hadn’t gone near these details. However, they assured me this was a common problem.

    Within 1 minute of the phone call ending, my site was back up and running.

    Very strange….but glad its working again!

    Great article btw! Glad i didnt need to use it in the end but very easy to understand if i had!

  35. RB says

    My wordpress site was working fine. Then I got the “Error Establishing the Database Connection” message. Doing a Google search, there was a lot of information on what to do. This was the most helpful to me. It ended up that my database options table had crashed. I repaired it per this post and my wordpress site it works fine again. Thanks!

  36. Asif says

    try checking for the php version as well, sometimes it is the php version on the server that could cause the issue.

返信を残す

コメントありがとうございます。すべてのコメントは私たちのコメントポリシーに従ってモデレートされ、あなたのメールアドレスが公開されることはありませんのでご留意ください。名前欄にキーワードを使用しないでください。個人的で有意義な会話をしましょう。