Express.js通过Sequelize.js连接MySQL数据库

创建连接

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
const Sequelize = require('sequelize');

const sequelize = new Sequelize('数据库名', '数据库账号', '数据库密码', {
host: '主机名',
dialect: 'mysql',
port: '端口',
operatorsAliases: false,
timezone: '+08:00', // 时区,中国+8
pool: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000
}
});

sequelize
.authenticate()
.then(() => {
console.log('***数据库连接成功');
})
.catch(err => {
console.error('数据库连接失败:', err);
});

module.exports = sequelize;
阅读更多