8

我需要将数据从表导出到 csv。我有以下结构(不是我的表,而是用于演示目的)

CREATE TABLE `mytable` (
  `id` int(11) DEFAULT NULL,
  `mycolumn` varchar(25) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1 

有数据(大约 3000 条记录)。现在我想导出其中一些记录(来自我通过 cronjob 运行的脚本)

SELECT * INTO OUTFILE '/tmp/mytable.sql'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
FROM mytable;

表明:

Query OK, 3 rows affected (0.00 sec)

然后我做:

ls: cannot access /tmp/mytable.sql: No such file or directory

我的文件在哪里?

4

1 回答 1

8

当您使用该INTO OUTFILE命令时,它会将数据导出到服务器的本地文件夹,而不是您正在执行查询的文件夹。

示例:您在计算机上(ip:192.168.0.100)并使用 mysql 命令连接到 mysqlserver(ip:192.168.0.101)mysql -uuser -h192.168.0.101 -A database:. 通过执行该SELECT * INTO OUTFILE文件将保存在 mysqlserver (ip: 192.168.0.101) 而不是您的计算机 (ip: 192.168.0.100) 上。

Now, you can use a script that creates a CSV file (in your cronjob - you select all the data, generate the file and send via scp to the other server).

Or - You can also have a NFS mounted on /shared/ and when you create the file automatically the other server has it.

Or - you can simply run a mysql command in a bash script like this from your first server.

mysql -uroot test -B -e "select * from test.mytable;" | sed 's/\t/","/g;s/^/"/;s/$/"/;s/\n//g' > /tmp/filename.csv

source: http://tlug.dnho.net/node/209

于 2012-01-26T02:11:53.757 回答