How should I tackle --secure-file-priv in MySQL?

前端 未结 21 1521
攒了一身酷
攒了一身酷 2020-11-22 06:04

I am learning MySQL and tried using a LOAD DATA clause. When I used it as below:

LOAD DATA INFILE \"text.txt\" INTO table mytable;
21条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-22 06:12

    I created a NodeJS import script if you are running nodeJS and you data is in the following form (double quote + comma and \n new line)

    INSERT INTO  VALUEs( **CSV LINE **)
    

    This one is configured to run on http://localhost:5000/import.

    I goes line by line and creates query string

    "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  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 is the config file

    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;
    
    

    Disclaimer: This is not a perfect solution - I am only posting it for devs who are under a timeline and have lots of data to import and are encountering this ridiculous issue. I lost a lot of time on this and I hope to spare another dev the same lost time.

提交回复
热议问题