const sql = require("mssql");
var sqlconfig = {
server:"192.168.200.5",
database:"DATA",
user: "labo",
password: "*********",
connectionTimeout: 30000,
port: 1433,
dialect: "mssql",
dialectOptions: {
"instanceName": "SQLEXPRESS"
},
};
new sql.ConnectionPool(sqlconfig).connect().then(pool => {
console.log(pool); return pool.query('select * from data where id = 2')
}).then(result=> {
console.dir(result)
}).catch(err => {
console.log(err);
})
Problem: Return Login failed for user 'Labo' ELOGIN when trying to login to Microsoft SQL Server using node.js.
What I have tried: Grab the example codes on npm to find the avoid extra problems. Error still persisted.
What I suspect: The username I inserted was "labo" but the one returned from err was 'Labo' (with capitalized 'L'). Is this a bug?
This is my code
Related
I am trying to connect my application to the database using the connection pool method, its connecting fine, and data insertion is happening fine without any issues but other queries in the same file are slowing down.
I have tried with release() method also not working properly.
How can release the pool to the next query once it's executed the current query?
Below is my dbpool.js file code where I am writing a common generalized database connection,
var pg = require('pg');
var PGUSER = 'postgres';
var PGDATABASE = 'test_database';
var config = {
user: PGUSER, // name of the user account
host: 'localhost',
database: PGDATABASE, // name of the database
password: 'password#AWS',
port: 5432,
max: 10,
idleTimeoutMillis: 10000
};
const pool = new pg.Pool(config);
const DB = {
query: function(query, callback) {
pool.connect((err, client, done) => {
if(err){ return callback(err); }
client.query(query, (err, results) => {
// done();
client.release();
// if(err) { console.error("ERROR: ", err) }
if(err) { return callback(err); }
callback(null, results.rows);
})
});
}
};
module.exports = DB;
I tried with both the done() and client.release() method but no luck. If I use both then I am getting an error message client is already released.
Below is my socket.js file code:
var express = require('express');
const connection = require('./dbpool.js');
if(arData == '0022'){
const queryText = "INSERT INTO alert(alert_data) VALUES('"+arData+"')";
connection.query(queryText,(err, res) => {
if(err){
console.log(err.stack);
}
});
}
if(arData == '0011'){
const queryText = "INSERT INTO table2(alert_data) VALUES('"+arData+"')";
connection.query(queryText,(err, res) => {
if(err){
console.log(err.stack);
}
});
}
function ReverseCommunication(){
const select1 = "SELECT * FROM alert WHERE action = '0' ORDER BY alert_id ASC LIMIT 1";
connection.query(select1, (err, res) =>{
if(err) {
console.log("Error1");
res.json({"error":true});
}
else{
console.log("res==",res);
}
});
}
setInterval(function(){
ReverseCommunication();
}, 2000)
With pool you shouldn't need to close the connection. With pool it will reuse the connection pool for subsequent request so you don't have to connect to the DB each time.
(i'm not a PG expert here, sure other could expand on that way better then I )
What works for us is to set up the dbpool file you have like this
const {Pool,Client} = require('pg');
const pool = new Pool({
user: process.env.POSTGRES_USER,
host: process.env.POSTGRES_URL,
database: process.env.POSTGRES_DATABASE,
password: process.env.POSTGRES_PASSWORD,
port: process.env.POSTGRES_PORT,
keepAlive: true,
connectionTimeoutMillis: 10000, // 10 seconds
max: 10
});
pool.connect()
.then(() => console.log('pg connected'))
.catch(err => console.error(err))
module.exports = pool
Then use the pool.query like you have now with pool.connect
Also, just a side note what lib are you using for PG? Noticed your queries are dynamic, you may want to adjust those to prevent possible SQL-injection.
I am building lambda function using visual studio code SAM option. I am trying to connect to RDS instance (MYSQL) which is in VPC network.
I tried connecting using following code. I am not getting any error but it is not connecting to DB. I have searched every where but didn't get any solution. I tried following but didn't worked
const fs = require('fs');
const mysqlssh = require('mysql-ssh');
mysqlssh.connect(
{
host: 'XXX.XXX.XX.XX',
user: 'ec2-user',
privateKey: fs.readFileSync('./XXXX-txlarge.pem')
},
{
host: '-staging-instanceXXXXX.rds.amazonaws.com',
user: 'user',
password: 'password',
database: 'db'
}
)
.then(client => {
client.query('SELECT * FROM users', function (err, results, fields) {
if (err) throw err
console.log(results);
mysqlssh.close()
})
})
Instead of using mysql-ssh which is tunneling based type of sql connection module try to use simple mysql module,
const fs = require('fs');
const mysql = require('mysql');
let connection = mysql.createConnection(
{
host: 'XXX.XXX.XX.XX-staging-instanceXXXXX.rds.amazonaws.com',
user: 'user',
password: 'password',
database: 'db'
}
);
connection.connect();
connection.query('SELECT * FROM users', function (err, results, fields) {
if (err) throw err
console.log("rows: " + rows);
context.succeed('Success');
});
PS: You might need to some of code as per you needs, but this is what you should try doing.
I'm trying to connect to postgres from Node using node-postgres, but even though the authentication is granted, I'm unable to search reach the tables. For example, when I perform:
const { Client } = require('pg')
const client = new Client({
user: 'username',
password: 'somepassword',
host: 'hostinfo',
port: '5432',
database: 'databaseInfo',
ssl: true,
})
const execute = async() => {
try {
await client.connect()
await client.query("BEGIN")
const results = await client.query("select * from User")
await client.query("COMMIT")
console.log(results.rows)
} catch(err) {
throw new Error(err)
} finally {
await client.end()
console.log("Finished")
}
}
execute()
it returns the User of the database, which is the 'username' that I used to access the database, not the content of the User table.
If I were to search for any other tables in the database, say the Review table, the error message shows
UnhandledPromiseRejectionWarning: Error: error: relation "review" does
not exist
If your table is really named "User" (with a capital "U"), you should be quoting its name when querying, like this:
const results = await client.query('select * from "User"')
When unquoted, PostgreSQL will think you're calling the function alias user, which is the same as current_user.
References:
https://www.postgresql.org/docs/current/functions-info.html
https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS
I ran into a mysql connection error which I seem to have fixed by guessing it was a JS variable scope issue.
In the route handler I've commented out the code that was throwing the following MySQL error: PROTOCOL_ENQUEUE_AFTER_FATAL_ERROR, and included the working code below.
require('dotenv').config();
const Hapi = require('hapi');
const mysql = require('mysql');
const Joi = require('joi');
const query = require('./lib/mysql/query');
const server = new Hapi.Server();
server.connection({
host: process.env.SERVER_HOST,
port: process.env.SERVER_PORT
});
const dbOne = mysql.createConnection({
host: process.env.DB_HOST,
user: process.env.DB_USER_ONE,
password: process.env.DB_PASSWORD_ONE
});
const dbTwo = mysql.createConnection({
host: process.env.DB_HOST_TEST,
user: process.env.DB_USER_TWO,
password: process.env.DB_PASSWORD_TWO,
});
server.route({
method: 'GET',
path:'/my-route',
config: {
validate: {
query: {
useDatabase2: Joi.valid('true'),
},
},
},
handler: (req, reply) => {
const useDatabase2 = req.query.useDatabase2 === 'true';
// This didn't work...
/*
const db = useDatabase2 ? dbTwo : dbOne;
db.query(query, (err, rows) => {
if (err) {
reply({
statusCode: 500,
error: "Internal Server Error",
message: err,
}).code(500);
} else {
// ...do something with the data
}
});
*/
// This works...
if (useDatabase2) {
dbTwo.query(query, (err, rows) => {
if (err) {
reply({
statusCode: 500,
error: "Internal Server Error",
message: err,
}).code(500);
} else {
// ...do something with the data
}
});
} else {
dbOne.query(query, (err, rows) => {
if (err) {
reply({
statusCode: 500,
error: "Internal Server Error",
message: err,
}).code(500);
} else {
// ...do something with the data
}
});
}
}
});
server.start(err => {
if (err) throw err;
});
I had assumed that const db was just a reference to the original variable (dbOne or dbTwo) within it's own scope and ceased to exist at the end of each request.
Is the db variable really conflicting with itself and causing the MySQL connection to fail!?
You might wanna look at this thread, which talks about variable reassignment for mysql connections -
https://github.com/mysqljs/mysql/issues/900
Also as a precaution, try adding the 'use strict' directive to your js files. It will help avoid the conflict of variables in your entire application
Finally got to the bottom of this. The variable scope wasn't the issue.
Something was causing the MySQL connection to fail (perhaps the database server's network or load), and as I wasn't pooling the connections, once a connection broke it stayed broke.
I switched mysql.createConnection({... to mysql.createPool({... so when one connection failed another replaced it, and so far so good.
I have a script that I want to run through the Heroku CLI. It's just a simple script to create a user in a postgressql database with Sequelize.
This is the script:
const argv = require('yargs').argv;
const Sequelize = require('sequelize');
const sqlizr = require('sqlizr');
require('dotenv').load();
// Check params
if (!argv.username) { throw new Error('username is required!'); }
if (!argv.password) { throw new Error('password is required!'); }
if (!argv.clientId) { throw new Error('client id is required!'); }
// Init db connection
const sequelize = new Sequelize(
process.env.DB_DATABASE,
process.env.DB_USER,
process.env.DB_PASS,
{
host: process.env.DB_HOST,
port: 5432,
dialect: 'postgres',
logging: false
}
)
var client_id = argv.clientId;
if(argv.clientId === -1){
client_id = 0;
}
console.log(sequelize)
sqlizr(sequelize, 'api/models/**/*.js');
// Check if user exists
console.log('Check is user exists...');
sequelize.models.USERS.count({
where: {
USERNAME: argv.username
}
})
.then(result => {
if (result > 0) {
console.error('user already exists!');
process.exit(1);
}
})
.then(() => {
console.log('Creating user...');
sequelize.models.USERS.create({
USERNAME: argv.username,
PASSWORD: argv.password,
CLNT_ID: client_id,
EMAIL: 'email#email.com',
PHONE: '123456789'
})
.then(result => {
console.log('User created successfully!');
})
.catch(error => {
console.error('Could not create user!', error);
})
.finally(result => {
process.exit(1);
});
});
Everything goes well if I execute this command locally:
node bin/createUser.js --username admin --password admin --clientId -1
But If i try to run this through the Heroku CLI like this:
heroku run bin/createUser.js --username admin --password admin --clientId -1
I get this in the terminal:
bin/createUser.js: line 4: syntax error near unexpected token `('
bin/createUser.js: line 4: `const yargs = require('yargs');'
I can't figure out what I'm doing wrong here. Hopefully someone can help me and explain why this is happening
You forgot to specify node in the command, so I suspect that Heroku is trying to run createUser.js as if it were a shell script.
You may need to install a node.js buildpack to be able to run the program on Heroku, but try:
heroku run node bin/createUser.js --username admin --password admin --clientId -1