mysql command-line backup method

1, the backup command

Format: mysqldump -h hostname -u username -p port -P password --database database name> .sql file name

mysqldump -h 192.168.1.100 -p 3306 -uroot -ppassword --database cmdb > /data/backup/cmdb.sql

 

2, backup compression

Export of data is likely relatively big, bad backup to remote, this time on the need for compression

Format: mysqldump -h hostname -u username -p port -P password --database database name | gzip> filename .sql.gz

For example: mysqldump -h192.168.1.100 -p 3306 -uroot -ppassword --database cmdb | gzip> /data/backup/cmdb.sql.gz

 

3, backup libraries with multiple tables

Format: mysqldump -h hostname -u username -p port -P password database --database watches 1 Table 2 ....> .sql file name

E.g. mysqldump -h192.168.1.100 -p3306 -uroot -ppassword cmdb t1 t2> /data/backup/cmdb_t1_t2.sql

 

4, back up multiple libraries

Format: mysqldump -h hostname -u username -p port -P password --databases database name database name 1 2 3 database name> .sql file name

例如:mysqldump -h192.168.1.100 -uroot -ppassword --databases cmdb bbs blog > /data/backup/mutil_db.sql

 

5, all database backup instance

Format: mysqldump -h hostname -u username -p port -P password --all-databases> .sql file name

例如:mysqldump -h192.168.1.100 -p3306 -uroot -ppassword --all-databases > /data/backup/all_db.sql

 

6 sql backup, the backup data is deleted with the database or table

Format: mysqldump -h hostname -u username -p port -P password --add-drop-table --add-drop-database database name> .sql file name 

例如:mysqldump -uroot -ppassword --add-drop-table --add-drop-database cmdb > /data/backup/all_db.sql

 

7, the structure of the database backup, the backup data is not

Format: mysqldump -h hostname -u -P port username password -p --no-data database name database name 1 2 3 database name> filename .sql

For example: mysqldump --no-data -databases db1 db2 cmdb> /data/backup/structure.sql

 

8, restore MySQL database commands

mysql -hhostname -uusername -ppassword databasename < backupfile.sql

 

9, reducing compression of MySQL database

gunzip < backupfile.sql.gz | mysql -uusername -ppassword databasename

 

10, will be transferred to the new database server

mysqldump -uusername -ppassword databasename | mysql –host=*.*.*.* -C databasename

11、--master-data 和--single-transaction

   Use --master-data = 2 in the mysqldump, records the position information and binlog file. --single-transaction isolation level will be set to repeatable-commited

12, into the database

Common source command, with the use into a database, mysql> source d: \ test.sql, the latter argument is a script file.

13, see the binlog log

View logs available binlog command mysqlbinlog binlog log name | more

 

14、general_log

Any operation General_log records database view general_log status and location of the command can show variables like "general_log%", the command may open general_log set global general_log = on

 

Incremental Backup

1, first do a full backup:

mysqldump -h10.6.208.183 -utest2 -p123 -P3310 --single-transaction --master-data = 2 test> test.sql this time will give a full backup file test.sql

In sql file we will see:
- the TO MASTER_LOG_FILE the CHANGE MASTER = 'bin-log.000002', MASTER_LOG_POS = 107; refers to all changes after the backup will be saved to the bin-log.000002 binary file.
2, two records increase in t_student table test library, and then execute the command flush logs. At this time will produce a new binary log file bin-log.000003, bin-log.000002 then save the changes after all is perfect, both to increase the recording operation is also stored in the bin-log.00002 in.

3, the increase in the two test recording in a database table, and a table and the table t_student accidentally deleted. increase a recording operation and a delete tables and t_student operations are recorded in the bin-log.000003.

 

Third, the recovery

1, first of all equipment introduced into the data

mysql -h10.6.208.183 -utest2 -p123 -P3310 <test.sql, mysql command line may be directly following introduction source

2, the recovery bin-log.000002

   mysqlbinlog bin-log.000002 |mysql -h10.6.208.183 -utest2 -p123  -P3310  

3, part of the recovery bin-log.000003

   Find the point in time general_log accidentally deleted, the more time points corresponding to the bin-log.000003 then find the appropriate point position needs to be restored to a position in front of the point of accidentally deleted.

The following parameters can be used to control the interval binlog

--start-position starting point --stop-position end point

--start-date --stop-date start time end time

After finding the recovery points, both began to recover.

  mysqlbinlog mysql-bin.000003 --stop-position=208 |mysql -h10.6.208.183 -utest2 -p123  -P3310 

 

Guess you like

Origin www.cnblogs.com/gcixx/p/11145473.html