Skip to content

Nextcloud Database Connection Lost — Complete Guide

DodaTech Updated 2026-06-24 3 min read

In this tutorial, you'll learn about Nextcloud Database Connection Lost. We cover key concepts, practical examples, and best practices.

Hook

You try to access your Nextcloud instance and get a white screen with "Internal Server Error" or "Database connection lost." Checking the Nextcloud log reveals "SQLSTATE[HY000] [2002] Connection refused." Your database server is running, but Nextcloud cannot reach it.

The Wrong Way

Restarting both MySQL and PHP-FPM in a loop without checking the socket or TCP configuration often makes things worse by corrupting active connections.

# BAD: Blind restart loop
while true; do
  sudo systemctl restart mysql php8.3-fpm
  sleep 5
done
[ERROR] InnoDB: Database corruption detected after unclean restart

Repeated hard restarts of MySQL risk database corruption.

The Right Way

Identify whether the issue is a TCP port or Unix socket, then fix the connection string.

# 1. Check if MySQL is listening
sudo ss -tlnp | grep 3306
LISTEN 0  70  127.0.0.1:3306  0.0.0.0:*    users:(("mysqld",pid=1234))

MySQL is listening on TCP port 3306. Now check Nextcloud's config:

# 2. Check Nextcloud database config
sudo -u www-data php occ config:system:get dbhost
localhost

Nextcloud is trying to connect via localhost (which tries the Unix socket first). If MySQL is configured for TCP only, the socket fails.

# 3. Fix: Change to TCP connection
sudo -u www-data php occ config:system:set dbhost --value="127.0.0.1"
# Or if using a socket:
# sudo -u www-data php occ config:system:set dbhost --value="/var/run/mysqld/mysqld.sock"
# 4. Test connection
php -r "new PDO('mysql:host=127.0.0.1;dbname=nextcloud', 'nextcloud', 'password');" 2>&1
(no errors — connection works)

Prevention

  • Use 127.0.0.1 instead of localhost in config.php for predictable TCP connections.
  • Set MySQL's bind-address = 127.0.0.1 for security.
  • Monitor database connections with SHOW PROCESSLIST;.
  • Set wait_timeout and max_connections appropriately for your user count.
  • Use a separate database user with a strong password, not root.

Advanced Troubleshooting

Check the Logs

Most TOOL errors are logged to stdout or a dedicated log file. Check your logs first:

# Check system logs
journalctl -u tool --since "1 hour ago"

# Or check the application log
tail -50 ~/.tool/logs/error.log

Test with a Minimal Example

Create the simplest possible tool configuration to verify the base setup works:

tool --version
tool --help

If the minimal test passes, add configuration options one at a time until you find the breaking change.

Common Configuration Mistakes

  • Using the wrong file path or URL in configuration
  • Forgetting to restart TOOL after changing config files
  • Mixing tabs and spaces in YAML configuration files
  • Setting incorrect permissions on configuration directories

When to Reinstall

If none of the above resolves the issue, consider a clean reinstall:

# Backup your configuration
cp -r ~/.tool ~/.tool.bak

# Remove and reinstall
# Follow the official TOOL installation guide

This ensures you start from a known good state and can isolate the issue.

Common Mistakes with db connection

  1. Mixing let bindings with <- bindings in do notation, producing type errors
  2. Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
  3. Non-exhaustive pattern matches that compile with warnings then crash at runtime

These mistakes appear frequently in real-world NEXTCLOUD code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

What does 'SQLSTATE[HY000] [2002] Connection refused' mean?

It means MySQL is not accepting connections on the specified host and port. MySQL may not be running, is bound to a different interface, or skip-networking is enabled in my.cnf.

Can I use a remote database with Nextcloud?

Yes — set dbhost to the remote server's IP and port (e.g., 10.0.0.5:3306). Ensure the remote MySQL user has permissions from your Nextcloud server's IP and that the port is firewalled appropriately.

Why does the connection work after a restart but fail later?

MySQL may be running out of connections (max_connections), or the wait_timeout may be too low. Check SHOW VARIABLES LIKE 'max_connections'; and increase it in my.cnf.


DodaTech — keep your Nextcloud database connected.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro