简体   繁体   English

在Wordpress上建立数据库连接时出错

[英]Error establishing a database connection on wordpress

i have a blog with wordpress but sometimes i have problem with that 我有一个WordPress博客,但有时我对此有疑问

i got blow error 我有打击错误

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 %s . 建立数据库连接时出错这意味着您的wp-config.php文件中的用户名和密码信息不正确,或者我们无法通过%s与数据库服务器联系。 This could mean your host's database server is down. 这可能意味着您主机的数据库服务器已关闭。

Are you sure you have the correct username and password? 您确定您具有正确的用户名和密码吗? Are you sure that you have typed the correct hostname? 您确定输入了正确的主机名吗? Are you sure that the database server is running? 您确定数据库服务器正在运行吗? If you're unsure what these terms mean you should probably contact your host. 如果不确定这些术语的含义,则应联系您的房东。 If you still need help you can always visit the WordPress Support Forums 如果您仍然需要帮助,可以随时访问WordPress支持论坛

and this is my config file 这是我的配置文件

define('WP_HOME','http://www.mysite.com');
define('WP_SITEURL','http://www.mysite.come');
define('DB_NAME', 'mydbname');

/** MySQL database username */
define('DB_USER', 'dbusername');

/** MySQL database password */
define('DB_PASSWORD', 'dbpassword');

/** MySQL hostname */
define('DB_HOST', 'mysite.com');

why i got this error sometimes what do you think about this problem? 为什么有时我会收到此错误,您如何看待这个问题?

i contact with my admins server and they said "we dont have any problem with server and mysql service" 我与我的管理员服务器联系,他们说“我们的服务器和mysql服务没有任何问题”

does my server have problem or my config have problem? 我的服务器有问题还是我的配置有问题?

Try changing your DB_HOST to localhost 尝试将您的DB_HOST更改为localhost

** MySQL hostname */
define('DB_HOST', 'localhost');

Furthermore, have you set up the database and filled in the username, password & database in your config? 此外,您是否已设置数据库并在配置中填写了用户名,密码和数据库?

why i got this error sometimes what do you think about this problem? 为什么有时我会收到此错误,您如何看待这个问题?

That's the key; 那是关键; if it only happens sometimes , it's an intermittent issue at the webhost. 如果仅在某些情况下发生,则这是网络主机上的间歇性问题。 The webhost needs to look in the MySQL logs and PHP logs for connection errors, timeouts, MySQL crashes, etc, anything that will point to the cause of the intermittent issues. Web主机需要在MySQL日志和PHP日志中查找连接错误,超时,MySQL崩溃等,这些都是指向间歇性问题的原因。

If you had "Error establishing a database connection" all the time, that points to a definite error you made in wp-config.php . 如果您始终遇到“建立数据库连接时出错”,则表明您在wp-config.php犯了一个确定的错误。

Tell the webhost and administrator to look at their logs to see when the MySQL server is going down and why. 告诉网络主机和管理员查看其日志,以查看MySQL服务器何时关闭以及原因。

If this is your own server, you need to know the OS and other details so you can search Stack Overflow for the information. 如果这是您自己的服务器,则需要了解操作系统和其他详细信息,以便可以在Stack Overflow中搜索相关信息。 Log locations vary, depending on the OS. 日志位置因操作系统而异。

If you get it 'sometimes' then it means the PHP Scrpt wasn't able to connect to the database. 如果“有时”得到它,则意味着PHP Scrpt无法连接到数据库。 One of the various reasons could be database server down for a while. 各种原因之一可能是数据库服务器关闭了一段时间。

If your are not using default port 3306 for MySql, do give the host name with port number. 如果您没有使用MySql的默认端口3306,请提供带有端口号的主机名。

define('WP_HOME','http://www.mysite.com');
define('WP_SITEURL','http://www.mysite.come');
define('DB_NAME', 'mydbname');

/** MySQL database username */
define('DB_USER', 'dbusername');

/** MySQL database password */
define('DB_PASSWORD', 'dbpassword');

/** MySQL hostname */
define('DB_HOST', 'localhost:3307'); //If you are not using default port number, do give post number along with localhost.

