Error establishing a database connection on wordpress

0 votes
asked Nov 29, 2010 by ulduz114

i have a blog with wordpress but sometimes i have problem with that

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. 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

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"

does my server have problem or my config have problem?

8 Answers

0 votes
answered Nov 29, 2010 by latox

Try changing your DB_HOST to localhost

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

Furthermore, have you set up the database and filled in the username, password & database in your config?

0 votes
answered Nov 29, 2010 by markratledge

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.

If you had "Error establishing a database connection" all the time, that points to a definite error you made in wp-config.php.

Tell the webhost and administrator to look at their logs to see when the MySQL server is going down and why.

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.

0 votes
answered Nov 30, 2010 by atif-mohammed-ameenu

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.

0 votes
answered Nov 24, 2012 by questino

If your are not using default port 3306 for MySql, do give the host name with port number.

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

0 votes
answered Nov 11, 2013 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:

define('WP_ALLOW_REPAIR', true);

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.

Regard LatestTutorial.com

0 votes
answered Nov 4, 2016 by jose-kj

first type mysql -u root -p and enter the your mysql password then type GRANT ALL PRIVILEGES ON mydbname.* TO dbusername@localhost; then FLUSH PRIVILEGES; and finally exit

0 votes
answered Nov 13, 2016 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

0 votes
answered Sep 15, 2017 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.

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...