我阅读了关于此的每一个帖子,但没有一个解决方案适用于我的情况。我做了一个函数将MySQL数据库表转储到CSV / TSV文件,继承代码:
function db_to_tsv($conn,$table) {
$file_path = "C:/wamp64/tmp/{$table}_dump.tsv";
$columns_array = $conn->query("SELECT * FROM $table LIMIT 1")->fetch();
unset($columns_array['id']);
$columns = implode(', ',array_keys($columns_array));
$headers_array = array_map(
function($value,$key) { return "'$key'"; },
$columns_array,
array_keys($columns_array)
);
$headers = implode(', ',$headers_array);
$query = "(SELECT $headers)
UNION
(SELECT $columns
FROM $table
INTO OUTFILE '$file_path'
FIELDS ENCLOSED BY '\"'
TERMINATED BY '\n'
ESCAPED BY '\"'
LINES TERMINATED BY '\r\n');
";
$export = $conn->query($query);
}
所以SQL查询是这样的:
(SELECT $headers)
UNION
(SELECT $columns
FROM $table
INTO OUTFILE 'C:/wamp64/tmp/{$table}_dump.csv'
FIELDS ENCLOSED BY '\"'
TERMINATED BY '\n'
ESCAPED BY '\"'
LINES TERMINATED BY '\r\n');
";
当我运行该函数时,我收到有关secure-file-priv
被设置的错误。
当我运行SHOW VARIABLES LIKE "secure_file_priv"
时,它会输出:
这就是我将输出文件指向C:/wamp64/tmp
显示出现的错误:
PS C:\wamp64\www\spider\cheb> php dump.php actions
"Dumping actions to C:/wamp64/actions_dump.tsv"
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error: 1290 The MySQL server is running with the --secure-file-priv option so it cannot execute this statement' in C:\wamp64\
www\spider\cheb\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver\PDOConnection.php:104
Stack trace:
#0 C:\wamp64\www\spider\cheb\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver\PDOConnection.php(104): PDO->query('(SELECT 'name',...')
#1 C:\wamp64\www\spider\cheb\vendor\doctrine\dbal\lib\Doctrine\DBAL\Connection.php(935): Doctrine\DBAL\Driver\PDOConnection->query('(SELECT 'name',...')
#2 C:\wamp64\www\spider\cheb\inc\spider_tools.php(275): Doctrine\DBAL\Connection->query('(SELECT 'name',...')
#3 C:\wamp64\www\spider\cheb\dump.php(9): Spider\db_to_tsv(Object(Doctrine\DBAL\Connection), 'actions')
#4 {main}
Next exception 'Doctrine\DBAL\Driver\PDOException' with message 'SQLSTATE[HY000]: General error: 1290 The MySQL server is running with the --secure-file-priv option so it cannot execute this statement' in C:\wam
p64\www\ in C:\wamp64\www\spider\cheb\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver\AbstractMySQLDriver.php on line 115
导入TSV文件时遇到了同样的问题:
LOAD DATA INFILE 'c:/wamp64/tmp/chebiId_inchi.tsv'
INTO TABLE chebi_inchi
FIELDS TERMINATED BY "\t"
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS
(chebi_id, inchi)
SET ID = NULL ;
但是我通过在代码中添加LOCAL
来解决这个问题,所以这很有效:
LOAD DATA LOCAL INFILE 'c:/wamp64/tmp/chebiId_inchi.tsv'
INTO TABLE chebi_inchi
FIELDS TERMINATED BY "\t"
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS
(chebi_id, inchi)
SET ID = NULL ;
但是对于输出文件,没有LOCAL选项。我尝试将此行secure_file_priv=''
添加到my.ini
文件的顶部,但这会导致某种错误,我无法在那里打开MySQL控制台。
如何停用secure-file-priv
设置?