0

嗨,我正在尝试通过 sequelize 连接到我的 MySQL 数据库,但无济于事..

我提供的连接数据是 100% 正确的,我可以通过 phpmyadmin 连接到我的 MySQL 数据库。

这是我的代码:

var Sequelize = require('sequelize-mysql').sequelize;

var dbConnector = function()
{
    var sequelize = new Sequelize
    (
        'dbname',
        'userName',
        'password',
        {
        host: "ip",
        port: port,
        dialect: 'mysql'
        }
    );

var getCoutries = function ()
{
    sequelize.query("SELECT * FROM `Country`")
        .success
        (
            function(myTableRows)
            {
                console.log(myTableRows)
            }
        )
        .error
        (
            function(error)
            {
                console.log(error); 
                      // geting an error:
                      //[Error: connect ECONNREFUSED]
                      //code: 'ECONNREFUSED',
                      //errno: 'ECONNREFUSED',
                      //syscall: 'connect',
                      //fatal: true }
            }
        )
    ;
    }

    getCoutries();
};
4

1 回答 1

0
var Sequelize = require('sequelize').Sequelize;

var dbConnector = function () {
    const DB_NAME = 'DB_Name'
    const DB_USER = 'root'
    const DB_PASSWORD = ''

    var sequelize = new Sequelize(DB_NAME, DB_USER, DB_PASSWORD,
        {
            host: "127.0.0.1",
            port: 3306,
            dialect: 'mysql'
        }
    );

    var getCoutries = function () {
        sequelize.query("SELECT * FROM `processor`").then(myTableRows => {
            console.log(myTableRows)
        }).catch(err => {
            console.log(err)
        })
    }

    getCoutries();
};

dbConnector()
"mysql2": "^2.2.5",
"sequelize": "^6.5.0",
于 2021-03-09T16:55:19.463 回答