[Solved] Cannot connect to MySQL server - error: MySQL server has gone away

Support intro

Sorry to hear you’re facing problems :slightly_frowning_face:

help.nextcloud.com is for home/non-enterprise users. If you’re running a business, paid support can be accessed via portal.nextcloud.com where we can ensure your business keeps running smoothly.

In order to help you as quickly as possible, before clicking Create Topic please provide as much of the below as you can. Feel free to use a pastebin service for logs, otherwise either indent short log examples with four spaces:

example

Or for longer, use three backticks above and below the code snippet:

longer
example
here

Some or all of the below information will be requested if it isn’t supplied; for fastest response please provide as much as you can :heart:

Nextcloud version (eg, 12.0.2): 17.0.2.1
Operating system and version (eg, Ubuntu 17.04): Ubuntu 19.10
Apache or nginx version (eg, Apache 2.4.25): Apache
PHP version (eg, 7.1): 7.3

mysql mariadb version: mysql Ver 15.1 Distrib 10.3.20-MariaDB, for debian-linux-gnu (aarch64) using readline 5.2
Hardware: Raspberry Pi 4 4GB

The issue you are facing:
The server cannot connect to MySQL (mariadb). When I use occ command, it gives an error message MySQL server has gone away

I’ve migrated the system from OpenSuse Leap 15.1 running on Raspberry Pi 3B+.


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

Steps to replicate it:

  1. Migrate the Nextcloud server to a new machine
  2. Setup mariadb

The error when I try to run occ I get the error:

> sudo -u www-data php /var/www/nextcloud/occ

