Backing up and restoring MySQL and MariaDB databases using SSH
Please use the “Print” function at the bottom of the page to create a PDF.
Valid for: Web Hosting
In this article, you will learn how to back up and restore MySQL and MariaDB databases using your SSH access. To do this, you use the command line programs mysqldump and mysql, which are already installed by default in Linux web hosting packages and on managed servers.
Note
The procedures described here are also suitable for large databases with more than 1 GB (gigabyte) of data.
Requirements
You need a web hosting package with SSH access (or a managed server). For an introduction to SSH, see the article What is SSH?
You need the access data for your database (host name, database name, user name, and password). To find this data, see the article Displaying MySQL/MariaDB Database Details in Your IONOS Account.
To restore a database, the data must be available as a backup file in SQL format (SQL dump).
Basic knowledge of the Linux shell is recommended.
Backing up a database with mysqldump
The mysqldump tool creates a backup file containing SQL commands that can be used to rebuild the database structure and restore the data. You can then import this backup into a database with the MySQL client to restore the data.
To back up your database with mysqldump, complete the following steps:
Establish an SSH connection to your webspace. Then carry out the following steps in the terminal window. For instructions on establishing an SSH connection, see the article Connecting to Your Webspace via Secure Shell (SSH) using PuTTY.
To create a complete backup of your database, run the mysqldump command using the following pattern:
mysqldump --no-tablespaces --host=HOSTNAME --user=USERNAME --password=PASSWORD DATABASE_NAME > FILE
Replace the placeholders in capital letters with the access data of the database you want to back up. Also enter the name you want to give your backup file.
Example:
mysqldump --no-tablespaces --host=db1234567890.hosting-data.io --user=dbu654321 --password=2gCjmnrmLkxnSefw dbs7654321 > dbs7654321_dump_29-04-2024.sql
Your backup file is created and saved in the directory in which you ran the mysqldump command. Depending on the amount of data, this may take some time. You can then download the file and keep it in a safe place.
Please Note
Do not close the terminal window until the backup is complete. Otherwise, the process will be canceled.
Restoring a database with the MySQL client
Upload the backup file to your webspace using SFTP. For instructions, see the article Transferring Files with FileZilla Using SFTP.
Establish an SSH connection to your webspace. Then carry out the following steps in the terminal window. For instructions on establishing an SSH connection, see the article Connecting to Your Webspace via Secure Shell (SSH) using PuTTY.
If your backup file is in a directory, change to that directory.
To restore your data, run the mysql command using the following pattern:
mysql --host=HOSTNAME --user=USERNAME --password=PASSWORD DATABASE_NAME < FILENAME
Replace the placeholders in capital letters with the access data of the database in which you want to restore the data.
Example:
mysql --host=db1234567890.hosting-data.io --user=dbu654321 --password=2gCjmnrmLkxnSefw dbs7654321 < dbs7654321_dump_29-04-2024.sql
Backup file in gzip format (.gz)
If the file is compressed in gzip format, combine the mysql command with the gunzip command using the following pattern:
gunzip -c FILENAME | mysql --host=HOSTNAME --user=USERNAME --password=PASSWORD DATABASE_NAME
Example:
gunzip -c dbs7654321_dump_29-04-2024.sql.gz | mysql --host=db1234567890.hosting-data.io --user=dbu654321 --password=2gCjmnrmLkxnSefw dbs7654321
Troubleshooting
Below you will find suggested solutions for common errors that can occur when carrying out these steps.
Using special characters
If your file names, passwords, or user names contain special characters, you must enclose them in single quotation marks (') in the mysqldump or mysql command.
Example:
mysqldump [...] --password='%gCjmnrmLk!xnSefw'
Error messages: mysqldump
mysqldump: Got error: 1044: Access denied for user [...] to database [...]
This error occurs, for example, if the specified database name cannot be found.
Suggested solution: Check that the database name is correct. Note that it is case-sensitive.
mysqldump: Got error: 1045: Access denied for user [...]
This error occurs when a user tries to log in to MySQL with invalid access data (user name or password).
Suggested solution: Check that the user name and password are correct. Note that they are case-sensitive.
mysqldump: Got error: 2005: Unknown MySQL server host [...]
This error occurs when MySQL cannot establish a connection to the specified database server.
Suggested solution: Check the host name for typing errors.
Error messages: MySQL client
ERROR 1044 (42000) at line X: Access denied for user [...]
This error occurs when a user tries to perform an action they do not have permission for. Depending on how the backup file was created, it may, for example, also contain an instruction to create a database, which is not possible here but also not necessary. For what to do in this case, see the article "1044 - Access Denied" Error when Importing a MySQL/MariaDB Database.
ERROR 1045 (28000): Access denied for user [...]
This error occurs when a user tries to log in to MySQL with invalid access data (user name or password).
Suggested solution: Check that the user name and password are correct. Note that they are case-sensitive.
ERROR 2005 (HY000): Unknown MySQL server host [...]
This error occurs when MySQL cannot establish a connection to the specified database server.
Suggested solution: Check the host name for typing errors.
Ignoring MySQL errors
You can instruct the MySQL client to ignore errors with the --force parameter. This means that the import is not stopped, even if errors occur. Instead, the client tries to continue with the next commands or records.
Example:
mysql --force --host=db1234567890.hosting-data.io --user=dbu654321 --password=2gCjmnrmLkxnSefw dbs7654321 < dbs7654321_dump_29-04-2024.sql
Please Note
Because errors are ignored, using --force can lead to problems with data integrity. Some records may be incorrect or missing, which can affect the overall quality of the database. We recommend validating the data after the import to make sure that all data has been imported correctly.
Additional information
IONOS automatically creates daily backups of your MySQL and MariaDB databases. To reset a database to an earlier state without your own backup file, or to have a deleted database restored, see the following article: