Nextcloud: failed to connect to the database

In my Nextcloud every day a shell script is generating a backup where all the volumes are safed.
Normally, when I execute my “backup-restore-file.sh” the following is happening:

  1. Delete the running container
  2. Delete the volumes
  3. Replace the docker-compose files with the files from the newest backup
  4. Start this docker-compose file
  5. Stop the new containers
  6. Delete the new volumes and replace it with them from the newest backup
  7. Start the containers

normaly all works and the backup is re-installed successfully.

but recently, when I am starting this “backup-restore” shell file this error shows up:

An unhandled exception has been thrown:
Doctrine\DBAL\DBALException: Failed to connect to the database: An exception occurred in driver: SQLSTATE[HY000] [2002] Connection timed out in /var/www/html/lib/private/DB/Connection.php:64
Stack trace:
.
.
.
.

Does anyone know why?

Hi,

Would be interesting which command is producing this error. Can you identify at which step in the script this error message comes up; what script code is producing this?

Maybe something changed with the docker containers now resulting in that error message, meaning that the compose files need some changes?

The most obvious is, that Nextcloud cannot establish a connection to the database. The big question is only why. Is the error message even longer and probably give some more hints?

Hi,

the error is produced when the script executes the following command (turns the maintenance-mode off):
docker exec -it docker_app_1 su -s /bin/bash “www-data” -c “php occ maintenance:mode --off”

because this is the first command which wants to change something on the running Nextcloud - but Nextcloud is not running.

this is my script:


#JUST STARTING THE PROCESS IF THERE IS A BACKUP TO RESTORE

cd /var/lib/nextcloud-backup/backup/
lastbackup_1=$(ls -Art tail -n 1)

if [ -z “$lastbackup_1” ]; then

echo “THERE IS NO BACKUP TO RESTORE”

else

#STOP AND DELETE THE RUNNING NEXTCLOUD

#STOP THE RUNNING CONTAINERS

docker stop docker_db_1
docker stop docker_redis_1
docker stop docker_cron_1
docker stop docker_app_1

#REMOVE THE RUNNING CONTAINERS

docker rm docker_db_1
docker rm docker_redis_1
docker rm docker_cron_1
docker rm docker_app_1

#DELETE THE VOLUMES

cd /var/lib/docker/volumes/
rm -r docker_nextcloud
rm -r docker_nextcloud_db

#GENERATE NEW EMPTY FOLDERS FOR THE NEW VOLUMES

mkdir -p docker_nextcloud/_data
mkdir -p docker_nextcloud_db/_data

#DELTE THE DOCKER-COMPOSE FILE

cd /home/ubuntu/anivo/docker/
rm -r nextcloud.yml
rm -r db.env

#SETUP THE NEW NEXTCLOUD WITH THE OLD BACKUP

#SEARCH THE NEWEST BACKUP

cd /var/lib/nextcloud-backup/backup/
lastbackup=$(ls -Art tail -n 1)

#COPY THE DOCKER-COMPOSE FILE FROM THE NEWEST BACKUP BACK WHERE IT USED TO BE

cp /var/lib/nextcloud-backup/backup/“${lastbackup}”/config.tar.gz /home/ubuntu/anivo/docker/

#UNPACK THE .tar.zip FOLDER WITH THE DOCKER-COMPOSE FILE AND REMOVE THE DOCKER-COMPOSE FILE

cd /home/ubuntu/anivo/docker/
tar xfvz config.tar.gz
rm -r config.tar.gz

#START THE DOCKER-COMPOSE

docker-compose -f nextcloud.yml up -d

#STOP THE RUNNING CONTAINERS

docker stop docker_db_1 docker_redis_1 docker_cron_1 docker_app_1

#REMOVE THE NEW GENERATED VOLUMES

cd /var/lib/docker/volumes/
rm -r docker_nextcloud
rm -r docker_nextcloud_db

#COPY THE VOLUMES FROM THE NEWEST BACKUP TO /var/lib/ (DEPENDS ON THE DOCKER-COMPOSE FILE), UNPACK THE FOLDERS AND REMOVE THE .tar.zip FOLDERS

cp /var/lib/nextcloud-backup/backup/“${lastbackup}”/docker_nextcloud_backup.tar.gz /var/lib/docker/volumes/

cp /var/lib/nextcloud-backup/backup/“${lastbackup}”/docker_nextcloud_db_backup.tar.gz /var/lib/docker/volumes/

tar xfvz docker_nextcloud_backup.tar.gz
tar xfvz docker_nextcloud_db_backup.tar.gz

rm -r docker_nextcloud_backup.tar.gz
rm -r docker_nextcloud_db_backup.tar.gz

#START THE DOCKER CONTAINERS AGAIN - NOW THEY USE THE VOLUMES FROM THE BACKUP

docker start docker_db_1 docker_redis_1 docker_cron_1 docker_app_1

#IN THE BACKUP THE MAINTENANCE-MODE IS ENABLED - NOW YOU NEED TO TURN IT OFF

docker exec -it docker_app_1 su -s /bin/bash “www-data” -c “php occ maintenance:mode --off”


Overall I am setting up a new Nextcloud where the new generated volumes are replaced with them from the backup.

This file is executed via a cronjob - mybee there are some differences to when I am executing this file manually with ./… .

However, I just found this question:

seems like this guy has the same problem than me, but in my case it worked before - why should now the php version be the problem.

you are sure your backup is OK? is the db up&running?

docker logs docker_db_1

nicer:

docker exec --user www-data docker_app_1 php occ maintenance:mode --off

(but unrelated to your problem.)

yeah, i used this 10-20 times. I had this error a time ago, but I just set up a new Nextcloud, took a backup and it was no problem to reinstall it.

Now after it is happening the second time I started wondering whats wrong.

so. did you check the logs of the container?