Google Plus

How to Backup and Restore MySQL Database on Fedora16, Centos and Redhat

Written by Mel Kham on . Posted in CentOS, Fedora, Linux tutorials

The mysqldump client is a backup program originally written by Igor Romanenko. It can be 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. However, mysqldump can also be used to generate files in CSV, other delimited text, or XML format.

 Mysql-server info

Source RPM  : mysql-5.5.20-1.fc16.src.rpm
 Build Date  : Fri 27 Jan 2012 09:17:21 AM CET
 Build Host  : x86-04.phx2.fedoraproject.org
 Relocations : (not relocatable)
 Packager    : Fedora Project
 Vendor      : Fedora Project
 URL         : http://www.mysql.com
 Summary     : The MySQL server and related files

1- Start  mysql  server

[pirat9@Fedora16 ~]$ sudo service mysqld   start

Connect  to the  Server   with

[pirat9@Fedora16 ~]$ mysql -u root -p
Output
 Enter password:
 Welcome to the MySQL monitor.  Commands end with ; or g.
 Your MySQL connection id is 2
 Server version: 5.5.20 MySQL Community Server (GPL)
Copyright (c) 2000, 2011, 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>

2- Create new database

In my case i created a new database called unixmen

mysql> create database unixmen;

Query OK, 1 row affected (0.00 sec)
-Show databases with

mysql> show databases;
 +--------------------+
 | Database           |
 +--------------------+
 | information_schema |
 | mysql              |
 | performance_schema |
 | test               |
 | test1              |
 | unixmen            |
 +--------------------+
 6 rows in set (0.00 sec)

mysql>

3- Backup  mysql database

Backup MySQL database to <databasebackupfiles>.sql

<databasename> = Target existing database name
<databasebackupfiles> = Preferred backed up file name

# mysqldump -u root -p <databasename> > <databasebackupfiles>.sql
 Enter password: <Type your mysql password and press enter

i will back up the database named <unixmen> to mylbackup file. This will taking backed up all the tables including the data :

# mysqldump -u root -p unixmen > backup.sql
 Enter password:

 View the content on mysqlbackup.sql

more mysqlbackup.sql

4- Restore MySQL database

To restore the backed up database named “mbackup.sql” to new database named “new” as below :

# mysql -u root -p <mysqlpassword> <databasename> < <databasebackupfiles>.sql

<mysqlpassword> = MySQL password
<databasename> = Database name that will be restore
<databasebackupfiles> = Backed up database file, normally .sql file

Create the new database mysqlnew :

mysql> CREATE DATABASE mysqlnew;
 Query OK, 1 row affected (0.01 sec)

As example, i will restore the mysqlbackup.sql backed up file to database named “new” :

# mysql -u root -p password new < mysqlbackup.sql

or

mysql -u root -p new < mysqlbackup.sql

For questions please refer to our Q/A forum at : http://ask.unixmen.com

Mel Kham

Founder of Unixmen, Living in Amsterdam. Am working in my free time to help people to understand the Opensource and to explain them in easy way how to make the fist steps to the the light. Working day and night with my Co-founder Zinovsky to keep this website live even with less resources.
  • Whyqaz

    Dont get me wrong, this post is great and accurate.

    But I want you see you try these methods on GB, sized databases, and more is involved like indexes. 

    It will take you forever to restore.

    Mylvmbackup is the answer.

  • Abdul Basit

    Nice post

  • Jassipox

    luv this post!!!!!!!!!

Like us on Facebook

This week Top Posts

Write for us

Recent Comments

Lolman

|

in cmd

Oliver

|

Do you think that it works on a Macbook Air 1,1?… ohh and, it must be installed in a different partition than Mac OS X, right?… can´t have i installed only Ubuntu on my hard drive?

Nova

|

I wonder if there is a way to create your own themes.

Red Adaya

|

Thank you! This worked for me!!!

 
IDG Tech Network
Copyright © 2008-2013 Unixmen.com .
Maintained by Anblik .