WordPress项目运行时出现「Error establishing a database connection」错误的解决方法咨询
Hey there, sorry to hear you're stuck with the Error establishing a database connection in WordPress—this is one of the most common (and frustrating) issues, but let's break down how to fix it step by step.
This is the #1 cause of this error. Your wp-config.php file holds all the critical info WordPress needs to connect to your database. Here's what to verify:
- Locate the
wp-config.phpfile in your WordPress root directory (you can access it via FTP, cPanel File Manager, or your hosting control panel). - Look for these four lines:
define('DB_NAME', 'your_database_name'); define('DB_USER', 'your_database_username'); define('DB_PASSWORD', 'your_database_password'); define('DB_HOST', 'localhost'); // Or sometimes something like 'mysql.yourdomain.com' - Common mistakes to fix:
- Typos or extra spaces in any of the values (even a single space can break the connection).
- Using the wrong
DB_HOST: Some hosts use non-standard values like127.0.0.1instead oflocalhost, or a specific hostname provided by your host. - Special characters in your password: If your password has symbols like
$or", make sure it's wrapped in single quotes (') instead of double quotes to avoid PHP parsing issues.
Sometimes the credentials are correct, but WordPress can't reach the MySQL server. To confirm this:
- Create a simple PHP test file (name it
test-db.php) with this code:<?php $host = 'your_db_host'; // Replace with your DB_HOST value $user = 'your_db_user'; // Replace with your DB_USER value $pass = 'your_db_password'; // Replace with your DB_PASSWORD value $dbname = 'your_db_name'; // Replace with your DB_NAME value $conn = mysqli_connect($host, $user, $pass, $dbname); if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } echo "Database connected successfully!"; mysqli_close($conn); ?> - Upload this file to your WordPress root directory and access it via your browser (e.g.,
https://yourdomain.com/test-db.php).- If it says "connected successfully", your server is reachable—move to the next step.
- If it throws an error, note the message:
- "Access denied" means your user/password is wrong, or the user doesn't have access from your server's IP.
- "Can't connect to MySQL server" means the MySQL service is down, or your host has firewall restrictions blocking the connection.
Even if your username/password are correct, the user might not have the right permissions to access the database:
- Log into your hosting control panel's database tool (most use phpMyAdmin, or you can use MySQL command line if you have access).
- If using phpMyAdmin:
- Select your database from the left sidebar.
- Go to the "Privileges" tab.
- Find your database user and click "Edit Privileges".
- Make sure the user has ALL PRIVILEGES checked for your database. If not, select them and click "Go".
- If using command line:
mysql -u root -p # Enter your root password when prompted SHOW GRANTS FOR 'your_db_user'@'your_db_host'; # If permissions are missing, run: GRANT ALL PRIVILEGES ON your_db_name.* TO 'your_db_user'@'your_db_host'; FLUSH PRIVILEGES;
It sounds obvious, but sometimes the database name in wp-config.php is wrong, or the database was accidentally deleted:
- Use phpMyAdmin or MySQL command line to list all databases:
- In phpMyAdmin, look at the left sidebar—your database should be listed there.
- In command line:
mysql -u root -p SHOW DATABASES;
- If your database isn't listed, you'll need to restore it from a backup (if you have one) or create a new one and re-install WordPress (last resort).
If all the above checks pass, the problem might be with your hosting server:
- MySQL service is down: Contact your hosting provider and ask if their MySQL servers are experiencing downtime. If you're self-hosting (e.g., on a VPS), check if the MySQL service is running:
- On Linux:
systemctl status mysql(ormariadbif you use MariaDB). If it's stopped, start it withsystemctl start mysql. - On Windows: Open Services (search for "Services" in the Start menu), find MySQL, and make sure it's set to "Running".
- On Linux:
- Resource limits: If your server is low on memory or CPU, MySQL might crash or become unresponsive. Check your hosting control panel's resource usage metrics (like cPanel's "Resource Usage" tool).
- Firewall restrictions: Some hosts block outgoing connections to MySQL ports (usually 3306). Ask your host if this is the case, or if your server's IP is allowed to connect to the MySQL server.
If none of the above works, your database might be corrupted. WordPress has a built-in repair tool:
- Edit your
wp-config.phpfile and add this line above the/* That's all, stop editing! Happy publishing. */line:define('WP_ALLOW_REPAIR', true); - Access the repair page via your browser:
https://yourdomain.com/wp-admin/maint/repair.php - Click either "Repair Database" or "Repair and Optimize Database" (the latter takes longer but fixes more issues).
- Once the repair is done, delete the
WP_ALLOW_REPAIRline fromwp-config.php(it's a security risk to leave it enabled).
Hope one of these steps gets your WordPress site back up and running smoothly! If you run into specific error messages or get stuck on any step, feel free to share more details.
内容的提问来源于stack exchange,提问作者Vaibhav Parihar

