我正在学习MySQL,并尝试使用LOAD DATA子句。当我使用它时,如下所示:

LOAD DATA INFILE "text.txt" INTO table mytable;

我得到了以下错误:

MySQL服务器正在使用——secure-file-priv选项运行,所以它不能执行这条语句

如何解决这个错误?

我已经检查了关于相同错误信息的另一个问题,但仍然找不到解决方案。

我使用的是MySQL 5.6


当前回答

@vhu我做了SHOW变量像“secure_file_priv”;它返回C:\ProgramData\MySQL\MySQL Server 8.0\Uploads\所以当我插入它时,它仍然不工作。

当我直接打开my.ini文件时,我发现路径的格式有点不同:

C:/ProgramData/MySQL/MySQL Server 8.0/Uploads

当我用它运行时,它工作了。唯一不同的是斜线的方向。

其他回答

对我有用的是:

Put your file inside of the folder specified in secure-file-priv. To find that type: mysql> show variables like "secure_file_priv"; Check if you have local_infile = 1. Do that typing: mysql> show variables like "local_infile"; If you get: +---------------+-------+ | Variable_name | Value | +---------------+-------+ | local_infile | OFF | +---------------+-------+ Then set it to one typing: mysql> set global local_infile = 1; Specify the full path for your file. In my case: mysql> load data infile "C:/ProgramData/MySQL/MySQL Server 8.0/Uploads/file.txt" into table test;

我创建了一个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;

免责声明:这不是一个完美的解决方案-我只是发布给那些在时间轴下有大量数据要导入的开发人员,并且遇到了这个荒谬的问题。我在这方面损失了很多时间,我希望其他开发人员也能节省同样的时间。

我在windows 10上遇到了这个问题。为了解决这个问题,我做了以下工作。

在windows搜索(左下角)中,我输入了“powershell”。 右键单击powershell并以管理员身份运行。 导航到服务器bin文件。(C:\Program Files\MySQL\MySQL Server 5.6\bin); 键入。/ mysqld “输入”

服务器按预期启动。

如果文件是本地的,请在命令中使用local

LOAD DATA LOCAL INFILE "text.txt" INTO table mytable;

MySQL使用这个系统变量来控制导入文件的位置

mysql> SHOW VARIABLES LIKE "secure_file_priv";
+------------------+-------+
| Variable_name    | Value |
+------------------+-------+
| secure_file_priv | NULL  |
+------------------+-------+

所以问题是如何改变系统变量,如secure_file_priv。

关闭mysqld Sudo mysqld_safe——secure_file_priv=""

现在你可能会看到:

mysql> SHOW VARIABLES LIKE "secure_file_priv";
+------------------+-------+
| Variable_name    | Value |
+------------------+-------+
| secure_file_priv |       |
+------------------+-------+