Moodle 4.2 DataBase Connection Error

Moodle 4.2 DataBase Connection Error

by 瑞辰 白 -
Number of replies: 6
After upgraded from Moodle 4.1+MariaDB 10.5 to Moodle 4.2+ MariaDB 10.11, moodle cannot access the database and have following debug info:

Error: Database connection failed

It is possible that the database is overloaded or otherwise not running properly.

The site administrator should also check that the database details have been correctly specified in config.php

Debug info: Permission denied
Error code: dbconnectionfailed
Stack trace:
  • line 587 of /lib/dml/mysqli_native_moodle_database.php: dml_connection_exception thrown
  • line 230 of /lib/dml/moodle_read_slave_trait.php: call to mysqli_native_moodle_database->raw_connect()
  • line 215 of /lib/dml/moodle_read_slave_trait.php: call to mysqli_native_moodle_database->set_dbhwrite()
  • line 340 of /lib/dmllib.php: call to mysqli_native_moodle_database->connect()
  • line 650 of /lib/setup.php: call to setup_DB()
  • line 32 of /config.php: call to require_once()
  • line 30 of /index.php: call to require_once()
I've checked config.php, grants for database user and php modules. They all work great, and I don't know what else could have any problem. Does anyone have any clues?
Average of ratings: -
In reply to 瑞辰 白

Re: Moodle 4.2 DataBase Connection Error

by Davor Budimir -
Create test PHP file, place it in the Moodle root folder to test the connection to see if its Moodle or the database.
<?php

$mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname, $dbport);

if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error);
}

echo 'Success... ' . $mysqli->host_info . "\n";

$mysqli->close();
In reply to Davor Budimir

回复: Re: Moodle 4.2 DataBase Connection Error

by 瑞辰 白 -
it is rendered as a blank page. Seems to be a database problem.
In reply to 瑞辰 白

Re: 回复: Re: Moodle 4.2 DataBase Connection Error

by Leon Stringer -
Picture of Core developers Picture of Particularly helpful Moodlers

I recently set up Moodle 4.2 with MariaDB 10.11.3 on CentOS Stream 9. Initially I had the message Error: Database connection failed but eventually fixed it.

I had to try lots of steps for this but I think what was needed was:

  1. dnf install mysql-selinux
  2. Reboot server
  3. Change $CFG->dbhost from 'localhost' to '127.0.0.1' in config.php.

Presumably steps 1 and 2 are only needed if SELinux is enforcing on the server. I'm not sure why step 3 was needed, I've never needed to do this before.

In reply to Leon Stringer

Re: 回复: Re: Moodle 4.2 DataBase Connection Error

by Visvanath Ratnaweera -
Picture of Particularly helpful Moodlers Picture of Translators
Could these "desirable" lines in /etc/hosts influencing the behaviour:

127.0.0.1       localhost
127.0.1.1 HOSTNAME
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

In reply to Visvanath Ratnaweera

Re: 回复: Re: Moodle 4.2 DataBase Connection Error

by Leon Stringer -
Picture of Core developers Picture of Particularly helpful Moodlers

Visvanath: Possibly, but I've never needed to change $CFG->dbhost before. For what it's worth my /etc/hosts is:

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
In reply to Leon Stringer

Re: 回复: Re: Moodle 4.2 DataBase Connection Error

by Visvanath Ratnaweera -
Picture of Particularly helpful Moodlers Picture of Translators
Hi Leon

No, I didn't mean to break the tradition by changing $CFG->dbhost from 'localhost' to '127.0.0.1'. Depending on the OS the hostname localhost is not known to the resolver. Yes, as in your case, most of the Linux distribution do this with the line "127.0.0.1 localhost" in /etc/hosts.