An unhandled exception has been thrown:
Doctrine\DBAL\DBALException: Failed to connect to the database: An exception occurred in driver: SQLSTATE[HY000] [2006] MySQL server has gone away in /var/www/nextcloud/lib/private/DB/Connection.php:64
Stack trace:
#0 /var/www/nextcloud/3rdparty/doctrine/dbal/lib/Doctrine/DBAL/Connection.php(420): OC\DB\Connection->connect()
#1 /var/www/nextcloud/3rdparty/doctrine/dbal/lib/Doctrine/DBAL/Connection.php(380): Doctrine\DBAL\Connection->getDatabasePlatformVersion()
#2 /var/www/nextcloud/3rdparty/doctrine/dbal/lib/Doctrine/DBAL/Connection.php(324): Doctrine\DBAL\Connection->detectDatabasePlatform()
#3 /var/www/nextcloud/3rdparty/doctrine/dbal/lib/Doctrine/DBAL/Connection.php(683): Doctrine\DBAL\Connection->getDatabasePlatform()
#4 /var/www/nextcloud/lib/private/DB/Connection.php(151): Doctrine\DBAL\Connection->setTransactionIsolation(2)
#5 /var/www/nextcloud/3rdparty/doctrine/dbal/lib/Doctrine/DBAL/DriverManager.php(195): OC\DB\Connection->__construct(Array, Object(Doctrine\DBAL\Driver\PDOMySql\Driver), Object(Doctrine\DBAL\Configuration), Object(Doctrine\Common\EventManager))
#6 /var/www/nextcloud/lib/private/DB/ConnectionFactory.php(156): Doctrine\DBAL\DriverManager::getConnection(Array, Object(Doctrine\DBAL\Configuration), Object(Doctrine\Common\EventManager))
#7 /var/www/nextcloud/lib/private/Server.php(635): OC\DB\ConnectionFactory->getConnection('mysql', Array)
#8 /var/www/nextcloud/3rdparty/pimple/pimple/src/Pimple/Container.php(118): OC\Server->OC\{closure}(Object(OC\Server))
#9 /var/www/nextcloud/lib/private/ServerContainer.php(120): Pimple\Container->offsetGet('OCP\\IDBConnecti...')
#10 /var/www/nextcloud/lib/private/AppFramework/Utility/SimpleContainer.php(161): OC\ServerContainer->query('OCP\\IDBConnecti...')
#11 /var/www/nextcloud/3rdparty/pimple/pimple/src/Pimple/Container.php(114): OC\AppFramework\Utility\SimpleContainer->OC\AppFramework\Utility\{closure}(Object(OC\Server))
#12 /var/www/nextcloud/lib/private/ServerContainer.php(120): Pimple\Container->offsetGet('DatabaseConnect...')
#13 /var/www/nextcloud/lib/private/Server.php(1554): OC\ServerContainer->query('DatabaseConnect...')
#14 /var/www/nextcloud/lib/private/Server.php(362): OC\Server->getDatabaseConnection()
#15 /var/www/nextcloud/3rdparty/pimple/pimple/src/Pimple/Container.php(118): OC\Server->OC\{closure}(Object(OC\Server))
#16 /var/www/nextcloud/lib/private/ServerContainer.php(120): Pimple\Container->offsetGet('OC\\Authenticati...')
#17 /var/www/nextcloud/lib/private/AppFramework/Utility/SimpleContainer.php(69): OC\ServerContainer->query('OC\\Authenticati...', true)
#18 /var/www/nextcloud/lib/private/AppFramework/Utility/SimpleContainer.php(99): OC\AppFramework\Utility\SimpleContainer->buildClass(Object(ReflectionClass))
#19 /var/www/nextcloud/lib/private/AppFramework/Utility/SimpleContainer.php(114): OC\AppFramework\Utility\SimpleContainer->resolve('OC\\Authenticati...')
#20 /var/www/nextcloud/lib/private/ServerContainer.php(145): OC\AppFramework\Utility\SimpleContainer->query('OC\\Authenticati...', true)
#21 /var/www/nextcloud/lib/private/AppFramework/Utility/SimpleContainer.php(69): OC\ServerContainer->query('OC\\Authenticati...', true)
#22 /var/www/nextcloud/lib/private/AppFramework/Utility/SimpleContainer.php(99): OC\AppFramework\Utility\SimpleContainer->buildClass(Object(ReflectionClass))
#23 /var/www/nextcloud/lib/private/AppFramework/Utility/SimpleContainer.php(114): OC\AppFramework\Utility\SimpleContainer->resolve('OC\\Authenticati...')
#24 /var/www/nextcloud/lib/private/ServerContainer.php(145): OC\AppFramework\Utility\SimpleContainer->query('OC\\Authenticati...', true)
#25 /var/www/nextcloud/lib/private/AppFramework/Utility/SimpleContainer.php(161): OC\ServerContainer->query('OC\\Authenticati...')
#26 /var/www/nextcloud/3rdparty/pimple/pimple/src/Pimple/Container.php(114): OC\AppFramework\Utility\SimpleContainer->OC\AppFramework\Utility\{closure}(Object(OC\Server))
#27 /var/www/nextcloud/lib/private/ServerContainer.php(120): Pimple\Container->offsetGet('OC\\Authenticati...')
#28 /var/www/nextcloud/lib/private/Server.php(374): OC\ServerContainer->query('OC\\Authenticati...')
#29 /var/www/nextcloud/3rdparty/pimple/pimple/src/Pimple/Container.php(118): OC\Server->OC\{closure}(Object(OC\Server))
#30 /var/www/nextcloud/lib/private/ServerContainer.php(120): Pimple\Container->offsetGet('OC\\User\\Session')
#31 /var/www/nextcloud/lib/private/AppFramework/Utility/SimpleContainer.php(161): OC\ServerContainer->query('OC\\User\\Session')
#32 /var/www/nextcloud/3rdparty/pimple/pimple/src/Pimple/Container.php(114): OC\AppFramework\Utility\SimpleContainer->OC\AppFramework\Utility\{closure}(Object(OC\Server))
#33 /var/www/nextcloud/lib/private/ServerContainer.php(120): Pimple\Container->offsetGet('UserSession')
#34 /var/www/nextcloud/lib/private/Server.php(1430): OC\ServerContainer->query('UserSession')
#35 /var/www/nextcloud/lib/private/Server.php(688): OC\Server->getUserSession()
#36 /var/www/nextcloud/3rdparty/pimple/pimple/src/Pimple/Container.php(118): OC\Server->OC\{closure}(Object(OC\Server))
#37 /var/www/nextcloud/lib/private/ServerContainer.php(120): Pimple\Container->offsetGet('OC\\App\\AppManag...')
#38 /var/www/nextcloud/lib/private/AppFramework/Utility/SimpleContainer.php(161): OC\ServerContainer->query('OC\\App\\AppManag...')
#39 /var/www/nextcloud/3rdparty/pimple/pimple/src/Pimple/Container.php(114): OC\AppFramework\Utility\SimpleContainer->OC\AppFramework\Utility\{closure}(Object(OC\Server))
#40 /var/www/nextcloud/lib/private/ServerContainer.php(120): Pimple\Container->offsetGet('AppManager')
#41 /var/www/nextcloud/lib/private/Server.php(1725): OC\ServerContainer->query('AppManager')
#42 /var/www/nextcloud/lib/private/legacy/app.php(346): OC\Server->getAppManager()
#43 /var/www/nextcloud/lib/private/legacy/app.php(114): OC_App::getEnabledApps()
#44 /var/www/nextcloud/lib/base.php(640): OC_App::loadApps(Array)
#45 /var/www/nextcloud/lib/base.php(1078): OC::init()
#46 /var/www/nextcloud/console.php(46): require_once('/var/www/nextcl...')
#47 /var/www/nextcloud/occ(11): require_once('/var/www/nextcl...')
#48 {main}

