Technology Encyclopedia Home >How to backup and restore data in MariaDB?

How to backup and restore data in MariaDB?

To backup and restore data in MariaDB, you can use the mysqldump utility for creating backups and the mysql command-line client for restoring them.

Backup:

To create a backup of your MariaDB database, you can use the following command:

mysqldump -u [username] -p[password] [database_name] > backup.sql

Replace [username], [password], and [database_name] with your actual MariaDB username, password, and the name of the database you want to backup. This command will create a SQL file named backup.sql containing the structure and data of the specified database.

Restore:

To restore a backup in MariaDB, first, ensure that the database you want to restore into exists. If it doesn't, create it using the CREATE DATABASE statement. Then, use the following command to restore the backup:

mysql -u [username] -p[password] [database_name] < backup.sql

Replace [username], [password], and [database_name] with your actual MariaDB username, password, and the name of the database you want to restore the backup into. This command will execute the SQL statements in backup.sql, restoring the database structure and data.

Example:

Suppose you have a database named my_database with the username root and password mypassword. To create a backup, run:

mysqldump -u root -pmypassword my_database > my_database_backup.sql

To restore the backup to a new database named my_database_restored, first create the new database:

CREATE DATABASE my_database_restored;

Then, run the restore command:

mysql -u root -pmypassword my_database_restored < my_database_backup.sql

Regarding cloud services, Tencent Cloud offers a managed MariaDB service called TencentDB for MariaDB, which provides automated backups with configurable retention periods. You can also perform manual backups and restores through the Tencent Cloud Console or API.