Solving Database Connection Issues
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
- 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?
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).