access denied while connecting to mysql through node.js - javascript

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.

Related

sql not connecting in node js

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"
});

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");
}
});

Unable to start my nodejs application

Hi I am trying to start a node-js based application but it is giving me some mongodb based error and I can't seem to start my plugin.
Error:
/home/jira-Plugins/cloud/test-plugin/node_modules/mongodb/lib/server.js:110
if(host.indexOf('\/') != -1) {
^
TypeError: Cannot call method 'indexOf' of undefined
at new Server (/home/jira-Plugins/cloud/test-plugin/node_modules/mongodb/lib/server.js:110:11)
at new MongoDB (/home/jira-Plugins/cloud/test-plugin/node_modules/jugglingdb-mongodb/lib/mongodb.js:51:18)
at Object.initializeSchema [as initialize] (/home/jira-Plugins/cloud/test-plugin/node_modules/jugglingdb-mongodb/lib/mongodb.js:26:22)
at new Schema (/home/jira-Plugins/cloud/test-plugin/node_modules/jugglingdb/lib/schema.js:106:13)
at Object.<anonymous> (/home/jira-Plugins/cloud/test-plugin/app.js:56:14)
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)
at node.js:902:3
Here are the configurations I am currently using:
database.js
module.exports = {
url : 'mongodb://localhost/test'
};
app.js:
var mongoose = require('mongoose');
/*var methodOverride = require('method-override'); // simulate DELETE and PUT (express4)*/
var database = require('./config/database');
/*var port = process.env.PORT || 8888;*/ // set the port
mongoose.connect(database.url);
console.log('Connected to Database: ' + database.url);
var Schema = require('jugglingdb').Schema;
var schema = new Schema('mongodb');
var schema = new Schema('mongodb', {
url: 'mongodb://localhost/test',
});
I am also using jugglingdb as crossDb ORM in my application
config.json
"store": {
"adapter": "jugglingdb",
"type": "mongodb"
},
After I implemented the suggestions provided the errors has completely changed:
Unhandled error: TypeError: Cannot call
method 'indexOf' of undefined
at new Server (/home/jira-Plugins/cloud/test-plugin/node_modules/mongodb/lib/server.js:110:11)
at new MongoDB (/home/jira-Plugins/cloud/test-plugin/node_modules/jugglingdb-mongodb/lib/mongodb.js:51:18)
at Object.initializeSchema [as initialize] (/home/jira-Plugins/cloud/test-plugin/node_modules/jugglingdb-mongodb/lib/mongodb.js:26:22)
at new Schema (/home/jira-Plugins/cloud/test-plugin/node_modules/atlassian-connect-express/node_modules/jugglingdb/lib/schema.js:105:13)
at /home/jira-Plugins/cloud/test-plugin/node_modules/atlassian-connect-express/lib/store/jugglingdb.js:34:32
at lib$rsvp$$internal$$initializePromise (/home/jira-Plugins/cloud/test-plugin/node_modules/atlassian-connect-express/node_modules/rsvp/dist/rsvp.js:541:9)
at new lib$rsvp$promise$$Promise (/home/jira-Plugins/cloud/test-plugin/node_modules/atlassian-connect-express/node_modules/rsvp/dist/rsvp.js:757:9)
at new JugglingDB (/home/jira-Plugins/cloud/test-plugin/node_modules/atlassian-connect-express/lib/store/jugglingdb.js:32:18)
at module.exports (/home/jira-Plugins/cloud/test-plugin/node_modules/atlassian-connect-express/lib/store/jugglingdb.js:168:10)
at Function.stores.create (/home/jira-Plugins/cloud/test-plugin/node_modules/atlassian-connect-express/lib/store/index.js:18:10)
at stores (/home/jira-Plugins/cloud/test-plugin/node_modules/atlassian-connect-express/lib/store/index.js:12:17)
at EventEmitter.Addon (/home/jira-Plugins/cloud/test-plugin/node_modules/atlassian-connect-express/lib/index.js:31:38)
at module.exports (/home/jira-Plugins/cloud/test-plugin/node_modules/atlassian-connect-express/lib/index.js:204:12)
at Object.<anonymous> (/home/jira-Plugins/cloud/test-plugin/app.js:32:13)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
Unhandled error: TypeError: Cannot read property 'settings' of undefined
at JugglingDB.proto.isMemoryStore (/home/jira-Plugins/cloud/test-plugin/node_modules/atlassian-connect-express/lib/store/jugglingdb.js:78:29)
at EventEmitter.exports.shouldRegister (/home/jira-Plugins/cloud/test-plugin/node_modules/atlassian-connect-express/lib/internal/registration.js:45:67)
at /home/jira-Plugins/cloud/test-plugin/node_modules/atlassian-connect-express/lib/internal/registration.js:85:54
at lib$rsvp$$internal$$tryCatch (/home/jira-Plugins/cloud/test-plugin/node_modules/atlassian-connect-express/node_modules/rsvp/dist/rsvp.js:493:16)
at lib$rsvp$$internal$$invokeCallback (/home/jira-Plugins/cloud/test-plugin/node_modules/atlassian-connect-express/node_modules/rsvp/dist/rsvp.js:505:17)
at /home/jira-Plugins/cloud/test-plugin/node_modules/atlassian-connect-express/node_modules/rsvp/dist/rsvp.js:1001:13
at Object.lib$rsvp$asap$$flush [as _onImmediate] (/home/jira-Plugins/cloud/test-plugin/node_modules/atlassian-connect-express/node_modules/rsvp/dist/rsvp.js:1198:9)
at processImmediate [as _immediateCallback] (timers.js:330:15)
You didn't include any info on what is in line 56 of your app.js.
The error looks like the mongodb driver is trying to find a slash in a variable called host and fails to do so because the host is undefined.
Isn't it because you have var schema defined twice?
var schema = new Schema('mongodb');
var schema = new Schema('mongodb', {
url: 'mongodb://localhost/test',
});
Maybe remove the first one where you don't have the url.
Also, are you using both Mongoose and JugglingDB in the same program?

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.

Categories