#$2 为数据库用户 、$3 为数据库密码
mysql -u$2 -p$3 << EOF
#切换到指定数据库
use 数据库名称
# 排除指定库表不清空
select concat('truncate table ',table_name,';') from information_schema.tables where table_schema='数据库名称'
and TABLE_TYPE = 'BASE TABLE' and table_name
NOT IN('排除的表名') into outfile '/home/truncate.sql';
# 指定库表清空
# select concat('truncate table ',table_name,';') from information_schema.tables # where table_schema='数据库名称'
# and TABLE_TYPE = 'BASE TABLE' and table_name
# IN('要清空的表名') into outfile '/home/truncate.sql';
# into outfile 是输出的sql脚本文件地址
source /home/truncate.sql;
exit
EOF
需要修改的一些配置
报错 1
The MySQL server is running with the --secure-file-priv option so it cannot execute this statement
#查看
mysql> show variables like '%secure_file_priv%';
secure_file_priv 为 null ,表⽰限制mysqld 不允许导⼊|导出
#找到 打开my.cnf 或 my.ini,加入以下语句后重启mysql
secure-file-priv = ""
当secure_file_priv的值没有具体值时,表⽰不对mysqld 的导⼊|导出做限制
报错 2(windows)
[Err] 1 - Can't create/write to file 'C:/truncate.sql' (Errcode: 13 - Permission denied)
#找到 打开my.cnf 或 my.ini,加入以下语句后重启mysql
tmpdir=D:/mysql/tmp