mysql查询结果输出跳出平台限制小妙招
发布于 2021-10-12 20:50
Mysql运维工作中,业务方会有很多导数需求,但很多平台做了限制,在语句级禁止了sql查询结果的导出。
例如:
select distinct concat(column_name,',') from information_schema.columns where table_name='test' into outfile '/data/test/test.txt' ;
报错ERROR:
ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement
查询对应参数显示
secure_file_prive=null,表明平台限制了mysqld 不允许导入导出。
可以通过修改参数的方法,实现into outfile 导出功能(设置完后需重启数据库才会生效)。
提问:怎样在不修改参数,不重启DB的情况下完成sql查询结果的导出?
下面分享两个小方法(任选其一就好):
Solution 1:
## 设置查询都结果都自动写入文件
Mysql> pager cat > /data/test/test.txt
PAGER set to 'cat > /data/test/test.txt'
## 查询test表的字段名
Mysql> select distinct concat(column_name,',') from information_schema.columns where table_name='test';
## 取消写入到文件
mysql> pager
Default pager wasn't set, using stdout.
注意: pager cat > 用“>”时,写入多条结果只会保存最后一条;若要保存多条,用追加“>>”。
## 平面文件可以进行任意替换编辑
sed -i 's/|//g' /data/test/test.txt
Solution 2:(三种格式,任选其一就好)
在shell 中执行 mysql 语句,将查询结果写入文件
格式A:
$ mysql -h localhost -uuser -p123456 -D mydb -e " select distinct concat(column_name,',') from information_schema.columns where table_name='test'" > /data/test/test.txt
格式B:
$ mysql -h localhost -uuser -p123456 < case.sql >/data/test/test.txt
$ cat case.sql
use mydb;
select distinct concat(column_name,',') from information_schema.columns where table_name='test';
格式C:
mysql -h localhost -uuser -p123456 -e "source case.sql" > /data/test/test.txt
本文来自网络或网友投稿,如有侵犯您的权益,请发邮件至:aisoutu@outlook.com 我们将第一时间删除。
相关素材