mariadb conf file my.cnf

[client]
default-character-set = utf8mb4
port = 3306
socket = /run/mysqld/mysqld.sock

[mysqld_safe]
log_error=/var/log/mysql/mysql_error.log
nice = 0
socket = /run/mysqld/mysqld.sock

[mysqld]
basedir = /usr
bind-address = 127.0.0.1
port = 3306
pid-file = /run/mysqld/mysqld.pid
socket = /run/mysqld/mysqld.sock

binlog_format = mixed
bulk_insert_buffer_size = 4M
character-set-server = utf8mb4
collation-server = utf8mb4_general_ci
concurrent_insert = 2
connect_timeout = 5
datadir = /var/lib/mysql
default_storage_engine = InnoDB
default_authentication_plugin=mysql_native_password
expire_logs_days = 10
general_log_file = /var/log/mysql/mysql.log
general_log = 0
innodb_buffer_pool_size = 128M
innodb_buffer_pool_instances = 1
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 16M
innodb_max_dirty_pages_pct = 90
innodb_file_per_table = 1
innodb_open_files = 400
innodb_io_capacity = 4000
innodb_flush_method = O_DIRECT
key_buffer_size = 128M
lc_messages_dir = /usr/share/mysql
lc_messages = en_US
log_bin = /var/log/mysql/mariadb-bin
log_bin_index = /var/log/mysql/mariadb-bin.index
log_error=/var/log/mysql/mysql_error.log
log_slow_verbosity = query_plan
log_warnings = 2
long_query_time = 1
max_allowed_packet = 64M
max_binlog_size = 100M
max_connections = 200
max_heap_table_size = 32M
myisam_recover_options = BACKUP
myisam_sort_buffer_size = 64M
query_cache_limit = 2M
query_cache_size = 32M
query_cache_type = 1
query_cache_min_res_unit = 2k
read_buffer_size = 2M
read_rnd_buffer_size = 1M
skip-external-locking
skip-name-resolve
slow_query_log_file = /var/log/mysql/mariadb-slow.log
slow-query-log = 1
sort_buffer_size = 4M
table_open_cache = 400
thread_cache_size = 128
tmp_table_size = 32M
tmpdir = /tmp
transaction_isolation = READ-COMMITTED
user = mysql
wait_timeout = 600
[mysqldump]
max_allowed_packet = 64M
quick
quote-names
[isamchk]
!includedir /etc/mysql/conf.d/
key_buffer = 8M

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

