I have been trying to establish connection between Node.js application and mysql and have tried everything and couldn't succeed.
I'm able to connect through PHP application. My port is default where 3306
Can anyone help me to resolve this?
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : 'root'
});
connection.connect(function(err) {
if (err) {
console.error('error connecting: ' + err.stack);
return;
}
console.log('connected as id ' + connection.threadId);
});
[This is the error message I got,I can able to connect through my php application][1]
This is the error message I got,I can able to connect through my php application
Uncomment "bind-address" and assign bind-address="0.0.0.0".
For More Information please refer this,
Solving a "communications link failure" with JDBC and MySQL
A better approach to connect to DB is to use pools to connect to DB.
Copying from here
var mysql = require('mysql');
var pool = mysql.createPool({
host : 'example.org',
user : 'bob',
password : 'secret'
});
pool.getConnection(function(err, connection) {
// connected! (unless `err` is set)
connection.release();
});
This also allow you to make parallel calls to your database.
Related
I was trying to connect to a remote server mongoDB through SSH and made the configurations as provided
import tunnel from 'tunnel-ssh';
const config = {
username: 'username',
Password: 'password',
host: process.env.SSH_SERVER, //192.168.9.104
port: 22,
dstHost: 'localhost',
dstPort: process.env.DESTINATION_PORT, //27017
localHost: '127.0.0.1',
localPort: 27018
};
This is the config that has been defined where i need to connect to the remote server 192.168.9.104. So the particular is chosen as the SSH host. Username and password for the same is provided. and the connection made is as follows.
class DB {
initDB() {
tunnel(config, (error, server) => {
if (error) {
console.log('SSH connection error: ' + error);
}
const url = 'mongodb://127.0.0.1:27018/myDBname';
mongoose.connect(url, { useNewUrlParser: true });
mongoose.plugin(toJson);
mongoose.plugin(setProperties);
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'DB connection error:'));
db.once('open', function() {
console.log('DB connection successful');
});
});
}
}
But when the db.init() function is called following error pops up
events.js:183
throw er; // Unhandled 'error' event
^
Error: All configured authentication methods failed
I am not able to figure out where the config goes wrong. i have tried using 127.0.0.1 for dstHost. as well as put the 192.168.9.104 as the dstHost as well but the error persists. kevin lee suggests a similar approach. this question is used as an example
There was an error with the documentation which suggested the config as mentioned above with the key "Password" but it should be "password" so the config would look something like this
const config = {
username: 'username',
password: 'password',
host: process.env.SSH_SERVER, //192.168.9.104
port: 22,
dstHost: 'localhost',
dstPort: process.env.DESTINATION_PORT, //27017
localHost: '127.0.0.1',
localPort: 27018
};
Rest of the implementation is spot on and tested.
I am trying to establish a connection to remote mongo server through ssh tunnel using mongoose
The implementation code is:
import tunnel from 'tunnel-ssh';
const config = {
username: 'username',
Password: 'password',
host: process.env.SSH_SERVER, //192.168.9.104
port: 22,
dstHost: process.env.DESTINATION_SERVER, //192.168.9.104
dstPort: process.env.DESTINATION_PORT, //27017
localHost: '127.0.0.1',
localPort: 27017
};
this is the config that i have created while the connection is as follows:
class DB {
initDB() {
tunnel(config, (error, server) => {
if (error) {
console.log('SSH connection error: ' + error);
}
const url = 'mongodb://' + process.env.MONGO_URL; //localhost:27017/DBname
mongoose.connect(url, { useNewUrlParser: true });
mongoose.plugin(toJson);
mongoose.plugin(setProperties);
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'DB connection error:'));
db.once('open', function() {
console.log('DB connection successful');
});
});
}
}
When the function initDB() is invoked the following error pops up
SSH connection error: ConfigError: host not set
events.js:183
throw er; // Unhandled 'error' event
^
ConfigError: host not set
The host is already set but this error seems to be somewhere in the config part but I doesnt seem to single out to the exact reason
your "host" property at "config" var isn't defined. try using hard coded value instead of env var, if it works it means process can't read env vars which might be caused since you R not importing dotenv module
i'm trying to connect to my database using MSSQL Server using NodeJS
but i have an error
{ ConnectionError: Failed to connect to 10.10.17.199:undefined in 15000ms
here is the beginning of my code:
var sql = require("mssql");
// config for your database
var config = {
user: 'mat1',
password: 'a',
server: '10.10.17.199\\MSSQLLocalDB',
database: 'master' ,
port: 1433
};
//\\MSSQLLocalDB
console.log("passed 1")
// connect to your database
sql.connect(config, function (err) {
if (err) console.log(err);
// create Request object
var request = new sql.Request();
what's wrong ?
You are getting an error trying to connect to the server. Maybe you dont have access to the server. Also can happen is timing out to connect, if thats the case you can modify the connection timeout setting in the connection configuration.
I want to connect my apps nodejs using node-posgres to PostgreSQL.
My apps in localhost (ubuntu) and my postgresql in virtual machine in the cloud with operating system OpenSuse.
This is my code :
var express = require('express');
var app = express();
var http = require('http');
var pg = require('pg');
var conString = "postgres://postgres:mypassword#myurl.com:5432/postgres";
app.get('/', function (req, res) {
res.send('HOLAAAA');
});
// respond with "SERVER" on the homepage
app.get('/server', function (req, res) {
var client = new pg.Client(conString);
client.connect(function (err) {
if (err) {
return console.error('could not connect to postgres', err);
}
console.log('CONNECT PASSED');
client.query('SELECT * FROM visit', function (err, result) {
if (err) {
return console.error('error running query', err);
}
console.log('QUERY PASSED');
console.log(result.rows[0].theTime);
//output: Tue Jan 15 2013 19:12:47 GMT-600 (CST)
client.end();
});
});
});
var server = app.listen(3000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});
But i got an error like this:
could not connect to postgres { [Error: connect ECONNREFUSED]
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect' }
Please help me how to solve this.
Thanks!
#Madhavan Kumar thank you very much for your help
the steps to resolve this were as follows:
On the remote server:-
1- find \ -name "postgresql.conf" to find place of config file
2- sudo nano /path/to/config/postgresql.conf to edit config file
3- change this #listen_addresses = 'localhost' to this listen_addresses = '*' then save and exit
4- find \ -name "pg_hba.conf" to find hba config file
5- sudo nano /path/to/config/pg_hba.conf to edit hba config file
6- add
host all all 0.0.0.0/0 md5
host all all ::/0 md5
at the end of the file, then save and exit
7- run /etc/init.d/postgresql restart to restart postgres
In the code connect like this:-
let sequelize = new Sequelize(
config.db.name,
config.db.username,
config.db.password,
{
host: config.ip,
port: config.port,
dialect : 'postgres'
}
)
First try a simple psql command from the local end,
psql -d DBNAME -h YOUR_IP -U USERNAME
it mayn't work for you. This can be because of two reasons, the VM's ip is not resolved by your local station. (or) is the VM in public cloud like amazon (or) your desktop. If it is in public group, the port 5432 on the VM is not open to the public world. You need to write a security group to do it.
If you are sure, it is not any one of the above two issues, better visit postgres /etc/postgresql/9.3/main/pg_hba.conf and see if remote connections are enabled. NodeJs app, you must test at the end.
I'm trying to create a mysql database to node.js server. I've installed mysql module through command prompt:
npm install mysql
Then I execute the following code:
var Client = require('mysql').Client;
console.log(Client);
Console display undefined. That is, Client is undefined. Please tell me why it is undefined?
I'm following this tutorial
http://utahjs.com/2010/09/22/nodejs-and-mysql-introduction/
Maybe the the tutorial is a little bit old. Just use the instruction on the node-mysql docs:
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'me',
password : 'secret'
});
connection.connect();
And you should be able to connect to your MySQL database.
The node js APIs having been changing updating a lot in recent past, so it is highly possible that the tutorial you have been following is out of date according to the version you are using. You can follow the code example here I am updating or you may refer to something else, the only part that matters is it should work at minimum cost.
var mysql = require('mysql');
app.use( connection(mysql, {
host: 'myhost',
user: 'user_name',
password: 'password',
port: 3306, //port mysql
database: 'database_name',
multipleStatements: 'true' //false by default
}, 'pool'));
req.getConnection(function(err, connection) {
connection.query("SELECT * FROM `table_name`;",function (error,row){
if(!error){
//do something.....
}
else console.log("Error : "+err);
});
//do something else...
});
Thank you...!