NC 21.0.4.1 MariaDB 10.3.31 Upgrade from PHP 7.4 to 8.0: Failed to connect to the database: An exception occurred in the driver: SQLSTATE[HY000] [2006] MySQL server has gone away in /var/www/html/nextcloud/lib/private/DB/Connection.php:85

Dear community,

My server hosts Nextcloud version 21.0.4.1 and has been upgraded to Ubuntu 20.04.3 LTS. In the course of this update, PHP 7.4 got replaced by PHP 8.0. After this, Nextcloud access to the database (MariaDB 10.3.31) was broken. We run Apache2 so that I accessed the Apache2 error log to identify a reason for this effect:

AH01071: Got error ‘PHP message: PHP Fatal error: Uncaught Doctrine\DBAL\Exception: Failed to connect to the database: An exception occurred in the driver: SQLSTATE[HY000] [2006] MySQL server has gone away in /var/www/html/nextcloud/lib/private/DB/Connection.php:85\nStack trace:\n#0
/var/www/html/nextcloud/3rdparty/doctrine/dbal/src/Connection.php(1486): OC\DB\Connection->connect()\n#1 /var/www/html/nextcloud/3rdparty/doctrine/dbal/src/Connection.php(1014): Doctrine\DBAL\Connection->getWrappedConnection()\n#2
/var/www/html/nextcloud/lib/private/DB/Connection.php(226): Doctrine\DBAL\Connection->executeQuery()\n#3
/var/www/html/nextcloud/3rdparty/doctrine/dbal/src/Query/QueryBuilder.php(210): OC\DB\Connection->executeQuery()\n#4
/var/www/html/nextcloud/lib/private/DB/QueryBuilder/QueryBuilder.php(287): Doctrine\DBAL\Query\QueryBuilder->execute()\n#5
/var/www/html/nextcloud/lib/private/AppConfig.php(345): OC\DB\QueryBuilder\QueryBuilder->execute()\n#6
/var/www/html/nextcloud/lib/private/AppConfig.php(110): OC\AppConfig->loadCo…’

Nextcloud version (eg, 20.0.5): 21.0.1.4
Operating system and version (eg, Ubuntu 20.04): Ubuntu 20.04.3 LTS
Apache or nginx version (eg, Apache 2.4.25): Apache 2.4.49
PHP version (eg, 7.4): 8.0

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

The output of your Nextcloud log in Admin > Logging:

The error is such severe that only the Apache error log, but not the Nextcloud log is further populated.

Worth to note, when I connect to the Nextcloud database via phpMyAdmin that is installed at the same server, I can access the Nextcloud database.

I have already purged the entire php stuff and re-installed php8.0* packages and re-activated the php8.0-fpm module at Apache2. No change. I can still access the Nextcloud database via phpMyAdmin, but not via the Nextcloud code.

Best regards,
Peter

For testing purposes, I created a PHP page on the same server to access the Nextcloud database. When I open the connection in the following way, I also receive the same error:

try {
$conn = new PDO(“mysql:host=127.0.0.1:3306;dbname=nextcloud”, $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo “Connected successfully”;
}

However, when I change the above code as follows, I can successfully connect via pdo_mysql to the Nextcloud database:

try {
$conn = new PDO(“mysql:host=localhost;dbname=nextcloud;port=3306”, $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo “Connected successfully”;
}

In earlier versions, I needed to leave the port field empty and to put the port number (3306) after the host IP, e.g. 127.0.0.1:3306. However, when I further maintain this style, then I get the connection error.

When changing the Nextcloud configuration from

‘dbhost’ => ‘127.0.0.1:3306’,
‘dbport’ => ‘’,

to

‘dbhost’ => ‘localhost’,
‘dbport’ => ‘3306’,

I overcome this issue.

Best regards,
Peter

the difference is that when you use localhost, the unix socket connector is used. whereas, when you use 127.0.0.1, TCP/IP connector is used.

So

doesn’t make sense.

It should be either

'dbhost' => '127.0.0.1',
'dbport' => '3306',

or

'dbhost' => 'localhost',
'dbport' => '',

Changing the dbhost to 127.0.0.1 and port to 3306 just causes my site to give the following message:

"Internal Server Error

The server encountered an internal error and was unable to complete your request.
Please contact the server administrator if this error reappears multiple times, please include the technical details below in your report.
More details can be found in the server log."