sequelize doesn't connect to MySql - node.js

Sequlize throws an error while applying migrations.
# sequelize --config config/config.js -m
Loaded configuration file "config/config.js".
Loaded configuration file "config/config.js".
Unable to connect to database: Error: Failed to authenticate for MySQL. Please double check your settings.
Config.js
"db": {
"development": {
"username": "imvgm_dev",
"password": "imvgm_dev",
"database": "imvgm_development",
"host": "localhost"
},
"production": {
"username": "imvgm_production",
"password": "imvgm_production",
"database": "imvgm_production",
"host": "localhost"
},
"testing": {
"username": "imvgm_testing",
"password": "imvgm_testing",
"database": "imvgm_testing",
"host": "localhost"
}
I can easily connect to db through
mysql --host=localhost --user=imvgm_dev --password=imvgm_dev imvgm_development
any thoughts?

I think you need to add "port" to your config.js.
And you need not specify config.js in the command line. This should work:
# sequelize -m

Related

Sequelize db migrate issue [duplicate]

This question already has answers here:
Dialect needs to be explicitly supplied as of v4.0.0
(19 answers)
Closed 3 years ago.
I am using node, express and sequelize where when i run sequelize db:migrate am getting the following issue,
Sequelize CLI [Node: 13.2.0, CLI: 5.5.1, ORM: 5.21.2]
Loaded configuration file "server/config/config.json".
ERROR: Dialect needs to be explicitly supplied as of v4.0.0
Config file for db authentication is,
{
"dev": {
"username": "user_1",
"password": "pass",
"database": "todos_dev",
"host": "127.0.0.1",
"dialect": "postgres",
"operatorsAliases": false
},
"test": {
"username": "root",
"password": null,
"database": "database_test",
"host": "127.0.0.1",
"dialect": "postgres",
"operatorsAliases": false,
"dialectOptions": {
"bigNumberStrings": true
}
},
"production": {
"username": "root",
"password": null,
"database": "database_production",
"host": "127.0.0.1",
"dialect": "postgres",
"operatorsAliases": false
}
}
sequlize is not able to get NODE_ENV means it is not able to decide which authentication detail it should use, you need to define that using
export NODE_ENV=development
or when you do config for DB connection you can check it as -
const env = process.env.NODE_ENV || 'development';
This will solve the issue.

node with postgres createdb raise error of role does not exists

I am building a api in node using postgresql. I am using sequelize as ORM. and when I am running following command to create database:
createdb bookstore
I am getting following error:
createdb: could not connect to database template1: FATAL: role "ubox18" does not exist
Below is my configuration file:
{
"development": {
"username": "postgres",
"password": "postgres",
"database": "bookstore",
"port": 5432,
"host": "127.0.0.1",
"dialect": "postgres"
},
"test": {
"username": "postgres",
"password": "postgres",
"database": "database_test",
"host": "127.0.0.1",
"dialect": "postgres"
},
"production": {
"username": "postgres",
"password": "postgres",
"database": "database_production",
"host": "127.0.0.1",
"dialect": "postgres"
}
}
Can anyone help what I am missing here?
Try:
createdb -U postgres bookstore
The 'createdb' utility pick the database user from the OS user, unless you choose another database user with the option -U

How to switch to new Postgres instance after installing new version of it?

I develop a Nodejs app with sequelize used. I have been using 9.6 and just installed 10.6 and also copied the database to the new version (so right now I have 2 copies of it, one for each server/version).
However, my app still uses the old one. I am not sure how to change that. Is it via code or should I just stop somehow the old server running and start the new one? I use pgAdmin by the way.
This is my config.json that sequelize uses:
{
"development": {
"username": "postgres",
"password": "364200",
"database": "userbase",
"host": "127.0.0.1",
"port": 5433, // was 5432
"dialect": "postgres"
},
"test": {
"username": "postgres",
"password": "364200",
"database": "userbase",
"host": "127.0.0.1",
"port": 5433, // was 5432
"dialect": "postgres"
},
"production": {
"username": "postgres",
"password": "364200",
"database": "userbase",
"host": "127.0.0.1",
"port": 5433, // was 5432
"dialect": "postgres"
}
}
index.js (sequelize)
sequelize = new Sequelize('userbase', 'postgres', '364200', {
host: '127.0.0.1',
port: '5433' // <--- ADD THIS HERE
dialect: 'postgres',
});
EDIT: Fixed it with #Chiller's help in comments

loopback 3 creating only test database for mongo

I exported NODE_ENV=local and defined my mongo datasource as follows in datasources.local.json
"mongo": {
"host": "localhost",
"port": 27017,
"url": "mongodb://localhost:27017/mydb",
"database": "mydb",
"password": "",
"name": "mongo",
"user": "",
"connector": "mongodb"
},
But the database name is getting created as 'test'.I also tried in datasources.json but it's still the same. I need the database name to be 'mydb'. Please help
What fixed the problem for me was to set the name of the object to "db" and not "mongo" or "mongods".
"db": {
"host": "localhost",
"port": 27017,
"url": "mongodb://localhost:27017/mydb",
"database": "mydb",
"password": "",
"name": "mongo",
"user": "",
"connector": "mongodb"
},

Connect to mLab mongoDB database using nodeJS Strapi

I just got started using strapi framework and I would like to use mLab as my mongoDB database, so I go to configure strapi and fill the following details:
{
"defaultConnection": "default",
"connections": {
"default": {
"connector": "strapi-mongoose",
"settings": {
"client": "mongo",
"host": "localhost",
"port": 27017,
"database": "development",
"username": "",
"password": ""
},
"options": {}
}
}
}
The details I get from mLab are:
mongodb://myUsername:myPassword#ds047891.mlab.com:41365/myDatabase
Here is my final config:
{
"defaultConnection": "default",
"connections": {
"default": {
"connector": "strapi-mongoose",
"settings": {
"client": "mongo",
"host": "ds047891.mlab.com",
"port": 41365,
"database": "myDatabase",
"username": "myUsername",
"password": "myPassword"
},
"options": {}
}
}
}
When I try to start strapi, I get the following error:
DEBUG (2748 on DESKTOP-HAL1ATE): Server wasn't able to start properly.
ERROR (2748 on DESKTOP-HAL1ATE): (hook:mongoose) takes too long to load
I think that I did not setup my configuration right, but I can't pinpoint where the problem is. I hope someone could, thanks.
I am Pierre, on of the creators of Strapi. I tried with the following configuration and it worked well:
{
"defaultConnection": "default",
"connections": {
"default": {
"connector": "strapi-mongoose",
"settings": {
"client": "mongo",
"host": "ds135777.mlab.com",
"port": "35777",
"database": "myDatabase",
"username": "myUsername",
"password": "myPassword"
},
"options": {}
}
}
}
Our configurations files look quiet similar.
What file did you updated (/config/environment/development/database.json or /config/environment/production/database.json)?
Are you sure you entered the correct username and password? Did you try to login to your MongoDB instance through the command line mongo ds135777.mlab.com:35777/myDatabase -u <dbuser> -p <dbpassword>?
UPDATE
In version >= 3 for mlab don't forget to specify authenticationDatabase
"options": {
"authenticationDatabase": "your_mlad_database_name",
"ssl": false
}

Resources