How to make Back Up and Restore from backup in MySQL Database.

In this tutorial i will show you easy ways to backup and restore the data in your MySQL database.

Back up From the Command Line: 

Here is the proper  syntax to backup in command>

$ mysqldump --opt -u [uname] -p[pass] [dbname] > [backupfile.sql]

Here:
  1. [uname] Your database username
  2. [pass] The password for your database (note there is no space between -p and the password)
  3. [dbname] The name of your database
  4. [backupfile.sql] The filename for your database backup
  5. [--opt] The mysqldump option 
For example, to backup a database named 'ranger' with the username 'root' and with password 'root' to a file ranger_backup.sql, using command:

$ mysqldump -uroot -proot ranger > ranger_backup.sql
 
Above command store your back file in home directory. If you want to backup in specific directory then go to your desired directory and execute the above command.
 
With mysqldump command you can specify certain tables of your database you want to backup.Each table name has to be separated by space.For example:

$ mysqldump -uroot -proot ranger commission content > ranger_backup.sql
 
Here>
  1. ranger is the databasename
  2. commission and  content are table names separated by space.
Sometimes it is necessary to back up more that one database at once.Each database name has to be separated by space.For example:

$ mysqldump -uroot -proot --databases ranger student erp > ranger_student_erp_backup.sql

  1. ranger,student and erp are databases name separated by space.
If you want to back up all the databases in the server at one time you should use the --all-databases option.For example:

$ mysqldump -uroot -proot --all-databases > alldb_backup.sql 

 Back up your MySQL Database with Compress:

If your mysql database is very big, you might want to compress the output of mysqldump. Just use the mysql backup command below and pipe the output to gzip.For example:
 
$ mysqldump -u [uname] -p[pass] [dbname] | gzip -9 > [backupfile.sql.gz]

 Restoring your MySQL Database:

  1. Create an appropriately named database on the target machine
  2. Load the file using the mysql command: 
$  mysql -u [uname] -p[pass] [dbname] < [backupfile.sql] 
 
 
Enjoy.........
 
 
 


 

Comments

Popular posts from this blog

How to run ofbiz as ubuntu Service.

JPA vs Spring JPA vs Spring Data JPA vs Hibernate

Java Array Interview Questions for Entry-Level Developers Part 01