无法通过sequelize连接到mysql

时间:2013-06-22 10:12:27

标签: mysql node.js sequelize.js

嗨我试图通过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();
};

1 个答案:

答案 0 :(得分: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",