mysql database export and import to local file

Reference: https://www.cnblogs.com/Jasper-changing/p/13924977.html

1. Export backup

1. The mysqldump command

mysqldump is a logical backup tool that comes with MySQL

Export a single database

mysqldump -h hostname/ip地址 -P 端口号 -u mysql用户名 -p mysql密码 --databases 数据库名s > /路径/生成的文件名.sql

1.1, export table

1.1.1 Export table structure and data

mysqldump -u root -p --set-gtid-purged=OFF database table1 table2 > mysqldump.sql

–Set-gtid-purged=off: When –set-gtid-purged=OFF is added, the binlog log will be recorded; if not, the binlog log will not be recorded. So when we use gtid as the master and slave
, we need to add –set-gtid-purged=OFF when using mysqldump to backup, otherwise you will import and restore the data on the master, and the master will not have the binlog log, and the synchronization will not be synchronized.

1.1.2 Only export table structure

mysqldump -u root -p --set-gtid-purged=OFF -d database table1 table2 > mysqldump.sql

-d parameter: equivalent to -–no-data, meaning that no data is exported, only the database table structure is exported;

1.1.3 Only export data

mysqldump -u root -p --set-gtid-purged=OFF -t database table1 table2 > mysqldump.sql

-t parameter: equivalent to -no-create-info, meaning that it only exports data without adding a CREATE TABLE statement;

1.1.4 Export a single sheet

(1) Export table structure and data (with where conditions)

mysqldump -u root -p --set-gtid-purged=OFF database table --where "限制条件" > mysqldump.sql

(2) Only export data (with where conditions)

mysqldump -u root -p --set-gtid-purged=OFF --no-create-info database table --where "限制条件" > mysqldump.sql

1.2, export the database

1.2.1 Export all databases

mysqldump -u root -p --all-databases > mysqldump.sql

1.2.2 Export a single database

mysqldump -u root -p --databases db1 > mysqldump.sql

1.2.3 Export multiple databases

mysqldump -u root -p --databases db1 db2 > mysqldump.sql
mysql -h ip地址 -P 端口 -u mysql用户名 -p mysql密码  要导入到的数据库名 < ./文件名 .sql

Two, import

1, mysql command

1.1, import table

mysql -u root -p database < mysqldump.sql

1.2, import the database

mysql -u root -p < mysqldump.sql

Reference: https://www.cnblogs.com/faberbeta/p/msqldump001.html

2, source command

Enter the mysql database console, such as
mysql -u root -p
mysql>use database
and then use the source command, the following parameters are script files (such as .sql used here)
mysql>source d:/dbname.sql

Guess you like

Origin blog.csdn.net/weixin_44578029/article/details/111594549