How to connect nodejs redis client to redis cloud server? - javascript

const redis = require('redis');
const client = redis.createClient({
host: 'redis-19606.redislabs.com',
port: 19606,
password: 'password'
});
client.on('ready', () => {
console.log('redis is connected');
});
client.on('error', (err) => {
console.log('redis is disconnected: ', err);
});
(async () => {
try {
await client.connect();
} catch (error) {
console.error('error while connecting redis', error);
}
})();
This somehow does not seem to work. What am I doing wrong? It keeps connecting to 127.0.0.1:6379 which is the default config instead of what I am passing. This is only happening with nodejs client of redis. go-redis the golang client for redis is working flawlessly.

Just a guess, but which Node Redis version do you use? I had difficulties upgrading myself. The configuration of the client has changed since version 4.x.x. Since version 4.x.x you have to use a confiuration according to Client Configuration. Therefore use
const client = redis.createClient({
socket: {
host: 'redis-19606.redislabs.com',
port: 19606,
}
});
or use a URL
const client = redis.createClient({
url: "redis://redis-19606.redislabs.com:19606"
});
Your client configuration matches the Node Redis version 3.x.x but not 4.x.x. Please see Redis NPM v3.1.2 and Redis NPM v4.0.1 for details.

you should maintain this format of Redis connecting string:
redis://:YOUR_PASSWORD#YOUR_ENDPOINT:YOUR_PORT

What version of redis are you using?
Do not explicitly conect to redis by writing "client.connect()" but
instead use this after setting host, port and password:
redisClient.on("connect", () => {})
and then you can use redisClient variable to get and set values.
Also if you're trying to connect to a redislabs server then I think your host name might be incorrect and double check your host from redislabs.

Try this new Redis("redis://username:authpassword#127.0.0.1:6380/4");
Check the documentation Link

Related

I can't access to mySQL with node js anymore

I have updated my node version, and now I can't access to my DB with my app. I tried many things:
-Change rout to 127.0.0.1.
-add my port number 3306
-add socketPath: '/Applications/MAMP/tmp/mysql/mysql.sock'
-kill node running
And probably others, but I always have an error. Actually, I have this one while running this code.
Error:
disconnected
error: connect ECONNREFUSED ::1:3306
My code who was working before:
const mysql = require('mysql');
const db = mysql.createConnection({
host: "localhost",
user: "root",
password: "",
database: "discord"
});
if (db.state === 'disconnected') {
db.connect(function (err) {
if (err) { return console.error('error: ' + err.message); }
console.log('Connected to the MySQL server.');
});
}
console.log(db.state);
module.exports = db
My issue was the version of discord.js because I have updated it as well. I've changed it in my package.json for the one I used before and everything is working well. It will be hard to update all my app to the new version. If you have any recommendation to update it easily without running into multiple issues, I'm here !

connecting to remote mongodb with nodejs

i have a remote mongo server and i want to connect to that with nodejs . i tried connecting directly with mongo shell and it was ok ... but i cant connect to that mongodb server in nodejs ..
the mongo command for connection :
mongo --host 192.168.10.33 --port 27017 -u mohammad -p "mypassw#12" --authenticationDatabase=access
i only want to convert that command into nodejs connection config..
something like:
uri = mongodb://mohammad:mypassw#12#192.168.10.33:27017/"
The URI should be encoded
uri = "mongodb://mohammad:mypassw%4012#192.168.10.33:27017/access"
mongo.connect(uri, { useNewUrlParser: true }, (err, db) => {});
Connecting to a remote Mongo can sometimes be tricky , mongoose can help a great lot with connecting to your remote db . The code below should be able to do the trick and connect .
const mongoose = require('mongoose');
mongoose.set('useFindAndModify', false);
const uri = "mongodb+srv://user:pass#clusterName.d4zrk.mongodb.net/databaseName?retryWrites=true&w=majority";
mongoose.connect(uri, { useNewUrlParser: true, useUnifiedTopology: true })
const db = mongoose.connection
db.on('error', (err) => {
console.log(err)
})
db.once('open', () => {
console.log('Database Connection Established!')
})
I guess the issue might be with your password, it looks like to contains a special character that is used by the mongodb driver to separate the credentials from the host:
mongodb://[username:password#]host1[:port1][,...hostN[:portN]][/[defaultauthdb][?options]]
If you have a # in your password, you have to apply an percent encoding it by adding a % character.
Here is the list of character to encode according to mongodb driver documentation: : / ? # [ ] #

unable to connect to database from node script and there is no error thrown

I have been working with nodejs google cloud functions for a while. I have been facing a weird issue where I can't connect to database servers and there is no error logged not even timeout. I am using node v14.2.0 with pg as postgres library. My nodejs code is
const { Client } = require('pg');
let connectionSetting = {
host: "host",
user: "user",
database: "db_name",
password: "password",
};
const client = new Client(connectionSetting);
console.log(connectionSetting);
client.connect(err => {
if (err) {
console.error(`Error connecting to db -> ${err}`);
}
console.log("Connection established with PgSql DB ");
});
There are no console logs or whatever.
This same code is working on other systems. The database is remote database hosted on gcp and I'm able to connect to it using tablePlus as GUI client.
Any help appreciated.
I found the issue. It has to do with the node version. I was using node current 14.2.0 so I installed node lts 12 and then everything works fine.
Thanks for all help

Error: read ECONNRESET when connected to a mysql server with Node.js

I'm trying to establish a simple connection to my database with the mysql npm package. At first glance, everything works fine and I can get the information I need, however, if I leave the server running for some time I get the following error:
Error: read ECONNRESET
at TCP.onStreamRead
const express = require('express');
const app = express();`
const mysql = require('mysql');
const db = mysql.createConnection({
host: 'XXXX.mysql.database.azure.com',
user: 'XXXXX',
password: 'XXXXX',
database: 'XXXXX'
})
db.connect((err)=>{
if(err){
console.log(err.message);
} else {
console.log('Connected to the database');
}
})
As far as I understand the problem stems from the database connection being in idle mode. Do I need to configure the Azure server or is there something else I need to do?
Couple of things to try:
You can try creating connection pool instead of **createConnection**
mysql.createPool({});
Modify your package.json like below:
"dependencies": {
"mysql": "git://github.com/mysqljs/mysql#e3e123e9af7c0829a6c1417d911572a75b4a5f95"
},
It is described in detail here:
Bad handshake or ECONNRESET Azure Mysql Nodejs
https://social.msdn.microsoft.com/Forums/en-US/c8fedbcc-909d-41ce-8c72-0374f76fdf82/cannot-connect-from-nodejs?forum=AzureDatabaseforMySQL
Hope it helps.

node.js - Can't connect to MYSQL database with Sequelize

I'm starting at Node.js and i'm trying to make a simple connection with Sequelize based on its documentation (http://docs.sequelizejs.com/manual/installation/getting-started.html#installation).
Here my db.js file :
const Sequelize = require('sequelize')
const db = new Sequelize('chat','root','root',{
host: 'localhost',
port: 3306,
dialect: 'mysql'
});
db
.authenticate()
.then(() => {
console.log('Connection has been established successfully.');
})
.catch(err => {
console.error('Unable to connect to the database:', err);
});
When executing this code, I have no success nor error message, just a message indicating 'String base operators deprecated' but nothing important i think.
I tried changing localhost to 127.0.0.1, remove port number and multiples thread but i'm stucked here...
Tested your code and all seemed to work fine, it connected successfully to the database instance I have running.
You might want to ensure mysql2 package is installed. Also check that the database chat has been created in MySQL workbench, and ensure the password is correct.

Categories