Updating the Credentials in wp-config.php
The most common cause of a database connection error is that the details in wp-config.php, WordPress’s main configuration file, don’t match those on the database server. After a site migration or a password change in particular, you need to check this file manually. Make sure the four main variables in the file (DB_NAME, DB_USER, DB_PASSWORD, DB_HOST) are correct.
Modern hosting panels generate increasingly complex passwords, so be careful not to leave any spaces when copying and pasting. If your database name or username starts with a prefix, that prefix must also appear in the file exactly. Even a single wrong character will break the entire connection.
- Access the root directory via FTP or File Manager.
- Open wp-config.php in a text editor.
- Update the database name, username and password lines.
Signs of Authentication Errors
Incorrect credentials usually trigger a connection error directly on the site’s front end and also block access to the admin dashboard entirely. In this case, the fastest fix may be to create a new user in your database management tool (such as phpMyAdmin) and grant it privileges.
- Create a new MySQL user from your control panel.
- Grant this user all privileges on the relevant database.
- Enter the new details in wp-config.php and test the connection.
| Cause | Fix | Difficulty | Estimated time | Risk Level |
|---|---|---|---|---|
| Incorrect Credentials | Editing wp-config.php | Low | 5 minutes | Secure |
| Corrupted Tables | Database Repair | Medium | 10 minutes | Low |
| Server Outage | Restarting the Service | High | 15 minutes | Medium |
| Permission Issues | Assigning User Privileges | Medium | 5 minutes | Secure |
| Insufficient Memory | Raising the Limit | Medium | 10 minutes | Low |
Using the WordPress Database Repair Tool
Sometimes the system throws an error even when the database connection works, because certain tables are corrupted. WordPress has a hidden repair mode for these situations. When you enable it, the database engine scans the tables and automatically fixes index errors.
This method is a lifesaver when tables are corrupted by heavy attacks on your site or a sudden server shutdown. Once the repair is complete, don’t forget to remove the code from the file for security reasons. Otherwise, anyone can access your database repair page from outside.
- Add the line
define( 'WP_ALLOW_REPAIR', true );to wp-config.php. - In your browser, go to yourdomain.com/wp-admin/maint/repair.php.
- Click the “Repair and Optimize Database” button to start the process.
Common Causes of Table Corruption
Database tables can be damaged when server resources run out during simultaneous write operations. Older setups using the MyISAM engine in particular are more prone to corruption than the modern InnoDB engine. By current standards, all tables should use the InnoDB format.
- Sudden power or service outages on the server.
- Database queries being interrupted before they complete.
- Temporary files that can’t be created due to insufficient disk space.
Checking the Status and Availability of the MySQL Server
If all the details are correct and the tables are intact, the problem may lie with the database server itself. On shared hosting, the database server can become overloaded and stop responding. If you manage your own server (VPS/VDS), you need to check the status of the MySQL or MariaDB service from the command line.
During high-traffic periods, the MySQL service may hit an “Out of Memory” (OOM) error and be killed by the operating system. Restarting the service is a temporary fix, but in the long run you’ll need more RAM or query optimization. Reviewing the server logs helps you pinpoint exactly when and why the crash happened.
- Check the service status over SSH with the 'systemctl status mysql' command.
- If the service is stopped, restart it with the 'systemctl start mysql' command.
- Analyze errors by checking the server error log (/var/log/mysql/error.log).
Source: Query Monitor Plugin
Related serviceDatabase Optimization ↗Manually Reviewing the Database Prefix and Host Settings
Most users enter “localhost” as the database host, but some hosting providers assign a different server or IP address for the database. If your site connects to a remote database server, make sure the IP address and port number (3306 by default) are configured correctly.
The database table prefix also plays a critical role. This prefix is 'wp_' by default but may have been changed for security reasons. If the $table_prefix variable in wp-config.php doesn’t match the actual table names in the database, the system can’t find the tables and throws an error even if it connects to the database.
- Confirm the database host address (IP or hostname) in your hosting panel.
- Check in phpMyAdmin which prefix the tables start with.
- Update the prefix in wp-config.php to match the database.
What to Watch for with Remote Database Connections
If you use a remote connection, the database server must allow (whitelist) requests from your web server. Make sure port 3306 is open on both sides in your firewall settings.
- Check the 'bind-address' setting on the database server.
- Add your web server’s IP address to the database access list.
- Use the 'telnet' or 'nc' command-line tools to test the connection.
Checking File Permissions and the Server-Side Firewall
File permissions are a rare cause of this error: if wp-config.php can’t be read at all (for example, with permissions set to 000), you’ll usually see PHP’s “Failed opening required” error or a blank page instead of this message. Still, verify that the file permissions are 644 or 600 and that the file is owned by the site’s user.
Also, if you use a remote database, the server firewall (such as CSF or iptables rules) may block port 3306 connections from the web server; ModSecurity, on the other hand, filters HTTP requests and doesn’t block the connection between PHP and MySQL. Review the firewall logs to see whether your site’s database access is being blocked.
- Set file permissions to 644 via FTP.
- Verify that your site’s IP address isn’t blocked in the server firewall (WAF).
- Make sure PHP limits (memory_limit) are sufficient for database operations.
Preventing Database Overload Caused by Plugins and Themes
A poorly coded plugin or theme can send hundreds of queries per second to the database and exceed the connection limits. If you’re getting a “Too many connections” error, it usually means a plugin is exhausting database resources. In that case, deactivate your plugins to find the source of the error.
Object caching systems such as Redis or Memcached can significantly reduce the load on the database. If your site gets high traffic, object caching can help prevent database connection errors. These systems keep frequently used data in RAM, minimizing the need to query the database.
- Rename the plugins folder to deactivate all plugins at once.
- Switch to a default WordPress theme to test for theme conflicts.
- Check whether Redis or Memcached support is enabled on the server.
Performance Optimization Tips
Reducing database load doesn’t just prevent errors; it also makes your site load faster. Cleaning up unnecessary revisions, trashed comments and leftovers from old plugins shrinks the size of your database.
- Clean up your database regularly with tools like WP-Optimize.
- Enable the ‘Slow Query Log’ to monitor query times.
- Optimize your database tables at regular intervals with the ‘OPTIMIZE TABLE’ command.
Fixing Database User and Privilege Problems
If the database user lacks the right privileges, data can’t be read or written even when the connection works. For WordPress to function properly, the user needs SELECT, INSERT, UPDATE, DELETE, CREATE, DROP and ALTER privileges. If any of these is missing, you’ll see errors, especially during updates.
Sometimes the database user may have been deleted, or its password may have been reset automatically by the system. In such cases, deleting the existing user and recreating it with the same name and password will refresh the privilege tables. In cPanel’s “MySQL Databases” screen, you can see whether the user is assigned to the database and what privileges it has.
- Manually reload MySQL privileges with the ‘FLUSH PRIVILEGES’ command.
- Verify in the panel that the user is assigned to the database.
- Update the user’s password in both the panel and wp-config.php at the same time.
Source: WordPress: Editing wp-config.php
Frequently Asked Questions
1. Why does a database connection error appear suddenly?
It usually appears suddenly after a server-side update, when the disk fills up, or when a plugin corrupts the database tables.
2. What does a database connection problem mean?
It means the site or app can’t reach the database server where its data is stored. With large online services such as government portals, the problem is usually on the service’s own servers, and users should simply try again later.
3. What can I use instead of “localhost”?
If localhost doesn’t work, try the dedicated database server address provided by your hosting company or the IP address 127.0.0.1.
4. Can a database repair cause data loss?
The repair process is generally designed to recover data, but with severely damaged tables there is a small risk of data loss.
5. What does database connection error 2002 mean?
Error code 2002 means PHP couldn’t reach the MySQL server at all; either the database service is down or the DB_HOST address or socket path is wrong.
Database connection errors are technical issues that can be fixed quickly once you diagnose them correctly. By following the steps above in order, you can make your site’s database access secure and up to current standards.
In Summary
In this guide, we looked at the five most effective ways to fix database errors, from the wp-config configuration to server service checks. To solve the problem, first verify your credentials, then check server resources and table integrity.
















