Can't connect to MySQL with Sequelize - javascript

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
}
});

Related

Why connecting to remote server using Sequelize uses local machie IP as target DB server IP?

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.

Didn't I supply the dialect?

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)

MySQL JSON column parsed on Linux but not Windows

There exists a database with a users table containing 2 columns, userId and saved. userId is a simple increment, and saved is JSON data.
There exists a single row:
userId: 1, saved: "{"value": 1}"
When the following code is run on Windows
const mysql = require('mysql2');
async function start() {
const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
database: 'database'
});
connection.query('SELECT saved FROM users WHERE userId = 1', function(err, results, fields) {
console.log(results);
})
}
start();
[ TextRow { saved: '{"value": 1}' } ] is displayed on Windows
[ TextRow { saved: { value: 1 } } ] is displayed on Linux
What exactly is going on here that parses my JSON on Linux but not on Windows?
The MySQL versions are both different
mysql Ver 14.14 Distrib 5.7.33, for Linux (x86_64)
mysql Ver 15.1 Distrib 10.4.17-MariaDB, for Win64 (AMD64)
I'm unsure how I'd get both of them to the same version. The Windows machine is running XAMPP and it seems like a pain to mess with. I'm trying right now to get the same versions of both installed.
The users table is InnoDB utf8mb4_general_ci
field
type
userId
int(10)
saved
longtext utf8mb4_bin
I'm not actually using this code for anything, I use an ORM to do this but it uses mysql2 to do querying and I traced the error down to mysql2 and just copy-pasted the example code from GitHub to test if it was the culprit. I would use promise-based code if I was actually working with it.
MySQL2 is indeed the same version on both platforms. 2.2.5.
Interestingly the issue doesn't occur with the mysql package. I assume this indicates something funky going on with the package itself.

Initialize a mysql connection in adonis v4

I want to initialize a MySQL connection with credentials that i'm getting from a master database, is possible to do this on AdonisV4?
using adonis lucid
I can't set the credentials in config, because they're specific for every request that is made.
You would invoke npm library related to mysql in terminal shell.
npm i --save mysql
AdonisJs uses the connection value defined inside the config/database.js file. Thus, you may to create a configuration file for a single database connection in your JavaScript application like following:
You can create an initial environment file for your master database like :
HOST=127.0.0.1
PORT=3306
NODE_ENV=development
CACHE_VIEWS=false
For several connections, you would setup your config file like this:
'use strict'
const Env = use('Env')
const Helpers = use('Helpers')
module.exports = {
connection: Env.get('DB_CONNECTION', 'mysql1'),
mysql1: {
client: 'mysql',
connection: {
host: Env.get('DB_HOST', 'database_host_1'),
port: Env.get('DB_PORT', '3306'),
user: Env.get('DB_USER', 'my_database_user_1'),
password: Env.get('DB_PASSWORD', 'OhMyAdonis'),
database: Env.get('DB_DATABASE', 'adonis')
}
},
mysql2: {
client: 'mysql',
connection: {
host: Env.get('DB_HOST', 'database_host_2'),
port: Env.get('DB_PORT', '3306'),
user: Env.get('DB_USER', 'my_database_user_2'),
password: Env.get('DB_PASSWORD', 'OhMySecondAdonis'),
database: Env.get('DB_DATABASE', 'another_adonis')
}
}
}
So, you can select any of the connections defined by the database configuration file at runtime with:
Database
.connection('mysql1')
.table('users')
Database
.connection('mysql2')
.table('users')
However, if yo have a lot of databases, this approach impacts the RAM server. So, if you have a master database like your case, choose connection dynamically following an ORM for Node.JS using a SQL query builder like Objection.js with knex - based support for MySQL.
A multitenancy approach could be adopted from here.

Connecting Postgres Heroku with Knex not working

We're having difficulty setting up a Heroku/Postgres database with Knex running our queries. We've set up our heroku/postgres db and created our tables, but we're having difficulty connecting to it with Knex. To make matters worse, there's almost no documentation on connecting Heroku/Postgres with Knex or any ORM so trying to figure this stuff out has been a real pain.
These are the connection patterns that I've tried.
var knex = require('knex')({
client: 'pg',
connection: {
user: username,
password: password,
host: host,
port: port,
database: database,
ssl: true
}
}
});
And... Note the ssl true was toggle and removed all together to no avail.
var knex = require('knex')({
client: 'pg',
connection: HEROKU_POSTGRESQL_COLOR_URL,
ssl: true
}
});
We've also tried this pattern as well:
var pg = require('knex')({
client: 'pg',
connection: HEROKU_POSTGRESQL_COLOR_URL
});
We haven't yet pulled down a copy of our localdb, so every test we run is basically a git commit. We're basically testing an insert query on a GET request to our root (index.html) page. So on any get request to the main page, it should insert something into our waterrates table. If I switch it from insert to select, it returns an object but you can't actually see any of the data in the object.
The inserts we're attempting to use are:
knex.select('*').from('waterrates').then(function(rows){
return rows;
});
knex('waterrates').insert({name: 'pleeeaseee work'}, {rate: 100}).then(function(rows){
console.log(rows);
})
knex.select().
We're actually uncertain where the error could be as attempting to connect doesn't yield any errors. It's probably something silly, but we have no idea where/how to troubleshoot this. Any help would be greatly appreciated!
Thanks,
B
I had an old version of PG installed which was causing the issue. We changed our package.json file to use the latest PG. Heroku updated it and it worked!
As a side note, if anyone looks at this in the future, heroku requires an SSL connection. Keep that in mind when you're working. The connection string that I provided above should work for you all.
B
For anyone who is dealing with this problem.
Please set "SSL" to false if you don't use paid dynos.
Heroku only assigns "SSL" to users who use paid dynos.
const db = knex({
client: 'pg',
connection: {
connectionString : process.env.DATABASE_URL,
ssl: false
}
});

Categories

Resources