方法一
# 链接到数据库
mysql -u 数据库登录用户名 -p[“密码”] [-h 数据库ip地址] [-P 端口号] [数据库名]
# 设置要保存数据的文件
pager cat > /tmp/test.txt ;
# 之后的所有查询结果都自动写入/tmp/test.txt',并前后覆盖
select * from table_name ;
方法二
# 链接到数据库
mysql -u 数据库登录用户名 -p[“密码”] [-h 数据库ip地址] [-P 端口号] [数据库名]
# 执行导出语句
mysql> select count(1) from table_name into outfile '/tmp/test.xls';
Query OK, 31 rows affected (0.00 sec)
在目录/tmp/下会产生文件test.xls
注意:需要在my.cof中设置secure-file-priv=’’,然后重启服务service mysqld restart。
方法三
mysql -h 127.0.0.1 -u root -p XXXX -P 3306 -e "select * from table" > /tmp/test/txt
注意:mysql会提示不支持在命令行输入密码,仍然输出结果。
如:mysql: [Warning] Using a password on the command line interface can be insecure.
原文:https://blog.csdn.net/qq_27870421/article/details/94554266