I tried searching for the answer, closest question i got was Sequelize CLI Not Finding Env Variables.
I compared my code and it was exactly like the answer provided, then just to debug i edited config file to set values manually instead of reading from .env
but sequelize-cli
still gives same error
ERROR: SequelizeDatabaseError: Access denied for user ''@'localhost' to database 'dbname'
at Query.formatError (/home/name/Documents/nodejs/project/db/node_modules/sequelize/lib/dialects/mysql/query.js:265:16)
at Query.run (/home/name/Documents/nodejs/project/db/node_modules/sequelize/lib/dialects/mysql/query.js:77:18)
at processTicksAndRejections (internal/process/task_queues.js:95:5)
at async /home/name/Documents/nodejs/project/db/node_modules/sequelize/lib/sequelize.js:619:16
at async Object.exports.handler (/home/name/Documents/nodejs/project/db/node_modules/sequelize-cli/lib/commands/database.js:49:7)
Here is my config.js
file
require("dotenv").config();
console.log(process.env.NODE_ENV, "it is being loaded correctly");
const config = {
development: {
username: "mysql",
password: "",
database: "dbname",
host: "localhost",
port: "3306",
dialect: "mysql",
dialectOptions: {
charset: "utf8mb4",
},
},
production: {
username: "mysql",
password: "",
database: "dbname",
host: "localhost",
port: "3306",
dialect: "mysql",
dialect: "mysql",
dialectOptions: {
charset: "utf8mb4",
},
},
};
console.log(config);
module.exports = config;
and lastly here is .sequelizerc
file
const path = require('path');
module.exports = {
'config': path.resolve('config', 'config.js')
}
Funny thing is this project was working perfectly on my last computer (macos) and my server(ubuntu) but i am facing this issue with ubuntu desktop. AFAIK it should not be an operating system problem.
here is models/index.js
"use strict";
require("dotenv").config();
const fs = require("fs");
const path = require("path");
const Sequelize = require("sequelize");
const basename = path.basename(__filename);
const env = process.env.NODE_ENV;
console.log(env);
const config = require(__dirname + "/../config/config")[env];
const db = {};
console.log("config check", config);
let sequelize;
if (config.use_env_variable) {
sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
sequelize = new Sequelize(
config.database,
config.username,
config.password,
config
);
}
fs.readdirSync(__dirname)
.filter((file) => {
return (
file.indexOf(".") !== 0 && file !== basename && file.slice(-3) === ".js"
);
})
.forEach((file) => {
const model = require(path.join(__dirname, file))(
sequelize,
Sequelize.DataTypes
);
db[model.name] = model;
});
Object.keys(db).forEach((modelName) => {
if (db[modelName].associate) {
db[modelName].associate(db);
}
});
db.sequelize = sequelize;
db.Sequelize = Sequelize;
module.exports = db;
UPDATE 1:
I have tried to connect to database programmatically and get same error.
original: Error: Access denied for user ''@'localhost' to database 'dbname'
at Packet.asError (/home/user/Documents/nodejs/project/db/node_modules/mysql2/lib/packets/packet.js:712:17)
at ClientHandshake.execute (/home/user/Documents/nodejs/project/db/node_modules/mysql2/lib/commands/command.js:28:26)
at Connection.handlePacket (/home/user/Documents/nodejs/project/db/node_modules/mysql2/lib/connection.js:425:32)
at PacketParser.onPacket (/home/user/Documents/nodejs/project/db/node_modules/mysql2/lib/connection.js:75:12)
at PacketParser.executeStart (/home/user/Documents/nodejs/project/db/node_modules/mysql2/lib/packet_parser.js:75:16)
at Socket.<anonymous> (/home/user/Documents/nodejs/project/db/node_modules/mysql2/lib/connection.js:82:25)
at Socket.emit (events.js:400:28)
at addChunk (internal/streams/readable.js:290:12)
at readableAddChunk (internal/streams/readable.js:265:9)
at Socket.Readable.push (internal/streams/readable.js:204:10) {
code: 'ER_DBACCESS_DENIED_ERROR',
errno: 1044,
sqlState: '42000',
sqlMessage: "Access denied for user ''@'localhost' to database 'dbname'"