"Failed to connect to the database: An exception occurred in driver: SQLSTATE[HY000] [2006] MySQL server has gone away"

Support intro
Hello, here are the connection to Db problem tough connect from console to mariadb from php -r successfully:

    [root@nextcloud config]# php -r '
    >
    > $servername = "nextcloud";
    > $username = "root";
    > $password = "";
    >
    > // Create connection
    > $conn = new mysqli($servername, $username, $password);
    >
    > // Check connection
    > if ($conn->connect_error) {
    >     die("Connection failed: " . $conn->connect_error);
    > }
    > echo "Connected successfully";
    >
    > '
    Connected successfully[ro
    [root@nextcloud config]#






    [root@nextcloud config]# cat /etc/os-release
    NAME="CentOS Linux"
    VERSION="7 (Core)"
    ID="centos"
    ID_LIKE="rhel fedora"
    VERSION_ID="7"
    PRETTY_NAME="CentOS Linux 7 (Core)"
    ANSI_COLOR="0;31"
    CPE_NAME="cpe:/o:centos:centos:7"
    HOME_URL="https://www.centos.org/"
    BUG_REPORT_URL="https://bugs.centos.org/"

    CENTOS_MANTISBT_PROJECT="CentOS-7"
    CENTOS_MANTISBT_PROJECT_VERSION="7"
    REDHAT_SUPPORT_PRODUCT="centos"
    REDHAT_SUPPORT_PRODUCT_VERSION="7"

    Server version: 5.5.64-MariaDB MariaDB Server

    [root@nextcloud config]# cat /etc/my.cnf
    [server]
    #skip-name-resolve
    bind-address = 0.0.0.0
    innodb_buffer_pool_size = 128M
    innodb_buffer_pool_instances = 1
    innodb_flush_log_at_trx_commit = 2
    innodb_log_buffer_size = 32M
    innodb_max_dirty_pages_pct = 90
    query_cache_type = 1
    query_cache_limit = 2M
    query_cache_min_res_unit = 2k
    query_cache_size = 64M
    tmp_table_size= 64M
    max_heap_table_size= 64M
    slow-query-log = 1
    slow-query-log-file = /var/log/mysql/slow.log
    long_query_time = 1

    [client-server]
    !includedir /etc/mysql/conf.d/
    !includedir /etc/mysql/mariadb.conf.d/

    [client]
    default-character-set = utf8mb4

    [mysqld]
    general_log = on
    general_log_file=/var/log/mysql/mysql-general.log
    character-set-server = utf8mb4
    collation-server = utf8mb4_general_ci
    transaction_isolation = READ-COMMITTED
    binlog_format = ROW
    innodb_large_prefix=on
    innodb_file_format=barracuda
    innodb_file_per_table=1
    log-error = /var/log/mysql/mysqld.error.log
    log-bin=/var/log/mysql/mysql-bin.log
    wait_timeout=600
    max_allowed_packet=64M




Could anybody help?

Nextcloud version (eg, 18.0.0): 18.0.0
Operating system and version (eg, Centos 17.04): CentOS Linux 7
Apache or nginx version (eg, Apache 2.4.25): Apache/2.4.6 (CentOS)
PHP version (eg, 7.1): PHP 7.3.14 (cli) (built: Jan 21 2020 09:59:50) ( NTS )

The issue you are facing:

Is this the first time you’ve seen this error? (Y/N):

Steps to replicate it:

  1. Just try to click on url http://{ip}

The output of your Nextcloud log in Admin > Logging:

... in ...nextcloud.log
...

`"message":{"Exception":"Doctrine\\DBAL\\DBALException","Message":"Failed to connect to the database: An exception occurred in driver: SQLSTATE[HY000] [2006] MySQL server has gone away"`
...

The output of your config.php file in /path/to/nextcloud (make sure you remove any identifiable information!):

From config.php
...
    'dbtype' => 'mysql',
    'dbhost' => '127.0.0.1:3306',
    'dbname' => 'nextcloud',
    'dbuser' => 'root',
    'dbpassword' => '',
    'dbtableprefix' => 'oc_',
    'installed' => false,
...

Is your mysql running?

Of course:

[root@nextcloud config]# systemctl status mariadb.service
● mariadb.service - MariaDB database server
   Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor preset: disabled)
   Active: active (running) since Чт 2020-02-06 10:22:31 MSK; 3h 11min ago
  Process: 19977 ExecStartPost=/usr/libexec/mariadb-wait-ready $MAINPID (code=exited, status=0/SUCCESS)
  Process: 19941 ExecStartPre=/usr/libexec/mariadb-prepare-db-dir %n (code=exited, status=0/SUCCESS)
 Main PID: 19976 (mysqld_safe)
   CGroup: /system.slice/mariadb.service
           ├─19976 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
           └─20413 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mysql/mysqld.error.log --pid-file=nextcloud.pid

фев 06 10:22:29 nextcloud systemd[1]: Starting MariaDB database server...
фев 06 10:22:29 nextcloud mariadb-prepare-db-dir[19941]: Database MariaDB is probably initialized in /var/lib/mysql already, nothing is done.
фев 06 10:22:29 nextcloud mariadb-prepare-db-dir[19941]: If this is not the case, make sure the /var/lib/mysql is empty before running mariadb-prepare-db-dir.
фев 06 10:22:29 nextcloud mysqld_safe[19976]: 200206 10:22:29 mysqld_safe Logging to '/var/log/mysql/mysqld.error.log'.
фев 06 10:22:29 nextcloud mysqld_safe[19976]: 200206 10:22:29 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
фев 06 10:22:31 nextcloud systemd[1]: Started MariaDB database server.
[root@nextcloud config]#

