php 在 wordpress 上建立数据库连接时出错
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4302738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error establishing a database connection on wordpress
提问by ulduz114
i have a blog with wordpress but sometimesi have problem with that
我有一个带有 wordpress 的博客,但有时我对此有疑问
i got blow error
我遇到了打击错误
Error establishing a database connectionThis 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
. This could mean your host's database server is down.
建立数据库连接时出错这意味着您的wp-config.php
文件中的用户名和密码信息不正确,或者我们无法联系 上的数据库服务器%s
。这可能意味着您主机的数据库服务器已关闭。
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 sometimeswhat 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?
我的服务器有问题还是我的配置有问题?
采纳答案by markratledge
why i got this error sometimeswhat 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.
这是关键;如果只是偶尔发生,则是网络主机的间歇性问题。网络主机需要查看 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. Log locations vary, depending on the OS.
如果这是您自己的服务器,您需要了解操作系统和其他详细信息,以便您可以搜索 Stack Overflow 以获取信息。日志位置因操作系统而异。
回答by Latox
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?
此外,您是否设置了数据库并在配置中填写了用户名、密码和数据库?
回答by Atif Mohammed Ameenuddin
If you get it 'sometimes'then it means the PHP Scrpt wasn't able to connect to the database. One of the various reasons could be database server down for a while.
如果您“有时”收到它,则表示 PHP Scrpt 无法连接到数据库。各种原因之一可能是数据库服务器关闭了一段时间。
回答by Questino
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
这会工作
回答by Waqas Ahmad
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). 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. 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-admin) 上都遇到相同的错误。如果两个页面上的错误消息“建立数据库连接时出错”相同,则继续下一步。如果您在 wp-admin 上遇到不同的错误,例如“一个或多个数据库表不可用。数据库可能需要修复”,那么您需要修复您的数据库。您可以通过在 wp-config.php 文件中添加以下行来完成此操作:
define('WP_ALLOW_REPAIR', true);
定义('WP_ALLOW_REPAIR',真);
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
回答by Nitesh Chaturvedi
get the correct DB host name from the database
从数据库中获取正确的数据库主机名
use following quesry for mysql select @@hostname; change the value of the DB_HOST to output of above query
使用以下查询 mysql select @@hostname; 将 DB_HOST 的值更改为上述查询的输出
回答by Jose Kj
first type mysql -u root -pand enter the your mysql password then type GRANT ALL PRIVILEGES ON mydbname.* TO dbusername@localhost; then FLUSH PRIVILEGES;and finally exit
首先键入mysql -u root -p并输入您的 mysql 密码,然后键入GRANT ALL PRIVILEGES ON mydbname.* TO dbusername@localhost;然后刷新特权;最后退出
回答by Anas Siddiqui
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); and repair it and don't forget to remove this line after repair.
有时会发生这种情况,只是因为凭据不正确,因此请检查您的凭据或尝试重置您的用户名和密码,有时主机问题或您可以修复数据库,只需转到您的 wp-config 文件并写入定义('WP_ALLOW_REPAIR',真的); 并修复它,修复后不要忘记移除此线。
回答by Samad
There are many reason for the issue if you get the error only sometimes
如果您只是偶尔收到错误,则问题的原因有很多
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.
If you got the error on both backend and frontend, check your database credentials are correct.
If the error only on the front end. try to repair database by adding the following line of code in wp-config.php
如果您的数据库很大,则无法处理查询。(您可能会在错误日志中看到“数据库连接过多”或“用户已经有超过 'max_user_connections' 个活动连接”错误。因此,请尝试减小数据库表的大小。
如果后端和前端都出现错误,请检查您的数据库凭据是否正确。
如果错误只出现在前端。尝试通过在 wp-config.php 中添加以下代码行来修复数据库
define('WP_ALLOW_REPAIR', true);
定义('WP_ALLOW_REPAIR',真);
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”并单击“修复和优化数据库”按钮。