Solving Database Connection Issues


"Unable to connect to the database" Error

When MailPoet is unable to connect to the database, a warning like this is displayed:

What Is The Problem?

MailPoet uses a  PHP Data Objects (PDO) extension to connect to the database. If your website is on a Unix server and localhost is used as a database hostname, then PDO will try to connect to the database via a socket file specified in the PHP configuration (during the compilation or via php.ini). As described in  the PHP documentation:
When the host name is set to  "localhost", then the connection to the server is made thru a domain socket. If PDO_MYSQL is compiled against libmysqlclient then the location of the socket file is at libmysqlclient's compiled in location. If PDO_MYSQL is compiled against mysqlnd a default socket can be set thru the  pdo_mysql.default_socket setting.
When that socket file is missing, MailPoet is unable to connect to the database.

What Is The Solution?

Solution 1

  1. Open the file wp-config.php file.
  2. Find the line define('DB_HOST', 'localhost'); and replace it with define('DB_HOST', '127.0.0.1'); 

Solution 2

  1. Find your mysql.sock file. Common locations include: 
    • /tmp/mysql.sock
    • /tmp/mysql/mysql.sock
    • /var/mysql/mysql.sock

    or, if you use MAMP or LAMP look for inside the MySQL's tmp folder

  2. Edit yourphp.ini file and properly set the value for pdo_mysql.default_sock
  3. Restart your web Apache server to pickup the changes.

Need Help?

If you need further assistance to fix this, please contact your hosting provider's technical support and provide a link to this article. Or if you want a developer to help you? We recommend those at Codeable (affiliate link).

"General Error: 2006 MySQL Server has Gone Away" error

We have a guide for troubleshooting this error available here.


Database Table Configuration

Note: before making any changes to your site's database, please ensure you have a complete backup of the database just in case. If you are uncertain about accessing or working with your site's database, we recommend reaching out to your hosting support.

If you are experiencing very slow page loads in the admin dashboard (i.e. Automations, Subscribers, Newsletters, etc) or very slow sending on your site, it may be caused by your database table configuration. Please check to make sure that your Database tables are using the InnoDB storage engine (not MyISAM)

Checking Database Table Engine

To check what engine your database tables are using, you can usually check via your hosting provider's dashboard, or else using a plugin on your WordPress site such as WP phpMyAdmin.

Once you have accessed your site's database, you can view the tables and should be able to see an "Engine" column. If this is "MyISAM" (or anything other than "InnoDB"), then you should switch it to "InnoDB".

Changing Database Table Engine

To change the engine for a database table, you can use an SQL statement like the example below. Please note you would need to run this for each table that you need to change, replacing my_table with the name of the table that you want to change.

ALTER TABLE my_table ENGINE = InnoDB;

Alternatively, many database tools / plugins have options directly in their UI to update/ALTER the database table(s):

Again, if you are unsure or uncomfortable with making changes to your database, then we'd recommend reaching out to your hosting support to ask about making these changes.

Or if you'd prefer to hire a developer to make the changes for you, we recommend reaching out to someone from Codeable: https://codeable.io (affiliate link)

Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.