Couldn’t it be stopped if php script from console gets no error?

[root@nextcloud config]# php -r '
>
> $servername = "nextcloud";
> $username = "root";
> $password = "";
>
> // Create connection
> $conn = new mysqli($servername, $username, $password);
>
> // Check connection
> if ($conn->connect_error) {
>     die("Connection failed: " . $conn->connect_error);
> }
> echo "Connected successfully";
>
> '
Connected successfully[ro
[root@nextcloud config]#

Here are lots of users and some of them know more and some less. That’s why I started from the basic question. :slight_smile:

I would restart Mariadb. Then go to your web page to see if it works. If not, then look at /var/log/mysql/mysqld.error.log if there’s something.

Anyway this problem is not easy to solve without doing some research. This may give some ideas https://haydenjames.io/mysql-server-has-gone-away-error-solutions/

Tried to make all of them. Some even make worse (offered innodb parameters stack mariadb running).
In whole - no ideas by now.

                                                                                                                                                                                                [root@nextcloud ~]# tail /var/log/mysql/mysqld.error.log
200206 18:44:06 InnoDB: Initializing buffer pool, size = 128.0M
200206 18:44:06 InnoDB: Completed initialization of buffer pool
200206 18:44:06 InnoDB: highest supported file format is Barracuda.
200206 18:44:06  InnoDB: Waiting for the background threads to start
200206 18:44:07 Percona XtraDB (http://www.percona.com) 5.5.61-MariaDB-38.13 started; log sequence number 1597971
200206 18:44:07 [Note] Plugin 'FEEDBACK' is disabled.
200206 18:44:07 [Note] Server socket created on IP: '0.0.0.0'.
200206 18:44:07 [Note] Event Scheduler: Loaded 0 events
200206 18:44:07 [Note] /usr/libexec/mysqld: ready for connections.
Version: '5.5.64-MariaDB'  socket: '/var/lib/mysql/mysql.sock'  port: 3306  MariaDB Server
[root@nextcloud ~]#

That looks to me you are using unix socket and not tcp socket.

From config_sample.php

  • Your host server name, for example localhost, hostname,
  • hostname.example.com, or the IP address. To specify a port use
  • hostname:####; to specify a Unix socket use
  • localhost:/path/to/socket.

‘dbhost’ => ‘’,

If you have unix socket then fix dbhost in the config.php.
‘dbhost’ => ‘localhost:/var/lib/mysql/mysql.sock’

Don’t hang me if I’m wrong. :smiley:

1 Like

Thank you for your response. I followed your advice but unhappy by now:

[root@nextcloud ~]# grep "dbhost" /var/www/html/nextcloud/config/config.php
'dbhost' => 'localhost:/var/lib/mysql/mysql.sock',
[root@nextcloud ~]# ping localhost
PING localhost (127.0.0.1) 56(84) bytes of data.
64 bytes from nextcloud (127.0.0.1): icmp_seq=1 ttl=64 time=0.078 ms
64 bytes from nextcloud (127.0.0.1): icmp_seq=2 ttl=64 time=0.061 ms
^C
--- localhost ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 0.061/0.069/0.078/0.011 ms
[root@nextcloud ~]# ping 127.0.0.1
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.062 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.061 ms
^C
--- 127.0.0.1 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 0.061/0.061/0.062/0.007 ms
[root@nextcloud ~]# vim /var/www/html/nextcloud/config/config.php




The server was unable to complete your request.

If this happens again, please send the technical details below to the server administrator.

More details can be found in the server log.

### Technical details

* Remote Address: x.x.x.x
* Request ID: Xjxib@x-nIt-QLf89LoW4AAAAAE
* Type: Doctrine\DBAL\DBALException
* Code: 0
* Message: Failed to connect to the database: An exception occurred in driver: SQLSTATE[HY000] [2006] MySQL server has gone away
* File: /var/www/html/nextcloud/lib/private/DB/Connection.php
* Line: 68

You know , may be this is the reason:

[root@nextcloud config]# ls
CAN_INSTALL  config.php  config.sample.php
[root@nextcloud config]#

I have config.php next to config.sample.php.
Nextcloud doesn’t exact know which one for it )

If it so, where could i change the name of work config?

[root@nextcloud config]# grep "dbhost" config.sample.php
'dbhost' => '',
[root@nextcloud config]# grep "dbhost" config.php
'dbhost' => '127.0.0.1:/var/lib/mysql/mysql.sock',
[root@nextcloud config]#

I may have missed it but have you tried the following without specifying the port/socket:

...
'dbtype' => 'mysql',
'dbhost' => 'localhost',
'dbname' => 'nextcloud',
'dbuser' => 'root',
'dbpassword' => '',
'dbtableprefix' => 'oc_',
...

Looking at the port you are using, it’s the standard one anyway and you are on localhost, so it shouldn’t be necessary to specify the port.

Having both config.php and config.sample.php in the same directory is not an issue. Nextcloud will only use the config.php file for configuration parameters. Make sure the permissions are set properly on all of your files and folders though.

You have CAN_INSTALL file in your config directory. You should not have config.php file there at the same time. Config.php is created when you go to your Nextcloud page with a browser. Then it will ask your db info and creates tables etc. deletes can_install and creates config.php. After that you can edit it.

Move config.php somewhere else, just in case. (or delete) Then goto your Nextcloud. Let’s see what happens.

1 Like

Thank you very much! It works!
Now after installation complete i have right config.php here:
[root@nextcloud ~]# ls /var/www/html/nextcloud/config/
config.php config.sample.php
[root@nextcloud ~]#

1 Like