I am not able connect Node.js project with Mamp dadabase - mysql

Hi I am using below code to connect with Mamp data base "http://localhost:8888/phpMyAdmin/server_databases.php?server=1" but not able to do it getting below error
code :-
const db = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'root',
database: 'nodejs.login',
})
db.connect((error) =>{
if(error){
console.log(error)
}else {
console.log("Mysql Connected....")
}
})
error:-
server startwd on Port 5003
{ Error: connect ECONNREFUSED 127.0.0.1:3306
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1117:14)
--------------------
at Protocol._enqueue (/Users/ruchir/Documents/Assesment_Node/node_modules/mysql/lib/protocol/Protocol.js:144:48)
at Protocol.handshake (/Users/ruchir/Documents/Assesment_Node/node_modules/mysql/lib/protocol/Protocol.js:51:23)
at Connection.connect (/Users/ruchir/Documents/Assesment_Node/node_modules/mysql/lib/Connection.js:116:18)
at Object.<anonymous> (/Users/ruchir/Documents/Assesment_Node/app.js:13:4)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
errno: 'ECONNREFUSED',
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 3306,
fatal: true }

your problem might be in the connection parameters, you are setting your password: 'root'.
In my experience the password usually is left blank: password: ''.

I am able to resolve it.i have check and find my Mamp server was running on 8889
so i have added one more variable in connetion port: '8889' at it works.

Related

I cannot connect to mysql database using node.js

I am total beginner to database.
I am using localhost(127.0.0.1) with port 9090.
I successfully created mysql database, ran/used it in windows 10 command prompt and MySQL Workbench.
But when I try to run it with node.js, I fail to connect to server.
Below is index.js file I tried to run in windows command line.
var mysql = require('mysql');
var client = mysql.createConnection({
user:'root',
password:'password',
});
client.query('USE mydb');
client.query('SELECT * FROM PRODUCTS',function(error, result, fields){
if(error){
console.log(error);
} else{
console.log(result);
}
});
And it results in this error.
D:\Coding\VSCode\nodejs>node index.js
Error: connect ECONNREFUSED 127.0.0.1:3306
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16)
--------------------
at Protocol._enqueue (D:\Coding\VSCode\nodejs\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at Protocol.handshake (D:\Coding\VSCode\nodejs\node_modules\mysql\lib\protocol\Protocol.js:51:23)
at Connection.connect (D:\Coding\VSCode\nodejs\node_modules\mysql\lib\Connection.js:116:18)
at Connection._implyConnect (D:\Coding\VSCode\nodejs\node_modules\mysql\lib\Connection.js:454:10)
at Connection.query (D:\Coding\VSCode\nodejs\node_modules\mysql\lib\Connection.js:196:8)
at Object.<anonymous> (D:\Coding\VSCode\nodejs\index.js:10:8)
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
}
I am at the stage not knowing what I am not knowing.
I tried to change port in createConnection() function like this
var client = mysql.createConnection({
user:'root',
password:'password',
port: '9090'
});
Then I get this error.
D:\Coding\VSCode\nodejs>node index.js
Error: ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by
server; consider upgrading MySQL client
at Handshake.Sequence._packetToError
(D:\Coding\VSCode\nodejs\node_modules\mysql\lib\protocol\sequences\Sequence.js:47:14)
at Handshake.ErrorPacket
(D:\Coding\VSCode\nodejs\node_modules\mysql\lib\protocol\sequences\Handshake.js:123:18)
at Protocol._parsePacket (D:\Coding\VSCode\nodejs\node_modules\mysql\lib\protocol\Protocol.js:291:23)
at Parser._parsePacket (D:\Coding\VSCode\nodejs\node_modules\mysql\lib\protocol\Parser.js:433:10)
at Parser.write (D:\Coding\VSCode\nodejs\node_modules\mysql\lib\protocol\Parser.js:43:10)
at Protocol.write (D:\Coding\VSCode\nodejs\node_modules\mysql\lib\protocol\Protocol.js:38:16)
at Socket.<anonymous> (D:\Coding\VSCode\nodejs\node_modules\mysql\lib\Connection.js:88:28)
at Socket.<anonymous> (D:\Coding\VSCode\nodejs\node_modules\mysql\lib\Connection.js:526:10)
at Socket.emit (events.js:315:20)
at addChunk (_stream_readable.js:309:12)
--------------------
at Protocol._enqueue (D:\Coding\VSCode\nodejs\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at Protocol.handshake (D:\Coding\VSCode\nodejs\node_modules\mysql\lib\protocol\Protocol.js:51:23)
at Connection.connect (D:\Coding\VSCode\nodejs\node_modules\mysql\lib\Connection.js:116:18)
at Connection._implyConnect (D:\Coding\VSCode\nodejs\node_modules\mysql\lib\Connection.js:454:10)
at Connection.query (D:\Coding\VSCode\nodejs\node_modules\mysql\lib\Connection.js:196:8)
at Object.<anonymous> (D:\Coding\VSCode\nodejs\index.js:10:8)
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) {
code: 'ER_NOT_SUPPORTED_AUTH_MODE',
errno: 1251,
sqlMessage: 'Client does not support authentication protocol requested by server; consider upgrading
MySQL client',
sqlState: '08004',
fatal: true
}
1.Open your windows terminal and login :
mysql -uroot -p
( then type you password )
use mysql;
update user set authentication_string=password(''), plugin='mysql_native_password' where user='root';
4 Restart you mysql db : sudo service mysql restart
Doing this in MySQL Workbench solved the problem.
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password
BY 'YourRootPassword';
FLUSH PRIVILEGES;

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

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

i am receiving error while making connection of mysql database(phpmyadmin) to node.js

{
Error: connect ECONNREFUSED 127.0.0.1:3306
at Object.exports._errnoException (util.js:1020:11)
at exports._exceptionWithHostPort (util.js:1043:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1086:14)
--------------------
at Protocol._enqueue (/home/ubuntu/workspace/proj/node_modules/mysql/lib/protocol/Protocol.js:145:48)
at Protocol.handshake (/home/ubuntu/workspace/proj/node_modules/mysql/lib/protocol/Protocol.js:52:23)
at Connection.connect (/home/ubuntu/workspace/proj/node_modules/mysql/lib/Connection.js:130:18)
at Object.<anonymous> (/home/ubuntu/workspace/proj/app.js:12:4)
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 Timeout.Module.runMain [as _onTimeout] (module.js:604:10)
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 3306,
fatal: true }
IDE Screenshot
Seems like your database is not running on port 3306.
Try and find out the port number and add the port attribute so your code is like this:
var db = mysql.createConnection({
host: "127.0.0.1",
user: "root",
password: "",
database: "udit1",
port: YOUR_PORT_NUMBER_HERE
});

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.