Connect to MySQL Server from node.js, but server in remote loaction - mysql

Connect to MySQL Server from Node.js But MySQL Server in remote location, How can i connect?
I tried below example :
var mysql = require("mysql");
var connection = mysql.createConnection({
host:"http://somelocation",
user:"root",
password:"roor"
});
connection.connect(function(err){
if (err) {
console.log("Error Connection to DB" + err);
return;
}
console.log("Connection established...");
});
connection.end(function(err){
});
But I am getting Error :
Error Connection to DBError: getaddrinfo ENOTFOUND https://exmaple.com
Thanks in advance !

Did you try to estimate connection without set protocol type in hostname field:
var connection = mysql.createConnection({
host:"somelocation.com",
user:"root",
password:"roor"
});
In docs I see only this case for hostmane field: https://www.npmjs.com/package/mysql

Related

Can't connect to mysql (express) with Node.js

Hello Stacksoverflowers,
I have a problem to connect my server.js file to Mysql via Express.
I've already insatlled mysql and express via npm.
when i run my server.js
The only thing it says in my console (MacOS default terminal) is:
"node server running now - using http://localhost:3000"
(I got no errors)
and my webpage also showing correct - "Node Server running - startpage" in the browser.
The problem is that they say nothing like "Connected to the MySQL server" or "Timeout: error messages" ?? (look at #3)
// Dan k
//#1
// My codes from server.js
const express = require('express');
const mysql = require('mysql');
const hostname = "localhost:";
const port = 3000;
//#2
// create connection to DB
const connection = mysql.createConnection({
host: "localhost",
user: 'root',
password: 'root',
port: 3000
});
//#3
// Connect to DB
connection.connect(function(err) {
if (!err) {
console.log('Connected to the MySQL server.');
}
else if (err)
{
return console.error('Timeout: ' + err.message);
}
});
// Routing with Express
const app = express();
//#4
// Startpage (root)
app.get('/', function(request, response){
response.send('Node Server running - startpage...');
response.end();
})
connection.end();
//#5
app.listen(port, (err) => {
if(err) throw err;
console.log("node server running now - using http://"+(hostname)+ .
(port)");
});
I got what is causing the issue. You are using the same port number 3000 to connect to your mysqlDB and at the same time using the same port to run your nodejs application.
This does throw an error but it takes a really long time to throw the error.
So the simple answer here is that the port that you are mentioning in your createConnection() method is incorrect and needs to be changed. This port should be port number on which your mysql DB is running. From the error it is clear that your mysql DB is not running on port 3306. You can check this post to figure out which port is your mysql DB running on. https://serverfault.com/questions/116100/how-to-check-what-port-mysql-is-running-on. Below is a reference for you. Please see the comments.
// create connection to DB
const connection = mysql.createConnection({
host: "localhost",
user: 'root',
password: 'root',
port: 3000 // You need to change this and put in the correct port number.
});

why do i get an error connection in nodejs - mysql (trying remote connection)

I recently started learning Nodejs to connect to MySQL and i'm having connection issues. I tried using putty to connect and i'm able to do so without any issues
this is my example.route.js file
const express = require('express'),
router = express.Router(),
mysql = require('mysql');
var connection = mysql.createConnection({
host: <ipaddress>,
port: <default Port>,
user: 'root',
password: <given password>,
database: <db name>,
connectionTimeout: 30000
});
connection.connect((error) => {
if (error) {
console.log('error connecting: ' + error);
} else {
console.log('Connected to server');
}
});
connection.query('SELECT * FROM test', function(err, results, fields) {
console.log('results in table: ' + results);
console.log('fields in table: ' + fields); // -1
connection.release();
if (err) throw error;
});
connection.end();
module.exports = router;
i get the following error => Error: connect ECONNREFUSED (Will update with complete error in a few hours)
As i mentioned before i used PUTTY in order to make sure there was an issue when connecting but i was able to connect to the given database name, host with the same user and password.
Not sure if this helps is an ubuntu server with MySQL
Anyone has an idea of why i'm getting the connection error? I would appreciate it the help

How to connect to a database on a remote server

I have a server, my-website.com, on a shared hodting plan. I created a database on the server. From the webadmin, the db details are :
From phpMyAdmin :
Server: localhost:3306
Database server
Server: Localhost via UNIX socket
Server type: Percona Server
Server version: 5.6.39-83.1 - Percona Server (GPL), Release 83.1, Revision da5a1c2923f
Protocol version: 10
User: user#localhost
Server charset: UTF-8 Unicode (utf8)
I try to connect to it via node :
const express = require('express')
const mysql = require('mysql')
const PORT = process.env.PORT || 5000
const app = express();
app.use(express.json());
const courses = [];
/** DB **/
app.get('/api/db_connect', function (req, res) {
var myConStatus = "undef conn";
var con = mysql.createConnection({
host: "191.181.181.81", // FAKE
Port: "3306",
user: "user#localhost",
database: "user_test_db",
password: "password123"
});
con.connect(function (err) {
myConStatus = "Connecting to db";
if (err) {
myConStatus = "ERR Conn";
throw err;
}
myConStatus = "Success Connection";
});
var sql = "CREATE TABLE customers (name VARCHAR(255), address VARCHAR(255))";
con.query(sql, function (err, result) {
myConStatus = "Creating table";
if (err) {
myConStatus = "ERR DB TABLE CREATE";
throw err;
}
myConStatus = "Table created";
});
res.send(myConStatus);
});
/** END DB **/
This is never successful and no table gets created.
What am I missing?
Not sure if I understand fully, but mySQL can be configured for connections from local processes or from remote. If you are trying to connect remotely to a server that is configured for local it will not connect.
You did not say if you got connections errors or not.
You can configure connections for standard TCP/IP or ssh TCP/IP as opposed to local socket/pipe - which maybe you have done.
See here https://www.percona.com/doc/percona-xtrabackup/LATEST/howtos/enabling_tcp.html and this quote
"Most of the Linux distributions do not enable by default to accept
TCP/IP connections from outside in their MySQL or Percona Server
packages"
If this is off the mark - just comment me and I will delete.

Error when connecting to mysql in Mamp

I get this error when I try to connect to mysql database on phpmyadmin from nodejs . I am using same port number 3306 that is for mysql.After 4,5 seconds i get this error. I am using mamp free version for this .
connect ETIMEDOUT at Connection._handleConnectTimeout
var express = require('express');
var bodyParser = require('body-parser');
var mysql = require('mysql');
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:true}));
//constants
const STATUS_SUCCESS = "success";
const STATUS_FAILURE = "failure";
mysql
var connection = mysql.createConnection({
host : '172.0.0.1',
user : '****',
password : '****',
database : 'parking_application'
});
connection.connect();
app.listen(4567,'localhost',function(){
console.log("server started");
});
Please Help.
Thanks in advance.
var mysql = require('mysql');
var connection = mysql.createConnection({
socketPath : '/Applications/MAMP/tmp/mysql/mysql.sock', //path to mysql sock in MAMP
user : 'userdb',
password : 'password',
host : '127.0.0.1',
database : 'yourdatabase'
});
connection.connect(function(err){
if(!err) {
console.log("Database is connected ... nn");
} else {
console.log("Error connecting database ... nn");
}
Add the socket MAMP path to mysql (mysql.sock) in the json object connection.
Restart your server.
172.0.0.1. it's actually 127.0.0.1 or just use localhost

Failed to connect to localhost:1433 - connect ECONNREFUSED 127.0.0.1:1433

I'm trying to connect to my local MySql server from node.js/express application using mssql with following config:
// Database connection
var sql = require('mssql');
var config = {
user: 'db_user',
password: 'db_pass',
server: 'localhost',
database: 'project_db'
};
sql.connect(config, function (err) {
if (err) {
console.log(err);
}
});
I get this error:
{ [ConnectionError: Failed to connect to localhost:1433 - connect ECONNREFUSED 127.0.0.1:1433]
name: 'ConnectionError',
message: 'Failed to connect to localhost:1433 - connect ECONNREFUSED 127.0.0.1:1433',
code: 'ESOCKET' }
I know I have to enable TCP/IP connections for my local MySql server, but I can not find how to do it on OSX El Capitan. There is nothing like control panel with settings. That's all available at System Preferences > MySql:
Any ideas how to fix this, guys? Thanks for trying to help.
mssql and mysql are two very different things... You should use the node-mysql or some other mysql client library if you are going to be running mysql on your dev machine
https://github.com/felixge/node-mysql
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'me',
password : 'secret',
database : 'my_db'
});
connection.connect();
connection.query('SELECT 1 + 1 AS solution', function(err, rows, fields) {
if (err) throw err;
console.log('The solution is: ', rows[0].solution);
});
connection.end();