总结将mysql的查询结果导出到文件的方法mysql
select user, host, password from mysql.user into outfile '/tmp/user.xls'; -- 执行上述命令会提示下面的错误 ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement -- 解决1,查看下面这个变量指示的路径,把文件导出到该路径下便可 SHOW VARIABLES LIKE "secure_file_priv"; select user, host, password from mysql.user into outfile '/var/lib/mysql-files/user.xls';
参考:https://stackoverflow.com/questions/32737478/how-should-i-tackle-secure-file-priv-in-mysqllinux
-- 设置 pager cat > /tmp/test.txt -- 验证,执行以下查询控制台不显示,查询结果在/tmp/test.txt文件中 select user, host, password from mysql.user; -- 取消设置 pager
# 写法一: mysql -D mysql -e "select host, user, password from user" > /tmp/user.xls; # 定法二:若是sql过长,能够这样写 mysql -h localhost -uroot -p123456 < t.sql > /tmp/result.txt # t.sql能够这样写 use mysql; select host, user, password from user; # 写法三: mysql -h localhost -uroot -p123456 -e "source t.sql" > /tmp/result.txt
须要执行一个复杂的sql,并将结果导出成excel格式,不能外网联结固不能用navicat等工具导出啦,在服务端经过命令行导出并传到本地。sql
法一:使用into outfile命令,遇到下面状况,放弃shell
-- 提示下面错误 ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement -- 查看下面变量是NULL SHOW VARIABLES LIKE "secure_file_priv";
法二:使用paper命令,导出的格式不方便转成excel,放弃工具
法三:使用mysql命令this
mysql -h xxx.com -uroot -p'password' -e " 复杂的查询SQL " > result-utf8.xls # 还有最重要的一步,在linux中默认是utf-8格式,须要转成gbk格式 iconv -futf8 -tgb2312 -oresult-gbk.xls result-utf8.xls
参考:.net