I have node server on which i used mysql module as shown below
const db = mysql.createConnection({
host:'localhost',
user: 'root',
password: '',
database: 'test'
});
db.connect(function(err){
if (err) console.log('Errors', err)
console.log('connenected');
})
when i replace ip address of remote database or ip address of my local machine on which xammp is running instead of localhost it fails somehow i tried to find out solution some says i need to use port in connection but it fails. Thanks in advance
Step1: Allow database to connect remote IP by given this SQL.
GRANT ALL PRIVILEGES ON *.* TO 'root'#'ipaddressofyourpc' IDENTIFIED BY 'password' WITH GRANT OPTION;
Step2: Flush the PRIVILEGES
FLUSH PRIVILEGES;
Step3:
var mysql = require('mysql');
var connection = mysql.createPool({
connectionLimit: 100,
host:'ipaddressofremotedb',
user:'usernameofdb',
password:'password',
database:'dbname',
port: 3306,
debug: false,
multipleStatements: true
});
Related
I have load balance with three server and 1 is main server storing data into database from server 2 and 3 via tunnel ssh using mongoose, currently I have only one port let's say 1900 but I wanna increase replica member 3 or more, Anyone know how to do?
Sorry for English might not correct, Thank you
const config = {
username: 'root',
host: '128.199.xx.xx',
port: 22,
agent: 345,
dstPort: 1900,
password: 'xxx'
};
tunnel(config, function (error, server) {
mongoose
.connect("mongodb://localhost:1900/db_name", {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
useCreateIndex: true,
replicaSet: "rs",
})
.then(() => {
console.log("MongoDB Connected");
})
});
I solved it by adding hosts to origin machine Thank you very much.We dont need any ssh tunnel any more
1. Allow your Local IP to your server
ufw allow from xx.xx.xx.xx to any port xxxx (database port)
Optional: Adding IP to host file in your local machine
For better security please addming username and password to access Databse
I have a Google cloud SQL instance and I want to be able to access it from my node.js application running locally on my machine.
I have enabled Cloud SQL instance access through public IP and created a network on GCP with my local machine IP and I have tested the connection in several ways:
MySQL Workbench
terminal
From my NodeJS application running locally using mysql2 to connect
All the above mentioned ways connected successfully and I can work on my cloud SQL instance as expected, the problem is when trying to allow my NodeJS app to connect using sequelize, I can see this error
UnhandledPromiseRejectionWarning: SequelizeAccessDeniedError: Access denied for user 'root'#'localhost' (using password: YES)
this is my connection code:
host: 'XXX.XX.XX.XX',
port: "",
user: "root",
password: 'password',
database: "DB_NAME",
dialect: "mysql",
pool: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000
}
const db = {};
const connection = await mysql.createConnection({ host, port, user, password });
await connection.query(`CREATE DATABASE IF NOT EXISTS \`${database}\`;`);
const sequelize = new Sequelize(database, user, password, { dialect: dialect });
db.Sequelize = Sequelize;
db.sequelize = sequelize;
// // init models and add them to the exported db object
db.orders = require("../../models/order.model.js")(sequelize, Sequelize);
db.sequelize.sync().then(function(){
console.log('DB connection sucessful.');
}, function(err){
// catch error here
});
It Seems that I should pass the host as somehow it was considering host to be localhost when it is not explicitly specified
So this:
const sequelize = new Sequelize(database, user, password, { dialect: dialect });
Should be updated to this:
const sequelize = new Sequelize(database, user, password, { host: host, dialect: dialect });
I am using Digitalocean managed PostgreSQL and trying to access the database from node.js (using pg-promise) which is a Google Cloud Function.
no pg_hba.conf entry for host "IP-ADDRESS", user "username", database "database", SSL off
Below is my db config
import * as pgPromise from 'pg-promise';
const dbConfig = {
username: 'username',
password: 'password',
host: 'host string',
port: PORTNUMBER,
database: 'database',
sslmode: 'require'
};
const pgp = pgPromise({});
const db = pgp(dbConfig);
export { db, pgp };
How to fix this? Please advise.
I want to connect to postgres in Aws server. I have installed "pg" to node.
I have tried to use the connection String, but there is no way to add private key there. So I also try with
const pool = new Pool({
user: 'dbuser',
host: 'database.server.com',
database: 'mydb',
password: 'secretpassword',
port: 3211,
privateKey : require('fs').readFileSync('./ssh_keys/my_key')
})
In both cases I get connection refused.
i am using GeddyJs with a heroku cedar app deployment. I am using Heroku Postgres services for the database.
I have configured the username/password/hostname/dbname in the config file on geddyjs but when i go to run node app.js it throws an error for no pg_hba.conf i know this related to SSL not being used while accessing the db remotely but i have no clue how to force SSL on the connection..
Here is the error log:
error: no pg_hba.conf entry for host "70.199.196.17", user "12345", database "database1", SSL off
at p.parseE (/Users/mikedevita/Web/Sites/gorelative.com/node/node_modules/pg/lib/connection.js:503:11)
at p.parseMessage (/Users/mikedevita/Web/Sites/gorelative.com/node/node_modules/pg/lib/connection.js:363:17)
at Socket.p.attachListeners (/Users/mikedevita/Web/Sites/gorelative.com/node/node_modules/pg/lib/connection.js:86:20)
at Socket.EventEmitter.emit (events.js:96:17)
at TCP.onread (net.js:397:14)
[Tue, 05 Mar 2013 22:39:49 GMT] ERROR Worker 843 died.
my config/development.js file:
var config = {
detailedErrors: true
, debug: true
, hostname: 'localhost'
, port: 3000
, model: {
defaultAdapter: 'postgres'
}
, db: {
postgres: {
port: 5432
, password: 'foobar'
, database: 'database1'
, host: 'ec2-107-21-126-45.compute-1.amazonaws.com'
, user: '12345'
}
}
, sessions: {
store: 'memory'
, key: 'sid'
, expiry: 14 * 24 * 60 * 60
}
};
module.exports = config;
You need to add ssl: true to your postgres config.
postgres: {
port: 5432
, password: 'foobar'
, database: 'database1'
, host: 'ec2-107-21-126-45.compute-1.amazonaws.com'
, user: '12345'
, ssl: true
}
Geddy simply passes this config object to the pg module. Check the pg.client wiki page for more info.
If you are trying to connect from outside heroku you need to connect with SSL. We only allow connections from outside heroku if they are encrypted with SSL
error: no pg_hba.conf entry for host "70.199.196.17", user "12345", database "database1", SSL off says that you can't connect with SSL off.
Also you may be sanitizing your database name with database1, but if you're not, I can guarantee you that your database name is not, in fact, database1.
Also you should NOT NOT NOT be hard-coding your credentials in a file. Read them out of your environment.