文件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"
}
}文件模型/ 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');
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;我正在用Heroku测试Postgres,这是工作。 https://warm-plateau-49175.herokuapp.com/
我需要更改为MySQL,它不起作用。用用户名和密码绝对是正确的
未处理的拒绝SequelizeAccessDeniedError:ER_ACCESS_DENIED_ERROR:拒绝用户'db_user'@'localhost'的访问(使用密码:YES)
测试mysql