When I work on WordPress sites, if I don’t do an undocumented trick, I can’t get WordPress to connect to the database. I get the ‘Error Establishing a Database Connection’ error.
If you’ve double-checked your wp-config.php settings to make sure the host, username, password and database match exactly, and you’re able to connect with the username, password and host on the command line, this trick might help you too.
It’s simple, append
If you don’t know what your socket is, use the mysql command line tool to find it. Here’s how:
From your shell type this (obviously substituting in your username, host and database)
MySQL will start up and give you a prompt. At the mysql prompt type
If you’ve double-checked your wp-config.php settings to make sure the host, username, password and database match exactly, and you’re able to connect with the username, password and host on the command line, this trick might help you too.
It’s simple, append
:/path/to/socket
to your hostname like this:define('DB_NAME', 'wordpress');
define('DB_USER', 'wordpress');
define('DB_PASSWORD', 'wordpress');
define('DB_HOST', 'localhost:/tmp/mysql.sock'); // my computer has the mysql socket on /tmp/mysql.sock
If you don’t know what your socket is, use the mysql command line tool to find it. Here’s how:
From your shell type this (obviously substituting in your username, host and database)
> mysql -u wordpress -p -h localhost wordpress
MySQL will start up and give you a prompt. At the mysql prompt type
s
(backslash + s). That will give you a screen full of status information like this: