To take a backup of MySQL database or databases, the database must exist in the database server and you must have access to it. The format of the command would be.
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.
How to Backup a Single MySQL Database?
To take a backup of single database, use the command as follows. The command will dump database [wordpress] structure with data on to a single dump file called wordpress.sql.
# mysqldump -u root -predhat wordpress > wordpress.sql
How to Backup All MySQL Databases?
If you want to take backup of all databases, then use the following command with option –all-database. The following command takes the backup of all databases with their structure and data into a file called all-databases.sql.
# mysqldump -u root -predhat --all-databases > all-databases.sql
How to Backup MySQL Database Structure Only?
If you only want the backup of database structure without data, then use the option –no-data in the command. The below command exports database [wordpress] Structure into a file wordpress _structure.sql.
# mysqldump -u root -predhat -–no-data wordpress > wordpress_structure.sql
How to Backup MySQL Database Data Only?
To backup database Data only without structure, then use the option –no-create-info with the command. This command takes the database [wordpress] Data into a file wordpress _data.sql.
# mysqldump -u root -predhat --no-create-db --no-create-info wordpress > wordpress_data.sql
How to Backup Single Table of Database?
With the below command you can take backup of single table or certain tables of your database. For example, the following command only take backup of wp_posts table from the database wordpress.
# mysqldump -u root -predhat wordpress wp_posts > wordpress_posts.sql
How to Backup Multiple Tables of Database?
If you want to take backup of multiple or certain tables from the database, then separate each table with space.
# mysqldump -u root -predhat wordpress wp_posts wp_comments > wordpress_posts_comments.sql
How to Backup Remote MySQL Database
The below command takes the backup of remote server [192.168.0.165] database [css4hosting] into a local server.
# mysqldump -h 192.168.0.165 -u root -predhat css4hosting > css4hosting.sql
How to Restore MySQL Database?
In the above tutorial we have seen the how to take the backup of databases, tables, structures and data only, now we will see how to restore them using following format.
# mysql -u [username] –p[password] [database_name] < [dump_file.sql]
How to Restore Single MySQL Database
To restore a database, you must create an empty database on the target machine and restore the database using msyql command. For example the following command will restore the wordpress.sql file to the wordpress database.
# mysql -u root -predhat wordpress < wordpress.sql
If you want to restore a database that already exist on targeted machine, then you will need to use the mysqlimport command.
# mysqlimport -u root -predhat wordpress < wordpress.sql
In the same way you can also restore database tables, structures and data. If you liked this article, then do share it with your friends.