mysqldump backup and recovery

First, back up a single database

1, the backup command: mysqldump

  MySQL database backup command that comes with a very good use. Is a logical backup, export the SQL statement. Process data is directly output from MySQL database backup or generated in the form of a logical file of SQL statements.

Single Instance syntax (Syntax):
mysqldump -u <username> -p <dbname> > /path/to/***.sql

Multi-instance Backup syntax (Syntax):
mysqldump -u <username> -p <dbname>  -S <sockPath> > /path/to/***.sql

eg: mysqldump -u root -p wordpress > /opt/wordpress_$(date +%F).sql

2, parameter parsing

--All- -A Databases: Export all database
 -Y --all- the Tablespaces: Export all table spaces
 -y --no- the Tablespaces: does not export any table space information
 --add-drop- added before each database create database drop database statement.
--add-drop-table data add drop table statement before each data table creation. (Turned on by default, use the Add-drop----skip the Table option to cancel)
 --add-Locks increase LOCK TABLES before and after each table is derived UNLOCK TABLE. (Turned on by default, use-Add- --skip Locks cancel options)
 --comments additional notes. The default is open, you can use --skip- cancel Comments
 --compact export less output information (for debugging). Note remove head and tail like structures. You can use options: - Skip-the Add-drop-the Add-Locks the Table --skip---skip-Comments --skip-DISABLE- Keys
 -c --complete- insert: Use the full insert statements (contains the column name) . Doing so can improve the insertion efficiency, but may be affected max_allowed_packet parameters which led to the insertion fails.
-C - the compress: Enable compression transfer all information between the client and server
 -B--databases: export several databases. Parameter name behind all the parameters are seen as the database name.
--debug output debug information for debugging. The default value is: d: t: o, / tmp /
--debug- info output debugging information and exit
 - default -character- the SET to set the default character set, the default value utf8
 --delayed- INSERT using delay insert mode (INSERT DELAYED) export data
 -E-- Events: Export Event .
--master- the Data: written binlog file backups in the backup file, into the recovery, incremental data began to recover from the log file after this. Value is 1, the file name and location binlog no comment was 2, then annotate binlog file name and location of the backup file will
 flush the log before export starts --flush-logs. Note: If you export multiple databases once (using the option --databases or --all-databases), will refresh the logs one by one database. Except that - Lock outer -all-tables or --master-data. In this case, the log will be refreshed once, so the corresponding table is locked at the same time. Therefore, if you plan to export both logs should be used and refreshed - Lock -all-the Tables or --master-data and --flush- logs.
--flush- privileges after export mysql database, issue a FLUSH PRIVILEGES statement. In order to properly recover, this option should be used to export the mysql database mysql database and dependent data at any time.
-SQL force ignore errors that occur during the export process.
-h - Host: you need to export host information
 --ignore-table does not export the specified table. When you specify more than one table to ignore, you need to be repeated several times, each time a table. Each table must specify both the database and table names. For example: - the ignore---ignore-database.table1 = Table = Table database.table2 ......
 the -X-- Lock -all-Tables: submit a request to lock all the tables in all databases to ensure data consistency. This is a global read lock, and automatically turn off --single-transaction and - Lock - the Tables option.
the -l - Lock -tables: Before starting the export lock all the tables. Lock table for READ LOCAL to allow concurrent insert MyISAM table. For transactional tables such as InnoDB and BDB, - single-transaction is a better choice, because it does not need to lock the table. Note that when exporting multiple databases, - Lock - the Tables per database tables are locked. Therefore, this option does not guarantee the exported file table between the logical consistency of the database. Exporting Tables in different databases can be completely different.
--single- Transaction: for backup innodb transaction database. Ensure the consistency of the backup, the principle is to set the isolation level when this session is Repeatable read, to ensure that this session (ie dump), will not see the other sessions have already submitted data.
-F: Refresh binlog, if binlog opened, - F parameter is automatically refreshed when the backup binlog switch.
--no-create- -n db: export data only, without adding CREATE DATABASE statement.
--no-create- -t info: export data only, without adding CREATE TABLE statement.
--no- -d the Data: does not export any data, just export the database table structure.
-p - password: password database connection
 -P - Port: connect to the database port number
 -u --user: Specifies the user name of the connection.

For example use:

a, export the entire database (including data in the database)
mysqldump -u username -p dbname > dbname.sql 
b, derived database structure (no data)
mysqldump -u username -p -d dbname > dbname.sql
c, a derived data tables in the database (containing data)
mysqldump -u username -p dbname tablename > tablename.sql
d, a table structure of data tables derived database (no data)
mysqldump -u username -p -d dbname tablename > tablename.sql

3, the recovery operation

Syntax (Syntax):
MySQL -u <username> -p <password> <dbname> </ opt / mytest_bak.sql # library must be preserved, empty library can also be
Description: Specifies dbname, corresponding to use <dbname>

4, an example of

 (1) No parameter database backup and recovery mytest

( 1 ) the backup operation
a, backup
mysqldump -uroot -p‘123456’ mytest > /mnt/mytest_bak_$(date +%F).sql

( 2 ) recovery operation
a, delete student table (library must be retained, empty library will do)
mysql -uroot -p'123456' -e "use mytest;drop table student;"
b, data recovery
mysql -uroot -p'123456' mytest < /mnt/mytest_bak.sql 
c, view the data
mysql -uroot -p'123456' -e "select * from mytest.student;"

(2) -B parameter backup and restore (recommended)

( 1 ) the backup operation
a, backup
mysqldump -uroot -p'123456' -B mytest > /mnt/mytest_bak_B.sql

Description: Add the - after the B parameters, backup files and more Create database commands and use mytest
Plus - the benefits of B parameters:
Plus - B parameters, and create libraries using the library statement to export data file already exists, do not need to manually create the original library is a library of operations, without having to manually building a database in the recovery process, you can direct reduction recovery.

( 2 ) recovery operation
a, delete mytest library
mysql -uroot -p'123456' -e "drop database mytest;"
b, data recovery
( 1 export file) with no arguments to import (not to be restored database import), an error
mysql -uroot - p'123456' < /mnt/mytest_bak.sql   
ERROR 1046 (3D000) at line 22: No database selected
( 2 ) the use of tape - the export file import parameter B (not to be restored database import), success
mysql -uroot -p'123456' < /mnt/mytest_bak_B.sql 
c, view the data
mysql -uroot -p'123456' -e "select * from mytest.student;"

(3) - compact parameter optimization backup file size small, reducing the output annotation (typically for debug Debug)

( 1 ) Backup
mysqldump -uroot -p'123456' --compact -B mytest > /mnt/mytest_bak_Compact.sql
Description:
Use --compact parameters, you can optimize the output size of the content, so less capacity for debugging. It will ignore-the Add-drop---skip the Table, - NO- the SET -names, - Skip-disable-Keys, - Skip-the Add-Locks, and several other parameters of the function.

(4) specify a compression command to compress the backup files

( 1 ) Backup
mysqldump -uroot -p'123456'  -B mytest | gzip > /mnt/mytest_bak_.sql.gz
Description:
mysqldump to export files are text files, high compression efficiency

(5) a plurality of backup databases

( 1 ) Description
By specify the relevant database -B parameter, sub-grid with a space before each database name. When the -B parameter, all full database of all columns, at this time is equivalent to the - A parameter.
( 2 ) Backup
mysqldump -uroot -p'123456' -B mytest wiki | gzip > /mnt/mytestAndWiki_bak.sql.gz

(6) points backups

  Sub-database backup is actually perform a backup statement to back up a library, when there are multiple libraries, they perform the same backup statement more than just a library name and the name of the backup file backup is different. Shell script may be automatically generated and executed by the corresponding operation may be a single backup all statements written in a shell script, to back up through the cron scheduled task.

Meaning sub-library is backed up in all libraries are backed up into a backup file, restore a database where the data is too much trouble, so the sub-database backup, conducive to recovery. Sub-backup script is as follows:

for dbname in ` mysql -uroot -p'123456' -e "show databases;" | grep -Evi "database|infor|perfor"`
do
    mysqldump -uroot -p"123456" --events -B $dbname | gzip > /mnt/${dbname}_bak.sql.gz
done

Description: $ {dbname} _BAK , by requiring the backup file name $ dbname_bak.sql.gz format command, but the system can not distinguish variables dbname $ or $ dbname_bak , so in this case you need to use braces "{} " enclose the variable up, it is $ {dbname} _bak.sql.gz up.

(7) -d parameters, only the backup of the database table structure

mysqldump -uroot -p'123456' -d mytest > /mnt/mytestDesc_bak.sql

(8) -A parameter backup the entire reservoir, and -F and switching refresh binlog

mysqldump -uroot -p'123456' -A -B -F > /mnt/All_bak.sql

(9) - master-data parameter write current binlog file number in the backup file

mysqldump -uroot -p'123456' --master-data=1 --compact mytest > /mnt/All_bak.sql

mysqldump -uroot -p'123456' --master-data=2 --compact mytest > /mnt/All_bak.sql

10) Several commonly used Case

Export table structure and the latest data 
mysqldump -uroot-platform88 -p> / the Data / MySQL / dump20181020.sql, then enter the password.

--routines, - R & lt stored procedures and functions deriving 
the mysqldump -uroot--R & lt platform88 -p> / Data / MySQL / dump20181022.sql

-ignore- Table skip a table 
the mysqldump -uroot--p---ignore = Table platform88.platformapitrace platform88> / Data / MySQL / dump20181022.sql
 If you want to skip more than one table, multiple use this option: 
the mysqldump -uroot- Table--R & lt --ignore = -p --ignore-platform88.platformapitrace = Table platform88.platformaccount platform88> / Data / MySQL / dump20181022.sql
Note that this option value must contain schema.

The -X-- Lock -all-tables, -l - Lock - before export tables locked table records 
the mysqldump -uroot--p--R & lt --ignore = Table platform88.platformapitrace - Lock -tables -B platform88> / Data / MySQL / dump20181022.sql
When using the lock tables, the database needs to be specified, i.e. - B parameter.

Data---no - D export table structure only 
mysqldump -uroot--R & lt -d -B platform88 -p> / Data / MySQL / dump20181022.sql 
--no-Create-info - T export data only, without creating table structure mysqldump -R & lt -p -t -B platform88 -uroot-> / Data / MySQL / dump20181022.sql - opt option packages - opt default implementation of the following options: --add-drop---add-Locks the Table Options --create---disable-Keys --extended-INSERT - Lock -tables --quick - the SET - charset acronym. Slave---dump [= value], - Data-Master [= value] primary common parameters of the architecture, from the primary backup. - Compact compressed --compact enabled by default following options: --skip-the Add-drop-the Table, the Add---skip-Locks, --skip-Comments, --skip-disable-Keys, and --skip- the SET -charset options.

 

Second, a single table backup

Syntax (Syntax): can not add - B parameter
mysqldump -u<username> -p<password> dbname tablename1 tablename2... > /path/to/***.sql

Examples

Example 1: backup library student table mytest
mysqldump -uroot -p'123456' mytest student > /mnt/table_bak/student_bak.sql

Example 2: backup all tables mytest library, the library is to back up mytest
mysqldump -uroot -p'123456' mytest  > /mnt/table_bak/all_bak.sql

Example 3: Backup mytest library and student test table
mysqldump -uroot -p'123456' mytest student test > /mnt/table_bak/two_bak.sql

Example. 4: - D parameter, only the backup table structure
mysqldump -uroot -p'123456' -d mytest stusent > /mnt/studentDesc_bak.sql

Example. 5: - T parameters, backup data only
mysqldump -uroot -p'123456' --compact -t mytest stusent > /mnt/studentData_bak.sql
INSERT INTO `student` VALUES (1,'Tom',20,'S11'),(2,'Jary',21,'S12'),(3,'King',25,'S10'),(4,'Smith',19,'S11'),(5,'??',20, ' S11 ' ), ( . 6 , ' John Doe ' , 20 is , ' S11 ' );

Third, different production scenarios engine backup command parameter

1, the key parameters of mysqldump

 

- B: specify multiple libraries, an increase in building a database backup file the statement and use statement
 - Compact: uncommented backup file for debugging, production scenarios do
 - A: Back up all libraries
 - F: Refresh binlog log
 - the MASTER data: location point increase binlog log file name and in the backup file corresponding to
 the -X-- lock -all- tables: table lock
 - L: read-only lock table
 - D: only backup table structure
 - T: data backup only
 --single- transaction: for backup innodb transaction database
   InnoDB tables when backup option is typically enabled when --single-transaction to ensure consistent backup, the principle is to set the isolation level for this session Repeatable read, to ensure that this session (ie dump), will not see the other sessions have already submitted data.

2, different engines backup command parameter usage

1 ) Myisam engine:
mysqldump -uroot -p123456 -A -B --master-data=1 -x| gzip > /data/all_$(date +%F).sql.gz

( 2 ) InnoDB engine:
mysqldump -uroot -p123456 -A -B  --master-data=1 --single-transaction > /data/bak.sql

( 3 ) a command given production DBA
a、for MyISAM
mysqldump --user=root --all-databases --flush-privileges --lock-all-tables \
--master-data=1 --flush-logs --triggers --routines --events \
--hex-blob > $BACKUP_DIR/full_dump_$BACKUP_TIMESTAMP.sql

b、for InnoDB
mysqldump --user=root --all-databases --flush-privileges --single-transaction \
--master-data=1 --flush-logs --triggers --routines --events \
--hex-blob > $BACKUP_DIR/full_dump_$BACKUP_TIMESTAMP.sql

Common Errors

But the accident appears "Warning: Using a password on the command line interface can be insecure. 

The first method, modify the database configuration file

1, we need to modify the database configuration file, this depends on the configuration of our database, some are in /etc/my.cnf, some are /etc/my.conf

We need the [client] section add the script (you can create your own files, specify import):

= localhost Host
User Database = user
password = 'password database'

2, the command using export and import database

# Export Database

mysqldump --defaults-extra-file=/etc/my.cnf database > database.sql

# Import Database

mysql --defaults-extra-file=/etc/my.cnf database < database.sql

 

This article reprint : https://www.cnblogs.com/linuxk/p/9371475.html

 

Guess you like

Origin www.cnblogs.com/yyy-blog/p/11073957.html