Skip to main content

How to recover MySQL databases from MySQL data files in VPS

Recovering MySQL databases from MySQL data files /var/lib/mysql

Updated over 5 months ago

There might be a situation when your VPS cannot be started. If you are trying to recover MySQL databases from VPS Recovery Mode or VPS backup files, follow these steps:

Step 1 - Install and stop MySQL server

Connect to your VPS via SSH and install MySQL server by running this command:

yum install mysql-server

It's recommended to use the same version of MySQL. However, using a newer version is also possible.

After the MySQL server is installed, it's time to stop it to add to the data folders:

service mysql stop

Step 2 - Copy the data

Use the following command to copy your old MySQL data to the new MySQL server data files' location:

cp -rf /mnt/var/lib/mysql/* /var/lib/mysql

After the command is executed, verify that the files are owned by the MySQL user. by changing the files' ownership:

chown -R mysql:mysql /var/lib/mysql

When all is good to go, start your MySQL server:

service mysql start

Step 3 - Final checks and export

Check and repair all the database tables to ensure no data is corrupted:

mysqlcheck -rA

NOTE

  • Depending on the version of MySQL you are running, you should run MYSQLCHECK

Lastly, export the databases:

mysqldump --all-databases > all_databases.sql

That's it. Now, your databases have been fully recovered!

Did this answer your question?