I am trying to connect to a remote server via ssh tunnel and then connect to a mysql.
I first tried on mysql workbench and it works fine. The "fake" information looks like this:
With the same information I tried to connect to mysql with nodeJS but I have no idea how to do it. Please help. This is what I have tried so far.
var mysql = require('mysql');
var tunnel = require('tunnel-ssh');
var config = {
username:'hoffnung8493',
Password:'password123',
host:'hoffnung.com',// not sure if its right
port:22,// not sure if its right
dstHost:'hoffnung.com',// not sure if its right
dstPort:3306, // not sure if its right
localHost:'127.0.0.1',// not sure if its right
localPort: 27000 // not sure if its right
};
tunnel(config, function(error, server){
var db = mysql.createConnection({
host: '127.0.0.1',// not sure if its right
user: 'stock_id',
password: 'stock_password',
database: 'database_name',
charset : 'utf8'
});
db.connect();
db.query(`show tables`, function(error, result){
console.log(error);
console.log(1, result);
})
})
module.exports = db; //probably wrong
Then from main.js I want to send query like this:
db = require('./lib/db.js');
db.query('show tables', function(error, result){
console.log(result)
});
On my console I get the following error message:
0|main | Error: All configured authentication methods failed
0|main | at tryNextAuth (/home/cabox/workspace/node_modules/tunnel-ssh/node_modules/ssh2/lib/client.js:376:17)
0|main | at SSH2Stream.onUSERAUTH_FAILURE (/home/cabox/workspace/node_modules/tunnel-ssh/node_modules/ssh2/lib/client.js:587:5)
0|main | at emitTwo (events.js:125:13)
0|main | at SSH2Stream.emit (events.js:213:7)
0|main | at parsePacket (/home/cabox/workspace/node_modules/ssh2-streams/lib/ssh.js:3929:10)
0|main | at SSH2Stream._transform (/home/cabox/workspace/node_modules/ssh2-streams/lib/ssh.js:669:13)
0|main | at SSH2Stream.Transform._read (_stream_transform.js:186:10)
0|main | at SSH2Stream._read (/home/cabox/workspace/node_modules/ssh2-streams/lib/ssh.js:251:15)
0|main | at SSH2Stream.Transform._write (_stream_transform.js:174:12)
0|main | at doWrite (_stream_writable.js:385:12)
After I fixed from "Password" ->"password", I added db.query after db.connect to see if it works and I got the following error
0|main | { Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'stock'#'firstpg-server197-22' (using password: YES)
0|main | at Handshake.Sequence._packetToError (/home/cabox/workspace/node_modules/mysql/lib/protocol/sequences/Sequence.js:47:14)
0|main | at Handshake.ErrorPacket (/home/cabox/workspace/node_modules/mysql/lib/protocol/sequences/Handshake.js:124:18)
0|main | at Protocol._parsePacket (/home/cabox/workspace/node_modules/mysql/lib/protocol/Protocol.js:278:23)
0|main | at Parser.write (/home/cabox/workspace/node_modules/mysql/lib/protocol/Parser.js:76:12)
0|main | at Protocol.write (/home/cabox/workspace/node_modules/mysql/lib/protocol/Protocol.js:38:16)
0|main | at Socket.<anonymous> (/home/cabox/workspace/node_modules/mysql/lib/Connection.js:91:28)
0|main | at Socket.<anonymous> (/home/cabox/workspace/node_modules/mysql/lib/Connection.js:502:10)
0|main | at emitOne (events.js:115:13)
0|main | at Socket.emit (events.js:210:7)
0|main | at addChunk (_stream_readable.js:266:12)
0|main | --------------------
0|main | at Protocol._enqueue (/home/cabox/workspace/node_modules/mysql/lib/protocol/Protocol.js:144:48)
0|main | at Protocol.handshake (/home/cabox/workspace/node_modules/mysql/lib/protocol/Protocol.js:51:23)
0|main | at Connection.connect (/home/cabox/workspace/node_modules/mysql/lib/Connection.js:118:18)
0|main | at /home/cabox/workspace/inven_man/lib/db2.js:24:4
0|main | at Server.<anonymous> (/home/cabox/workspace/node_modules/tunnel-ssh/index.js:89:13)
0|main | at Object.onceWrapper (events.js:314:30)
0|main | at emitNone (events.js:105:13)
0|main | at Server.emit (events.js:207:7)
0|main | at emitListeningNT (net.js:1349:10)
0|main | at _combinedTickCallback (internal/process/next_tick.js:135:11)
0|main | code: 'ER_ACCESS_DENIED_ERROR',
0|main | errno: 1045,
0|main | sqlMessage: 'Access denied for user \'stock\'#\'firstpg-server197-22\' (using password: YES)',
0|main | sqlState: '28000',
0|main | fatal: true }
0|main | 1 undefined
I cant test it right now, but it should be sufficient to set port in the options to 27000.
tunnel(config, function(error, server){
var db = mysql.createConnection({
host: '127.0.0.1',
port: 27000, // the local port you defined for your tunnel
user: 'stock_id',
password: 'stock_password',
database: 'database_name',
charset : 'utf8'
});
db.connect();
})
Beside that the options are case sensitive, so it should be password and not Password.
var config = {
username:'hoffnung8493',
password:'password123',
host:'hoffnung.com',// not sure if its right
port:22,// not sure if its right
dstHost:'hoffnung.com',// not sure if its right
dstPort:3306, // not sure if its right
localHost:'127.0.0.1',// not sure if its right
localPort: 27000 // not sure if its right
};
First of all, I'd like to suggest you to change / hide the domain name in the future, just for caution cases.
Secondly, can you post the output you get from the console? If you get any output.
Thirdly, as this is an ssh connection matter, you need to make sure you are set as allowed to connect to the remote server, on port 22 in this case, whether in it's settings / firewall settings.
Regarding the settings, double check the variable values to be sure not to confuse between the MySQL and SSH connections variables, rely on the fact that the MySQL Workbench connection is working.
Related
I'm creating a discord bot that pulls from a pre-established database of patch note news I have, it's my first time dealing with NodeJS and MySQL. I've got the default connection code in the file right now to test the connection and it looks like this:
require('dotenv').config();
const { Client, Intents } = require('discord.js');
const { token } = require('./config.json');
const mysql = require('mysql');
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
const con = mysql.createConnection({
host: process.env.HOST,
port: process.env.PORT,
user: process.env.MYSQLUSER,
db: process.env.DATABSE,
password: process.env.MYSQLPASSWORD,
});
con.connect((err) => {
if (err) throw err;
console.log("Connected");
})
I've tried hosting the application local on my machine with a remote connection to the SQL database and on the actual server that it lives on, so far this is all I'm getting:
/home/niemer5/news-bot/node_modules/mysql/lib/protocol/Parser.js:437
throw err; // Rethrow non-MySQL errors
^
Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'niemer5_discordbot'#'localhost' (using password: NO)
at Handshake.Sequence._packetToError (/home/niemer5/news-bot/node_modules/mysql/lib/protocol/sequences/Sequence.js:47:14)
at Handshake.ErrorPacket (/home/niemer5/news-bot/node_modules/mysql/lib/protocol/sequences/Handshake.js:123:18)
at Protocol._parsePacket (/home/niemer5/news-bot/node_modules/mysql/lib/protocol/Protocol.js:291:23)
at Parser._parsePacket (/home/niemer5/news-bot/node_modules/mysql/lib/protocol/Parser.js:433:10)
at Parser.write (/home/niemer5/news-bot/node_modules/mysql/lib/protocol/Parser.js:43:10)
at Protocol.write (/home/niemer5/news-bot/node_modules/mysql/lib/protocol/Protocol.js:38:16)
at Socket.<anonymous> (/home/niemer5/news-bot/node_modules/mysql/lib/Connection.js:88:28)
at Socket.<anonymous> (/home/niemer5/news-bot/node_modules/mysql/lib/Connection.js:526:10)
at Socket.emit (node:events:394:28)
at addChunk (node:internal/streams/readable:315:12)
--------------------
at Protocol._enqueue (/home/niemer5/news-bot/node_modules/mysql/lib/protocol/Protocol.js:144:48)
at Protocol.handshake (/home/niemer5/news-bot/node_modules/mysql/lib/protocol/Protocol.js:51:23)
at Connection.connect (/home/niemer5/news-bot/node_modules/mysql/lib/Connection.js:116:18)
at Object.<anonymous> (/home/niemer5/news-bot/index.js:22:5)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12)
at node:internal/main/run_main_module:17:47 {
code: 'ER_ACCESS_DENIED_ERROR',
errno: 1045,
sqlMessage: "Access denied for user 'niemer5_discordbot'#'localhost' (using password: NO)",
sqlState: '28000',
fatal: true
}
The user exists, it has ALL privileges, I set everything up in cPanel and also tried granting privs in SQL CLI and so far no luck it just errors out with a 1045 access denied error and I have no clue what is happening. Looking for a little guidance here!
edit:
Solved my problem. After removing the line
connection.connect(function(err) {
if (err) throw err;
});
I got the raw MySQL error message saying that the login credentials for MySQL are wrong. I already checked my passwort, but din't noticed the wrong username. Very sorry fr my dumb question.
I have a NodeJS / Express app. It was already working fine but know I dont'get a connection any more to MySQL (MariaDB). Always I try to start the app (with pm2) the start failed, because a exception is thrown.
I have already checked if MySQL is running and I also restarted MySQL and NodeJS app and pdated node-mysql. This is my code for connecting to mysql:
//local mysql db connection
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : 'xxx',
database : 'lizenzverwaltung'
});
connection.connect(function(err) {
if (err) throw err;
});
module.exports = connection;
The error message on startup is:
0|www | at Socket.<anonymous> (/var/www/html/lizenzverwaltung/node_modules/mysql/lib/Connection.js:525:10)
0|www | at Socket.emit (events.js:182:13)
0|www | at Socket.EventEmitter.emit (domain.js:442:20)
0|www | --------------------
0|www | at Protocol._enqueue (/var/www/html/lizenzverwaltung/node_modules/mysql/lib/protocol/Protocol.js:144:48)
0|www | at Protocol.handshake (/var/www/html/lizenzverwaltung/node_modules/mysql/lib/protocol/Protocol.js:51:23)
0|www | at Connection.connect (/var/www/html/lizenzverwaltung/node_modules/mysql/lib/Connection.js:119:18)
0|www | at Object.<anonymous> (/var/www/html/lizenzverwaltung/models/db.js:11:12)
0|www | at Module._compile (internal/modules/cjs/loader.js:702:30)
0|www | at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
0|www | at Module.load (internal/modules/cjs/loader.js:612:32)
0|www | at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
0|www | at Function.Module._load (internal/modules/cjs/loader.js:543:3)
0|www | at Module.require (internal/modules/cjs/loader.js:650:17)
If I comment out the line
if (err) throw err;
I can start the NodeJS app, but get the following error, as soon, as I try to run a SQL query:
0|www | at Protocol._validateEnqueue (/var/www/html/lizenzverwaltung/node_modules/mysql/lib/protocol/Protocol.js:212:16)
0|www | at Protocol._enqueue (/var/www/html/lizenzverwaltung/node_modules/mysql/lib/protocol/Protocol.js:138:13)
0|www | at Connection.query (/var/www/html/lizenzverwaltung/node_modules/mysql/lib/Connection.js:201:25)
0|www | at Function.verifyLicense [as verify] (/var/www/html/lizenzverwaltung/models/licenseModel.js:29:9)
0|www | at Function.LicenseController.veryLicense (/var/www/html/lizenzverwaltung/controllers/licenseController.js:51:18)
0|www | at /var/www/html/lizenzverwaltung/routes/license.js:33:21
0|www | at Layer.handle [as handle_request] (/var/www/html/lizenzverwaltung/node_modules/express/lib/router/layer.js:95:5)
0|www | at next (/var/www/html/lizenzverwaltung/node_modules/express/lib/router/route.js:137:13)
0|www | at Route.dispatch (/var/www/html/lizenzverwaltung/node_modules/express/lib/router/route.js:112:3)
0|www | at Layer.handle [as handle_request] (/var/www/html/lizenzverwaltung/node_modules/express/lib/router/layer.js:95:5) code: 'PROTOCOL_ENQUEUE_AFTER_FATAL_ERROR', fatal: false }
0|www | error { Error: Cannot enqueue Query after fatal error.
0|www | at Protocol._validateEnqueue (/var/www/html/lizenzverwaltung/node_modules/mysql/lib/protocol/Protocol.js:212:16)
0|www | at Protocol._enqueue (/var/www/html/lizenzverwaltung/node_modules/mysql/lib/protocol/Protocol.js:138:13)
0|www | at Connection.query (/var/www/html/lizenzverwaltung/node_modules/mysql/lib/Connection.js:201:25)
0|www | at Function.verifyLicense [as verify] (/var/www/html/lizenzverwaltung/models/licenseModel.js:29:9)
0|www | at Function.LicenseController.veryLicense (/var/www/html/lizenzverwaltung/controllers/licenseController.js:51:18)
0|www | at /var/www/html/lizenzverwaltung/routes/license.js:33:21
0|www | at Layer.handle [as handle_request] (/var/www/html/lizenzverwaltung/node_modules/express/lib/router/layer.js:95:5)
0|www | at next (/var/www/html/lizenzverwaltung/node_modules/express/lib/router/route.js:137:13)
0|www | at Route.dispatch (/var/www/html/lizenzverwaltung/node_modules/express/lib/router/route.js:112:3)
0|www | at Layer.handle [as handle_request] (/var/www/html/lizenzverwaltung/node_modules/express/lib/router/layer.js:95:5) code: 'PROTOCOL_ENQUEUE_AFTER_FATAL_ERROR', fatal: false } result null
0|www | POST /v1/license/verify/ 200 19.128 ms - 16
I already searched but didn't found a solution. Do you have any tips? My MySQL server should working fine, PHPMyAdmin and my Wordpress sites are running fine.
Thanks,
Klaus
Solved my problem. After removing the line
connection.connect(function(err) {
if (err) throw err;
});
I got the raw MySQL error message saying that the login credentials for MySQL are wrong. I already checked my passwort, but din't noticed the wrong username. Very sorry for my dumb question.
i'm working on a google cloud project and i get this error when i run node index.js / try to access mysql database remotely.
this is the complete error message:
Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'root'#'external_ip' (using password: YES)
at Handshake.Sequence._packetToError (/home/it21695/nodeproject/node_modules/mysql/lib/protocol/sequences/Sequence.js:52:14)
at Handshake.ErrorPacket (/home/it21695/nodeproject/node_modules/mysql/lib/protocol/sequences/Handshake.js:130:18)
at Protocol._parsePacket (/home/it21695/nodeproject/node_modules/mysql/lib/protocol/Protocol.js:279:23)
at Parser.write (/home/it21695/nodeproject/node_modules/mysql/lib/protocol/Parser.js:76:12)
at Protocol.write (/home/it21695/nodeproject/node_modules/mysql/lib/protocol/Protocol.js:39:16)
at Socket.<anonymous> (/home/it21695/nodeproject/node_modules/mysql/lib/Connection.js:103:28)
at Socket.emit (events.js:182:13)
at addChunk (_stream_readable.js:277:12)
at readableAddChunk (_stream_readable.js:262:11)
at Socket.Readable.push (_stream_readable.js:217:10)
--------------------
at Protocol._enqueue (/home/it21695/nodeproject/node_modules/mysql/lib/protocol/Protocol.js:145:48)
at Protocol.handshake (/home/it21695/nodeproject/node_modules/mysql/lib/protocol/Protocol.js:52:23)
at Connection.connect (/home/it21695/nodeproject/node_modules/mysql/lib/Connection.js:130:18)
at Object.<anonymous> (/home/it21695/nodeproject/index.js:10:12)
at Module._compile (internal/modules/cjs/loader.js:678:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:689:10)
at Module.load (internal/modules/cjs/loader.js:589:32)
at tryModuleLoad (internal/modules/cjs/loader.js:528:12)
at Function.Module._load (internal/modules/cjs/loader.js:520:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:719:10)
this is my index.js code (the test DB and books table both exist):
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'external_ip',
user : 'root',
password : 'password',
database : 'test',
});
connection.connect();
connection.query('SELECT * from books', function (error, results, fields) {
if (error) throw error;
console.log(results);
});
connection.end();
i have added the mysql port (3306) to the firewall exceptions and i've granted privileges for root user. i turned the mysql and node.js external ips to static. i use the passwords that google cloud has assigned.
+------------------+----------------+------------+
| user | host | grant_priv |
+------------------+----------------+------------+
| root | % | Y |
| root | external_ip | Y |
| mysql.infoschema | localhost | N |
| mysql.session | localhost | N |
| mysql.sys | localhost | N |
| root | localhost | Y |
| stats | localhost | N |
+------------------+----------------+------------+
mysql v8.0.11node.js v10.1.0npm v5.6.0
Before checking the application any further make sure to check the user credentials and access privileges on the database host server.
This problem is related to permissions not with nodejs.
First of all i recommend you to install MySQL Workbech and perform your test from that tool.
Also you need to make some configurations into your MySQL database to allow those remote root connections, take a look at this post:
How to allow remote connection to mysql
I am able to connect database from SQLyog, but when connecting from Node.js getting following error:
Database IP : 10.0.0.200
Error when connecting to db: { Error: ER_DBACCESS_DENIED_ERROR: Access denied for user 'myuser'#'10.0.0.100' to database 'db_new'
at Handshake.Sequence._packetToError (\node_modules\mysql\lib\protocol\sequences\Sequence.js:52:14)
at Handshake.ErrorPacket (\node_modules\mysql\lib\protocol\sequences\Handshake.js:103:18)
at Protocol._parsePacket (\node_modules\mysql\lib\protocol\Protocol.js:279:23)
at Parser.write (\node_modules\mysql\lib\protocol\Parser.js:76:12)
at Protocol.write (\node_modules\mysql\lib\protocol\Protocol.js:39:16)
at Socket.<anonymous> (\node_modules\mysql\lib\Connection.js:103:28)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at readableAddChunk (_stream_readable.js:176:18)
at Socket.Readable.push (_stream_readable.js:134:10)
--------------------
at Protocol._enqueue (\node_modules\mysql\lib\protocol\Protocol.js:145:48)
at Protocol.handshake (\node_modules\mysql\lib\protocol\Protocol.js:52:23)
at Connection.connect (\node_modules\mysql\lib\Connection.js:130:18)
at Timeout.handleDisconnect [as _onTimeout] (\techdoc-connection.js:25:16)
at ontimeout (timers.js:386:14)
at tryOnTimeout (timers.js:250:5)
at Timer.listOnTimeout (timers.js:214:5)
code: 'ER_DBACCESS_DENIED_ERROR',
errno: 1044,
sqlMessage: 'Access denied for user \'myuser\'#\'10.0.0.100\' to database \'db_new\'',
sqlState: '42000',
fatal: true }
Code to connect to DB is as follows,
connection = mysql.createConnection({
host: settings.dbhost,
user: settings.user,
password: settings.password,
database: settings.database
});
Any ideas on how to fix this?
Related: Github issue on mysqljs
When encountering this issue, one common problem is that the user isn't being GRANTED with all the privileges. To grant with all the privileges, you may execute an SQL command as mysql suggests,
mysql> show grants;
+-------------------------------------------------------------------------------------------------------------+
| Grants for RollingRidge#% |
+-------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'RollingRidge'#'%' IDENTIFIED BY PASSWORD '*REDACTED' |
| GRANT ALL PRIVILEGES ON `rollingridge`.* TO 'RollingRidge'#'%' |
+-------------------------------------------------------------------------------------------------------------+
Hope this helps.
When I try to connect from node to my MySql database that runs on the same machine (debian), with a specific user and password I get an "Access denied" error.
If I try the same, with the same user and set no password in the database and node, everything works.
Also connection from the terminal WITH password works fine.
I already tried to delete user and database and recreated them. But nothing works.
This is my node code:
var http = require('http');
var fs = require('fs');
var Sequelize = require('sequelize')
var sequelize = new Sequelize('mysql://user:password#localhost:3306/databasename');
http.createServer(function (req, res) {
console.dir(req.url);
sequelize.query("SELECT ##version;").spread(function(results, metadata) {
console.log(results);
})
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('ok');
}).listen(8080);
What I get on the console is this:
Possibly unhandled Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'user'#'localhost' (using password: YES)
at Handshake.Sequence._packetToError (/var/www/html/node/node_modules/mysql/lib/protocol/sequences/Sequence.js:30:14)
at Handshake.ErrorPacket (/var/www/html/node/node_modules/mysql/lib/protocol/sequences/Handshake.js:67:18)
at Protocol._parsePacket (/var/www/html/node/node_modules/mysql/lib/protocol/Protocol.js:197:24)
at Parser.write (/var/www/html/node/node_modules/mysql/lib/protocol/Parser.js:62:12)
at Protocol.write (/var/www/html/node/node_modules/mysql/lib/protocol/Protocol.js:37:16)
at Socket.ondata (_stream_readable.js:555:20)
at emitOne (events.js:101:20)
at Socket.emit (events.js:191:7)
at readableAddChunk (_stream_readable.js:176:18)
at Socket.Readable.push (_stream_readable.js:134:10)
--------------------
at Protocol._enqueue (/var/www/html/node/node_modules/mysql/lib/protocol/Protocol.js:110:26)
at Protocol.handshake (/var/www/html/node/node_modules/mysql/lib/protocol/Protocol.js:42:41)
at Connection.connect (/var/www/html/node/node_modules/mysql/lib/Connection.js:81:18)
at ConnectorManager.connect (/var/www/html/node/node_modules/sequelize/lib/dialects/mysql/connector-manager.js:314:16)
at Object.create (/var/www/html/node/node_modules/sequelize/lib/dialects/mysql/connector-manager.js:138:19)
at createResource (/var/www/html/node/node_modules/generic-pool/lib/generic-pool.js:258:13)
at dispense (/var/www/html/node/node_modules/generic-pool/lib/generic-pool.js:250:9)
at Object.me.acquire (/var/www/html/node/node_modules/generic-pool/lib/generic-pool.js:316:5)
at CustomEventEmitter.fct (/var/www/html/node/node_modules/sequelize/lib/dialects/mysql/connector-manager.js:248:19)
at CustomEventEmitter.<anonymous> (/var/www/html/node/node_modules/sequelize/lib/emitters/custom-event-emitter.js:24:18)
Any clues?
Ok, the problem was, package.json did not contain the dependency (mysql or sequelize). I still don't know, why I could connect to the database without password though.
But now it works.