Monday, September 1, 2014

Mysqldump commands for Backup & Restore Mysql Databases

12:47 PM

Mysqldump used to dump a database or a collection of databases for backup or transfer to
another SQL server (not necessarily a MySQL server). The dump typically contains SQL
statements to create the table, populate it, or both.  




Mysqldump can create a copy of current databases which will act as backup. It is very
important to take a backup of mysql databases,to face situation such as
corrupted database , data lose, server failure etc.

Syntax

root@linuxstorages:~# mysqldump -u [username] -p[password] -h [Remote Server] databasename > /path/backup_database.sql

U - Mysql Database user name

P - Mysql Database user password

H - Remote Server

Databasename - database name available in Mysql database


In three way we can use mysqldump commands to backup database

Mysqldump for single database :

root@linuxstorages:~#mysqldump -u root -ppassword123 linuxstorages >
/backup/linuxstorages.sql

Mysqldump for Multiple databases :

root@linuxstorages:~# mysqldump -u root -ppassword123 --databases testdb1 testdb2 > /backup/testdb1-testdb2.sql

Mysqldump for All databases :

root@linuxstorages:~# mysqldump -u root -ppassword123 --all-databases > /backup/all-databases.sql

Mysqldump on Remote Server :

root@linuxstorages:~# mysqldump -u root -ppassword@123 -h 10.5.1.3 linuxstorages > /backup/linuxstorages.sql

Mysqldump table in database :

Single Table:

root@linuxstorages:~# mysqldump -u root -ppassword123 [databasename] [tablename] > /backup/single-table.sql

Multiple Tables:

root@linuxstorages:~# mysqldump -u root -ppassword123 databasename table-1 table-2 table-3 > /backup/Multiple-table.sql

Use mysqldump --help for multiple option which can be used.

root@linuxstorages:~# mysqldump --help

Restore Mysql Database

Syntax:

root@linuxstorages:~# mysql -u [username] -p[password] databasename < /path/backup_database.sql

First create a empty new database

root@linuxstorages:~# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 454941
Server version: 5.5.31-0ubuntu0.12.04.1 (Ubuntu)

Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>show databases;

mysql>create database linuxstorages;
Query OK, 1 row affected (0.03 sec)   

exit

Now import the backup .sql file

root@linuxstorages:~# mysql -u root -p password linuxstorages < /path/linuxstorages.sql


Other best way to import database is

root@linuxstorages:~# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 454941
Server version: 5.5.31-0ubuntu0.12.04.1 (Ubuntu)

Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>show databases;



mysql>use linuxstorages;
Database changed

mysql>source /path/linuxstorages.sql;

Written by

2 comments :

  1. can you help me to set something to backup all my databases every 4 hours and then delete the older ones automatically ?

    ReplyDelete
  2. We can write script for that process.

    ReplyDelete

 

© 2014 Linux Storages | Updated . All rights resevered. Designed by Templateism