<?php
$CONFIG = array (
  'instanceid' => '',
  'passwordsalt' => '',
  'secret' => '',
  'trusted_domains' => 
  array (
    0 => '',
  ),
  'datadirectory' => '',
  'overwrite.cli.url' => '',
  'version' => '17.0.2.1',
  'dbtype' => 'mysql',
  'dbname' => 'nextcloud',
  'dbhost' => '127.0.0.1:3306',
  'dbport' => '',
  'dbtableprefix' => 'oc_',
  'dbuser' => '',
  'dbpassword' => '',
  'mysql.utf8mb4' => true,
  'default_language' => 'en',
  'default_locale' => 'en_US',
  'defaultapp' => 'files',
  'knowledgebase' => 'true',
  'allow_user_to_change_display_name' => true,
  'installed' => true,
  'maintenance' => false,
  'loglevel' => 2,
  'updater.release.channel' => 'stable',
  'theme' => '',
  'overwritehost' => '',
  'overwriteprotocol' => 'https',
  'overwritewebroot' => '/',
  'htaccess.RewriteBase' => '/',
  'proxy' => '',
  'trusted_proxies' => 
  array (
    0 => '127.0.0.1',
  ),
  'login_form_autocomplete' => false,
  'filelocking.enabled' => true,
  'memcache.distributed' => '\\OC\\Memcache\\Redis',
  'memcache.locking' => '\\OC\\Memcache\\Redis',
  'memcache.local' => '\\OC\\Memcache\\Redis',
  'redis' => 
  array (
    'host' => '127.0.0.1',
    'port' => 6379,
    'timeout' => 0,
  ),
  'twofactor_enforced' => 'true',
  'twofactor_enforced_groups' => 
  array (
  ),
  'twofactor_enforced_excluded_groups' => 
  array (
  ),
  'ldapIgnoreNamingRules' => false,
  'ldapProviderFactory' => 'OCA\\User_LDAP\\LDAPProviderFactory',
  'app_install_overwrite' => 
  array (
    0 => 'calendar',
  ),
  'data-fingerprint' => '',
  'appcodechecker' => true,
);

Output of mysqladmin status

Uptime: 923  Threads: 8  Questions: 60  Slow queries: 0  Opens: 32  Flush tables: 1  Open tables: 26  Queries per second avg: 0.065

Listening port query

MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE 'PORT';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port          | 3306  |
+---------------+-------+
1 row in set (0.003 sec)


The output of your Apache/nginx/system log in /var/log/domain-error.log:

[Tue Jan 14 21:12:58.787666 2020] [proxy_fcgi:error] [pid 1363:tid 281472910430608] [client 107.152.104.215:63012] AH01079: failed to make connection to backend: httpd-UDS
[Wed Jan 15 00:03:54.561331 2020] [proxy:error] [pid 1546:tid 281473117618576] (2)No such file or directory: AH02454: FCGI: attempt to connect to Unix domain socket /run/phpfpm.sock (localhost) failed
[Wed Jan 15 00:03:54.564389 2020] [proxy_fcgi:error] [pid 1546:tid 281473117618576] [client 107.152.104.215:32336] AH01079: failed to make connection to backend: httpd-UDS
[Wed Jan 15 01:18:50.534498 2020] [proxy:error] [pid 1537:tid 281473398391184] (2)No such file or directory: AH02454: FCGI: attempt to connect to Unix domain socket /run/phpfpm.sock (localhost) failed
[Wed Jan 15 01:18:50.538022 2020] [proxy_fcgi:error] [pid 1537:tid 281473398391184] [client 107.152.104.215:15942] AH01079: failed to make connection to backend: httpd-UDS

Solved it was my config file.

1 Like

What in your config file??? Had it occurred to you that someone else might get the same issue and want a little more info than “it was my config file”?

I’d say it was dbhost and dport