Nodejs Failing to Connect To WampServer MySQL Database - mysql

I am new to web development and I have started working on Nodejs.
I am trying to connect to the MySQL Server on port 3308, WampServer but instead I always get connected to localhost server.
To ensure that I am using right ports and hostnames, I have tried to setup the connection using MYSQL Workbench and it worked!
But still, I am not able to figure out how to connect with mysql driver.
const sql = require('mysql');
var con = sql.createConnection({
host: '127.0.0.1:3308',
user: 'root',
password: '',
port: 3308
});
con.connect(function (err) {
if (err) throw err;
console.log("Connected!");
});
createDatabaseQuery= theQ =>{
return "CREATE DATABASE ".concat(theQ);
}
con.query(createDatabaseQuery('nodedb'), function(err) {
if (err) {
throw err;
}
});
Here is my output:
[Running] node "c:\Users\Asus\WebstormProjects\Impulse\MySQLTest.js"
c:\Users\Asus\WebstormProjects\Impulse\MySQLTest.js:8
if (err) throw err;
^
Error: connect ECONNREFUSED 127.0.0.1:3306
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1137:16)
--------------------
at Protocol._enqueue (c:\Users\Asus\WebstormProjects\Impulse\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at Protocol.handshake (c:\Users\Asus\WebstormProjects\Impulse\node_modules\mysql\lib\protocol\Protocol.js:51:23)
at Connection.connect (c:\Users\Asus\WebstormProjects\Impulse\node_modules\mysql\lib\Connection.js:116:18)
at Object.<anonymous> (c:\Users\Asus\WebstormProjects\Impulse\MySQLTest.js:7:5)
at Module._compile (internal/modules/cjs/loader.js:1158:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
at Module.load (internal/modules/cjs/loader.js:1002:32)
at Function.Module._load (internal/modules/cjs/loader.js:901:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
at internal/main/run_main_module.js:18:47 {
errno: 'ECONNREFUSED',
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 3306,
fatal: true
}
[Done] exited with code=1 in 1.198 seconds

Related

Error: connect ECONNREFUSED 127.0.0.1:3306 ( DB not connecting in node js with mysql)

I was building a simple rest API with node and mysql. But while creating the connection to the database I am getting this error.
Error: connect ECONNREFUSED 127.0.0.1:3306
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16)
--------------------
at Protocol._enqueue (/Users/shehriyarnadeem/Documents/others/learning_projects/linkedinclone/node_modules/mysql/lib/protocol/Protocol.js:144:48)
at Protocol.handshake (/Users/shehriyarnadeem/Documents/others/learning_projects/linkedinclone/node_modules/mysql/lib/protocol/Protocol.js:51:23)
at Connection.connect (/Users/shehriyarnadeem/Documents/others/learning_projects/linkedinclone/node_modules/mysql/lib/Connection.js:116:18)
at Object. (/Users/shehriyarnadeem/Documents/others/learning_projects/linkedinclone/config/database.js:12:6)
at Module._compile (internal/modules/cjs/loader.js:1138:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
at Module.load (internal/modules/cjs/loader.js:986:32)
at Function.Module._load (internal/modules/cjs/loader.js:879:14)
at Module.require (internal/modules/cjs/loader.js:1026:19)
at require (internal/modules/cjs/helpers.js:72:18) { errno: 'ECONNREFUSED', code: 'ECONNREFUSED', syscall: 'connect',
address: '127.0.0.1', port: 3306, fatal: true }
database.js
var mysql = require("mysql");
let pool = mysql.createConnection({ host: "localhost", user:
"root", port: "3306" /* port on which phpmyadmin run */, password:
"",
database: "dbname", });
pool.connect((err) => { console.log(err); });
module.exports = pool;

connection to a remote mysql database using node

Good mornig to you guys. I want to establish a connection to a remote mysql database using node js. but i am facing this error. I do not know if I wrongly specifies the access path to the db
code
var mysql = require('mysql');
var pool = mysql.createPool({
host: "http://kamerun-it.com/mysql",
connectionLimit : 100,
database: "****",
user: "****",
password: "*****",
multipleStatements: true
});
error
throw err;
^
Error: getaddrinfo ENOTFOUND http://kamerun-it.com/mysql
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:60:26)
--------------------
at Protocol._enqueue (F:\kamerun it\aspi-api\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at Protocol.handshake (F:\kamerun it\aspi-api\node_modules\mysql\lib\protocol\Protocol.js:51:23)
at PoolConnection.connect (F:\kamerun it\aspi-api\node_modules\mysql\lib\Connection.js:119:18)
at Pool.getConnection (F:\kamerun it\aspi-api\node_modules\mysql\lib\Pool.js:48:16)
at Object. (F:\kamerun it\aspi-api\app\model\db.js:16:8)
at Module._compile (internal/modules/cjs/loader.js:956:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
at Module.load (internal/modules/cjs/loader.js:812:32)
at Function.Module._load (internal/modules/cjs/loader.js:724:14)
at Module.require (internal/modules/cjs/loader.js:849:19) {
errno: 'ENOTFOUND',
code: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname: 'http://kamerun-it.com/mysql',
fatal: true
}
You probably have an error in your config with the host URL.
Here's a working example with a connection to a remote MySQL:
const mysql = require("mysql");
const connection = mysql.createPool({
host: "remotemysql.com",
user: "aKlLAqAfXH",
password: "PZKuFVGRQD",
database: "aKlLAqAfXH"
});
connection.query(
"SELECT hexcode FROM colours WHERE precedence = 2",
(err, result) => {
err ? console.log(err) : console.log(result[0].hexcode);
}
);
and here's one with mistaken host parameter:
const mysql = require("mysql");
const connection = mysql.createPool({
host: "WRONGremotemysql.com",
user: "aKlLAqAfXH",
password: "PZKuFVGRQD",
database: "aKlLAqAfXH"
});
connection.query(
"SELECT hexcode FROM colours WHERE precedence = 2",
(err, result) => {
err ? console.log(err) : console.log(result[0].hexcode);
}
);
The second one returns the same ENOTFOUND error.
Check if that's the correct URL, if the database can be accessed remotely and via which port you can use it.

Unable to connect to google cloud sql service using node.js

I am trying to connect to my google-cloud-sql instance using my node.js mysql module and I am unable to connect to it. I referred to the following links mysql docs and google getting started but could not understand what's going on.
var connection = mysql.createConnection({
acquireTimeout: 30000,
host : '35.184.77.86',
user : 'XXX',
password : 'XXX',
database : 'name',
});
connection.connect(function(err){
if (err) {
console.error(chalk.red('error connecting: ' + err.stack));
return;
}
console.log(chalk.green('connected as id ' + connection.threadId));
});
connection.query('SELECT * FROM likes', function(err, rows, fields)
{
if (err) {
console.log(chalk.red(err));
}else{
console.log(fields);
}
});
connection.end();
I am getting the following error while connecting.
error connecting: Error: connect ETIMEDOUT
at Connection._handleConnectTimeout (/Users/narenpalep/Desktop/javascript/Project_Portfolio/node_modules/mysql/lib/Connection.js:419:13)
at Socket.g (events.js:292:16)
at emitNone (events.js:86:13)
at Socket.emit (events.js:185:7)
at Socket._onTimeout (net.js:338:8)
at ontimeout (timers.js:386:14)
at tryOnTimeout (timers.js:250:5)
at Timer.listOnTimeout (timers.js:214:5)
--------------------
at Protocol._enqueue (/Users/narenpalep/Desktop/javascript/Project_Portfolio/node_modules/mysql/lib/protocol/Protocol.js:145:48)
at Protocol.handshake (/Users/narenpalep/Desktop/javascript/Project_Portfolio/node_modules/mysql/lib/protocol/Protocol.js:52:23)
at Connection.connect (/Users/narenpalep/Desktop/javascript/Project_Portfolio/node_modules/mysql/lib/Connection.js:130:18)
at Object.<anonymous> (/Users/narenpalep/Desktop/javascript/Project_Portfolio/db/db.js:11:12)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
Error: connect ETIMEDOUT
Try running with the below code also check the connection and for the right IP. There might be internal firewall block between the server and the system
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
});

Error when trying to connect mysql to Nodejs

I have installed mysql with using npm then created a connection file :
var express = require("express");
var mysql = require('mysql');
var connection = mysql.createConnection({
host : '127.0.0.1',
user : 'root',
password : '',
database : 'address_book',
});
var app = express();
connection.connect(function(err){
if(!err) {
console.log("Database is connected ... nn");
} else {
console.log(err);
};
When i connect using node connection.js I get this error :
{ Error: connect ECONNREFUSED 127.0.0.1:3306
at Object.exports._errnoException (util.js:1033:11)
at exports._exceptionWithHostPort (util.js:1056:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1105:14)
--------------------
at Protocol._enqueue (/home/haddad/projects/plannerback/node_modules/mysql/lib/protocol/Protocol.js:141:48)
at Protocol.handshake (/home/haddad/projects/plannerback/node_modules/mysql/lib/protocol/Protocol.js:52:41)
at Connection.connect (/home/haddad/projects/plannerback/node_modules/mysql/lib/Connection.js:130:18)
at Object.<anonymous> (/home/haddad/projects/plannerback/db/connect.js:12:20)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 3306,
fatal: true }
I try to ping to localhost 3306 , Port is not used :
PING 3306 (0.0.12.234) 56(124) bytes of data.
^C
--- 3306 ping statistics ---
201 packets transmitted, 0 received, 100% packet loss, time 201592ms

cannot connect mysql with nodejs

I am absolutely new to node.js. while Iam trying to connect mysql with node.js I got this. Really looking for a help.
{ Error: connect ECONNREFUSED 127.0.0.1:8889
at Object.exports._errnoException (util.js:1022:11)
at exports._exceptionWithHostPort (util.js:1045:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1087:14)
at Protocol._enqueue (/Applications/MAMP/htdocs/htbf/nodejsdb/mysql/node_modules/mysql/lib/protocol/Protocol.js:141:48)
at Protocol.handshake (/Applications/MAMP/htdocs/htbf/nodejsdb/mysql/node_modules/mysql/lib/protocol/Protocol.js:52:41)
at Connection.connect (/Applications/MAMP/htdocs/htbf/nodejsdb/mysql/node_modules/mysql/lib/Connection.js:130:18)
at Connection._implyConnect (/Applications/MAMP/htdocs/htbf/nodejsdb/mysql/node_modules/mysql/lib/Connection.js:461:10)
at Connection.query (/Applications/MAMP/htdocs/htbf/nodejsdb/mysql/node_modules/mysql/lib/Connection.js:206:8)
at Object. (/Applications/MAMP/htdocs/htbf/nodejsdb/mysql/app1.js:11:12)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 8889,
fatal: true }
my code is
var mysql = require('mysql');
var connection = mysql.createConnection({
host: 'localhost',
port: '8889',
user: 'root',
password: 'root',
socket: '/Applications/MAMP/tmp/mysql/mysql.sock',
database: 'login_db'
});
connection.query('SELECT * from test_table',function(err,rows,feilds){
if(!err){
console.log(rows);
} else{
console.log(err);
}
});
connection.end();