WordPress can’t connect to the database.
Error Establishing a Database Connection
Key Points: WordPress can’t reach your MySQL database — either credentials are wrong, the server is down, or your DB is in trouble.
So you’ve just installed WordPress (or maybe changed hosts), and instead of a shiny new dashboard, you’re greeted by a sad little message: “Error establishing a database connection.” No drama. Just cold rejection.
👁️🗨️ Behind the Curtain
WordPress needs four pieces of info to talk to your database: name, username, password, and host. If even one of those is wrong — or if the server’s offline — it throws in the towel immediately.
🛠️ Mission: Reconnect
- Inspect the Connection Details: Open your
wp-config.phpfile and confirm these values:define('DB_NAME', 'your_db_name'); define('DB_USER', 'your_username'); define('DB_PASSWORD', 'your_password'); define('DB_HOST', 'localhost');Tip: Some hosts use an external DB host (like
mysql.yourhost.com), not justlocalhost. - Ping the Database: Create a file called
dbtest.phpwith this code:connect_error) { die('Connection failed: ' . $mysqli->connect_error); } echo 'Connected!'; ?>Visit that file in your browser to see if the connection works without WordPress.
- Is the Server Even Awake? Ask your hosting support if the MySQL service is running. On shared hosting, DB servers sometimes get overwhelmed or crash silently.
- Repair the Database: Add this to
wp-config.php:define('WP_ALLOW_REPAIR', true);Then go to:
yoursite.com/wp-admin/maint/repair.phpand run a repair. (Remove the line afterward!)
🔒 Pro Tips from the Database Whisperer
- Keep a text file with your DB credentials stored securely offline — it’ll save you tons of time.
- Use cPanel > MySQL > Users to reset passwords if you’re unsure.
- Set up uptime monitoring to alert you if your DB or hosting is flaky.
🎯 Final Thoughts
This error isn’t a bug — it’s WordPress refusing to pretend everything’s okay when your site’s brain (the database) is missing. Once you get the credentials right, you’re back in business.