10 MySQL's data backup and recovery

01- Data Backup

# 1, MySQLdump command to back up 
MySQLdump is a very useful tool for MySQL database backup provided.
When MySQLdump command is executed, the database can be backed up to a text file, the text actually contains several CREATE and INSERT statements, you can use these statements to re-create tables and insert data.
The basic syntax is as follows:
mysqldump -u user -h host -p password dbname[tbname, [tbname...]]>filename.sql
user represents the user name; host indicates the host name of the logged-on user; password to login password;
dbname need to back up a database for the name; tbName dbname database table data to be backed up, a plurality of tables can be specified to be backed up;
Right arrow symbol ' > ' tells MySQLdump backup data table definition and data written to the backup file;
filename.sql for the backup file name.

# 2, all the tables using a single database and the backup MySQLdump 
the mysqldump -u booksdb the root -p> / Users / huangyanpeng / Desktop / db_test / booksdb_20190909.sql

# 3, using MySQLdump backup of the database in a table 
syntax is as follows:
mysqldump -u user -h host -p dbname [tbname, [tbname...]] > filename.sql
tbname represented in the database table name, separated by a space between a plurality of table names.
The difference between all the tables and backup database backup table: To specify the table name to be backed up after the database name dbname.

# 4, a plurality of backup databases using MySQLdump 
the mysqldump -u User -H Host -p --databases [dbname, [dbname ...]]> filename.sql
Use - After databases parameter must specify the name of the at least one database, separated by spaces between a plurality of database name.
Further, using --all- Databases all system parameters database can be backed up, the following statements:
mysqldump -u user -h host -p --all-databases > filename.sql 

# Run Help command 
mysqldump - Help

# 5, MySQLhotcopy use tools to quickly back up 
the fastest way to back up the database or single tables, but it can only run on the machine where the database directory, and can only back up MyISM type of table. MySQLhotcopy run on Unix systems.
Syntax is as follows:
mysqlhotcopy db_name_1, ... db_name_n  /path/to/new_directory

db_name_1, ... db_name_n are required to back up the database name;
/ path / to / new_directory specify the backup file directory.
Use MySQLhotcopy test database backup to usr / / the backup directory, input sentence as follows:
mysqlhotcopy  -u root -p test /usr/backup
To perform MySQLhotcopy, you must have access table file backup, SELECT privilege which has tables, RELOAD privileges and LOCK TABLES privileges.

note:
( 1 ) mysqlhotcopy only the table of contents to another location where the copy only for backup MyISAM and ARCHIVE tables.
( 2 ) The error message occurs when backing up InnoDB table types of data.
( 3) Due to its local copy a file format, it is also not portable to other hardware or operating system.

02- Data Recovery

# 1, using the MySQL command to restore 
a text file containing CREATE, INSERT statement to have been backed up, you can use the MySQL command into the database.
sql backup file contains the CREATE, INSERT statement (sometimes there DROP statement).
The syntax is as follows:
mysql -u root -p [dbname] < filename.sql
parameter:
user is the user name backup.sql in the implementation of the statement; - the p-represented enter username and password;
dbname is the name of the database; create MySQLdump tools filename.sql file if the file containing the statements to create the database, implementation of the time do not need to specify the database name.
Example:
mysql -u root -p booksDB < /.../booksdb_20190909.sql
Before the statement is executed, you must first create booksDB MySQL database server.
If the server is already logged in, you can also use the source file into SQL commands.
source filename.sql
First switch to the appropriate database.

# 2, directly copied into the database directory

# 3, MySQLhotcopy quickly recover 
files after MySQLhotcopy backup can also be used to restore the database, stop running when the MySQL server,
Copy the database files to a backup location (MySQL data folder) and re-start the service MySQL to store data.
If you do this as root, you must specify the owner of the database file, enter the following statement:
chown -R mysql.mysql  /var/lib/mysql/dbname

Example:
MySQLhotcopy copied from the backup to restore the database, enter the following statement:
cp -R /usr/backup/test  usr/local/mysql/data
Running this statement, restart the server, MySQL will return to backup status.

note:
( 1 After) If you want to restore the database already exists, delete DROP database, and then restore to succeed.
( 2) In addition to be compatible between different versions of MySQL, data recovery can only be used after.

03- Data Migration

# 1, migration between versions of the same MySQL database 
easiest way is by copying the database file directory, just try to MyISAM engine table.
For InnoDB tables, you can not directly copy the files the way back up the database.
Therefore, the most common and safest way is to use MySQLdump command to export data, and then use the Import command in the target MySQL database server.
Example:
Migrating www.abc.com MySQL database on the host to all hosts on www.bcd.com. Www.abc.com command executed on the host as follows:
mysqldump -h www.abc.com -uroot -p password dbname | mysql -h www.bcd.com -uroot -p password
Data MySQLdump introduced directly through the pipeline at | passed command MySQL database www.bcd.com introduced host.
dbname is the name of the database need to be migrated, if you want to migrate the entire database, using the parameters --all- Databases.

# 2, migrate between different versions of MySQL database 
old - "New:
( 1 ) engine for MyISAM tables, you can directly copy the database file, you can use MySQLhotcopy tools, MySQLdump tool.
( 2 ) For the InnoDB table engine, generally only use MySQLdump export data. Then use the MySQL command to import to the target server.
New - "Old: The best use MySQLdump name export and then import the target database.

# 3, migration between different databases

Import and export of table 04-

# 1, use SELECT ... INTO OUTFILE to export a text file 
(1 Shi) MySQL database export data, allowing the use of the SELECT statement that contains the exported definitions that this time the export data.
( 2 ) the file is created on the server host, one must have write privileges (FILE permission) to use this syntax.
( . 3) " SELECT ... the INTO the OUTFILE 'filename' " form of the SELECT statement can be selected by row writing a file, filename is not an existing file.

The basic syntax is as follows:
SELECT columnlist  FROM table WHERE condition INTO OUTFILE 'filename' [OPTIONS]
--OPTIONS 选项

 

Guess you like

Origin www.cnblogs.com/pgxpython/p/11725721.html