How to backup and restore MySQL databases on Linux

Back up the database using the following command:

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

The parameters of the said command as follows:

[username] - A valid MySQL username.

[password] - A valid MySQL password for the user.

[database_name] - A valid Database name you want to take backup.

[dump_file.sql] - The name of backup dump file you want to generate.

Restore the backup to a local database server

The mysql command will let you take the contents of a .sql file backup, and restore it directly to a database. This is the syntax for the command:

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

Restore the backup to a remote database server

You can also use the mysql command to restore a .sql file backup to a remote MySQL server. If you have another server running MySQL, and you have the database credentials, you can define a hostname in the command by adding the -h flag to specify a hostname. This changes the syntax of the command to:

mysql –h [hostname] –u [username] –p[password] [database_name] < [dump_file.sql]

As long as you have the correct credentials and the remote server is running, you will be able to restore the database remotely.