Error establishing a database connection

November 17, 2019 545 views
MySQL

[Example link](http://)Hi out there,
How to Fix Error Establishing a Database Connection in WordPress.

root@ubuntu-s-1vcpu-1gb-fra1-01:/var/www/html# sudo apache2ctl configtest
Syntax OK

root@ubuntu-s-1vcpu-1gb-fra1-01:/var/www/html# mysql -u wordpressuser -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 106
Server version: 8.0.17-0ubuntu2 (Ubuntu)

Copyright © 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type ‘help;’ or ’\h’ for help. Type ’\c’ to clear the current input statement.
mysql>



Log (/var/log/mysql):


2019-11-11T18:29:51.203151Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.17-0ubuntu2) initializing of server in progress as process 3781
2019-11-11T18:29:53.294422Z 5 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
2019-11-11T18:29:54.459659Z 5 [System] [MY-013172] [Server] Received SHUTDOWN from user boot. Shutting down mysqld (Version: 8.0.17-0ubuntu2).
2019-11-11T18:29:54.461458Z 0 [System] [MY-013170] [Server] /usr/sbin/mysqld (mysqld 8.0.17-0ubuntu2) initializing of server has completed
2019-11-11T18:29:58.403067Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.17-0ubuntu2) starting as process 3834
2019-11-11T18:29:59.057690Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2019-11-11T18:29:59.059194Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/tmp' in the path is accessible to all OS users. Consider choosing a different directory.
2019-11-11T18:29:59.086590Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.17-0ubuntu2'  socket: '/tmp/tmp.22e4DysKK3/mysqld.sock'  port: 0  (Ubuntu).
2019-11-11T18:30:00.637729Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.17-0ubuntu2)  (Ubuntu).
2019-11-11T18:30:01.839200Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.17-0ubuntu2) starting as process 3900
2019-11-11T18:30:02.329334Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2019-11-11T18:30:02.331139Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/tmp' in the path is accessible to all OS users. Consider choosing a different directory.
2019-11-11T18:30:02.352924Z 6 [System] [MY-013172] [Server] Received SHUTDOWN from user boot. Shutting down mysqld (Version: 8.0.17-0ubuntu2).
2019-11-11T18:30:03.432415Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.17-0ubuntu2)  (Ubuntu).
2019-11-11T18:30:05.160324Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.17-0ubuntu2) starting as process 4074
2019-11-11T18:30:05.687570Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2019-11-11T18:30:05.709989Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.17-0ubuntu2'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  (Ubuntu).
2019-11-11T18:30:05.878247Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: '/var/run/mysqld/mysqlx.sock' bind-address: '::' port: 33060
2019-11-11T18:55:24.118428Z 13 [Warning] [MY-013360] [Server] Plugin sha256_password reported: ''sha256_password' is deprecated and will be removed in a future release. Please use caching_sha2_password instead'
2019-11-11T19:28:13.706696Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.17-0ubuntu2)  (Ubuntu).
2019-11-11T19:29:12.004674Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.17-0ubuntu2) starting as process 747
2019-11-11T19:29:13.942482Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2019-11-11T19:29:13.990490Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.17-0ubuntu2'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  (Ubuntu).
2019-11-11T19:29:14.060615Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: '/var/run/mysqld/mysqlx.sock' bind-address: '::' port: 33060
2019-11-15T18:22:59.886643Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.17-0ubuntu2) starting as process 797
2019-11-15T18:23:01.911322Z 0 [System] [MY-010229] [Server] Starting crash recovery...
2019-11-15T18:23:01.951817Z 0 [System] [MY-010232] [Server] Crash recovery finished.
2019-11-15T18:23:02.066518Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2019-11-15T18:23:02.102460Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.17-0ubuntu2'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  (Ubuntu).
2019-11-15T18:23:02.126061Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: '/var/run/mysqld/mysqlx.sock' bind-address: '::' port: 33060
2019-11-15T19:21:45.882040Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.17-0ubuntu2)  (Ubuntu).
2019-11-15T19:22:08.709186Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.17-0ubuntu2) starting as process 745
2019-11-15T19:22:10.868065Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2019-11-15T19:22:10.942818Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.17-0ubuntu2'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  (Ubuntu).
2019-11-15T19:22:11.189623Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: '/var/run/mysqld/mysqlx.sock' bind-address: '::' port: 33060
2019-11-15T20:18:40.982980Z 0 [Warning] [MY-010909] [Server] /usr/sbin/mysqld: Forcing close of thread 8  user: 'root'.
2019-11-15T20:18:42.412360Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.17-0ubuntu2)  (Ubuntu).
2019-11-15T20:18:43.084571Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.17-0ubuntu2) starting as process 1956
2019-11-15T20:18:43.764264Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2019-11-15T20:18:43.805636Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.17-0ubuntu2'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  (Ubuntu).
2019-11-15T20:18:43.920747Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: '/var/run/mysqld/mysqlx.sock' bind-address: '::' port: 33060
2019-11-15T20:39:44.427272Z 0 [Warning] [MY-010909] [Server] /usr/sbin/mysqld: Forcing close of thread 8  user: 'root'.
2019-11-15T20:39:44.430980Z 0 [Warning] [MY-010909] [Server] /usr/sbin/mysqld: Forcing close of thread 9  user: 'wordpressuser'.
2019-11-15T20:39:45.378017Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.17-0ubuntu2)  (Ubuntu).
2019-11-15T20:39:45.968480Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.17-0ubuntu2) starting as process 2158
2019-11-15T20:39:46.582804Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2019-11-15T20:39:46.614586Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.17-0ubuntu2'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  (Ubuntu).
2019-11-15T20:39:46.736693Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: '/var/run/mysqld/mysqlx.sock' bind-address: '::' port: 33060
2019-11-15T20:43:18.102275Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.17-0ubuntu2)  (Ubuntu).
2019-11-15T20:43:18.699887Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.17-0ubuntu2) starting as process 2282
2019-11-15T20:43:19.267894Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2019-11-15T20:43:19.299304Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.17-0ubuntu2'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  (Ubuntu).
2019-11-15T20:43:19.421627Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: '/var/run/mysqld/mysqlx.sock' bind-address: '::' port: 33060
2019-11-15T20:44:37.318964Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.17-0ubuntu2)  (Ubuntu).
2019-11-15T20:44:37.935948Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.17-0ubuntu2) starting as process 2407
2019-11-15T20:44:38.492693Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2019-11-15T20:44:38.522970Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.17-0ubuntu2'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  (Ubuntu).
2019-11-15T20:44:38.668758Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: '/var/run/mysqld/mysqlx.sock' bind-address: '::' port: 33060
2019-11-15T20:50:13.975514Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.17-0ubuntu2)  (Ubuntu).
2019-11-15T20:50:14.630744Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.17-0ubuntu2) starting as process 2566
2019-11-15T20:50:15.192521Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2019-11-15T20:50:15.220560Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.17-0ubuntu2'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  (Ubuntu).
2019-11-15T20:50:15.373810Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: '/var/run/mysqld/mysqlx.sock' bind-address: '::' port: 33060
2019-11-15T21:07:13.873221Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.17-0ubuntu2)  (Ubuntu).
2019-11-15T21:07:14.450268Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.17-0ubuntu2) starting as process 2692
2019-11-15T21:07:15.031561Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2019-11-15T21:07:15.059804Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.17-0ubuntu2'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  (Ubuntu).
2019-11-15T21:07:15.163325Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: '/var/run/mysqld/mysqlx.sock' bind-address: '::' port: 33060
2019-11-15T21:53:12.972191Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.17-0ubuntu2)  (Ubuntu).
2 Answers

