Are you seeing the ‘Error establishing a database connection’ notice on your WordPress website? It is a fatal error that makes your WordPress website inaccessible to the users.

您是否在WordPress网站上看到“建立数据库连接错误”通知? 这是一个致命错误,使用户无法访问WordPress网站。

This error occurs when WordPress is unable to make a connection to the database. A number of things can affect your WordPress database connection which makes it a bit difficult for beginners to troubleshoot.

当WordPress无法建立与数据库的连接时,会发生此错误。 许多因素都会影响您的WordPress数据库连接,这使初学者很难进行故障排除。

In this article, we will show you how to easily fix the error establishing a database connection in WordPress.

在本文中,我们将向您展示如何轻松修复在WordPress中建立数据库连接的错误。

是什么导致在WordPress中建立数据库连接时出错? (What Causes Error Establishing a Database Connection in WordPress?)

The ‘Error establishing a database connection’ issue can be caused by incorrect database information in your WordPress settings, corrupt database, or an irresponsive database server.

“建立数据库连接错误”问题可能是由于WordPress设置中的数据库信息不正确,数据库损坏或数据库服务器无响应。

A database is a software which makes it easy to store, organize, and retrieve data into other software.

数据库是一种软件,可以轻松地将数据存储,组织和检索到其他软件中。

As a content management system, WordPress uses a database to store all your content and other website data. It then connects to the database each time someone visits your website.

作为内容管理系统,WordPress使用数据库来存储您的所有内容和其他网站数据。 每次有人访问您的网站时,它就会连接到数据库。

WordPress needs the following information for connecting to the database:

WordPress需要以下信息来连接数据库:

  • Database name

    数据库名称
  • Database username

    数据库用户名
  • Database password

    数据库密码
  • Database server

    数据库服务器

This information is stored in your WordPress configuration file called wp-config.php.

此信息存储在名为wp-config.php的 WordPress配置文件中。

If any of these items are incorrect, WordPress would fail to connect to your database server, and you’ll see the ‘Error establishing a database connection’ error.

如果以上任何一项都不正确,WordPress将无法连接到您的数据库服务器,并且您会看到“建立数据库连接错误”错误。

It is one of the most common WordPress errors. Apart from incorrect credentials, this error can also appear if the database server is down, or the database files are corrupt.

这是最常见的WordPress错误之一 。 除了不正确的凭据外,如果数据库服务器已关闭或数据库文件已损坏,也会出现此错误。

Let’s take a look at how to fix error establishing database connection issue in WordPress with step by step troubleshooting.

让我们看一下如何通过逐步故障排除来解决WordPress中建立数据库连接问题的错误。

1.检查您的WordPress数据库凭证 (1. Check Your WordPress Database Credentials)

The most common reason for database connection error in WordPress is incorrect database credentials. If you have recently moved your WordPress site to a new host, then this could be the most likely reason.

WordPress中数据库连接错误的最常见原因是数据库凭据不正确。 如果您最近将WordPress网站移到了新主机上 ,那么这可能是最可能的原因。

Your WordPress database credentials are stored in the wp-config.php file. It is the WordPress configuration file that contains important WordPress settings including database information.

您的WordPress数据库凭据存储在wp-config.php文件中。 这是WordPress配置文件,其中包含重要的WordPress设置,包括数据库信息。

If you have not edited wp-config.php file before, then take a look at our guide on how to edit wp-config.php file in WordPress.

如果您之前从未编辑过wp-config.php文件,请查看我们的指南, 了解如何在WordPress中编辑wp-config.php文件 。

You’ll be looking for the following lines in the wp-config.php file.

您将在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' );

You need to make sure that the information for the database name, username, password, and database host is correct.

您需要确保数据库名称,用户名,密码和数据库主机的信息正确。

You can confirm this information from your WordPress hosting account dashboard. Simply log in to your hosting account and click on MySQL databases under the database section.

您可以从WordPress托管帐户信息中心确认此信息。 只需登录到您的托管帐户,然后单击“数据库”部分下MySQL数据库。

