sql not connecting in node js - javascript

I'm trying to connect my database with node js but it is giving me this error .
here is my code
//to connect to sql
var mysql=require('mysql');
var conn=mysql.createConnection({
host:"localhost",
user:"root",
password:"",
database:"bns"
});
conn.connect(function (err) {
if (err) {
console.log(err);
return;
}
console.log("connected");
conn.query("SELECT * FROM employee", function (err, recordset) {
if (err) {
console.log(err);
}
else {
console.log(recordset);
}
conn.close();
});
});
and I'm getting this error
Error: connect ECONNREFUSED ::1:3306
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1157:16)
--------------------
at Protocol._enqueue (C:\Users\DELL\OneDrive\Desktop\node-demo\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at Protocol.handshake (C:\Users\DELL\OneDrive\Desktop\node-demo\node_modules\mysql\lib\protocol\Protocol.js:51:23)
at Connection.connect (C:\Users\DELL\OneDrive\Desktop\node-demo\node_modules\mysql\lib\Connection.js:116:18)
at Object. (C:\Users\DELL\OneDrive\Desktop\node-demo\other.js:11:6)
at Module._compile (node:internal/modules/cjs/loader:1097:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1149:10)
at Module.load (node:internal/modules/cjs/loader:975:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:17:47 {
errno: -4078,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '::1',
port: 3306,
fatal: true
}
[nodemon] clean exit - waiting for changes before restart

I've had an exactly same issue.
var conn=mysql.createConnection({
host:"127.0.0.1",
user:"root",
password:"",
database:"bns"
});
try this code. I think it will work.
Now only problem i hav is why it works.. lol

You should specify the port number by which the server has to establish a connection. In your case, you had missed the port number. The code below must eliminate the problem.
var conn=mysql.createConnection({
host:"localhost",
user:"root",
password:"",
port: 3306,
database:"bns"
});

Related

I have a problem with getting started sequelize with postgres

i wrote this code from sequelize documentation.
i created a databade in pgadmin with the name of users and pasword 12345 and o thought it should be connected to the db. but it does not. at first it write the " Connection has been established successfully. "
in the terminal but then i catch an error.
const { Sequelize } = require('sequelize');
const sequelize = new Sequelize('postgres://Admin:12345:5432/users')
try {
sequelize.authenticate();
console.log('Connection has been established successfully.');
} catch (error) {
console.error('Unable to connect to the database:', error);
}
the error:
reject(new sequelizeErrors.HostNotFoundError(err));
^
HostNotFoundError [SequelizeHostNotFoundError]: getaddrinfo ENOTFOUND admin
at Client._connectionCallback (C:\Users\moham\OneDrive\Desktop\jalese1\node_modules\sequelize\lib\dialects\postgres\connection-manager.js:133:24)
at Client._handleErrorWhileConnecting (C:\Users\moham\OneDrive\Desktop\jalese1\node_modules\pg\lib\client.js:305:19)
at Client._handleErrorEvent (C:\Users\moham\OneDrive\Desktop\jalese1\node_modules\pg\lib\client.js:315:19)
at Connection.emit (node:events:527:28)
at Socket.reportStreamError (C:\Users\moham\OneDrive\Desktop\jalese1\node_modules\pg\lib\connection.js:52:12)
at Socket.emit (node:events:527:28)
at emitErrorNT (node:internal/streams/destroy:157:8)
at emitErrorCloseNT (node:internal/streams/destroy:122:3)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
parent: Error: getaddrinfo ENOTFOUND admin
at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:71:26) {
errno: -3008,
code: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname: 'admin'
},
original: Error: getaddrinfo ENOTFOUND admin
at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:71:26) {
errno: -3008,
code: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname: 'admin'
}
}
where is the problem?

Unable to connect to MSSQL server through Node, while Intellij connection works (MacOS)

