How to Baskup and Restore a MySQL database

Source: Internet
Author: User
Tags gz file mysql backup phpmyadmin

If you ' re storing anything in MySQL databases so you don't want to lose, it's very important to make regular backups O F your data to protect it from loss. This tutorial would show you both easy ways to backup and restore the data in your MySQL database. You can also use this process to move your data to a new Web server.

    • Back to the Command line (using mysqldump)
    • Back up your MySQL Database with Compress
    • Restoring your MySQL Database
    • Backing up and Restoring using PHPMyAdmin

Back to the Command line (using mysqldump)

If you have a shell or Telnet access to your Web server, you can backup your MySQL data by using the mysqldump command. This command connects to the MySQL server and creates an SQL dump file. The dump file contains the SQL statements necessary to re-create the database. Here is the proper syntax:

$ mysqldump--opt-u [uname]-p[pass] [dbname] > [Backupfile.sql]
    • [uname] Your Database username
    • [Pass] The password for your database (note there is no space between-p and the password)
    • [dbname] The name of your database
    • [Backupfile.sql] the filename for your database backup
    • [--opt] The mysqldump option

For example, to-backup a database named ' Tutorials ' with the username ' root ' and with no password to a file tut_backup.sql , you should accomplish the This command:

$ mysqldump-u root-p Tutorials > Tut_backup.sql

This command would backup the ' tutorials ' database into a file called Tut_backup.sql which would contain all the SQL Stateme NTS needed to re-create the database.

With mysqldump command you can specify certain tables of the your database you want to backup. For example, php_tutorials and Asp_tutorials tables from the ' tutorials ' database accomplish the command b Elow. Each table name is separated by space.

$ mysqldump-u root-p Tutorials php_tutorials asp_tutorials > Tut_backup.sql

Sometimes it is the necessary to-back-to-one database at once. In this case you can use the--database option followed by the list of databases you would like to backup. Each database name is separated by space.

$ mysqldump-u root-p--databases Tutorials Articles Comments > Content_backup.sql

If you want-to-back-the-databases in the server at one time should use the--all-databases option. It tells MySQL to dump all the databases it had in storage.

$ mysqldump-u root-p--all-databases > Alldb_backup.sql

The mysqldump command has also some and other useful options:

--add-drop-table: Tells MySQL to add a DROP TABLE statement before each CREATE table in the dump.

--no-data: Dumps only the database structure and not the contents.

--add-locks: Adds the LOCK TABLES and UNLOCK TABLES statements you can see in the dump file.

The mysqldump command has advantages and disadvantages. The advantages of using mysqldump is, that it's simple-to-use and it-takes care's table locking issues for you. The disadvantage is, the command locks tables. If the size of your tables is very big mysqldump can lock out users for a long period of time.

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, then you'll get the output as gzip file.

$ mysqldump-u [uname]-p[pass] [dbname] | Gzip-9 > [backupfile.sql.gz]

If you want to extract the. gz file, use the command below:

$ gunzip [backupfile.sql.gz]

Restoring your MySQL Database

Above We backup the tutorials database into Tut_backup.sql file. To re-create the tutorials database you should follow the steps:

    • Create an appropriately named database on the target machine
    • Load the file using the MySQL command:
$ mysql-u [uname]-p[pass] [Db_to_restore] < [Backupfile.sql]

Has a look how can restore your Tut_backup.sql file to the tutorials database.

$ mysql-u Root-p Tutorials < Tut_backup.sql

To restore compressed backup files you can do the following:

Gunzip < [backupfile.sql.gz] | Mysql-u [uname]-p[pass] [dbname]

If you need-to-restore a database that already exists, you'll need to the use Mysqlimport command. The syntax for Mysqlimport is as follows:

Mysqlimport-u [uname]-p[pass] [dbname] [Backupfile.sql]

Backing up and Restoring using PHPMyAdmin

It is assumed so you have PhpMyAdmin installed since a lot of Web service providers use it. To backup your MySQL database using PHPMyAdmin just follow a couple of steps:

    • Open PhpMyAdmin.
    • Select your database by clicking the database name in the the "the" screen.
    • Click the Export link. This should bring up a new screens that says View dump of database (or something similar).
    • In the Export area, click the Select all link to choose all of the tables in your database.
    • In the SQL options area, click on the right options.
    • Click on the Save as File option and the corresponding compression option and then click the ' Go ' button. A dialog box should appear prompting to save the file locally.

Restoring your database is easy as well as the backing it up. Make the following:

    • Open PhpMyAdmin.
    • Create an appropriately named database and select it by clicking the database name in the the the "the" screen. If you would like to rewrite the backup of over a existing database then click in the database name, select all the check Bo Xes next to the table names and select Drop to delete all existing tables in the database.
    • Click the SQL link. This should bring up a new screens where you can either type in SQL commands, or upload your SQL file.
    • Use the browse button to find the database file.
    • Click Go button. This would upload the backup, execute the SQL commands and re-create your database.

MySQL Backup Software

If you is administrating multiple MySQL servers, you can consider using a commercial solution for MySQL Backup. One solution could be Backup Bird. It is a full cloud server backup service, which monitor the progress of each backup and backup your files as well as your D Atabases.

How to Baskup and Restore a MySQL database

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.