mysql backup

http://www.cnblogs.com/kissdodog/p/4174421.html

quote


1. Data backup
  1. Use the mysqldump command to backup

  the mysqldump command to back up the data in the database into a text file. The structure of the table and the data in the table will be stored in the generated text file.

  How the mysqldump command works is simple. It first finds out the structure of the table that needs to be backed up, and then generates a CREATE statement in the text file. Then, convert all records in the table into an INSERT statement. Then through these statements, you can create a table and insert data.

  1. Backup a database

  mysqldump basic syntax:

  mysqldump -u username -p dbname table1 table2 ...-> BackupName.sql

  where:

dbname parameter indicates the name of the database;
table1 and table2 parameters indicate the name of the table to be backed up, if it is empty The entire database is backed up; the
BackupName.sql parameter table is used to design the name of the backup file, and an absolute path can be added in front of the file name. Usually the database is divided into a file with the suffix sql;
  use the root user to back up the person table under the test database

mysqldump -u root -p test person > D:\backup.sql
  

  The generated script is as follows:

  

  MySQL will be recorded at the beginning of the file version, backup hostname, and database name.

  In the file, the comments beginning with "--" are SQL language comments, and those beginning with "/*!40101" are comments related to MySQL. 40101 is the version number of the MySQL database. If the MySQL version is higher than 1.11, the content between /*!40101 and */ will be executed as a SQL command, and if it is lower than 4.1.1, it will be treated as a comment.

  2. Backup multiple databases

  Syntax :

mysqldump -u username -p --databases dbname2 dbname2 > Backup.sql
  with the --databases option, followed by multiple databases

mysqldump -u root -p --databases test mysql > D :\backup.sql
  3.

  Backup all databases The syntax of the mysqldump command to back up all databases is as follows:

mysqldump -u username -p -all-databases > BackupName.sql
  Example:

mysqldump -u -root -p -all-databases > D:\ all.sql
  2. Directly copy the entire database directory

  MySQL has a very simple backup method, which is to copy the database files in MySQL directly. This is the easiest and fastest method.

However, before that, the server must be stopped first, so as to ensure that the data in the database will not change during replication. If there is still data writing during the process of copying the database, it will cause data inconsistency. This is fine in a development environment, but it is difficult to allow backup servers in a production environment.

  Note: This method does not work for tables of the InnoDB storage engine, it is convenient for tables of the MyISAM storage engine. At the same time, the version of MySQL when restoring is preferably the same.

  3. Use the mysqlhotcopy tool to quickly

  back up. You can see that it is a hot backup by looking at the name. Therefore, mysqlhotcopy supports backup without stopping the MySQL server. Also, mysqlhotcopy's backup method is faster than mysqldump. mysqlhotcopy is a perl script mainly used under Linux systems. It uses LOCK TABLES, FLUSH TABLES and cp for fast backups.

  Principle: First add a read lock to the database to be backed up, then use FLUSH TABLES to write the data in memory back to the database on the hard disk, and finally, copy the database file to be backed up to the target directory.

  The command format is as follows:

[root@localhost ~]# mysqlhotcopy [option] dbname1 dbname2 backupDir/
dbname: database name;
backupDir: which folder to backup to;
  common options:

--help: view mysqlhotcopy help;
--allowold: if backup If the same backup file exists in the directory, add _old to the old backup file;
--keepold: If the same backup file exists in the backup directory, do not delete the old backup file, but rename the old file;
--flushlog : After this generation, the updates to the database will be recorded in the log;
--noindices: Only data files are backed up, not index files;
--user=username: used to specify the user name, which can be replaced by -u;
--password=password: used to specify the password, which can be replaced by -p. When using -p, there is no space between the password and -p;
--port=port number: used to specify the access port, you can use -P instead;
--socket=socket file: used to specify the socket file, you can use -S Instead;
  mysqlhotcopy does not come with mysql, you need to install the Perl database interface package; the download address is: http://dev.mysql.com/downloads/dbi.html

  At present, this tool can only backup MyISAM type tables.

2. Data restoration
  1. The syntax for restoring the database backed up by the mysqldump command is as follows:

  mysql -u root -p [dbname] < backup.sq

  Example:

mysql -u root -p < C:\backup.sql
  2. Restore direct copy Backup

  of When restoring in this way, you must ensure that the version numbers of the two MySQL databases are the same. The MyISAM type table is valid, but it is not available for the InnoDB type table, and the table space of the InnoDB table cannot be directly copied.

  





quote


Mysql database backup and restore commonly used commands listen to the voice
Browse : 3338|Update: 2012-05-07 10:32|Label: mysql backup
Mysql database backup and restore commonly used commands are the key to Mysql database backup and restore, there is no command, There is no way to start anything, let alone backup and restore. Only by giving the system this command and letting it execute can the Mysql database backup and restore operations be completed. The following are the commonly used commands for the operation.

1. Backup command
1. Command to backup MySQL database

<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> mysqldump-hhostname-uusername-ppassword databasename>backupfile.sql

2 . Backup MySQL database in the format with delete table

Backup MySQL database in the format with delete table, so that the backup can overwrite the existing database without manually deleting the original database.

<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> mysqldump---add-drop-table-uusername-ppassword databasename>backupfile.sql

3. Directly compress MySQL database backup

<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> mysqldump-hhostname-uusername-ppassword databasename|gzip>backupfile.sql.gz

4. Backup a certain table(s) in the MySQL database

<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> mysqldump-hhostname-uusername-ppassword databasename specific_table1 specific_table2>backupfile. sql

5. Backup multiple MySQL databases at the same time

<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> mysqldump-hhostname-uusername-ppassword --databases databasename1 databasename2 databasename3>multibackupfile.sql

6. Only backup database structure

<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-- > mysqldump –no-data –databases databasename1 databasename2 databasename3>structurebackupfile.sql

7. Backup all databases on the server

<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> mysqldump – all-databases>allbackupfile.sql

Second, restore command
1, restore MySQL database command

<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> mysql-hhostname-uusername-ppassword databasename<backupfile.sql

2. Restore the compressed MySQL database

<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www .CodeHighlighter.com/
--> gunzip<backupfile.sql.gz|mysql-uusername-ppassword databasename

3. Transfer the database to a new server

<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter .com/
--> mysqldump-uusername-ppassword databasename|mysql --host=*.*.*.*-C databasename

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326208085&siteId=291194637