Unable to connect to cpanel's phpmyadmin database with node.js - mysql

I'm starting to experiment and explore node.js for a project which involves using tables. So far i've created a MySQL user and database within cpanel and have granted the user full access to the database. I've read the documentation for the npm mysql , but am unable to establish a connection, have tried adding socket path but still does not work, any help would be appreciated.
Node.js
var express = require('express');
var app = express();
var mysql = require('mysql');
var con= mysql.createConnection({
connectionLimit : 100,
host : 'localhost',
port : 3306,
user : '*******',
password : '*******',
database : '*******',
});
con.query('SELECT * FROM users', function(err, results) {
if(err){
console.log('Query error: ', err);
return;
}
console.log(results);
});
Error Message
Query error: Error: connect ECONNREFUSED 127.0.0.1:3306
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16)
--------------------
at Protocol._enqueue (E:\Pharmabot\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at Protocol.handshake (E:\Pharmabot\node_modules\mysql\lib\protocol\Protocol.js:51:23)
at Connection.connect (E:\Pharmabot\node_modules\mysql\lib\Connection.js:116:18)
at Connection._implyConnect (E:\Pharmabot\node_modules\mysql\lib\Connection.js:454:10)
at Connection.query (E:\Pharmabot\node_modules\mysql\lib\Connection.js:196:8)
at Object.<anonymous> (E:\Pharmabot\database\dbconnect.js:14:5)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14) {
errno: -4078,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 3306,
fatal: true
}
}

there are some great packages, you must try theme first. there are some packages that support all db server language like sql, mysql2, mongodb, postgres etc.

Related

im trying to connect mysql with nodejs but have error

im trying to connect mysql with nodejs but have error
i think its a mysql problem not nodejs .
this is the error
Error: connect ECONNREFUSED 127.0.0.1:3306
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1145:16)
--------------------
at Protocol._enqueue (C:\Users\Owner\Desktop\MySQL\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at Protocol.handshake (C:\Users\Owner\Desktop\MySQL\node_modules\mysql\lib\protocol\Protocol.js:51:23)
at Connection.connect (C:\Users\Owner\Desktop\MySQL\node_modules\mysql\lib\Connection.js:116:18)
at Object.<anonymous> (C:\Users\Owner\Desktop\MySQL\server.js:23:12)
at Module._compile (internal/modules/cjs/loader.js:1015:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1035:10)
at Module.load (internal/modules/cjs/loader.js:879:32)
at Function.Module._load (internal/modules/cjs/loader.js:724:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)
at internal/main/run_main_module.js:17:47 {
errno: 'ECONNREFUSED',
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 3306,
fatal: true
}
also i dont have mysql in my task manager
i think i did something but i got error before it disapeared
my js code is
const mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
port : 3306,
user : 'root',
password : 'philipthe1',
database : 'todo'
});
connection.connect();
connection.query("select * from items",(error,res,fields)=>{
if(error){
console.error(error);
}
else{
console.log(res)
}
})
connection.end();
It is a mysql error, cuz it says you cannot connect to db.
Can you add the code in which you're trying to connect?
i was able to restart mysql by doing windows key and r on keyboard then services.msc
then it gave option start server
the docs for it is in mysql docs under restarting server

Trouble connecting to mysql in NodeJs on Windows 10

I have tried to connect NodeJS to mysql on Windows 10, but continuously failed to.
The following is the code, which from https://www.w3schools.com/nodejs/nodejs_mysql_create_db.asp
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "yourusername",
password: "yourpassword"
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
con.query("CREATE DATABASE mydb", function (err, result) {
if (err) throw err;
console.log("Database created");
});
});
and the following is the error message when I run code.
if(err) throw err;
^
Error: connect ECONNREFUSED 127.0.0.1:3306
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1134:16)
--------------------
at Protocol._enqueue (C:\Users\tete6\NodeJS\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at Protocol.handshake (C:\Users\tete6\NodeJS\node_modules\mysql\lib\protocol\Protocol.js:51:23)
at Connection.connect (C:\Users\tete6\NodeJS\node_modules\mysql\lib\Connection.js:116:18)
at Object.<anonymous> (C:\Users\tete6\NodeJS\demo_db_connection.js:9:5)
at Module._compile (internal/modules/cjs/loader.js:955:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)
at Module.load (internal/modules/cjs/loader.js:811:32)
at Function.Module._load (internal/modules/cjs/loader.js:723:14)
at Function.Module.runMain (internal/modules/cjs/loader.js:1043:10)
at internal/main/run_main_module.js:17:11 {
errno: 'ECONNREFUSED',
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 3306,
fatal: true
}
To solve this error, I tried ...
change "localhost" -> "127.0.0.1", but failed.
add "port: 3306", but failed.
check if the port is available to be connected, and I found that it's never my port, fireWall problem.
My xampp's mysql works well.
Even mongoDB module triggers this same error message.
I gleaned some info that this econnrefused error message is mainly the problem of NodeJS, not of sql module.
Is anyone to show me silver shining from this?
Have you started your XAMPP Server?
connect ECONNREFUSED 127.0.0.1:3306 -- this error came, when you not started your XAMPP server.
At the first you should right install mysql into windows 10
and see this video link how to connect to mysql with node js

Getting ECONNREFUSED 127.0.0.1:3306 error while conencting toa MySQL db using Node.js

I am trying to connect to a MySQL server via node.js but getting ECONNREFUSED 127.0.0.1:3306.
I am running node from my windows 10 desktop and MySQL is running on a remote Linux server.I have admin access to MySQL but just cant reach to it via node.
My Code:
var conn = mysql.createConnection({
hostname: "My mySQL server",
port: 3306,
user: "adminid",
password: "adminpassword"
});
conn.connect(function(err) {
if (err) throw err;
console.log("Connected");
conn.end();
});
=================================
if (err) throw err;
^
Error: connect ECONNREFUSED 127.0.0.1:3306
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1097:14)
--------------------
at Protocol._enqueue
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:754:12)
The argument is host not hostname, and since you didn't specify a host argument it's defaulting to 'localhost'.
Change your init:
let conn = mysql.createConnection({
host: "My mySQL server",
port: 3306,
user: "adminid",
password: "adminpassword"
});
Always double-check the documentation when you have issues like this.

