Recovering a database when the server will not start
A broken operating system rarely means broken data. From rescue mode you can start the database briefly, dump it cleanly and move it to safety.
Can I just copy the database files?
You can, and you should, but a file copy alone is risky to restore. InnoDB and PostgreSQL data directories are tied to the exact server version and may hold transactions that were in flight when the system stopped. A copy is your safety net; a logical dump made by the database itself is what you restore from with confidence. This guide does both: first a raw copy, then a clean dump.
Step one: take a raw copy
Boot rescue mode from the console, assemble the arrays and mount the system read-only. Copy the whole data directory to another machine before you try anything that writes, so a mistake cannot make things worse.
mdadm --assemble --scan mount -r /dev/md127 /mnt du -sh /mnt/var/lib/mysql /mnt/var/lib/postgresql 2>/dev/null tar -C /mnt/var/lib -cf - mysql | zstd -T0 | ssh vault@198.51.100.9 "cat > db1-raw-$(date +%F).tar.zst"
If the database lived on a separate volume, mount that volume instead; /mnt/etc/fstab tells you where it was. A machine with a fast port sends tens of gigabytes in minutes.
Step two: start the database inside a chroot
Remount read-write, enter the installed system with chroot and start the database server by hand, without networking. You run the exact binaries that wrote the files, so versions match. Start it as the database user, never as root.
mount -o remount,rw /mnt for fs in dev proc sys run; do mount --bind /$fs /mnt/$fs; done chroot /mnt /bin/bash mkdir -p /run/mysqld && chown mysql:mysql /run/mysqld runuser -u mysql -- mariadbd --skip-networking --skip-grant-tables &
On MySQL the binary is mysqld. --skip-grant-tables lets you in without the application passwords, and --skip-networking keeps the server unreachable from outside while it runs in that state.
And for PostgreSQL?
Use pg_ctl from the installed version with the data directory of your cluster, listening only on a local socket. On Debian and Ubuntu the binaries live under /usr/lib/postgresql/<version>/bin; on Enterprise Linux under /usr/pgsql-<version>/bin or /usr/bin.
mkdir -p /run/postgresql && chown postgres:postgres /run/postgresql runuser -u postgres -- /usr/lib/postgresql/17/bin/pg_ctl -D /var/lib/postgresql/17/main \ -o "-c listen_addresses='' -c config_file=/etc/postgresql/17/main/postgresql.conf" start
Step three: dump and copy out
Dump every database to compressed files and stream them off the machine. For MariaDB and MySQL a single-transaction dump is consistent for InnoDB tables; for PostgreSQL, pg_dumpall captures roles too, and per-database custom-format dumps restore faster and selectively.
mariadb-dump --all-databases --single-transaction --routines --events | zstd -T0 > /root/all-$(date +%F).sql.zst runuser -u postgres -- pg_dumpall --globals-only > /root/pg-globals.sql runuser -u postgres -- pg_dump -Fc -d shopdb -f /root/shopdb.dump
Copy the files to another server with scp or rsync, check their size and test-restore one on the target before you consider the rescue done.
What if the database will not start?
Read its error log first; the cause is usually stated. For InnoDB tables that crash on start, MariaDB and MySQL offer recovery levels that skip increasingly more work; start at 1 and raise one step at a time, dumping as soon as the server stays up.
| Setting | Effect | Use |
|---|---|---|
innodb_force_recovery=1 | Ignores corrupt pages it finds | First attempt |
innodb_force_recovery=3 | Skips transaction rollback | Crashes during rollback |
innodb_force_recovery=4 and above | Skips more internal work, read-only in effect | Last resort, dump immediately |
PostgreSQL has no equivalent switch; if the cluster refuses to start because of damaged WAL, copy the raw directory and restore from your last backup or replica. The Ship database dumps to a second server guide sets up the regular dumps that make this situation far less stressful.
How do I finish the session?
Stop the database cleanly inside the chroot (mariadb-admin shutdown or pg_ctl stop), leave the chroot, unmount and decide what comes next. Often the best path is a console reinstall followed by a restore of the dump, which leaves you with a clean system and verified data. The Rescue mode walkthrough guide covers repairing the system in place instead, and Database servers lists machines that suit a database rebuild.
Related pages
Rescue mode walkthrough
A full rescue session from start to finish: boot the network system, find and mount your drives, repair, chroot in and return to disk boot.
Read moreShip database dumps to a second server
Schedule compressed, encrypted MariaDB or PostgreSQL dumps and send them to a Storage machine elsewhere, with retention and restore tests.
Read moreDatabase servers
NVMe machines with large memory for PostgreSQL, MySQL, MariaDB and MongoDB, from a single primary to replicated pairs.
Read moreIPMI and rescue mode
What out-of-band management is, and how console power control, reinstall and rescue boot cover the same needs on our machines.
Read moreQuestions and answers
Can I recover a database if the operating system is broken?
Usually yes. Boot rescue mode, chroot into the installed system, start the database by hand and dump it.
Why not just copy /var/lib/mysql?
A raw copy is version-specific and may contain unfinished transactions. Keep it as a safety net and restore from a logical dump.
Is --skip-grant-tables safe here?
Yes, combined with --skip-networking. The server is reachable only through the local socket during the rescue.
What does innodb_force_recovery do?
It lets MariaDB or MySQL start despite damaged InnoDB data by skipping recovery steps. Raise it one level at a time and dump immediately.
Where should I copy the dump?
To another server, such as a Storage machine in a different data centre, and test a restore there.