How To Backup A MySQL Database With Command Line In Linux

Do you have a MySQL database that you want to backup in Linux? This article will focus on how to export a MySQL database with command line in Debian or Ubuntu but the steps should work with most Linux distros. Read on for how to export a MySQL database in Linux.

How To Export A MySQL Database With Command Line In Debian or Ubuntu

The command to export a database in mysql with command line is

mysqldump

The mysqldump command should come with any installation of mysql or mariadb The format to use it is as follows:

mysqldump -u username -p database_name > output_file.sql

For example, if you had a mysql username called ‘testuser’ and with a database named ‘sample_database’, you could backup the mysql database ‘sample_database’ with the following command.

mysqldump -u testuser -p sample_database > output_file.sql

(Note I call the output sample_database_output.sql but you could name it whatever you want.) After entering the above command, you will be prompted to enter the MySQL password for that user. The following screenshot is what the above command should look like:

An example for how to backup a mysql database in Debian or Ubuntu.
An example for how to backup a mysql database in Debian or Ubuntu.

The output file will have SQL data, but you could actually name the file whatever you want, so the file extension doesn’t have to end in .sql. It could end in .txt, or whatever you like. You can go here to see what a sample output SQL file should look like. With the link I mentioned, I outputted the database called mysql which comes in every mysql installation. Want to go further with MySQL? Read here for how to create a new database in MySQL and here for how to create a new username in MySQL. Additionally, read here for how to import a MySQL database. Did you find this article about how to backup a MySQL database with command line in Linux useful? If so, please share this article with your friends.

Leave a Reply

Your email address will not be published. Required fields are marked *