Problem join database mysql in virtual machine with express node

I have a virtual machine on which is install ubuntu server with mysql and phpmyadmin I try to connect through express node js.
If I type 192.168.11.120/phpmyadmin (virtual machine address in the network) on the physical machine's browser, I can access it.
After doing a npm install mysql, it's my code
var mysql = require('mysql');
var connection = mysql.createConnection({
host : '192.168.11.120',
user : 'test',
password : 'test'
});
connection.connect();
connection.query('SELECT 1 + 1 AS solution', function(err, rows, fields)
{
if (err) {console.log(err)} ;
});
connection.end();
The console shows me the following error after doing a "node index.js".
{ Error: connect ECONNREFUSED 192.168.11.120:3306
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1106:14)
--------------------
at Protocol._enqueue (C:\Users\Boubacar\Desktop\ISTA\server\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at Protocol.handshake (C:\Users\Boubacar\Desktop\ISTA\server\node_modules\mysql\lib\protocol\Protocol.js:51:23)
at Connection.connect (C:\Users\Boubacar\Desktop\ISTA\server\node_modules\mysql\lib\Connection.js:119:18)
at Object.<anonymous> (C:\Users\Boubacar\Desktop\ISTA\server\index.js:8:12)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
errno: 'ECONNREFUSED',
code: 'ECONNREFUSED',
syscall: 'connect',
address: '192.168.11.120',
port: 3306,
fatal: true }
I can not find out if it's a problem related to virtualbox or it's the code that is not good

Why is node script failing a localhost connect to MySQL

I can't figure out why a script to is failing to connect to the MySQL instance.
I can connect to the database from the mysql client on the command line, with the exact same credentials.
I can connect to the database from a remote client, using the same credentials.
The database connect script works fine if I set the parameters to a remote database.
Neither host or socket parameters works through the script.
I make dumb mistakes at times but if this is another one, I can't see it.
An outside observer would help me here.
==================== DB Connect code
var db = require('./config');
var mysql = require('mysql');
var connection = mysql.createConnection({
host: db.db_parms.host,
port: db.db_parms.port,
user: db.db_parms.user,
password: db.db_parms.password,
database: db.db_parms.context,
debug:true
});
connection.connect(function(err) {
if(err){
console.log("SQL CONNECT ERROR: " + err);
} else {
console.log("SQL CONNECT SUCCESSFUL.");
}
});
connection.query("SELECT now()", function(error,results,fields) {
console.log("Results:" + results);
console.log("Fields:" + fields);
});
exports.connection = connection;
=== Error Stack returned:
SQL CONNECT ERROR: Error: connect ECONNREFUSED 127.0.0.1:3307
at Object.exports._errnoException (util.js:1036:11)
at exports._exceptionWithHostPort (util.js:1059:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1080:14)
--------------------
at Protocol._enqueue (/home/kingram/node/apps/fss_register/node_modules/mysql/lib/protocol/Protocol.js:141:48)
at Protocol.handshake (/home/kingram/node/apps/fss_register/node_modules/mysql/lib/protocol/Protocol.js:52:41)
at Connection.connect (/home/kingram/node/apps/fss_register/node_modules/mysql/lib/Connection.js:130:18)
at Object.<anonymous> (/home/kingram/node/apps/fss_register/db/db_connect.js:19:12)
at Module._compile (module.js:556:32)
at Object.Module._extensions..js (module.js:565:10)
at Module.load (module.js:473:32)
at tryModuleLoad (module.js:432:12)
at Function.Module._load (module.js:424:3)
at Module.runMain (module.js:590:10)
========= config.js with host = 'localhost':
var db_parms = {
host: 'localhost',
port: 3307,
user:'foo',
password:'bar',
database:'thisone'
}
exports.db_parms = db_parms;
========= config.js with host = socket:
host: '/var/run/mysqld/mysqld.sock',
=== Error Stack returned:
SQL CONNECT ERROR: Error: getaddrinfo ENOTFOUND /var/run/mysqld/mysqld.sock /var/run/mysqld/mysqld.sock:3307
at errnoException (dns.js:28:10)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:76:26)
--------------------
at Protocol._enqueue (/home/kingram/node/apps/fss_register/node_modules/mysql/lib/protocol/Protocol.js:141:48)
at Protocol.handshake (/home/kingram/node/apps/fss_register/node_modules/mysql/lib/protocol/Protocol.js:52:41)
at Connection.connect (/home/kingram/node/apps/fss_register/node_modules/mysql/lib/Connection.js:130:18)
at Object.<anonymous> (/home/kingram/node/apps/fss_register/db/db_connect.js:19:12)
at Module._compile (module.js:556:32)
at Object.Module._extensions..js (module.js:565:10)
at Module.load (module.js:473:32)
at tryModuleLoad (module.js:432:12)
at Function.Module._load (module.js:424:3)
at Module.runMain (module.js:590:10)
I'm not sure if the error from the socket is a permissions issue or not and if that is relevant.
As it turns out, a check of the my.conf shows that the bind-addresss was set to the IP of the server and not to localhost.
Setting host: '<ip-address>' fixed the issue.