deployd mongodb connection error - node.js

I'm learning Angular js and trying to run deployd server. Followed http://terraltech.com/how-to-setup-deployd-on-ubuntu-server/ steps mentioned here.
When I try to add a row from deployd dashboard am getting below error as a response
{"message":"Database connection error","status":400}
Here is my production.js server configuration.
var deployd = require('deployd');
var server = deployd({
port: process.env.PORT || 5000,
env: 'production',
db: {
host: 'SAMLP15070001',
port: 27017,
name: 'deployd',
credentials: {
username: 'deployd',
password: 'deployd'
}
}
});
server.listen();
server.on('listening', function() {
console.log("Server is listening");
});
server.on('error', function(err) {
console.error(err);
process.nextTick(function() { // Give the server a chance to return an error
process.exit();
});
});
Here is my mongo information which I configured in production.js
> getHostName( )
SAMLP15070001
> show dbs;
admin 0.078GB
local 0.078GB
test 0.078GB
I'm not sure whey is my deployd db is not getting displayed here.
Can someone help me in resolving this issue ?

From your mongo you have to execute the following commands:
mongo shell
use admin
db.addUser( { user: "deployd", pwd: "deployd", roles: [ "userAdminAnyDatabase" ] } )
use deployd
db.addUser( { user: "deployd", pwd: "deployd", roles: [ "readWrite", "dbAdmin" ] } )

Related

Connection to postgresql db from node js

I'm tyring to make a connection from my nodejs script to my db connection, but seems like there is a suspicius issue i'm not able to figure out.
At the moment, this is my code:
const { Pool } = require('pg');
const pool = new Pool({
user: 'user',
host: '192.168.1.xxx',
database: 'database',
password: 'password',
port: 5432,
});
pool.on('error', (err, client) => {
console.error('Error:', err);
});
const query = `SELECT * FROM users`;
pool.connect()
.then((client) => {
client.query(query)
.then(res => {
for (let row of res.rows) {
console.log(row);
}
})
.catch(err => {
console.error(err);
});
})
.catch(err => {
console.error(err);
});
The issue seems to be in pool.connect(), but i can't understand what i'm missing because i got no errors in the log. I've installed pg module in the directory of my project with npm install --prefix pg and i know modules are loaded correctly.
I edited postgresql.conf:
# - Connection Settings -
listen_addresses = '*'
and pg_hba.conf
host database user 192.168.1.0/24 md5
to make the database reachable via lan and seems liek it works, because i'm able to connect successfully with apps like DBeaver...but i can't with NodeJS.
It's possible there is some kind of configuration i've to active?

How to handle error of closed MySQL connection in Node.js?

I've built a Node.js API that connects to a MySQL database. I use node-mysql2 as driver. The API and the database run in separate Docker container. At some point after deployment in a Kubernetes cluster I get the following error:
Error: Can't add new command when connection is in closed state
at PromiseConnection.query (/usr/src/app/node_modules/mysql2/promise.js:92:22)
I wonder why this error happens and how to catch and handle it using Node.js. These are code snippets of my Node.js API:
const mysql = require('mysql2/promise')
...
async function main() {
try {
const client = await mysql.createConnection({
host: DATABASE_HOST,
port: DATABASE_PORT,
user: DATABASE_USERNAME,
password: DATABASE_PASSWORD,
database: DATABASE_NAME
})
client.on('error', error => {
process.stderr.write(`${error.code}\n`) // PROTOCOL_CONNECTION_LOST
process.stderr.write(`An error occurred while connecting to the db: ${error.message}\n`)
process.exit(1)
})
} catch (error) {
process.stderr.write(`Error while creating db connection: ${error.code}\n`)
}
...
}
...
main().catch(err => {
process.stderr.write(`Error message: ${err.message}\n`)
process.exit(1)
})
Do you have an idea how to handle this error?
Do you close the connection after finishing with it?
client.end();
Also considered using a pool?
const pool = mysql.createPool({
host: DATABASE_HOST,
user: DATABASE_USERNAME,
database: DATABASE_NAME,
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0
});
More info about pools: https://github.com/sidorares/node-mysql2#using-connection-pools

orientjs not using the servers list?