I'm running into a strange behaviour where I'm unable to connect to an instance of MSSQL server running locally on docker when using a Node js script, but I can connect to it using Intellij's built-in JDBC connector.
This is the script:
import mysql from "mysql"
const connection = mysql.createConnection({
host : 'localhost',
user : 'sa',
password : 'P#55w0rd!',
database : 'ml_project',
port: 1433,
connectionLimit: 15,
queueLimit: 30,
acquireTimeout: 1000000
});
connection.connect(function(err) {
if ( !err ) {
console.log("Connected to MySQL");
} else if ( err ) {
console.log(err);
}
});
connection.query('SELECT 1 + 1 AS solution', function (error, results, fields) {
if (error) throw error;
console.log(results[0].solution);
connection.end();
});
Which outputs the following error:
Error: Connection lost: The server closed the connection.
at Protocol.end (/Users/IoannouK/ml_project/node_modules/mysql/lib/protocol/Protocol.js:112:13)
at Socket.<anonymous> (/Users/IoannouK/ml_project/node_modules/mysql/lib/Connection.js:94:28)
at Socket.<anonymous> (/Users/IoannouK/ml_project/node_modules/mysql/lib/Connection.js:526:10)
at Socket.emit (events.js:322:22)
at endReadableNT (_stream_readable.js:1187:12)
at processTicksAndRejections (internal/process/task_queues.js:84:21)
--------------------
at Protocol._enqueue (/Users/IoannouK/ml_project/node_modules/mysql/lib/protocol/Protocol.js:144:48)
at Protocol.handshake (/Users/IoannouK/ml_project/node_modules/mysql/lib/protocol/Protocol.js:51:23)
at Connection.connect (/Users/IoannouK/ml_project/node_modules/mysql/lib/Connection.js:116:18)
at Object.<anonymous> (/Users/IoannouK/ml_project/dist/mysql.js:17:12)
at Module._compile (internal/modules/cjs/loader.js:1156:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1176:10)
at Module.load (internal/modules/cjs/loader.js:1000:32)
at Function.Module._load (internal/modules/cjs/loader.js:899:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
at internal/main/run_main_module.js:18:47 {
fatal: true,
code: 'PROTOCOL_CONNECTION_LOST'
}
/Users/IoannouK/ml_project/dist/mysql.js:27
throw error;
^
And this is the settings panel in my Intellij which works:
I have also tried connecting to a remote database, as well as using a python script, but I received the same results.
const express = require("express");
var sql = require("mssql");
const multer = require("multer");
var config = {
user: "jsudjac",
password: "$#jsuapple",
server: "10.1.4.125",
database: "JSULLC_Test",
};
var dataSource = {
user: "********",
password: "*******",
server: "10.2.2.2",
database: "Stock_Test",
};
const dbconn1 = sql.connect(config, (err) => {
if (!err) {
console.log("Connected to the database");
} else {
console.log("Problem in connecting to database");
console.log(err);
console.log("testing");
}
});

can't get mysql module to work with node js

I am compleeeeetly new to Node js and wanted to start my very first project to get used to it. I am using the excess framework.
I found some nice mysql module which was easily downloaded and installed. The module is correctly placed in the modules folder.
I tried to call it in my app.js file like that:
...
var mysql = require('mysql');
...
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '27sparks&&55',
database : 'react'
});
connection.connect();
app.locals.mysql = connection;
connection.query('SELECT * FROM comment', function(err, rows, fields) {
if (err) throw err;
console.log(rows);
});
connection.end();
When I try to run the www in the /bin directory with this command : node www to start my localhost project I now get following error in the terminal:
/Applications/XAMPP/xamppfiles/htdocs/nodeschool/learn/app.js:79
if (err) throw err;
^
Error: connect ECONNREFUSED 127.0.0.1:3306
at Object.exports._errnoException (util.js:837:11)
at exports._exceptionWithHostPort (util.js:860:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1060:14)
--------------------
at Protocol._enqueue (/Applications/XAMPP/xamppfiles/htdocs/nodeschool/learn/node_modules/mysql/lib/protocol/Protocol.js:135:48)
at Protocol.handshake (/Applications/XAMPP/xamppfiles/htdocs/nodeschool/learn/node_modules/mysql/lib/protocol/Protocol.js:52:41)
at Connection.connect (/Applications/XAMPP/xamppfiles/htdocs/nodeschool/learn/node_modules/mysql/lib/Connection.js:123:18)
at Object.<anonymous> (/Applications/XAMPP/xamppfiles/htdocs/nodeschool/learn/app.js:75:12)
at Module._compile (module.js:434:26)
at Object.Module._extensions..js (module.js:452:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Module.require (module.js:365:17)
at require (module.js:384:17)
Try getting feedback from the connect line (code taken from here) :
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'example.org',
user : 'bob',
password : 'secret'
});
connection.connect(function(err) {
if (err) {
console.error('error connecting: ' + err.stack);
return;
}
console.log('connected as id ' + connection.threadId);
});
Also, try setting the port to 3306 manually, even though it defaults to it.
Stupid questions worth asking : is your MySQL instance up and running ? Is it listening on the good port ? Is the password correct ?
you are probably trying to connect to the wrong socket
add a socketPath to your create connection option object like this
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '27sparks&&55',
database : 'react',
socketPath : 'path/to/your/mysqlsocket' // /*example: /var/run/mysqld/mysqld.sock on ubuntu*/
});

