how to back up and restore a mysql database

MySQL dump

Use mysqldump command, this command connect to the database, then create a SQL dump file. The dump file contains the required re-building a database of SQL statements. The correct syntax is as follows:

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

  • uname: database user name
  • pass: password database connection
  • dbname: To back up a library
  • backupfile.sql: backup file name
  • –opt: mysqldump option

example:

$ mysqldump -u root -p Tutorials > tut_backup.sql;

Of course, you can also specify a backup table

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

In some cases, you may need a one-time dump multiple databases, in which case, you can use the '-database' option, the back to keep up with the list you want to dump the database, separated by a space between the database name:

$ mysqldump -u root -p --database Tutorials Articles Comments > content_backup.sql;

If all libraries disposable dump the database server, you can use the '-all-databases' option

$ mysqldump -u root -p --all-databases > alldb_backup.sql;

mysqldump command there are some other options:

  • -add-drop-table: SQL statement to add a statement to drop table before creating the table
  • -no-data: dump data only and is not dump the contents
  • add-locks: add lock table and unlock table in the dump file statements

mysqldump advantages large column  how to back up and restore a mysql database disadvantages:

  • The advantages of
    ease of use, lock the table to solve the problem

  • Drawback
    drawback is that the lock table statements, if the table is large, mysqldump will be organized in a very long time user access

MySQL database backup to archive

If your database is very large, you may want to compress the output of your mysqldump. Use the following command mysql backup pipeline output gzip, you can get a compressed file

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

Unzip command

$ gunzip [backupfile.sql.gz]

MySQL database recovery

The above describes the backup Tutorials library to tut_backup.sql, in order to re-create the Tutorial database, you need
the following two steps:

  • Create a suitable database name on the target machine
  • Use the mysql command to load a backup file

$ mysql -u [uname] -p[pass] [db_to_restore] < [backupfile.sql];

How do you look at backupfile.sql recovery Tutorials Library

$ mysql -u root -p Tutorials < backupfile.sql;

Recover from the compressed file

gunzip < [backupfile.sql.gizp] | mysql -u [uname] -p [pass] [dbname];

If you want to restore the library already exists, you need to use the command mysqlimport

mysqlimport -u [uname] -p[pass] [dbname] [backupfile.sql];

Guess you like

Origin www.cnblogs.com/sanxiandoupi/p/11692507.html