nodejs Sequelize6连接mysql8的配置怎么写

官方文档的构造器很多,这里仅展示一种
const { Sequelize } = require('sequelize');
/**
 * dbname
 * username
 * password
 * options
 */
const sequelize = new Sequelize('dbname', 'username', 'password', {
    host: '127.0.0.1',  //注意一定不要写成localhost,不然会连不上数据库,报最后附上的错误
    dialect: 'mysql',
    port: 3306,
    timezone: '+08:00',
});
 
async function connect() {
    try {
        await sequelize.authenticate();
        console.log('Connection has been established successfully.');
    } catch (error) {
        console.error('Unable to connect to the database:', error);
    }
}
 
connect()
 
##################################################
host写localhost会报的错,改成ipv4地址连接就行了

original: Error: connect ECONNREFUSED ::1:3306
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1555:16) {
errno: -4078,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '::1',
port: 3306,
fatal: true
}

posted @ 2024-06-20 19:12  idonotcare  阅读(93)  评论(0)    收藏  举报