Connect app to RethinkDB server on AWS - javascript

I am attempting to connect my app, which is currently on localhost, to a rethinkdb server on AWS. I used the rethinkdb AMI to get the server configured and up and running. However, I keep getting a failed to connect message. I am using rethinkdbdash to attempt the connection (https://github.com/neumino/rethinkdbdash). The following is my connection code and no passsword on the db for right now. Anyone know how to connect to it:
let r = rethinkdbdash({
db: 'test',
user: 'rethinkdb',
servers: [{host: 'my.aws.ip.addr', port: '28015'}]
});

Made a mistake in the connection syntax... the actual syntax should have been:
let r = rethinkdbdash(
{
host: 'ip.addr.here',
db: 'test',
});

Related

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.

mosquitto+mqtt.js got "Connection refused: Not authorized"

I built mosquitto on CentOS7 and a node.js client based on mqtt.js,installing with
yum install mosquitto mosquitto-clients
The local test
> mosquitto_sub -h localhost -t test
> mosquitto_pub -h localhost -t test -m "hello world"
works fine, but when I ran:
var mqtt = require('mqtt')
var client = mqtt.connect('mqtt://192.168.1.70')
client.on('connect', function () {
client.subscribe('presence')
client.publish('presence', 'Hello mqtt')
})
client.on('message', function (topic, message) {
// message is Buffer
console.log(message.toString())
client.end()
})
I got Error: Connection refused: Not authorized
The mosquitto.conf is like:
pid_file /var/run/mosquitto.pid
persistence true
persistence_location /var/lib/mosquitto/
log_dest file /var/log/mosquitto/mosquitto.log
allow_anonymous true
and I use systemctl restart mosquitto to restart it several time, which doesn't help. The firewall is down and log file stays empty.
A screenshot on status:
Can anyone help please?
UPDATE:
It turns out that the mosquitto service is somehow broken as the status shows Active: active (exited).
I use mosquitto -p 1884 -v cmd to run another mosquitto process on port 1884, it works fine. Then I try to reload the conf using
> /etc/init.d/mosquitto reload. It gives me
Reloading mosquitto configuration (via systemctl): Job for mosquitto.service invalid.
[FAILED]
So there IS something wrong with mosquitto service.
Not a final solution but I manage to fix this by remove-reboot-install process, the status went green as follow:
SOLUTION
I managed to find out the reason it doesn't work. I've installed rabbitmq on my server, it uses its "rabbitmq_mqtt" which consumes port 1883. Reassigning a port will solve this problem.
I managed to find out the reason. I've installed rabbitmq on my server, it uses its "rabbitmq_mqtt" which consumes port 1883. Reassigning a port will solve this problem. The problem is simple, but yeah, the CLI should have given me more information.
You need to add the authorize information to mqtt connect method.Just like this.
var client=mqtt.connect("ws://192.168.1.1", {
username: "yourUsername",
password: "yourPassword"
}
Add the Authorization details for the client to connect
var mqtt = require('mqtt')
var client = mqtt.connect('mqtt://192.168.1.70', {
username: '<username>',
password: '<password>'
});
client.on('connect', function () {
client.subscribe('presence')
client.publish('presence', 'Hello mqtt')
})
client.on('message', function (topic, message) {
// message is Buffer
console.log(message.toString())
client.end()
})

MQTT.js: How to close / connect to another broker after failed connection attempt? (for using bridged mosquitto)

I did set up two mosquitto broker with websocket support and am able to connect to them with mqtt.js
Now I tried to implement a fault-prove version with an array of possible mqtt brokers, which should be tried to connect to in order until a successful connection. If a connection fails, the next broker should be tried... so far so good, but if i try to connect to an offline broker, somehow mqtt.js tries to reconnect endlessly. I am not able to close the connection attempt and connect to the next one.
var client = mqtt.connect("ws://firstbrokerip:9001");
client.on('connect', function() {
//consoleLog("[BROWSER] MQTT js-Client:"," Connected","green");
client.subscribe("testchannel");
});
client.on('offline', function() {
//consoleLog("[BROWSER] MQTT js-Client:", ' Offline',"red");
client.end();
client = mqtt.connect("ws://secondbrokerip:9001");
});
Any ideas of how can I close the connection and connect to the next ?
(Plz don't care about the custom ConsoleLog function)
You don't need to implement fail over, it's baked into the module:
From the mqtt.js doc (https://github.com/mqttjs/MQTT.js#connect)
You can also specify a servers options with content: [{ host:
'localhost', port: 1883 }, ... ], in that case that array is iterated
at every connect.
So you pass the connect method options object with a key called servers which is an array of brokers to connect to.
client = mqtt.connect({
servers: [
{
host: 'firstbroker.ip',
port: 9001,
protocol: 'ws'
},
{
host: 'secondbroker.ip',
port: 9001,
protocol: 'ws'
}
]
});

Sails.js MongoDB in bluemix not working

I have a node.js app using the sails.js framework and I'm trying to deploy this app on the bluemix cloud service.
I am trying a MongoDB instance in compose.io and I have a rather standard connection configuration in my local.js file:
connectMongo: {
adapter: 'sails-mongo',
host: 'sl-eu-lon-2-portal.1.dblayer.com',
port: 10438,
database: 'some-db'
}
It is not working. It's not deploying.
The error it gives is:
ERR error: A hook (`orm`) failed to load!
ERR error: Error: Failed to connect to MongoDB.
This means, of course, that the database is
But strangely it also gives this
ERR { [MongoError: connect ECONNREFUSED 127.0.0.1:27017]
Which doesn't make any sense, as I am not using port 27017, as noted above I am using 10438.
The app is running locally, so I get that I am missing something on connecting to the database via the bluemix configurations, but I can't understand how come the 27017 pops up there.
So your setup seems correct. Compare it with my setup:
env/production.js
connections: {
prodMongoDb: {
adapter: 'sails-mongo',
host: process.env.MONGO_PORT_27017_TCP_ADDR,
port: 27017,
database: 'my_database'
}
},
models: {
connection: 'prodMongoDb',
migrate: 'safe'
}
env/development.js
connections: {
devMongoDb: {
adapter: 'sails-mongo',
host: 'localhost',
port: 27017,
database: 'my_database'
}
},
models: {
connection: 'devMongoDb',
migrate: 'safe'
}
The fact that you are specifying a port 10438 but get an error regarding 27017 means that sails is not picking up your connection definition. How do you start your app?
Starting it like this:
npm start NODE_ENV="production"
will make sails pick up the production config.

Categories