hi I am developing an application in nodejs which uses orientdb.
I am using orientjs version 2.2.10
var OrientDB = require('orientjs');
var server = OrientDB({
host: '127.0.0.1',
port: 2424,
username: 'root',
password: '123',
servers: [{
host: "192.168.0.159",
port: 2425
}],
});
server.connect().then(() => {
console.log("connected");
}).catch(err => {
console.log(err);
})
above is a simple code to check whether the driver connects to the server. however when there is no orientdb instance running in localhost i get the connection refused error. it looks like that when connecting it first tries host in main object and never tries to connect to hosts in servers array. I have also tried orientjs v3, result is the same.
can someone tell me what could be the issue here?
thanks
You can try like this:
databaseConfig = {
"host": '127.0.0.1',
"port": 2424,
"username": 'root',
"password": 132,
"name": 'databaseName',
"user": 'root'
};
let server = OrientDB(databaseConfig);
let db = server.use(databaseConfig);
db.open()
.then(()=>{
console.log('Connected!')
});

MongoDB not authorized error on Ubuntu 16.04

I am installing a server on Digitalocean and have a MongoDB issue. I installed Mongo on the server and allowed remote access.
I created two users. One is super-user for me and the other is a regular user for the website users. I am able to connect to the mongo through Robomongo 3T (a mongo client) remotely for both users.
However, when I use the application from a browser, a simple login request gives an authentication error. Here is the error:
MongoError: not authorized on DATABASE_NAME to execute command { find: "users", filter: { email: "YYY", password: "XXX" }, limit: 1, singleBatch: true, batchSize: 1 }
Here is the realted server-side code:
Connecting the database at server.js:
MongoClient.connect(config.mongo_url, (err, client) => {
if(err) throw err
console.log('db connected')
app.locals.db = client
// start the server
app.listen(process.env.PORT || 3000, ()=>console.log('listening on port 3000!'))
});
Trying to connect to the local mongo on the server:
database.collection(config.mongo_col_user)
.findOne({
email: username,
password: hash
}, (err, user)=>{
if(err) return done(err)
if(!user) return done(null, false, {msg: 'not found'})
if(user.password)
return done(null, user)
})
I use pm2 on the server side and I use ecosystem.config.js which has the below code:
module.exports = {
apps : [
{
name: "NAME",
script: "SCRIPT_NAME",
watch: true,
env: {
"mongo_url": 'mongodb://USER_NAME:PASSWORD#localhost:27017?authMechanism=SCRAM-SHA-1&authSource=DATABASE_NAME',
"mongo_db_name": 'DATABASE_NAME',
"mongo_col_query": "COLLECTION_NAME",
}
}
]
}
Could you help with the problem please?
I have figured it out.
When I did make changes to pm2 environment file ecosystem.config.js, I did always restart pm2 with pm2 restart all. However, when you change the file, you should tell pm2 that you changed it by pm2 reload ecosystem.config.js --update-env.
Additionally, the correct syntax is no
mongodb://USER_NAME:PASSWORD#localhost:27017?authMechanism=SCRAM-SHA-1&authSource=DATABASE_NAME'
but
mongodb://USER_NAME:PASSWORD#localhost:27017/DATABASE_NAME'

getaddrinfo ENOTFOUND when using tedious in node.js

I'm trying to connect to a local SQL Express server using Tedious but keep getting
failed Error: getaddrinfo ENOTFOUND
Am I using the wrong address here?
var Connection = require('tedious').Connection;
var config = {
userName: 'sa',
password: 'mypassword',
server: 'LOCALHOST\\SQLEXPRESS',
};
var connection = new Connection(config);
connection.on('connect', function(err) {
// If no error, then good to go...
if(err) {
console.log(err);
return;
}
executeStatement();
}
);
"Microsoft style strings of hostname\instancename are not supported."
- pekim
I posted the same issue on github and here is the full answer: https://github.com/pekim/tedious/issues/118
Just as #Cotten said, but here is an example.
TCP/IP connection must be enabled and the port shouldn't be included within the server string, it must go inside the configuration as a numeric value.
var config = {
server: 'your_ip_address',
authentication: {
type: 'default',
options: {
userName: 'your_username',
password: 'your_password'
}
},
options: {
database: 'your_database',
port: 1234 //your port number
}
};

Resources