Mysqldump

来自姬鸿昌的知识库
Jihongchang讨论 | 贡献2024年7月10日 (三) 04:32的版本
跳到导航 跳到搜索

mysqldump 命令可以备份数据库中的数据。

备份时是在备份文件中保存了 create 语句和 insert 语句。

mysqldump -u root -pPassword -T 目标目录 dbname table [option];

Password 参数表示 root 用户的密码,密码紧挨着 -p 选项;

目标目录 参数是指导出的文本文件的路径;

dbname 参数表示数据库的名称;

table 参数表示表的名称;

option 表示附件选项。


示例

C:\Users\Administrator>mysqldump -u root -p123456 -T E:\record\2024\7\10\ testdb student "--fields-terminated-by=," "--fields-optionally-enclosed-by=""
mysqldump: [Warning] Using a password on the command line interface can be insecure.
mysqldump: Got error: 1290: The MySQL server is running with the --secure-file-priv option so it cannot execute this statement when executing 'SELECT INTO OUTFILE'

"--fields-terminated-by=," 设置 , 为字段的分隔符,默认值是"\t";

"--fields-optionally-enclosed-by=""设置字符括上 char、varchar 和 text 等字符型字段

出现

mysqldump: [Warning] Using a password on the command line interface can be insecure.
mysqldump: Got error: 1290: The MySQL server is running with the --secure-file-priv option so it cannot execute this statement when executing 'SELECT INTO OUTFILE'

的原因是安装MySQL的时候限制了导入与导出的目录权限,只允许在规定的目录下才能导出、导入。 可以通过

show variables like "secure_file_priv";

查看允许导出、导入的目录位置

Variable_name Value
secure_file_priv C:\ProgramData\MySQL\MySQL Server 5.7\Uploads\

所以就只能导出到 "C:\ProgramData\MySQL\MySQL Server 5.7\Uploads\" 这个位置


然后又因为路径中包含空格,所以需要用双引号括起来,又因为是 Windows ,还需要把路径中的 "\" 换成 "/",最终就是

C:\ProgramData\MySQL>mysqldump -u root -p123456 --tables testdb student -T "C:/ProgramData/MySQL/MySQL Server 5.7/Uploads/"
mysqldump: [Warning] Using a password on the command line interface can be insecure.

然后就能看到目标目录下生成了两个文本文件,分别是

"C:\ProgramData\MySQL\MySQL Server 5.7\Uploads\student.sql" DDL

和 "C:\ProgramData\MySQL\MySQL Server 5.7\Uploads\student.txt" 数据