How to address “Error establishing a database connection” in WordPress

“Error establishing a database connection” is one the  most common error for custom WordPress installation. It could be due to one of the following reasons

  1. MySQL username and password is incorrect in wp-config.php file.
  2. MySQL database is down
  3. MySQL database is corrupted ( Don’t get scared by this reason. This is least likely reason)

MySQL username and password is incorrect in wp-config.php file.

Most likely you will get this error during installation of wordpress on your server or local machine if you have not updated database username and password in wp-config.php.

Please check following section in wp-config.php file.

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

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

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

/** MySQL hostname */
define('DB_HOST', 'localhost');

/** Database Charset to use in creating database tables. */
define('DB_CHARSET', 'utf8');

/** The Database Collate type. Don't change this if in doubt. */
define('DB_COLLATE', '');

 

To avoid getting this error, please follow WordPress installation guide.

MySQL database is down

There could be multiple reasons for database to be done. Simply restart MySQL database using following command

sudo service mysql start

Most likely this will solve your issue. If it does not solve, it will throw following error.

$sudo service mysql start
Job for mysql.service failed because the control process exited with error code. See "systemctl status mysql.service" and "journalctl -xe" for details.

Here is the output of both the commands

$sudo systemctl status mysql.service
● mysql.service - MySQL Community Server
Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
Active: inactive (dead) (Result: exit-code) since Sun 2018-04-29 02:10:42 UTC; 53s ago
Process: 10667 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid (code=exited, status=1/FAILURE)
Process: 10626 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/SUCCESS)
Main PID: 3812 (code=killed, signal=KILL)

Apr 29 02:10:41 ubuntu-512mb-server systemd[1]: Failed to start MySQL Community Server.
Apr 29 02:10:41 ubuntu-512mb-server systemd[1]: mysql.service: Unit entered failed state.
Apr 29 02:10:41 ubuntu-512mb-server systemd[1]: mysql.service: Failed with result 'exit-code'.
Apr 29 02:10:42 ubuntu-512mb-server systemd[1]: mysql.service: Service hold-off time over, scheduling restart.
Apr 29 02:10:42 ubuntu-512mb-server systemd[1]: Stopped MySQL Community Server.
Apr 29 02:10:42 ubuntu-512mb-server systemd[1]: mysql.service: Start request repeated too quickly.
Apr 29 02:10:42 ubuntu-512mb-server systemd[1]: Failed to start MySQL Community Server
$ sudo journalctl -xe
-- Unit mysql.service has begun starting up.
Apr 29 02:10:41 ubuntu-512mb-server audit[10666]: AVC apparmor="STATUS" operation="profile_replace" profile="unconfined" name="/usr/sbin/mysqld" pid=10666 comm="apparmor_parser"
Apr 29 02:10:41 ubuntu-512mb-server kernel: audit: type=1400 audit(1524967841.661:66): apparmor="STATUS" operation="profile_replace" profile="unconfined" name="/usr/sbin/mysqld" pid=10666 comm="apparmor_parser"
Apr 29 02:10:41 ubuntu-512mb-server mysqld[10667]: Initialization of mysqld failed: 0
Apr 29 02:10:41 ubuntu-512mb-server systemd[1]: mysql.service: Control process exited, code=exited status=1
Apr 29 02:10:41 ubuntu-512mb-server systemd[1]: Failed to start MySQL Community Server.
-- Subject: Unit mysql.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
-- 
-- Unit mysql.service has failed.
-- 
-- The result is failed.
Apr 29 02:10:41 ubuntu-512mb-server systemd[1]: mysql.service: Unit entered failed state.
Apr 29 02:10:41 ubuntu-512mb-server systemd[1]: mysql.service: Failed with result 'exit-code'.
Apr 29 02:10:42 ubuntu-512mb-server systemd[1]: mysql.service: Service hold-off time over, scheduling restart.
Apr 29 02:10:42 ubuntu-512mb-server systemd[1]: Stopped MySQL Community Server.
-- Subject: Unit mysql.service has finished shutting down
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
-- 
-- Unit mysql.service has finished shutting down.

This tells me this is not norma mysql shutdown and I need to check further logs. Use following command after trying MySQL restart.

$sudo tail -30 /var/log/mysql/error.log
2018-04-29T02:15:31.641869Z 0 [ERROR] Failed to initialize builtin plugins.
2018-04-29T02:15:31.641872Z 0 [ERROR] Aborting

2018-04-29T02:15:31.643813Z 0 [Note] Binlog end
2018-04-29T02:15:31.643871Z 0 [Note] Shutting down plugin 'MyISAM'
2018-04-29T02:15:31.644102Z 0 [Note] /usr/sbin/mysqld: Shutdown complete

2018-04-29T02:15:32.135955Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-04-29T02:15:32.138090Z 0 [Note] /usr/sbin/mysqld (mysqld 5.7.21) starting as process 10937 ...
2018-04-29T02:15:32.141779Z 0 [Note] InnoDB: PUNCH HOLE support available
2018-04-29T02:15:32.141810Z 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2018-04-29T02:15:32.141818Z 0 [Note] InnoDB: Uses event mutexes
2018-04-29T02:15:32.141825Z 0 [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier
2018-04-29T02:15:32.141831Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.3
2018-04-29T02:15:32.141837Z 0 [Note] InnoDB: Using Linux native AIO
2018-04-29T02:15:32.142157Z 0 [Note] InnoDB: Number of pools: 1
2018-04-29T02:15:32.142291Z 0 [Note] InnoDB: Using CPU crc32 instructions
2018-04-29T02:15:32.144099Z 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M
2018-04-29T02:15:32.144141Z 0 [ERROR] InnoDB: mmap(137428992 bytes) failed; errno 12
2018-04-29T02:15:32.144149Z 0 [ERROR] InnoDB: Cannot allocate memory for the buffer pool
2018-04-29T02:15:32.144154Z 0 [ERROR] InnoDB: Plugin initialization aborted with error Generic error
2018-04-29T02:15:32.144161Z 0 [ERROR] Plugin 'InnoDB' init function returned error.
2018-04-29T02:15:32.144165Z 0 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
2018-04-29T02:15:32.144169Z 0 [ERROR] Failed to initialize builtin plugins.
2018-04-29T02:15:32.144172Z 0 [ERROR] Aborting

2018-04-29T02:15:32.146255Z 0 [Note] Binlog end
2018-04-29T02:15:32.146304Z 0 [Note] Shutting down plugin 'MyISAM'
2018-04-29T02:15:32.146569Z 0 [Note] /usr/sbin/mysqld: Shutdown complete

Please note the error statements highlighted in bold.

Root cause.

This error is coming because my system is falling short of RAM memory. Normally when you use server with 512 MB RAM, this kind of issues occur.

Solution

  1. Increase the RAM of the server by upgrading your server capacity
  2. Add swapfile. I solved above problem by adding swapfile. Please refer to this excellent guide to add swap file.

 

MySQL database is corrupted

If your MySQL installation is corrupted, I hope you have some database backup available with you. There are options to recover data. If you have backup data available, best option is to reinstall MySQL. Please follow how to install MySQL on Ubuntu guide.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.