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 @   Lucky小黄人^_^  阅读(238)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 一文读懂知识蒸馏
· 终于写完轮子一部分:tcp代理 了,记录一下
历史上的今天:
2020-02-22 剑指offer 36. 两个链表的第一个公共结点
2020-02-22 剑指offer 56.删除有序链表中的重复结点
点击右上角即可分享
微信分享提示