使用nodejs将.sql转储文件加载到新数据库中

时间:2017-06-13 10:49:18

标签: mysql node.js

我有.sql转储文件,我想加载到新创建的数据库中。我已经尝试了几种技术,但只有node-mysql-importer一个结果。在较大的转储文件上也失败了。说4MB。在较大的文件上,节点会因系统扬声器频繁发出哔声而崩溃。

这是我正在尝试的代码。有人可以帮我从这里出去吗。

"use strict"

module.exports = function (models){

    function create (data){

        var importer = require('node-mysql-importer')

        importer.config({
            'host': 'rspstaging.ckj1nkr8t9bx.us-east-1.rds.amazonaws.com',
            'user': 'rspadmin',
            'password': 'rspadmin123',
            'database': 'mydb'
        })

        return importer.importSQL('sms.sql').then( () => {
            console.log('all statements have been executed')
            return{status:true, code: 200, data: "all statements have been executed"}
        }).catch( err => {
            console.log(`error: ${err}`)
            return{status: false, code: 400, data: "error"}
        })
    }



    function create (){
        var mysql = require('mysql');
        var fs = require('fs');
        var readline = require('readline');
        var myCon = mysql.createConnection({
           host: 'rspstaging.ckj1nkr8t9bx.us-east-1.rds.amazonaws.com',
           port: '3306',
           database: 'mydb',
           user: 'rspadmin',
           password: 'rspadmin123'
        });
        var rl = readline.createInterface({
          input: fs.createReadStream('./sms.sql'),
          terminal: false
         });
        rl.on('line', function(chunk){
            myCon.query(chunk.toString('ascii'), function(err, sets, fields){
             if(err) console.log(err);
            });
        });
        rl.on('close', function(){
          console.log("finished");
          myCon.end();
          return{status:true, code: 200, data: "all statements have been executed"}
        });

    }


    function create (){
        var execsql = require('execsql'),
        dbConfig = {
            host: 'rspstaging.ckj1nkr8t9bx.us-east-1.rds.amazonaws.com',
            user: 'rspadmin',
            password: 'rspadmin123'
        },
        sql = 'use mydb;',
        sqlFile = '/sms.sql';
        execsql.config(dbConfig)
        .exec(sql)
        .execFile(sqlFile, function(err, results){
            console.log(results);
        }).end();
    }

    function create(){
            var sync = require('db-sync'),
            dbConfig = {
                host: 'rspstaging.ckj1nkr8t9bx.us-east-1.rds.amazonaws.com',
                database: 'mydb',
                user: 'rspadmin',
                password: 'rspadmin123'
            },
            tableBlacklist = [];

        sync.fill(dbConfig, './sms.sql');
    }


    return {
        create: create
    }
};

0 个答案:

没有答案