mysql可使用 into outfile 參數(shù)把表中數(shù)據(jù)導(dǎo)出到csv,例如可用以下命令把user表的數(shù)據(jù)導(dǎo)出到user.csv
select?*?from?user?into?outfile?'/tmp/user.csv'?fields?terminated?by?','?optionally?enclosed?by?'"'?lines?terminated?by?'rn
執(zhí)行后,user表的數(shù)據(jù)會(huì)導(dǎo)出到/tmp/user.csv。?
參數(shù)說(shuō)明:
into outfile ‘導(dǎo)出的目錄和文件名’?
指定導(dǎo)出的目錄和文件名
fields terminated by ‘字段間分隔符’?
定義字段間的分隔符
optionally enclosed by ‘字段包圍符’?
定義包圍字段的字符(數(shù)值型字段無(wú)效)
lines terminated by ‘行間分隔符’?
定義每行的分隔符?
問(wèn)題分析
以上命令在mysql5.6下運(yùn)行沒(méi)有問(wèn)題,但在mysql5.7下運(yùn)行則出現(xiàn)了以下錯(cuò)誤。
ERROR?1290?(HY000):?The?MySQL?server?is?running?with?the?--secure-file-priv?option?so?it?cannot?execute?this?statement
??查看官方文檔,secure_file_priv參數(shù)用于限制LOAD DATA, SELECT …OUTFILE, LOAD_FILE()傳到哪個(gè)指定目錄。
-
secure_file_priv 為?NULL?時(shí),表示限制mysqld不允許導(dǎo)入或?qū)С觥?/p>
-
secure_file_priv 為?/tmp?時(shí),表示限制mysqld只能在/tmp目錄中執(zhí)行導(dǎo)入導(dǎo)出,其他目錄不能執(zhí)行。
-
secure_file_priv?沒(méi)有值時(shí),表示不限制mysqld在任意目錄的導(dǎo)入導(dǎo)出。
??查看?secure_file_priv?的值,默認(rèn)為NULL,表示限制不能導(dǎo)入導(dǎo)出。
mysql>?show?global?variables?like?'%secure_file_priv%'; +------------------+-------+|?Variable_name????|?Value?| +------------------+-------+|?secure_file_priv?|?NULL??| +------------------+-------+1?row?in?set?(0.00?sec)
因?yàn)?secure_file_priv?參數(shù)是只讀參數(shù),不能使用set global命令修改。
mysql>?set?global?secure_file_priv=''; ERROR?1238?(HY000):?Variable?'secure_file_priv'?is?a?read?only?variable
解決方法
打開my.cnf 或 my.ini,加入以下語(yǔ)句后重啟mysql。
secure_file_priv=''
??查看secure_file_priv修改后的值
mysql>?show?global?variables?like?'%secure_file_priv%'; +------------------+-------+|?Variable_name????|?Value?| +------------------+-------+|?secure_file_priv?|???????| +------------------+-------+1?row?in?set?(0.00?sec)
修改后再次執(zhí)行,成功導(dǎo)出。
';
執(zhí)行后,user表的數(shù)據(jù)會(huì)導(dǎo)出到/tmp/user.csv。
參數(shù)說(shuō)明:
into outfile ‘導(dǎo)出的目錄和文件名’
指定導(dǎo)出的目錄和文件名
fields terminated by ‘字段間分隔符’
定義字段間的分隔符
optionally enclosed by ‘字段包圍符’
定義包圍字段的字符(數(shù)值型字段無(wú)效)
lines terminated by ‘行間分隔符’
定義每行的分隔符
問(wèn)題分析
以上命令在mysql5.6下運(yùn)行沒(méi)有問(wèn)題,但在mysql5.7下運(yùn)行則出現(xiàn)了以下錯(cuò)誤。
ERROR?1290?(HY000):?The?MySQL?server?is?running?with?the?--secure-file-priv?option?so?it?cannot?execute?this?statement
? 查看官方文檔,secure_file_priv參數(shù)用于限制LOAD DATA, SELECT …OUTFILE, LOAD_FILE()傳到哪個(gè)指定目錄。
-
secure_file_priv 為 NULL 時(shí),表示限制mysqld不允許導(dǎo)入或?qū)С觥?/p>
-
secure_file_priv 為 /tmp 時(shí),表示限制mysqld只能在/tmp目錄中執(zhí)行導(dǎo)入導(dǎo)出,其他目錄不能執(zhí)行。
-
secure_file_priv 沒(méi)有值時(shí),表示不限制mysqld在任意目錄的導(dǎo)入導(dǎo)出。
? 查看 secure_file_priv 的值,默認(rèn)為NULL,表示限制不能導(dǎo)入導(dǎo)出。
mysql>?show?global?variables?like?'%secure_file_priv%'; +------------------+-------+|?Variable_name????|?Value?| +------------------+-------+|?secure_file_priv?|?NULL??| +------------------+-------+1?row?in?set?(0.00?sec)
因?yàn)?secure_file_priv 參數(shù)是只讀參數(shù),不能使用set global命令修改。
mysql>?set?global?secure_file_priv=''; ERROR?1238?(HY000):?Variable?'secure_file_priv'?is?a?read?only?variable
解決方法
打開my.cnf 或 my.ini,加入以下語(yǔ)句后重啟mysql。
secure_file_priv=''
? 查看secure_file_priv修改后的值
mysql>?show?global?variables?like?'%secure_file_priv%'; +------------------+-------+|?Variable_name????|?Value?| +------------------+-------+|?secure_file_priv?|???????| +------------------+-------+1?row?in?set?(0.00?sec)
修改后再次執(zhí)行,成功導(dǎo)出。
mysql>?select?*?from?user?into?outfile?'/tmp/user.csv'?fields?terminated?by?','?optionally?enclosed?by?'"'?lines?terminated?by?'rn'; Query?OK,?15?rows?affected?(0.00?sec)
? ??