I'm trying to create a database with Sequelize. I keep getting this error.
Error: Dialect needs to be explicitly supplied as of v4.0.0
at new Sequelize
Here's my "new Sequelize" constructor (or whatever you call it):
var sequelize = new Sequelize("seqGenZoo_db", "root", {
host: "localhost",
dialect: "mysql",
pool: {
max: 5,
min: 0,
idle: 10000
}
});
Looks like the dialect is provided. What's the deal? I've installed the mysql and mysql2 nmp packages, if that means anything.
Error 1045 indeed means you have incorrect user/password pair. And according to the official documentation you should indicate database, username, password and options parameters:
public constructor(database: string, username: string, password: string, options: Object)
Related
I'm mounting an API in Google Cloud Run that connects to an MySQL DB server using Sequelize.
This is the standard setup:
index.js file
const DB_CONFIG = require('../db/db.config');
const Sequelize = require('sequelize')
let sequelize = new Sequelize(
DB_CONFIG.NAME,
DB_CONFIG.USER,
DB_CONFIG.PASSWORD,
{ host: DB_CONFIG.HOST, dialect: DB_CONFIG.DIALECT, pool: DB_CONFIG.POOL }
)
db.config.js file
module.exports = {
NAME: process.env.DB_NAME,
HOST: process.env.DB_HOST,
USER: process.env.DB_USER,
PASSWORD: process.env.DB_PASSWORD,
DIALECT: process.env.DB_DIALECT,
POOL: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000
}
};
The problem comes when I start the project, it fails with an error that shows it is trying to connect to the same IP as the local machine public IP:
{
code: 'ER_ACCESS_DENIED_ERROR',
errno: 1045,
sqlState: '28000',
sqlMessage: "Access denied for user 'DB_USER'#'LOCAL_MACHINE_IP' (using password: YES)",
sql: undefined
}
The problem happens either if I write the final values directly in the db.config file or the values are read from process.env.
It is important to not that I'm using the Google Cloud Secret Manager to inject this values to process.env.
Also, the execution logs in Google Cloud Run shows that the API is trying to connect using the local IP.
What could be happening, and how can I search the cause of this error?
There is nothing wrong with that IP address as the host name in a mysql user account indicates the client you are connecting from, not the mysql server's IP address you are connecting to.
See mysql manual on account names for details.
After migrating to v5 from v4, Sequelize logs question marks on the console instead of the values of the SQL queries.
For instance, this is what is shown on the console:
INSERT INTO `Product` (`uid`,`title`,`price`,`isPerishable`,`categoryId`) VALUES (?,?,?,?,?);
This is my Sequelize instance:
db = new Sequelize({
dialect: 'mysql',
database: process.env.DB_NAME,
username: process.env.DB_USER,
password: process.env.DB_PASS,
host: process.env.DB_HOST,
operatorsAliases: operatorsAliases,
logging: console.log,
});
Whether before, on version 4, the values were being displayed correctly.
What I am expecting to be logged is somethings like:
INSERT INTO `Product` (`uid`,`title`,`price`,`isPerishable`,`categoryId`) VALUES (DEFAULT,'iPhone X',999.99,false,'1');
You will still have the question marks, but you will see the input right next to the query.
Just add this to the Sequalize config
logQueryParameters: true
I am constantly running into errors where it is yelling at me to mention dialect explicitely.
I am trying to talk to 2 databases and am using sequelize to perform migrations. I am using msSQL and mySQL.
Looking at other suggestions and blogs :
I have tried adding dialect explicitly in database configs.
I have tried adding NODE_ENV = 'development'.
I have tried passing Dialect explicitly in Sequelize instances.
username: global.config.dbMssqlUsername,
password: global.config.dbMssqlPassword,
database: global.config.dbMssqlName,
host: global.config.dbMssqlHostname,
timezone: 'America/New_York',
dialect: 'mssql',
operatorsAliases: false,
pool: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000
}
username: global.config.dbMysqlUsername,
password: global.config.dbMysqlPassword,
database: global.config.dbMysqlName,
host: global.config.dbMysqlHostname,
timezone: 'America/New_York',
dialect: 'mysql',
operatorsAliases: false,
pool: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000
}
On console logging this DB object:
I see mysqlDialect and also mssqlDialect.
So it looks like it is creating it.
Also, I am following this guide :
https://medium.com/unetiq/using-multiple-databases-with-nodejs-and-sequelize-59e0abcbbe6f
But it doesn't seem to be working when trying to add models stage.
I am still getting ERROR: Dialect needs to be explicitly supplied as of v4.0.0 !!
I'm trying to use the Meteor package perak:meteor-mqtt-collection to connect to CloudMQTT, but am unsure about how to interpret the syntax for the mqttConnect function:
Collection.mqttConnect(uri, topics, options, mqttOptions)
"where mqttOptions is an object that is supplied to mqtt.connect([url],options) in the MQTT.js library for configuring the underlying options of the MQTT.js-client. See the docs."
So far my Meteor test-code looks like this:
Goals = new Meteor.Collection('dbGoals');
if (Meteor.isClient) {
Goals.insert({
topic: "goals",
message: "Hello world from Meteor Web Client",
broadcast: true
});
}
if (Meteor.isServer) {
Meteor.startup(function () {
Goals.mqttConnect("m10.cloudmqtt.com", ["goals"], {
insert: true,
raw: true
},
{ servers: [{ host: 'm10.cloudmqtt.com', port: 12310 }],
clientId:"uniqueIdforEachMqttClient",
username: "myMqttUserName",
password: "myMqttUserPass",
clean:false
});
And gets the following error:
C:\Users\user\AppData\Local\.meteor\packages\meteor-tool\1.1.10\mt-os.windows.x86_32\dev_bundle\server-lib\node_modules\fibers\future.js:245
throw(ex);
^
TypeError: Cannot call method 'replace' of null
at Object.connect (C:\Users\user\AppData\Local\.meteor\packages\perak_mqtt-collection\1.0.4\npm\node_modules\mqtt\lib\connect\index.js:62:35)
at [object Object].Mongo.Collection.mqttConnect (packages/perak_mqtt-collection/packages/perak_mqtt-collection.js:37:1)
at E:\Data\Projects\Project2016\design\sw\mqttColl\.meteor\local\build\programs\server\boot.js:249:5
at mqttColl.js:25:11
=> Exited with code: 8
=> Your application is crashing.
The line mqttColl.js:25:11 is:
Goals.mqttConnect("m10.cloudmqtt.com", "goals", {
I know the object with my servers: options works with MQTT.js running on Node and CloudMQTT, but I'm not sure that I've got the mqttConnect() function parameters entered correctly for the perak Meteor package. For starters, it seems unlikely that the MQTT broker's URL would be needed in multiple places nor that the topic goals should be in brackets, but I'm just not clear on the parameter syntax.
Any suggestions?
You should add protocol into the URL: mqtt://m10.cloudmqtt.com.
I consistently get a SequelizeConnectionRefusedError when trying to connect to a MySQL database on my server.
The login credentials are correct, the port is open, everything seems good (and works like a charm in the dev environment).
Sorry for the scarce background information, but I'm dumbfounded here - I really don't know what could be causing this problem.
This is my output from mysql --version
mysql Ver 14.14 Distrib 5.5.43, for debian-linux-gnu (x86_64) using readline 6.3
And this is the code I'm using to initialize Sequelize. The table I want it to use doesn't exist yet, but I'm fairly sure that hasn't got anything to do with this problem. I've tried logging in with the root user as well, but no dice - I still get the same error.
var sequelize = new Sequelize("database", username, password, {
host: "localhost",
dialect: "mysql",
port: 3306,
define: {
paranoid: true
}
});
var Model = sequelize.define("Model", {
md5: {type: Sequelize.STRING(128)},
ip: {type: Sequelize.STRING(256)},
url: {type: Sequelize.STRING(1024)}
});
sequelize.sync();
This is running on Ubuntu 14.04, where node is being run behind Passenger (although the error appears if I run the application with node directly as well). I'm running nginx and PHP on the same server, where another PHP application is connecting to the database, if that's of any relevance.
What could be causing this problem?
I tried to connect to the database directly with the MySQL module as well, but that gave me the same error. When looking for solutions to the same problem, but related to the MySQL module rather than Sequelize, I found this: connect ECONNREFUSED - node js , sql.
What I needed was to supply the mysql module with a socketPath key. Here's how I changed my code to make it work:
var sequelize = new Sequelize("database", username, password, {
host: "localhost",
dialect: "mysql",
logging: function () {},
pool: {
max: 5,
min: 0,
idle: 10000
},
dialectOptions: {
socketPath: "/var/run/mysqld/mysqld.sock"
},
define: {
paranoid: true
}
});