Mysql uses mysqldump for data backup

Article reference:
https://www.cnblogs.com/markLogZhu/p/11398028.html
https://www.cnblogs.com/chenmh/p/5300370.html


1. Command description

1.1 Common command format

mysqldump -u -p --databases 数据库名 > 脚本名 					#备份指定数据库
mysqldump -u -p --all-databases  > 脚本名						#备份所有数据库
mysqldump -u -p --databases 数据库名 --tables 表名 > 脚本名		#备份指定表

1.2 Parameter description

parameter name abbreviation meaning
–host -h Server IP address
–port -P Server port number
–user -u MySQL username
–Pasword -p MySQL password
–databases Specify the database to be backed up
–all-databases Back up all databases on the mysql server
–compact -C Compressed mode, which produces less output
–no-data Do not export data, only export the table structure
–comments Add comment information
–complete-insert Output completed insert statement
–lock-tables Before backup, lock all database tables
–no-create-db/–no-create-info Prohibit generating database creation statement
–force Continue the backup operation when an error occurs
–default-character-set Specify the default character set
–add-locks Lock the database table when backing up the database table

Two, export the instance

2.1 Export all databases

This command will export all databases including system databases

mysqldump -uroot -proot --all-databases > backup/all.sql

2.2 Export all data of the two databases db1 and db2

Use a space to separate the two databases

mysqldump -uroot -proot --databases db1 db2 > backup/two_db.sql

2.3 Export tab1 and tab2 tables in db1

mysqldump -uroot -proot --databases db1 --tables tab1 tab2  > backup/two_tables.sql

Note: The export specified table can only be exported for one database, and the content of the exported table is different from the exported database. There is no judgment statement to create a database in the export text of the export specified table, only delete table-create table-import data; and Ownership of the export table. See below:

Insert picture description here
If you don't want to have a statement to create a database, you can add a parameter: -no-create-info

2.4 Export the data of tab1 table id=1 in db1

Integer field:

mysqldump -uroot -proot --databases db1 --tables tab1 --where='id=1'  > backup/tab1.sql

String field:

mysqldump -uroot -proot  --databases db1 --tables tab1 --where="name='a'"  > backup/tab1.sql

2.5 Only export table structure without exporting data

mysqldump -uroot -proot --no-data --databases db1 > backup/db1.sql

2.6 Export data across servers

mysqldump --host=192.168.43.1 -uroot -proot --databases db1 | mysql --host=192.168.43.2 -uroot -proot db2

The ip is 192.168.43.1 all the data server database db1 imported into ip is 192.168.43.2 server db2 database, db2 database must exist otherwise an error


mysqldump --host=192.168.43.1 -uroot -proot -C --databases db1 | mysql --host=192.168.43.2 -uroot -proot db2

You can also add the parameter -C to compress and transfer to speed up the transfer


Three, import examples

3.1 System command line import

mysqladmin -uroot -p create db1 
mysql -uroot -p  db1 < /backup/db1.sql

Note: Before importing the backup database, if db1 does not exist, it needs to be created; and the database name is the same as the database name written in the db1.sql file before it can be imported.


3.2 Import into mysql

mysql > use db1
mysql > source /backup/db1.sql

Guess you like

Origin blog.csdn.net/lendsomething/article/details/109000973