This would work 这会工作

First thing you should do is to make sure that you are getting the same error on both the front-end of the site, and the back-end of the site (wp-admin). 您应该做的第一件事是确保在站点的前端和站点的后端(wp-admin)都收到相同的错误。 If the error message is the same on both pages “Error establishing a database connection”, then proceed onto the next step. 如果错误消息在“建立数据库连接时出错”两个页面上均相同,则继续进行下一步。 If you are getting a different error on the wp-admin for instance something like “One or more database tables are unavailable. 如果在wp-admin上遇到其他错误,例如“一个或多个数据库表不可用。 The database may need to be repaired”, then you need to repair your database. 可能需要修复数据库”,那么您需要修复数据库。 You can do this by adding the following line in your wp-config.php file: 您可以通过在wp-config.php文件中添加以下行来做到这一点:

define('WP_ALLOW_REPAIR', true); define('WP_ALLOW_REPAIR',true);

http://cdn4.wpbeginner.com/wp2-9/dbrepair.gif http://cdn4.wpbeginner.com/wp2-9/dbrepair.gif

Remember, the user does not need to be logged in to access this functionality when this define is set. 请记住,设置了此定义后,用户无需登录即可访问此功能。 This is because its main intent is to repair a corrupted database, Users can often not login when the database is corrupt. 这是因为其主要目的是修复损坏的数据库。数据库损坏时,用户通常无法登录。 So once you are done repairing and optimizing your database, make sure to remove this from your wp-config.php. 因此,一旦完成修复和优化数据库的操作,请确保将其从wp-config.php中删除。

Regard LatestTutorial.com 关于LatestTutorial.com

get the correct DB host name from the database 从数据库中获取正确的数据库主机名

use following quesry for mysql select @@hostname; 对mysql使用以下查询选择@@ hostname; change the value of the DB_HOST to output of above query 将DB_HOST的值更改为上述查询的输出

first type mysql -u root -p and enter the your mysql password then type GRANT ALL PRIVILEGES ON mydbname.* TO dbusername@localhost ; 首先键入mysql -u root -p并输入您的mysql密码,然后在mydbname上键入GRANT ALL PRIVILEGESES。* TO dbusername @ localhost ; then FLUSH PRIVILEGES; 然后是冲洗特权; and finally exit 最后退出

Some time this happen , just because of incorrect credentials so check your credentials or try to reset your user name and passward , some time host issue or you can repair the database just go to your wp-config file and write define ( 'WP_ALLOW_REPAIR' , true); 有时会发生这种情况,只是因为凭据不正确,所以请检查您的凭据或尝试重置用户名和密码,有时会出现主机问题,或者您可以修复数据库,只需转到wp-config文件并编写define('WP_ALLOW_REPAIR',真正); and repair it and don't forget to remove this line after repair. 并对其进行修复,并且别忘了在修复后删除此行。

There are many reason for the issue if you get the error only sometimes 如果您有时仅收到错误,则有很多原因可以解决此问题

  1. If your database is very big, it can not handle the queries. 如果您的数据库很大,则无法处理查询。 ( You probably see 'too many database connections' or 'user already has more than 'max_user_connections' active connections' errors on error logs. So try to reduce database tables size. (您可能会在错误日志上看到“数据库连接过多”或“用户已经具有超过“ max_user_connections个活动连接”的错误。)因此,请尝试减小数据库表的大小。

  2. If you got the error on both backend and frontend, check your database credentials are correct. 如果后端和前端均出现错误,请检查数据库凭据是否正确。

  3. If the error only on the front end. 如果错误仅在前端。 try to repair database by adding the following line of code in wp-config.php 尝试通过在wp-config.php中添加以下代码行来修复数据库

define('WP_ALLOW_REPAIR', true); define('WP_ALLOW_REPAIR',true);

Then go to ' http://www.yoursite.com/wp-admin/maint/repair.php ' and click 'repair and optimize database' button. 然后转到“ http://www.yoursite.com/wp-admin/maint/repair.php ”,然后单击“修复和优化数据库”按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM