一、使用mysqldump导出
mysqldump -uusername -ppwd -hhostname -PPort database secure_flow_warning > /home/user/a.txt;
mysqldump -u用户名 -p密码 数据库名 表名 --where="筛选条件" > 导出文件路径
例子:
从test数据库的test_data表中导出id大于100的数据到 /tmp/test.sql 这个文件中
mysqldump -uroot -p123456 test test_data --where=" id > 100" > /tmp/test.sql
二、mysql导出select结果到文件
2013-07-02今天导出指定列数据,把之前的事情忘得一干二净。
mysql -hhostname -uuser -Pport -ppassword databasename -e "SELECT id,name from idc_info" > ./aaaa.txt
mysql -hxx -uxx -pxx -e "query statement" db > file
例如:
mysql -uusername -ppwd -hhostname -PPort database -e "select * from table1" > a.t
#在into outfile "/home/user/a.txt"没有权限的情况下,还可以用>导出数据。
mysql -h127.0.0.1 -uroot -p000000 -e "select * from table" test > 1.txt
host ip user password query statement database filename
这样会输出列名信息,如果不想输出列名信息:
mysql -h127.0.0.1 -uroot -p000000 -N -e "select * from table" test > 1.txt
host ip user password query statement database filename
或
mysql -hxxx -uxx -pxx
select * from table into outfile 'xxx.txt';
例如:
mysql -h127.0.0.1 -uroot -p000000
select * from a into outfile '1.txt';
两种方法效果一样的
注:-e 参数应该是export的缩写。
第二种方式的mysql文档:
SELECT [select options go here] INTO {OUTFILE | DUMPFILE} filename
EXPORT_OPTIONS
FROM table_references [additional select options go here]
例如:
mysql -h127.0.0.1 -uroot -p000000
select * from table into outfile "1.txt" fields terminated by '\t' lines terminated by '\r\n'
第一种方法和第二种方法的结合:使用 mysql -e执行导出到文件的sql语句
mysql -hxx -uxx -pxx -e "query statement" db
例如:
mysql -h127.0.0.1 -uroot -p000000 -e "select * from table into outfile '1.txt' fields terminated by ',' lines terminated by '\r\n'" test
如果不想输出列名信息:
mysql -h127.0.0.1 -uroot -p000000 -N -e "select * from table into outfile '1.txt' fields terminated by ',' lines terminated by '\r\n'" test
默认情况下, mysql -e导出的文件,列是用"\t"分隔,行是用"\r\n"分隔(dos),行是用"\n"分隔
(unix 追加一种方式:
select col002,col005,col004,col008 into outfile 'e:/mysql/i0812.txt' fields terminated by '|' lines terminated by '\r\n' from table where col003 in (select col001 from qdbm) order by col005;