Nextcloud Database Connection Lost — Complete Guide
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.1instead oflocalhostinconfig.phpfor predictable TCP connections. - Set MySQL's
bind-address = 127.0.0.1for security. - Monitor database connections with
SHOW PROCESSLIST;. - Set
wait_timeoutandmax_connectionsappropriately 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
- Mixing let bindings with <- bindings in do notation, producing type errors
- Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
- 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
DodaTech — keep your Nextcloud database connected.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro