我正在学习MySQL,并尝试使用LOAD DATA子句。当我使用它时,如下所示:
LOAD DATA INFILE "text.txt" INTO table mytable;
我得到了以下错误:
MySQL服务器正在使用——secure-file-priv选项运行,所以它不能执行这条语句
如何解决这个错误?
我已经检查了关于相同错误信息的另一个问题,但仍然找不到解决方案。
我使用的是MySQL 5.6
我正在学习MySQL,并尝试使用LOAD DATA子句。当我使用它时,如下所示:
LOAD DATA INFILE "text.txt" INTO table mytable;
我得到了以下错误:
MySQL服务器正在使用——secure-file-priv选项运行,所以它不能执行这条语句
如何解决这个错误?
我已经检查了关于相同错误信息的另一个问题,但仍然找不到解决方案。
我使用的是MySQL 5.6
当前回答
它正在按预期工作。你的MySQL服务器已经启动了——secure-file-priv选项,该选项限制你可以使用load DATA INFILE从哪个目录加载文件。
使用SHOW VARIABLES LIKE "secure_file_priv";查看已配置的目录。
你有两个选择:
将文件移动到secure-file-priv指定的目录。 禁用secure-file-priv。必须从启动时删除,并且不能动态修改。要做到这一点,请检查MySQL启动参数(取决于平台)和my.ini。
其他回答
我在这方面遇到了各种各样的问题。我在修改my。cnf和其他版本试图显示的各种疯狂的东西。
对我有用的是:
我得到的错误
MySQL服务器正在使用——secure-file-priv选项运行,所以它不能执行这条语句
我可以通过打开/usr/local/mysql/support-files/mysql来修复它。服务器,更改如下行:
$bindir/mysqld_safe --datadir="$datadir" --pid-file="$mysqld_pid_file_path" -- $other_args >/dev/null &
wait_for_pid created "$!" "$mysqld_pid_file_path"; return_value=$?
to
$bindir/mysqld_safe --datadir="$datadir" --pid-file="$mysqld_pid_file_path" --secure-file-priv="" $other_args >/dev/null &
wait_for_pid created "$!" "$mysqld_pid_file_path"; return_value=$?
我在“secure-file-priv”上也遇到了同样的问题。在.ini文件中注释不起作用,在'secure-file-priv'指定的目录中移动文件也不起作用。
最后,正如dbc建议的那样,使'secure-file-priv'等于一个空字符串就可以工作了。所以如果有人在尝试了上面的答案后被困住了,希望这样做会有所帮助。
对于mysql 8.0版本,你可以这样做:
mysql.server stop
mysql.server start --secure-file-priv=''
这招在Mac High Sierra很管用。
我创建了一个NodeJS导入脚本,如果你正在运行NodeJS,你的数据是以下形式(双引号+逗号和\n新行)
INSERT INTO <your_table> VALUEs( **CSV LINE **)
这个配置为在http://localhost:5000/import上运行。
我逐行创建查询字符串
"city","city_ascii","lat","lng","country","iso2","iso3","id"
"Tokyo","Tokyo","35.6850","139.7514","Japan","JP","JPN","1392685764",
...
server.js
const express = require('express'),
cors = require('cors'),
bodyParser = require('body-parser'),
cookieParser = require('cookie-parser'),
session = require('express-session'),
app = express(),
port = process.env.PORT || 5000,
pj = require('./config/config.json'),
path = require('path');
app.use(bodyParser.json());
app.use(cookieParser());
app.use(cors());
app.use(
bodyParser.urlencoded({
extended: false,
})
);
var Import = require('./routes/ImportRoutes.js');
app.use('/import', Import);
if (process.env.NODE_ENV === 'production') {
// set static folder
app.use(express.static('client/build'));
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
});
}
app.listen(port, function () {
console.log('Server is running on port: ' + port);
});
ImportRoutes.js
const express = require('express'),
cors = require('cors'),
fs = require('fs-extra'),
byline = require('byline'),
db = require('../database/db'),
importcsv = express.Router();
importcsv.use(cors());
importcsv.get('/csv', (req, res) => {
function processFile() {
return new Promise((resolve) => {
let first = true;
var sql, sqls;
var stream = byline(
fs.createReadStream('../PATH/TO/YOUR!!!csv', {
encoding: 'utf8',
})
);
stream
.on('data', function (line, err) {
if (line !== undefined) {
sql = 'INSERT INTO <your_table> VALUES (' + line.toString() + ');';
if (first) console.log(sql);
first = false;
db.sequelize.query(sql);
}
})
.on('finish', () => {
resolve(sqls);
});
});
}
async function startStream() {
console.log('started stream');
const sqls = await processFile();
res.end();
console.log('ALL DONE');
}
startStream();
});
module.exports = importcsv;
Db.js是配置文件
const Sequelize = require('sequelize');
const db = {};
const sequelize = new Sequelize(
config.global.db,
config.global.user,
config.global.password,
{
host: config.global.host,
dialect: 'mysql',
logging: console.log,
freezeTableName: true,
pool: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000,
},
}
);
db.sequelize = sequelize;
db.Sequelize = Sequelize;
module.exports = db;
免责声明:这不是一个完美的解决方案-我只是发布给那些在时间轴下有大量数据要导入的开发人员,并且遇到了这个荒谬的问题。我在这方面损失了很多时间,我希望其他开发人员也能节省同样的时间。
对于运行MySQL 5.6.23的MacOS Mojave,我在写文件时遇到了这个问题,但没有加载它们。(以前版本的Mac OS没有)。由于这个问题的大多数答案都是针对其他系统的,我想我应该发布my.cnf文件来解决这个问题(以及一个套接字问题),以防它对其他Mac用户有帮助。这是/etc/my.cnf
[client]
default-character-set=utf8
[mysqld]
character-set-server=utf8
secure-file-priv = ""
skip-external-locking
(国际化与问题无关。)
没有其他要求。只需要关闭MySQL服务器,然后在首选项中再次打开(我们说的是Mac)。