Hello @sshami,

I can see you can connect to MySQL with the user wordpressuser however this doesn’t mean the user actually has access to the needed database. You can try and perform the following

mysql -u wordpressuser -p wordpressdatabase

And see if you are going to be able to access it.

Alternatively, the Error establishing a database connection can be caused by a problem with the database to just wrong configuration of your wp-config.php file. Luckily, there is a good article just for these occasions here at DigitalOcean - Article.

Please follow up the article and see if you’ll be able to resolve the matter. If you don’t we can follow up on that from there.

Regards,
KDSys

by Brian Boucheron
WordPress is one of the most popular open source content management systems in the world. Although it started out focused on blogging, over the years it has developed into a more flexible platform for websites in general. After almost fifteen years of development it is quite polished and robust, yet issues can still come up. If you've recently attempted to load your WordPress-powered website and instead saw a message stating "Error Establishing Database Connection", the cause is most often...

Hi @KDsys,

Thanks for your time !

With the following command, I am able to login mysql

mysql -u wordpressuser -p wordpressdatabase

If you see no output after running following zgrep command, your server is not running out of memory.

zgrep -a "allocate memory" /var/log/mysql/error.log*
root@ubuntu-s-1vcpu-1gb-fra1-01:~# zgrep -a "allocate memory" /var/log/mysql/error.log*
root@ubuntu-s-1vcpu-1gb-fra1-01:~#

By the above output result, it seems no running memory issue.

Config file also looks good.

define( 'DB_NAME', 'wordpress');

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

/** MySQL database password */
define( 'DB_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', '' );

define('FS_METHOD', 'direct');

define('WP_ALLOW_REPAIR', true);


Able to connect database:

root@ubuntu-s-1vcpu-1gb-fra1-01:/var/www/html# mysqlshow -u wordpressuser -p
Enter password:
+--------------------+
|     Databases      |
+--------------------+
| information_schema |
| wordpress          |
+--------------------+
root@ubuntu-s-1vcpu-1gb-fra1-01:/var/www/html#

http://www.example.com/wp-admin/maint/repair.php

Error establishing a database connection

I guess problem might be in Authentication and salts keys in the config file wp-config.php
Is it possible to remove the authentication keys from the config file and then try to connect just for the testing purpose.

I followed whole article what you have provided me but all looks good.

Thanks in advance.

edited by MattIPv4
Have another answer? Share your knowledge.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!