我正在尝试使用Sequelize CLI工具运行数据库迁移,但是我遇到了一个问题,我的ENV变量没有被该工具处理。在github repo中,它在版本2.0.0中说(我在2.4.0)你可以直接访问config/config.js
中的ENV变量,如此,process.env.DB_HOSTNAME,但是我得到一个错误,指示没有值从变量传入
错误:
Unable to connect to database: SequelizeAccessDeniedError: ER_ACCESS_DENIED_ERROR: Access denied for user ''@'localhost' (using password: NO)
config.js:
module.exports = {
"development": {
"username": process.env.LOCAL_USERNAME,
"password": process.env.LOCAL_PASSWORD,
"database": process.env.LOCAL_DATABASE,
"host": "127.0.0.1",
"dialect": "mysql",
"migrationStorageTableName": "sequelize_meta"
},
}
.ENV:
LOCAL_DATABASE="db_name"
LOCAL_USERNAME="root"
LOCAL_PASSWORD="test"
答案 0 :(得分:4)
您忘了要求dotenv
模块:
require('dotenv').config(); // this line is important!
module.exports = {
"development": {
"username": process.env.LOCAL_USERNAME,
"password": process.env.LOCAL_PASSWORD,
"database": process.env.LOCAL_DATABASE,
"host": "127.0.0.1",
"dialect": "mysql",
"migrationStorageTableName": "sequelize_meta"
},
}