file config / config.json
{
"development": {
"username": "root",
"password": null,
"database": "example",
"host": "example.sqlite",
"dialect": "sqlite"
},
"stage": {
"username": "db_user",
"password": "db_password",
"database": "db_name",
"host": "localhost",
"dialect": "mysql"
},
"production": {
"username": "root",
"password": null,
"database": "example",
"host": "example.sqlite",
"dialect": "sqlite"
}
}
file models / index.js
'use strict';
var fs = require('fs');
var path = require('path');
var Sequelize = require('sequelize');
var basename = path.basename(module.filename);
var env = process.env.NODE_ENV || 'development';
var config = require(__dirname + '/../config/config.json')[env];
var db = {};
if (process.env.DATABASE_URL) {
var match = process.env.DATABASE_URL.match(/postgres:\/\/([^:]+):([^@]+)@([^:]+):(\d+)\/(.+)/);
sequelize = new Sequelize(match[5], match[1], match[2], {
dialect: 'postgres',
protocol: 'postgres',
port: match[4],
host: match[3],
logging: true //false
})
} else {
var sequelize = new Sequelize(config.database, config.username, config.password, config);
}
fs
.readdirSync(__dirname)
.filter(function(file) {
return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
})
.forEach(function(file) {
var model = sequelize['import'](path.join(__dirname, file));
db[model.name] = model;
});
Object.keys(db).forEach(function(modelName) {
if (db[modelName].associate) {
db[modelName].associate(db);
}
});
db.sequelize = sequelize;
db.Sequelize = Sequelize;
module.exports = db;
我正在使用Postgres对heroku进行测试,这是有效的。 https://warm-plateau-49175.herokuapp.com/
我需要更改为MySQL,它不起作用。用户名和密码肯定是正确的
未处理拒绝SequelizeAccessDeniedError:ER_ACCESS_DENIED_ERROR:用户'db_user'@'localhost'拒绝访问(使用密码:YES)