Question

how to solve tihis

Error:

**Warning**: mysqli_real_connect(): (HY000/2002): Connection refused in **/home/gify.com.bd/public_html/wp-includes/class-wpdb.php** on line **1982**
`Connection refused`

# Error establishing a database connection

This either means that the username and password information in your `wp-config.php` file is incorrect or that contact with the database server at `localhost:3306` could not be established. This could mean your host’s database server is down.

-   Are you sure you have the correct username and password?
-   Are you sure you have typed the correct hostname?
-   Are you sure the database server is running?

If you are unsure what these terms mean you should probably contact your host.

Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
October 3, 2024

Hey there 👋,

This error means WordPress can’t connect to your MySQL database.

This can be caused by a few things, so you should check the following:

  1. Check your wp-config.php settings:

    • Open your wp-config.php file (located in your WordPress directory) and check if the database details are correct:
      define('DB_NAME', 'your_database_name');
      define('DB_USER', 'your_database_user');
      define('DB_PASSWORD', 'your_database_password');
      define('DB_HOST', 'localhost:3306');
      
    • Make sure that the DB_NAME, DB_USER, DB_PASSWORD, and DB_HOST are correct.
  2. Check your MySQL server status:

    • On your Droplet, check that the MySQL service is running. You can check this by running the following command on your server:
      sudo systemctl status mysql
      
    • If it’s not running, start the service with:
      sudo systemctl start mysql
      
  3. Verify your MySQL Credentials:

    • Try connecting to the MySQL database manually using the credentials from wp-config.php while connected via SSH on your Droplet:
      mysql -u your_database_user -p
      
    • If you can connect, that means the credentials are correct. If not, double-check the username and password in wp-config.php.
  4. Check MySQL logs:

    • If MySQL is running but WordPress still can’t connect, check the MySQL error logs to identify the issue:
      sudo tail -f /var/log/mysql/error.log
      

Feel free to share the output of your logs and the MySQL service status!

Let me know how it goes or if you need more help troubleshooting!

- Bobby

KFSys
Site Moderator
Site Moderator badge
October 4, 2024

Heya,

The “Connection refused” error you’re encountering in WordPress means that WordPress is unable to establish a connection to the MySQL database. Here’s a step-by-step approach to troubleshoot and resolve the issue:

1. Check MySQL Service Status

Ensure that the MySQL service is running on your server. Run the following command to check the status:

sudo systemctl status mysql

If MySQL is not running, you can start it with:

sudo systemctl start mysql

2. Verify Database Credentials in wp-config.php

The error suggests that WordPress is unable to connect to the database due to incorrect credentials. Open the wp-config.php file and verify that the database settings are correct:

define( 'DB_NAME', 'your_database_name' );
define( 'DB_USER', 'your_database_user' );
define( 'DB_PASSWORD', 'your_database_password' );
define( 'DB_HOST', 'localhost' );  // Or your database host (e.g., 127.0.0.1)

Ensure that:

  • DB_NAME matches the name of your database.
  • DB_USER and DB_PASSWORD are correct.
  • DB_HOST is set to localhost if MySQL is running on the same server. If you’re using a different port or external database, modify it accordingly (e.g., localhost:3306 or your_remote_host:port).

3. Test the MySQL Connection from the Command Line

Try connecting to MySQL from the command line using the same credentials found in wp-config.php:

mysql -u your_database_user -p your_database_name

If you can connect successfully, it means the credentials are correct. If not, double-check the username and password.

4. Check MySQL Host Configuration

If your MySQL server is configured to listen only on certain IP addresses or not on localhost, this could cause the issue. Open your MySQL configuration file (typically /etc/mysql/mysql.conf.d/mysqld.cnf or /etc/my.cnf) and check the following line:

bind-address = 127.0.0.1

Ensure the bind address is either set to 127.0.0.1 for local connections or commented out for all connections. Restart MySQL after changes:

sudo systemctl restart mysql

The “Connection refused” error indicates that your WordPress site cannot connect to the MySQL database. Here are some steps to troubleshoot and resolve the issue:

1. Check Your wp-config.php File

Open your wp-config.php file (located in the root of your WordPress installation) and ensure the following values are correct:

/** The name of the database for WordPress */
define('DB_NAME', 'your_database_name');

/** MySQL database username */
define('DB_USER', 'your_database_user');

/** MySQL database password */
define('DB_PASSWORD', 'your_database_password');

/** MySQL hostname */
define('DB_HOST', 'localhost'); // This is often 'localhost', but it could be different for your host.

2. Verify Database Credentials

  • Ensure that the database name, username, and password are correct. You can usually verify these in your hosting control panel (like cPanel).
  • If you’ve recently changed your password, make sure it’s updated in the wp-config.php file.

3. Check Database Hostname

  • The default hostname is often localhost, but some hosting providers use different settings (like an IP address or specific hostname).
  • If you are unsure, check with your hosting provider for the correct hostname.

4. Test Database Server

  • Using phpMyAdmin: If your host provides access to phpMyAdmin, try logging in using the credentials from wp-config.php. If you cannot connect, the credentials are likely incorrect.
  • Using Command Line: If you have shell access, try connecting to the MySQL server using:
mysql -u your_database_user -p -h localhost

5. Ensure MySQL Server is Running

  • Contact your hosting provider to check if the MySQL server is operational. If it’s down, they should be able to resolve it.

6. Firewall and Security Settings

  • Ensure that your server’s firewall or security settings are not blocking the database connection.
  • If you have recently made changes to your server configuration or security settings, review them to see if anything might be affecting the connection.

7. Check for Server Resource Limits

  • Sometimes, if your website exceeds resource limits (like memory or connection limits), it can affect the database connection. Check with your hosting provider if you might be hitting any resource caps.

8. Debugging

If you have access to error logs, check the logs for more detailed error messages. This can provide clues about what’s going wrong.

Conclusion

After going through these steps, if you’re still experiencing issues, it’s best to reach out to your hosting provider’s support team. They can provide more specific insights based on their server setup and logs.

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.