利用mysqldump命令导出为csv格式文件

解决方法

先导出为txt文件,其内容是以逗号“,”分隔的,得到txt文件后,再自行处理为.csv或者.xls文件。

参数说明:

-t, --no-create-info   Don't write table creation info.

-T, --tab=name Create tab-separated textfile for each table to given path. (Create .sql and .txt files.)  NOTE: This only works if mysqldump is run on the same machine as the mysqld server.

--fields-terminated-by=name  Fields in the output file are terminated by the given string.

--fields-enclosed-by=name   Fields in the output file are enclosed by the given character.

1. 先查看可导出位置(不然会涉及到权限问题)

mysql> show variables like 'secure_file_priv';
+------------------+-----------------------+
| Variable_name | Value |
+------------------+-----------------------+
| secure_file_priv | /var/lib/mysql-files/ |   //如果出现权限问题,尝试把该目录所有者改成mysql
+------------------+-----------------------+
row in set (0.00 sec)

2. 执行导出命令

[root@server-10 ~]# mysqldump -uroot -p -t -T /var/lib/mysql-files/  mydb customers  --fields-terminated-by=',' --fields-enclosed-by='\"'

3. 查看一下

[root@server- ~]# ls -l /var/lib/mysql-files/
total
-rw-r--r-- root root Aug : customers.sql    //会同步生成同名sql文件,内容为空
-rw-rw-rw- mysql mysql Aug : customers.txt
[root@server- ~]# cat /var/lib/mysql-files/customers.txt
"","Coyote Inc.","200 Maple Lane","Detroit","MI","","USA","Y Lee","ylee@coyote.com"
"","Mouse House","333 Fromage Lane","Columbus","OH","","USA","Jerry Mouse",\N
"","Wascals","1 Sunny Place","Muncie","IN","","USA","Jim Jones","rabbit@wascally.com"
"","Yosemite Place","829 Riverside Drive","Phoenix","AZ","","USA","Y Sam","sam@yosemite.com"
"","E Fudd","4545 53rd Street","Chicago","IL","","USA","E Fudd",\N

4. 如果不想每个字段带冒号,则省略--fields-enclosed-by='\"'即可

[root@server- ~]# mysqldump -uroot -p -t -T /var/lib/mysql-files/  mydb customers  --fields-terminated-by=','

5. 再查看一下输出的变化

[root@server- ~]# ls -l /var/lib/mysql-files/
total
-rw-r--r-- root root Aug : customers.sql
-rw-rw-rw- mysql mysql Aug : customers.txt
[root@server- ~]# cat /var/lib/mysql-files/customers.txt
,Coyote Inc., Maple Lane,Detroit,MI,,USA,Y Lee,ylee@coyote.com
,Mouse House, Fromage Lane,Columbus,OH,,USA,Jerry Mouse,\N
,Wascals, Sunny Place,Muncie,IN,,USA,Jim Jones,rabbit@wascally.com
,Yosemite Place, Riverside Drive,Phoenix,AZ,,USA,Y Sam,sam@yosemite.com
,E Fudd, 53rd Street,Chicago,IL,,USA,E Fudd,\N

结束.

上一篇:matlab问题集总


下一篇:Eclipse里面的一些常规设置