Could not connect to the database.

Could not connect to the database.

by 力苦 time -
Number of replies: 6

I encountered a database connection error when using Docker to deploy Moodle. While I can manually connect to the database through the command line, Moodle fails to connect automatically when it starts.

Operating System: DSM 7.2.1-69057 Update 4

Docker Version: 20.10.23, build 876964a

Moodle Version: 4.4.2 (Build: 20240812)

MariaDB Version: 11.5.2

I used the Docker image provided by Bitnami to deploy Moodle. After manually entering the Moodle container, I can successfully connect to the MariaDB database using the following command:

mariadb -h IP -u moodle -pmoodlepass moodle

However, when I start the container, Moodle reports that it is unable to connect to the database. The following error message appears in the container logs:  [ERROR] Could not connect to the database.

I have checked the config.php file to confirm that the database host, username, password, and database name are correct. I have also ensured that the database user has sufficient permissions and that there are no issues with the network configuration.

I have checked the symbolic links within the container to ensure that the mysql command points to /opt/bitnami/mysql/bin/mariadb.

I tried delaying the start of the Moodle container to ensure the database service was ready, but the problem persists.

The environment variables are correctly set, and I verified these variables using docker inspect.

I want to know if there are any other configurations I might have overlooked or if there is a better way to debug this issue. Any suggestions or guidance would be greatly appreciated.

Is there a way to see the response from the database when Moodle tries to connect?

Average of ratings: -
In reply to 力苦 time

Re: Could not connect to the database.

by Ken Task -
Picture of Particularly helpful Moodlers

Ia this for development or production?
 
https://hub.docker.com/r/bitnami/moodle
Warning: This quick setup is only intended for development environments. You are encouraged to change the insecure default credentials and check out the available configuration options in the Environment Variables section for a more secure deployment.

I don't use bitnami nor docker, but assume linux.

Native distro logs are normally in /var/log/ so check that directory out via shell.  But with Bitnami, might be in /opt/bitnami/

find / -name \*.log

Good luck!

'SoS', Ken

In reply to 力苦 time

Re: Could not connect to the database.

by Howard Miller -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
(Once again) I look at the docs page for this and think "what are they doing?". Why provide all those environment variables for stuff you can set up in Moodle's config file.

Is there a particular reason you are using Bitnami? Our problem is that we have no idea what they have done. It's not that hard to get Moodle running in Docker. There is quite a long discussion in another thread at the moment. But basically, it just needs a straightforward LAMP stack.
In reply to Howard Miller

回复: Re: Could not connect to the database.

by 力苦 time -
I switched to the official Moodle image, is it moodlehq/moodle-php-apache? Using this image, I was indeed able to install Moodle successfully, but I have a question: this image should include the Moodle code, right? However, despite trying for a long time, I couldn’t find the Moodle code, so I had to download the Moodle code myself to my volume and map it to the container. What is the proper way to handle this?

Additionally, I think the image should have had the tidy extension pre-installed, but I had to install it myself.

My first installation took about 30 minutes; is this normal?

I noticed that Bitnami's image uses a lot of environment variables. I assume this is because their goal is to provide a graphical interface so that users do not need to use the command line or manually modify complex configuration files. Initially, I chose their image for the convenience of a graphical installation, but it turned out to be much more complicated than manually configuring things.
In reply to 力苦 time

回复: Could not connect to the database.

by 力苦 time -
Thank you, everyone. I switched to the official image and resolved the issue. There may have been some minor problems with the Bitnami image.