Error establishing a database connection digitalocean wordpress new virtual host
A common issue!
When setting up a new virtual host on DigitalOcean with WordPress, you may encounter an error establishing a database connection. Here are some common causes and solutions:
- Incorrect database credentials:
- Check your
wp-config.php
file to ensure the database username, password, and hostname are correct. - Verify that the database credentials match the ones you created in the DigitalOcean control panel.
- Check your
- Database not created or not accessible:
- Make sure you created a database in the DigitalOcean control panel and granted the necessary permissions to the WordPress user.
- Check the database name, username, and password in the
wp-config.php
file and ensure they match the ones you created.
- Incorrect database host:
- Verify that the database host is set to
localhost
or the IP address of your DigitalOcean droplet. - If you're using a managed database service like DigitalOcean Managed Databases, ensure the host is set to the service's hostname.
- Verify that the database host is set to
- Firewall or security group issues:
- Check your DigitalOcean droplet's firewall or security group settings to ensure they allow incoming traffic on the database port (usually 3306 for MySQL).
- If you're using a managed database service, ensure the service's firewall rules allow incoming traffic.
- WordPress configuration issues:
- Verify that the
wp-config.php
file is correctly configured and that the database connection settings are correct. - Check for any typos or incorrect values in the
wp-config.php
file.
- Verify that the
- Database connection timeout:
- Increase the database connection timeout in your
wp-config.php
file by adding the following line:define('WP_DB_TIMEOUT', 30); // adjust the value as needed
- Increase the database connection timeout in your
- Check the database logs:
- Review the database logs to see if there are any errors or issues with the connection.
- Check the MySQL error log for any errors related to the connection.
To troubleshoot the issue, you can try the following:
- Check the DigitalOcean droplet's logs for any errors related to the database connection.
- Use a tool like
mysql
ormysqladmin
to connect to the database and verify the connection. - Use a plugin like
WP_Debug
to enable debugging in WordPress and see if there are any errors related to the database connection. - Try connecting to the database using the
wp-cli
tool to see if there are any issues with the connection.
If none of these solutions work, please provide more details about your setup, including the DigitalOcean droplet's configuration, the WordPress version, and any error messages you've encountered.