Fri 8 Sep 2006
WordPress broken – “Error establishing a database connection”
Posted by Richard under Configuration , SoftwareComments Off
Well, it has been some months since I last used WordPress and I could not access the blog for some reason. All that appeared was a nicely presented page with the message
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 DB_HOST. 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?
Luckily, my host lets me do practically anything, so it was off to Google to find a bit out about mySQL.
The first thing to do is open a session to your server. I use PuTTY which is a freeware Telnet client that lets me open a secure session to my server. Navigate to the directory where WordPress is installed. On my installation, this is /var/www/html/worpress. There is a file called wp-config.php in this directory. Check the first few lines of this file using the head command as shown below
$ head -7 /var/www/html/wordpress/wp-config.php
<?php
// ** MySQL settings ** //
define('DB_NAME', 'wordpressdb'); // The name of the database
define('DB_USER', 'username'); // Your MySQL username
define('DB_PASSWORD', 'password'); // ...and password
define('DB_HOST', 'localhost'); // 99% chance you won't need to change this value
I then used this data to try connecting to the database manually and was promptly thrown out!
$mysql -h 'localhost' -u 'username' -p 'password'
ERROR 1045 (28000): Access denied for user 'username'@'localhost' (using password: YES)
Somehow, the password for my database had been changed. Had I got it right, I would have received the reply
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 126 to server version: 4.1.9-standard-logType 'help;' or '\h' for help. Type '\c' to clear the buffer.
If you get the error ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2), then your SQL server is not running. See my other post on restarting MySQL (when I sort it out!)
I therefore decided to create a new privilege (user) and granted them access to the wordpress database given by DB_NAME.
$mysql
mysql> GRANT USAGE ON * . * TO 'newuser'@'localhost' IDENTIFIED BY 'newpassword' WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 ;
Query OK, 0 rows affected (0.01 sec)mysql> GRANT ALL PRIVILEGES ON `wordpressdb` . * TO 'newuser'@'localhost' WITH GRANT OPTION ;
Query OK, 0 rows affected (0.00 sec)mysql> quit
Bye
$
All you have to do then is edit the wp-config.php file we started with (/var/www/html/wordpress/wp-config.php) to use the new username and password. I used the pico editor on my Linux server to do this. It's very simple, but if you need some help, then try here
For an alternate solution, you can try the answer provided by astrashe at WordPress.org
I used this to get a more specific error message -- replace the connection parameters with what you're using on your server, put this in a file called something.php, and pull it up in your browser.
HTML:Don't forget to replace mysql_host, mysql_user and mysql_password with the data you obtained from wp-config.php