debian wheezy / cassandra / nodejs

i try to use node-cassandra-cql to communicate with cassandra(1.1.12) but when i try a simple connexion :
node connect.js
connect.js source :
var cql = require('node-cassandra-cql');
var client = new cql.Client({hosts: ['localhost'], keyspace: 'LIBRE'});
client.execute('SELECT * FROM Persons', [],
function(err, result) {
if (err) {
console.log('execute failed', err);
} else {
for (var i = 0; i < result.rows.length; i++) {
//console.log('id=' + result.rows[i].get('id') + ' test_value=' + result.rows[i].get('test_value'));
console.log(result.rows[i].get('Name'));
}
process.exit(0);
}
}
);
it return an error :
execute failed { [PoolConnectionError]
name: 'PoolConnectionError',
info: 'Represents a error while trying to connect the pool, all the connections failed.',
individualErrors:
[ { [Error: connect ECONNREFUSED]
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect' } ],
stack: 'undefined\n (event loop)\n at Client.execute (/var/www/clients/client1/web1/web/node_modules/node-cassandra-cql/index.js:189:9)\n at Object. (/var/www/clients/client1/web1/web/connect.js:6:8)\n at Module._compile (module.js:456:26)\n at Object.Module._extensions..js (module.js:474:10)\n at Module.load (module.js:356:32)\n at Function.Module._load (module.js:312:12)\n at Function.Module.runMain (module.js:497:10)\n at startup (node.js:119:16)\n at node.js:906:3',
query: 'SELECT * FROM Persons' }
i try to specify a port (9160) but it's the same issue.
thanks for your help
Simon
As stated in the description, node-cassandra-cql (and the rest of drivers that use the Cassandra binary protocol) only works with Cassandra 1.2 or above.

access denied while connecting to mysql through node.js

i am trying to connect to mysql server (host : localhost) through node.js, i am really new to node.js and i have no idea on how to do this, please help!!!
current code which i am using to connect to the server
var client = require('socket.io').listen(8080).sockets,
mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '',
database : 'testDatabase',
port : '3306'
});
connection.connect();
var strQuery = 'select * from users';
connection.query( strQuery, function(err, rows){
if(err){
throw err;
}else{
console.log(rows);
}
});
connection.end();
which apparently gives the below error
Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'prashanthsun9'#'localhost
' (using password: YES)
at Handshake.Sequence._packetToError (C:\wamp\www\node_chat\js\node_modules\
mysql\lib\protocol\sequences\Sequence.js:30:14)
at Handshake.ErrorPacket (C:\wamp\www\node_chat\js\node_modules\mysql\lib\pr
otocol\sequences\Handshake.js:99:18)
at Protocol._parsePacket (C:\wamp\www\node_chat\js\node_modules\mysql\lib\pr
otocol\Protocol.js:205:24)
at Parser.write (C:\wamp\www\node_chat\js\node_modules\mysql\lib\protocol\Pa
rser.js:62:12)
at Protocol.write (C:\wamp\www\node_chat\js\node_modules\mysql\lib\protocol\
Protocol.js:37:16)
at Socket.<anonymous> (C:\wamp\www\node_chat\js\node_modules\mysql\lib\Conne
ction.js:73:28)
at Socket.EventEmitter.emit (events.js:95:17)
at Socket.<anonymous> (_stream_readable.js:746:14)
at Socket.EventEmitter.emit (events.js:92:17)
at emitReadable_ (_stream_readable.js:408:10)
--------------------
at Protocol._enqueue (C:\wamp\www\node_chat\js\node_modules\mysql\lib\protoc
ol\Protocol.js:110:48)
at Protocol.handshake (C:\wamp\www\node_chat\js\node_modules\mysql\lib\proto
col\Protocol.js:42:41)
at Connection.connect (C:\wamp\www\node_chat\js\node_modules\mysql\lib\Conne
ction.js:99:18)
at Object.<anonymous> (C:\wamp\www\node_chat\js\server.js:11:12)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
Please help, please help!!!
Your connection is not yet setup when you try your query.
To debug, change this line:
connection.connect();
to
connection.connect(function(err, conn) {
if(err) {
console.log('MySQL connection error: ', err);
process.exit(1);
}
});
So you'll get the error trace of why your connection is not ok.
Try to upgrade your npm mysql version to ^2.1.1.

Categories