Solving Database Connection Issues
- "Unable to connect to the database" error
- "General Error: 2006 MySQL Server has Gone Away" error
- Database table configuration
"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?
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.
What Is The Solution?
Solution 1
- Open the file wp-config.php file.
- Find the line
define('DB_HOST', 'localhost');
and replace it withdefine('DB_HOST', '127.0.0.1');
Solution 2
- 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 - Edit your
php.ini
file and properly set the value forpdo_mysql.default_sock
- Restart your web Apache server to pickup the changes.
Need Help?
"General Error: 2006 MySQL Server has Gone Away" error
We have a guide for troubleshooting this error available here.
Database Table Configuration
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)