MySQL 导出 表结构,执行 .sql 文件导入结构或者数据

1、MySQL 导出不同类型的表结构

1.1 导出结构不导出数据

mysqldump -h 主机地址 -u root -p 密码 -d 数据库名  > xxx.sql  # 加 -d 参数

如果发现加了 -d 参数还是会导出数据,可以尝试再加个 --no-data 参数

1.2 导出数据不导出结构

mysqldump -h 主机地址 -u root -p 密码 -t 数据库名  > xxx.sql  

1.3 导出数据和表结构

mysqldump -h 主机地址 -u root -p 密码 数据库名  > xxx.sql # 不加 -d 参数

1.4 导出特定表的结构

多张表(test1,test2,test3)结构及表数据用用空格隔开

mysqldump -h 主机地址  -u root -p 密码 -B 数据库名 --table 表1,表2  > xxx.sql  

1.5 可能得报错

报错1

mysqldump: Got error: 1105: [parser:1149]You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use when using LOCK TABLES

解决方案是加上 --skip-lock-tables 参数

报错2

Unknown table 'column_statistics' in information_schema (1109)

解决方案是加 --column-statistics=0 参数

警告

mysqldump: [Warning] Using a password on the command line interface can be insecure.
Warning: A partial dump from a server that has GTIDs will by default include the GTIDs of all transactions, even those that changed suppressed parts of the database. If you don't want to restore GTIDs, pass --set-gtid-purged=OFF. To make a complete dump, pass --all-databases --triggers --routines --events.
Warning: A dump from a server that has GTIDs enabled will by default include the GTIDs of all transactions, even those that were executed during its extraction and might not be represented in the dumped data. This might result in an inconsistent data dump.

解决方案是加参数--set-gtid-purged=OFF

一条导出成功的命令

mysqldump -h192.0.12.76 -uroot -p12345678 -d mytest_db > mytest_db_structra.sql --skip-lock-tables --column-statistics=0 --no-data --set-gtid-purged=OFF

2、执行 .sql 文件,导入数据

2.1 方案一:登录 mysql 后使用 source 命令

use my_test_db # 进入某个数据库
source xxx/xx/xx.sql

2.2 方案二:直接使用 mysql 命令

shell 直接输入下面的命令

mysql -h 主机地址 -u root -p my_test_db < xxx/xx/xx.sql

3、备份数据库

mysqldump 数据库名 >数据库备份名
mysqldump -A -u用户名 -p密码 数据库名>数据库备份名
mysqldump -d -A --add-drop-table -uroot -p >xxx.sql

4、提示

如果百度或者谷歌搜不到解决方案,不如直接问问 chatgpt,也许它会有解决方案。

5、参考:

Mysql 批量导出表结构(数据)

mysql mysqldump只导出表结构或只导出数据的实现方法

mysqldump导出报错column_statistics

Mysql使用命令执行sql文件

mysql数据库导入sql文件Mysql导入导出.sql文件的方法

MySQL--mysqldump命令详解 (mysqldump 命令大全)

posted @ 2023-02-22 00:05  Lucky小黄人^_^  阅读(351)  评论(0)    收藏  举报