使用 sequelize-automate 时出错 "ConnectionError [SequelizeConnectionError]: Failed to connect to localhost:1443 - Could not connect (sequence)"

Error "ConnectionError [SequelizeConnectionError]: Failed to connect to localhost:1443 - Could not connect (sequence)" using sequelize-automate

尝试使用 sequelize-automate 自动创建数据库 table 模型时出现连接错误。

但是,我用于 sequelize 的连接有效,但格式不同,我不确定 sequelize-automate 是否允许我使用相同的格式。

const Sequelize = require('sequelize');
const util = require('util');
const dbSequelize = require('../public/dbconfigSequelize.json');

let databaseUrl = util.format(
  dbSequelize.server,
  dbSequelize.username,
  dbSequelize.password,
  dbSequelize.host,
  dbSequelize.database
)

function getConnection() {
  console.log('creating a new connection');
  return new Sequelize(databaseUrl, {
    pool: {
      max: 1
    },
    logging: console.log
  });
}

function checkAuthenticate() {
  sequelize
    .authenticate()
    .then(() => {
      console.log('Connection has been established successfully.');
    })
    .catch(err => {
      console.error('Unable to connect to the database:', err);
    });
}

const sequelize = getConnection();
checkAuthenticate();

module.exports = sequelize;

package.json

"scripts": {
    "generate-sequelize-models": "sequelize-automate -c sequelize-automate.config.json"
  }

续集-automate.config.json

{
  "dbOptions": {
    "database": "mydbname",
    "username": "myusername",
    "password": "mypassword",
    "dialect": "mssql",
    "host": "localhost",
    "logging": false
  },
  "options": {
    "type": "js",
    "dir": "./server/models"
  }
}

错误:

PS C:\Users\username\Documents\myelectronapp> npm run generate-sequelize-models

> myelectronapp@0.1.0 generate-sequelize-models C:\Users\username\Documents\myelectronapp
> sequelize-automate -c sequelize-automate.config.json

Database options: {"database":"mydbname","username":"myusername","password":"****","dialect":"mssql","host":"localhost","port":3306,"logging":false}
Automate options: {"type":"js","camelCase":false,"dir":"./server/models","emptyDir":false}
ConnectionError [SequelizeConnectionError]: Failed to connect to localhost:3306 - Could not connect (sequence)
    at C:\Users\username\Documents\myelectronapp\node_modules\sequelize\lib\dialects\mssql\connection-manager.js:134:17
    at tryCatcher (C:\Users\username\Documents\myelectronapp\node_modules\bluebird\js\release\util.js:16:23)
    at Promise._settlePromiseFromHandler (C:\Users\username\Documents\myelectronapp\node_modules\bluebird\js\release\promise.js:547:31)
    at Promise._settlePromise (C:\Users\username\Documents\myelectronapp\node_modules\bluebird\js\release\promise.js:604:18)
    at Promise._settlePromise0 (C:\Users\username\Documents\myelectronapp\node_modules\bluebird\js\release\promise.js:649:10)
    at Promise._settlePromises (C:\Users\username\Documents\myelectronapp\node_modules\bluebird\js\release\promise.js:725:18)
    at _drainQueueStep (C:\Users\username\Documents\myelectronapp\node_modules\bluebird\js\release\async.js:93:12)
    at _drainQueue (C:\Users\username\Documents\myelectronapp\node_modules\bluebird\js\release\async.js:86:9)
    at Async._drainQueues (C:\Users\username\Documents\myelectronapp\node_modules\bluebird\js\release\async.js:102:5)
    at Immediate.Async.drainQueues [as _onImmediate] (C:\Users\username\Documents\myelectronapp\node_modules\bluebird\js\release\async.js:15:14)
    at processImmediate (internal/timers.js:456:21) {
  name: 'SequelizeConnectionError',
  parent: ConnectionError: Failed to connect to localhost:3306 - Could not connect (sequence)
      at ConnectionError (C:\Users\username\Documents\myelectronapp\node_modules\tedious\lib\errors.js:13:12)
      at Connection.socketError (C:\Users\username\Documents\myelectronapp\node_modules\tedious\lib\connection.js:1288:56)
      at C:\Users\username\Documents\myelectronapp\node_modules\tedious\lib\connection.js:1114:21
      at SequentialConnectionStrategy.connect (C:\Users\username\Documents\myelectronapp\node_modules\tedious\lib\connector.js:87:14)
      at Socket.onError (C:\Users\username\Documents\myelectronapp\node_modules\tedious\lib\connector.js:100:12)
      at Socket.emit (events.js:311:20)
      at emitErrorNT (internal/streams/destroy.js:92:8)
      at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
      at processTicksAndRejections (internal/process/task_queues.js:84:21) {
    message: 'Failed to connect to localhost:3306 - Could not connect (sequence)',
    code: 'ESOCKET'
  },
  original: ConnectionError: Failed to connect to localhost:3306 - Could not connect (sequence)
      at ConnectionError (C:\Users\username\Documents\myelectronapp\node_modules\tedious\lib\errors.js:13:12)
      at Connection.socketError (C:\Users\username\Documents\myelectronapp\node_modules\tedious\lib\connection.js:1288:56)
      at C:\Users\username\Documents\myelectronapp\node_modules\tedious\lib\connection.js:1114:21
      at SequentialConnectionStrategy.connect (C:\Users\username\Documents\myelectronapp\node_modules\tedious\lib\connector.js:87:14)
      at Socket.onError (C:\Users\username\Documents\myelectronapp\node_modules\tedious\lib\connector.js:100:12)
      at Socket.emit (events.js:311:20)
      at emitErrorNT (internal/streams/destroy.js:92:8)
      at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
      at processTicksAndRejections (internal/process/task_queues.js:84:21) {
    message: 'Failed to connect to localhost:3306 - Could not connect (sequence)',
    code: 'ESOCKET'
  }
}

检查一下,这不是来自 sequelize-automate 但很可能你的问题无论如何都不是特定的,否则其他人会很好地记录它

https://github.com/typeorm/typeorm/issues/2133

不禁注意到标题中的错误与上面跟踪中的错误不同...为什么它使用 mySQL 的默认端口而不是 mssql

在我的例子中,错误出现是因为我错了 dialectmssql 而不是 mysql

const sequelize = new Sequelize('Users', 'admin', 'pswd', {
    host: 'localhost',
    port: 3307,
    dialect: 'mssql'
});

dialect 更新为 mysql 修复了它。