If you’re anything like me, your website has multiple MySQL databases and, after a while. it can start to become difficult to manage. At times, when things go wrong, you can rely on your wp-config.php file to recover your database name and password… But what if you make the horrible mistake of overwriting your existing wp-config.php file with the wrong one. or simply deleting it by accident?
“Error Establishing a Database Connection”
This happened to me recently, and I spent a number of hours searching forums and knowledge bases trying to figure out how to fix it. Most discussions involved trying to insert a new password directly into the MySQL database using special hashing techniques (encrypting a new, plain text password), and then updating your wp-config.php file accordingly. But the solutions I found is so much easier!
It’s a simple case of adding a new user to your database
The only thing you need for this solution to work is the name of your database, and you can easily get this through your cPanel. This fix should get your site back up and running in less than five minutes:
- Log in to your cPanel, and go to MySQL Databases.
- Scroll to the section that says ‘MySQL Users’ > ‘Add New User’, then create a new user, remembering to copy your username and password into a safe location.
- Scroll to ‘Add User To Database’, then add the user you just created to the relevant database. If you’re lucky, you’ll only have one database to choose from. If there’s more than one, look for a WordPress database. The syntax is usually username_wp123 (where username is the first eight characters of your host login, and the numbers were randomly generated at the time of installation).
- Next, grant ALL PRIVILEGES to the new user.
- Now it’s just a simple matter of inserting this information into a new wp-config.php file. If you’ve deleted all instances of this file, you can use wp-config-sample.php to create a new one (this file is included in your WP root directory by default).
/** The name of the database for WordPress */ define('DB_NAME', 'database_name_here'); /** MySQL database username */ define('DB_USER', 'username_here'); /** MySQL database password */ define('DB_PASSWORD', 'password_here');
- Edit, save, then upload the file via FTP into your root WordPress directory.
- Your website should now function normally! (If not, leave a comment.)