This will take you to the database management page in your hosting dashboad. From here, you can find out your database name and the username.

这将带您进入托管仪表板中的数据库管理页面。 在这里,您可以找到您的数据库名称和用户名。

Below that you will find the database users and links to change the user password.

在其下,您将找到数据库用户以及用于更改用户密码的链接。

Once you have confirmed your database name, username, and password, you can change that information in your wp-config.php file if needed.

确认数据库名称,用户名和密码后,可以根据需要在wp-config.php文件中更改该信息。

After that, try visiting your website again to see if the database connection error has gone.

之后,请尝试再次访问您的网站以查看数据库连接错误是否已消失。

If you can still see the error, then this means that something else is wrong. Continue reading for more troubleshooting steps.

如果您仍然可以看到错误,则表示还有其他问题。 继续阅读以获取更多故障排除步骤。

2.检查您的数据库主机信息 (2. Check Your Database Host Information)

If you are confident that your database name, username, and password information is correct, then you may want to make sure that you are using the correct database host information.

如果您确信数据库名称,用户名和密码信息正确无误,则可能需要确保使用的数据库主机信息正确。

Most WordPress hosting companies use localhost as your database host. However, some managed WordPress hosting companies use separate servers to host databases. In that case, your database host information will not be localhost.

大多数WordPress托管公司都使用localhost作为您的数据库主机。 但是,某些托管WordPress托管公司使用单独的服务器托管数据库。 在这种情况下,您的数据库主机信息将不是localhost。

You need to contact your WordPress hosting company to confirm your database host information.

您需要联系WordPress托管公司以确认您的数据库托管信息。

3.修复WordPress数据库 (3. Repair WordPress Database)

If you are getting a different error on the wp-admin, for instance, something like “One or more database tables are unavailable. The database may need to be repaired”, then you need to repair your database.

例如,如果在wp-admin上遇到其他错误,则类似“一个或多个数据库表不可用。 数据库可能需要修复”,那么您需要修复数据库。

You can do this by adding the following line in your wp-config.php file. Make sure to add it just before ‘That’s all, stop editing! Happy blogging’ line wp-config.php.

您可以通过在wp-config.php文件中添加以下行来实现。 确保在“就这样,停止编辑!”之前添加它。 快乐的博客行wp-config.php。

define('WP_ALLOW_REPAIR', true);

Once you have done that, you can see the settings by visiting this page: http://www.yoursite/wp-admin/maint/repair.php

完成此操作后,您可以通过访问以下页面来查看设置:http://www.yoursite/wp-admin/maint/repair.php

Note: the user does not need to be logged in to access the database repair page. Once you are done repairing and optimizing your database, make sure to remove this code from your wp-config.php.

注意:用户无需登录即可访问数据库修复页面。 修复和优化数据库后,请确保从wp-config.php中删除此代码。

4.检查您的数据库服务器是否关闭 (4. Check if Your Database Server is Down)

If everything seems to be correct, and WordPress still cannot connect to the database, then your database server (MySQL server) may be down.

如果一切似乎正确,并且WordPress仍然无法连接到数据库,则您的数据库服务器(MySQL服务器)可能已关闭。

This could happen due to heavy traffic on a server. Basically, your host server just cannot handle the load (especially when you are on shared hosting).

由于服务器上的流量过大,可能会发生这种情况。 基本上,您的主机服务器无法处理负载(尤其是在共享主机上时 )。

Your site will get really slow and for some users it may even output the error. So the best thing you should do is get on the phone or live chat with your hosting provider and ask them if your MySQL server is responsive.

您的网站将变得非常缓慢,对于某些用户而言,它甚至可能会输出错误。 因此,您应该做的最好的事情是打电话或与托管服务提供商进行实时聊天,并询问他们您MySQL服务器是否响应。

If you have other websites running on the same server, then you can check those sites to confirm that your SQL server is down.

如果您在同一服务器上运行其他网站,则可以检查这些网站以确认SQL Server已关闭。

If you do not have any other site on the same hosting account, then simply go to your hosting dashboard and try to access phpMyAdmin and connect the database.

如果同一托管帐户上没有其他任何站点,则只需转到托管仪表板并尝试访问phpMyAdmin并连接数据库。

If you can connect, then we need to verify if your database user has sufficient permission. Create a new file called testconnection.php and paste the following code in it:

如果可以连接,那么我们需要验证您的数据库用户是否具有足够的权限。 创建一个名为testconnection.php的新文件,并将以下代码粘贴到其中:


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

Make sure to replace the username and password. You can now upload this file to your website and access it via web browser.

确保替换用户名和密码。 您现在可以将此文件上传到您的网站,并通过网络浏览器访问它。

If the script connected successfully, then it means that your user has sufficient permission, and there is something else that is wrong.

如果脚本成功连接,则表明您的用户具有足够的权限,并且存在其他错误。

Go back to your wp-config file to make sure that everything there is correct (re-scan for typos).

返回到wp-config文件,以确保所有内容正确(重新扫描输入错误)。

对用户有用的其他解决方案 (Other Solutions That Have Worked for Users)

If the above-mentioned troubleshooting tips failed to fix the database connection error on your website, then you may try these additional steps.

如果上述故障排除技巧未能解决您网站上的数据库连接错误,则可以尝试执行这些附加步骤。

Reported by our users, these steps have helped some users resolve the database connection error on their websites.

由我们的用户报告,这些步骤已帮助某些用户解决了他们网站上的数据库连接错误。

1. Update WordPress Site URL

1.更新WordPress网站网址

Try updating the WordPress site URL using phpMyAdmin. Simply access phpMyAdmin from your hosting account dashboard, and select your WordPress database.

尝试使用phpMyAdmin更新WordPress网站URL。 只需从托管帐户仪表板访问phpMyAdmin,然后选择WordPress数据库即可。

After that click on the SQL menu on the top and enter the following MySQL query.

之后,单击顶部SQL菜单,然后输入以下MySQL查询。


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

Don’t forget to provide your own site URL and change wp_options to your own table name as you may have changed the WordPress table prefix.

不要忘记提供您自己的站点URL并将wp_options更改为您自己的表名,因为您可能已经更改了WordPress表前缀 。

2. Rebooting Web Server

2.重新启动Web服务器

Users on dedicated servers, local server, and virtual private servers (VPS) can try rebooting their servers.

专用服务器,本地服务器和虚拟专用服务器(VPS)上的用户可以尝试重新引导其服务器。

This will restart your web and database server which may fix some temporary glitches causing the error.

这将重新启动您的Web和数据库服务器,这可能会修复一些临时错误,从而导致该错误。

3. Ask for help

3.寻求帮助

If everything else fails, then you may need to contact your web hosting company. All good WordPress hosting companies will help you troubleshoot the problem, point you in the right direction, or even fix it for you.

如果其他所有操作均失败,则您可能需要联系您的网络托管公司。 所有好的WordPress托管公司都将帮助您解决问题,为您指明正确的方向,甚至为您解决问题。

You can also hire WordPress developers from Codeable that can help you fix this issue for reasonable rates. The best part is that these developers are highly-vetted by Codeable team, so you know they can be trusted.

您也可以从Codeable 雇用WordPress开发人员 ,以合理的价格帮助您解决此问题。 最好的部分是,这些开发人员受到Codeable团队的高度审查,因此您可以信任他们。

We hope this article helped you fix the error establishing a database connection in WordPress. You may also want to see our WordPress troubleshooting guide for tips on resolving WordPress issues on your own.

我们希望本文能帮助您解决在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.

如果您喜欢这篇文章,请订阅我们的YouTube频道 WordPress视频教程。 您也可以在Twitter和Facebook上找到我们。

翻译自: https://www.wpbeginner/wp-tutorials/how-to-fix-the-error-establishing-a-database-connection-in-wordpress/

更多推荐

如何修复在WordPress中建立数据库连接的错误