如何在node.js应用程序中导入mysqldump?

yrwegjxp  于 2021-06-23  发布在  Mysql
关注(0)|答案(0)|浏览(312)

我找到的唯一解决方案是节点mysql导入器,它没有按预期做任何事情。在控制台中,它显示

importSQL done: ./lib/api/sqldumps/footballers2655c10.sql
DONE!
SQL dump successfully loaded

但数据库里什么都没有!
这是我的密码:

import http from 'http'
import app from './app'
import importer from 'node-mysql-importer'

import db from './api/models'
import config from './api/config'

const port = process.env.PORT || 3000

var mysql = require('mysql')

var con = mysql.createConnection({
  'host': config.db.host,
  'user': config.db.user,
  'password': config.db.password
})

con.connect(function(err) {
  if (err) throw err
  con.query("CREATE DATABASE IF NOT EXISTS footballers", function (err, result) {
    if (err) throw err
    sqldumpImporter ()
  })
})

const server = http.createServer(app)

function sqldumpImporter () {
  importer.config({
    'host': config.db.host,
    'user': config.db.user,
    'password': config.db.password,
    'database': config.db.database
  })

  importer.importSQL('./lib/api/sqldumps/footballers2655c10.sql').then( () => {
    console.log('SQL dump successfully loaded')
    syncSequelize()
  }).catch( err => {
    console.log(`error: ${err}`)
  })
}

function syncSequelize() {
  db.sequelize.sync()
    .then(() => {
      server.listen(port, () => console.log(`listening on port ${port}`))
    })
}

最初没有错误,但由于表不存在,程序停止执行。

ERROR: Error: ER_NO_SUCH_TABLE: Table 'footballers.footballers' doesn't exist
[nodemon] app crashed - waiting for file changes before starting...

执行后数据库仍为空。下面是我尝试加载的sql文件:https://github.com/danivijay/footballers-app/blob/master/server/lib/api/sqldumps/footballers2655c10.sql
这是我的代码库:https://github.com/danivijay/footballers-app/tree/master/server
我的代码出了什么问题?用nodejs导入mysqldump(.sql)文件有更好的解决方案吗?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题