Unable to connect to mysql database in nodejs application in gcp - mysql

I am unable to connect to my database hosted on cPanel from my Nodejs application in Google Cloud Platform.
I used Google Cloud Platform Cloud Shell, First I clone the application, second I run the command npm install and then npm start.
Application was unable to start when I run npm start.
Here's the error I am getting in Google Cloud Platform :-
/home/ejekanshjain/pams-rest-api/node_modules/mysql/lib/protocol/Parser.js:437
throw err; // Rethrow non-MySQL errors
^
Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'ejekanshjain'#'35.185.177.100' (using password: YES)
at Handshake.Sequence._packetToError (/home/ejekanshjain/pams-rest-api/node_modules/mysql/lib/protocol/sequences/Sequence.js:47:14)
at Handshake.ErrorPacket (/home/ejekanshjain/pams-rest-api/node_modules/mysql/lib/protocol/sequences/Handshake.js:123:18)
at Protocol._parsePacket (/home/ejekanshjain/pams-rest-api/node_modules/mysql/lib/protocol/Protocol.js:291:23)
at Parser._parsePacket (/home/ejekanshjain/pams-rest-api/node_modules/mysql/lib/protocol/Parser.js:433:10)
at Parser.write (/home/ejekanshjain/pams-rest-api/node_modules/mysql/lib/protocol/Parser.js:43:10)
at Protocol.write (/home/ejekanshjain/pams-rest-api/node_modules/mysql/lib/protocol/Protocol.js:38:16)
at Socket.<anonymous> (/home/ejekanshjain/pams-rest-api/node_modules/mysql/lib/Connection.js:91:28)
at Socket.<anonymous> (/home/ejekanshjain/pams-rest-api/node_modules/mysql/lib/Connection.js:525:10)
at Socket.emit (events.js:182:13)
at addChunk (_stream_readable.js:283:12)
--------------------
at Protocol._enqueue (/home/ejekanshjain/pams-rest-api/node_modules/mysql/lib/protocol/Protocol.js:144:48)
at Protocol.handshake (/home/ejekanshjain/pams-rest-api/node_modules/mysql/lib/protocol/Protocol.js:51:23)
at Connection.connect (/home/ejekanshjain/pams-rest-api/node_modules/mysql/lib/Connection.js:119:18)
at Object.<anonymous> (/home/ejekanshjain/pams-rest-api/db/db.js:14:16)
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 Module.require (internal/modules/cjs/loader.js:637:17)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! pams-rest-api#1.0.0 start: `node server.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the pams-rest-api#1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /home/ejekanshjain/.npm/_logs/2020-01-06T05_34_54_117Z-debug.log
Here's is my db file in nodejs :-
const mysql = require('mysql')
const dotenv = require('dotenv').config()
// Create connection to mysql database
const connection = mysql.createConnection({
host: 111.118.214.167,
user: ekanshjain,
password: password,
database: ekansh_db
})
// Open the MySQL connection
try {
connection.connect((err) => {
if (err) throw err
})
} catch (err) {
console.log("Error connecting to DB", err)
}
module.exports = connection
Can anyone suggest me why I am getting this and how could I solve this?

Related

node server in expressjs stops working after a while on its own

after running npm start, some time passes and then i get this error in the terminal. I am new to express js and node paired with mysql, so I can't really understand the errors below, hopefully that's enough context for this. The project is a simple table display of the database's table content, and it works fine, I just have to run npm start every 10-40 seconds.
events.js:291
throw er; // Unhandled 'error' event
^
Error: Connection lost: The server closed the connection.
at Protocol.end (/Volumes/HDD 500GB/* Documents/2 webdev/projects/nodejs/db connection demo/nodeapp/node_modules/mysql/lib/protocol/Protocol.js:112:13)
at Socket.<anonymous> (/Volumes/HDD 500GB/* Documents/2 webdev/projects/nodejs/db connection demo/nodeapp/node_modules/mysql/lib/Connection.js:94:28)
at Socket.<anonymous> (/Volumes/HDD 500GB/* Documents/2 webdev/projects/nodejs/db connection demo/nodeapp/node_modules/mysql/lib/Connection.js:526:10)
at Socket.emit (events.js:326:22)
at endReadableNT (_stream_readable.js:1226:12)
at processTicksAndRejections (internal/process/task_queues.js:80:21)
Emitted 'error' event on Connection instance at:
at Connection._handleProtocolError (/Volumes/HDD 500GB/* Documents/2 webdev/projects/nodejs/db connection demo/nodeapp/node_modules/mysql/lib/Connection.js:423:8)
at Protocol.emit (events.js:314:20)
at Protocol._delegateError (/Volumes/HDD 500GB/* Documents/2 webdev/projects/nodejs/db connection demo/nodeapp/node_modules/mysql/lib/protocol/Protocol.js:398:10)
at Protocol.end (/Volumes/HDD 500GB/* Documents/2 webdev/projects/nodejs/db connection demo/nodeapp/node_modules/mysql/lib/protocol/Protocol.js:116:8)
at Socket.<anonymous> (/Volumes/HDD 500GB/* Documents/2 webdev/projects/nodejs/db connection demo/nodeapp/node_modules/mysql/lib/Connection.js:94:28)
[... lines matching original stack trace ...]
at processTicksAndRejections (internal/process/task_queues.js:80:21) {
fatal: true,
code: 'PROTOCOL_CONNECTION_LOST'
}
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! nodeapp#0.0.0 start: `node ./bin/www`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the nodeapp#0.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/thodoristhomaidis/.npm/_logs/2021-01-22T03_09_42_711Z-debug.log
thodoristhomaidis#Theos-iMac nodeapp % npm i -g mysql
+ mysql#2.18.1
added 11 packages from 15 contributors in 0.589s
thodoristhomaidis#Theos-iMac nodeapp % npm start
> nodeapp#0.0.0 start /Volumes/HDD 500GB/* Documents/2 webdev/projects/nodejs/db connection demo/nodeapp
> node ./bin/www
Database is connected successfully !
GET /users/form 304 7.634 ms - -
GET /css/form-style.css 404 3.291 ms - 1532
events.js:291
throw er; // Unhandled 'error' event
^
Error: Connection lost: The server closed the connection.
at Protocol.end (/Volumes/HDD 500GB/* Documents/2 webdev/projects/nodejs/db connection demo/nodeapp/node_modules/mysql/lib/protocol/Protocol.js:112:13)
at Socket.<anonymous> (/Volumes/HDD 500GB/* Documents/2 webdev/projects/nodejs/db connection demo/nodeapp/node_modules/mysql/lib/Connection.js:94:28)
at Socket.<anonymous> (/Volumes/HDD 500GB/* Documents/2 webdev/projects/nodejs/db connection demo/nodeapp/node_modules/mysql/lib/Connection.js:526:10)
at Socket.emit (events.js:326:22)
at endReadableNT (_stream_readable.js:1226:12)
at processTicksAndRejections (internal/process/task_queues.js:80:21)
Emitted 'error' event on Connection instance at:
at Connection._handleProtocolError (/Volumes/HDD 500GB/* Documents/2 webdev/projects/nodejs/db connection demo/nodeapp/node_modules/mysql/lib/Connection.js:423:8)
at Protocol.emit (events.js:314:20)
at Protocol._delegateError (/Volumes/HDD 500GB/* Documents/2 webdev/projects/nodejs/db connection demo/nodeapp/node_modules/mysql/lib/protocol/Protocol.js:398:10)
at Protocol.end (/Volumes/HDD 500GB/* Documents/2 webdev/projects/nodejs/db connection demo/nodeapp/node_modules/mysql/lib/protocol/Protocol.js:116:8)
at Socket.<anonymous> (/Volumes/HDD 500GB/* Documents/2 webdev/projects/nodejs/db connection demo/nodeapp/node_modules/mysql/lib/Connection.js:94:28)
[... lines matching original stack trace ...]
at processTicksAndRejections (internal/process/task_queues.js:80:21) {
fatal: true,
code: 'PROTOCOL_CONNECTION_LOST'
}
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! nodeapp#0.0.0 start: `node ./bin/www`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the nodeapp#0.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/thodoristhomaidis/.npm/_logs/2021-01-22T03_11_13_709Z-debug.log
Try to use this code to handle server disconnect:
var db_config = {
host: 'localhost',
user: 'root',
password: '',
database: 'example'
};
var connection;
function handleDisconnect() {
connection = mysql.createConnection(db_config); // Recreate the connection, since
// the old one cannot be reused.
connection.connect(function(err) { // The server is either down
if(err) { // or restarting (takes a while sometimes).
console.log('error when connecting to db:', err);
setTimeout(handleDisconnect, 2000); // We introduce a delay before attempting to reconnect,
} // to avoid a hot loop, and to allow our node script to
}); // process asynchronous requests in the meantime.
// If you're also serving http, display a 503 error.
connection.on('error', function(err) {
console.log('db error', err);
if(err.code === 'PROTOCOL_CONNECTION_LOST') { // Connection to the MySQL server is usually
handleDisconnect(); // lost due to either server restart, or a
} else { // connnection idle timeout (the wait_timeout
throw err; // server variable configures this)
}
});
}
handleDisconnect();
In your code i am missing the parts after connection = mysql.createConnection(db_config);
can you check this

MySQL connection error with NodeJS on Heroku (works in Dev)

My application works correctly in Localhost with following connection details:
const mysql = require("mysql");
var mysqlConnection = mysql.createConnection({
host: "gatorxxxx.hostgator.com",
user: "ermaulik_chdb",
password: "<password>",
database: "ermaulik_chdb",
port: <port>
})
mysqlConnection.connect((err) => {
if(!err) {
console.log("Connection")
} else {
console.log(err)
}
});
But, it doesn't work when I host this application on Heroku and throws following exception.
Just to give more context, my database in hosted in cpanel of hostgator.
2020-05-23T16:47:32.542824+00:00 app[web.1]: server is running on port: 54806
2020-05-23T16:47:32.812651+00:00 app[web.1]: Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'ermaulik_chdb'#'ec2-3-84-45-11.compute-1.amazonaws.com' (using password: YES)
2020-05-23T16:47:32.812658+00:00 app[web.1]: at Handshake.Sequence._packetToError (/app/node_modules/mysql/lib/protocol/sequences/Sequence.js:47:14)
2020-05-23T16:47:32.812659+00:00 app[web.1]: at Handshake.ErrorPacket (/app/node_modules/mysql/lib/protocol/sequences/Handshake.js:123:18)
2020-05-23T16:47:32.812659+00:00 app[web.1]: at Protocol._parsePacket (/app/node_modules/mysql/lib/protocol/Protocol.js:291:23)
2020-05-23T16:47:32.812660+00:00 app[web.1]: at Parser._parsePacket (/app/node_modules/mysql/lib/protocol/Parser.js:433:10)
2020-05-23T16:47:32.812661+00:00 app[web.1]: at Parser.write (/app/node_modules/mysql/lib/protocol/Parser.js:43:10)
2020-05-23T16:47:32.812661+00:00 app[web.1]: at Protocol.write (/app/node_modules/mysql/lib/protocol/Protocol.js:38:16)
2020-05-23T16:47:32.812662+00:00 app[web.1]: at Socket.<anonymous> (/app/node_modules/mysql/lib/Connection.js:88:28)
2020-05-23T16:47:32.812662+00:00 app[web.1]: at Socket.<anonymous> (/app/node_modules/mysql/lib/Connection.js:526:10)
2020-05-23T16:47:32.812662+00:00 app[web.1]: at Socket.emit (events.js:310:20)
2020-05-23T16:47:32.812663+00:00 app[web.1]: at addChunk (_stream_readable.js:286:12)
2020-05-23T16:47:32.812663+00:00 app[web.1]: --------------------
2020-05-23T16:47:32.812664+00:00 app[web.1]: at Protocol._enqueue (/app/node_modules/mysql/lib/protocol/Protocol.js:144:48)
2020-05-23T16:47:32.812664+00:00 app[web.1]: at Protocol.handshake (/app/node_modules/mysql/lib/protocol/Protocol.js:51:23)
2020-05-23T16:47:32.812665+00:00 app[web.1]: at Connection.connect (/app/node_modules/mysql/lib/Connection.js:116:18)
2020-05-23T16:47:32.812665+00:00 app[web.1]: at Object.<anonymous> (/app/connection.js:11:17)
2020-05-23T16:47:32.812666+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:1133:30)
2020-05-23T16:47:32.812666+00:00 app[web.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:1153:10)
2020-05-23T16:47:32.812666+00:00 app[web.1]: at Module.load (internal/modules/cjs/loader.js:977:32)
2020-05-23T16:47:32.812667+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:877:14)
2020-05-23T16:47:32.812667+00:00 app[web.1]: at Module.require (internal/modules/cjs/loader.js:1019:19)
2020-05-23T16:47:32.812667+00:00 app[web.1]: at require (internal/modules/cjs/helpers.js:77:18) {
2020-05-23T16:47:32.812668+00:00 app[web.1]: code: 'ER_ACCESS_DENIED_ERROR',
2020-05-23T16:47:32.812668+00:00 app[web.1]: errno: 1045,
2020-05-23T16:47:32.812669+00:00 app[web.1]: sqlMessage: "Access denied for user 'ermaulik_chdb'#'ec2-3-84-45-11.compute-1.amazonaws.com' (using password: YES)",
2020-05-23T16:47:32.812669+00:00 app[web.1]: sqlState: '28000',
2020-05-23T16:47:32.812669+00:00 app[web.1]: fatal: true
2020-05-23T16:47:32.812670+00:00 app[web.1]: }
Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'ermaulik_chdb'#'ec2-3-84-45-11.compute-1.amazonaws.com'
you got an Error. because your MySQL database connection is Not Established.
Firstly, please check the Installed LAMP, Nodejs Acc. to your Code version supported. in the Heroku server. properly.
In the second step, check the MYSQL DB GRANT PRIVILEGES.
SHOW GRANTS FOR 'root'#'localhost';
the Third one is to Make sure your Nodejs Code & MySql DB
in the same server. If not you need to allow permission a server to connect OutSide the server.
and make sure the host, username, password, HOST all things are correct. I hope these things help you resolve a problem. thanks :)

node.js MySQL connection not established - only when using XAMPP

I'm trying to establish a MySQL connection with my node.js server, but when I do, I get the following error:
F:\Programmering\nodejs-server>node dbConnection.js
F:\Programmering\nodejs-server\dbConnection.js:10
if (err) throw err;
^
Error: connect ECONNREFUSED 127.0.0.1:3306
at Object._errnoException (util.js:1022:11)
at _exceptionWithHostPort (util.js:1044:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1198:14)
--------------------
at Protocol._enqueue (F:\Programmering\nodejs-server\node_modules\mysql\lib\
protocol\Protocol.js:145:48)
at Protocol.handshake (F:\Programmering\nodejs-server\node_modules\mysql\lib
\protocol\Protocol.js:52:23)
at Connection.connect (F:\Programmering\nodejs-server\node_modules\mysql\lib
\Connection.js:130:18)
at Object.<anonymous> (F:\Programmering\nodejs-server\dbConnection.js:9:5)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
I've tried using other ports, but still same error message. When I use XAMPP, and start MySQL service, it connects fine. These are the messages when I install mysql using npm:
F:\Programmering\nodejs-server>npm install mysql
npm WARN nodejs-server#1.0.0 No repository field.
+ mysql#2.15.0
added 11 packages in 7.809s
I have been using the w3schools tutorial. This is my code:
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "myusername",
password: "mypassword"
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
});
What am I doing wrong? Please help! Thank you!

Bad handshake or ECONNRESET Azure Mysql Nodejs

Similar issues have been reported, but I can't work around with there solutions.
I have an express.js app using KNEX to connect to a mysql database hosted in Azure.
I can connect to the DB perfectly from console command or mysql workbench.
However, no way to achieve the same from my node app.
My connection object is the same though:
config.knex = require('knex')({
client: 'mysql',
connection: {
host: 'wineserver.mysql.database.azure.com',
user: 'BurgerAndGreenBeans#wineserver',
password: 'FromageDeChevre',
database: 'gdpr',
insecureAuth: true
},
pool: {
min: 0
},
debug: ['ComQueryPacket']
});
Everytime I start the app, I get alternatively either one error or another one:
#1
Unhandled rejection Error: read ECONNRESET
at exports._errnoException (util.js:1050:11)
at TCP.onread (net.js:582:26)
--------------------
at Protocol._enqueue (C:\Projects\gdpr\node_modules\mysql\lib\protocol\Protocol.js:141:48)
at Protocol.handshake (C:\Projects\gdpr\node_modules\mysql\lib\protocol\Protocol.js:52:41)
at Connection.connect (C:\Projects\gdpr\node_modules\mysql\lib\Connection.js:130:18)
at C:\Projects\gdpr\node_modules\knex\lib\dialects\mysql\index.js:106:18
at Promise._execute (C:\Projects\gdpr\node_modules\bluebird\js\release\debuggability.js:300:9)
at Promise._resolveFromExecutor (C:\Projects\gdpr\node_modules\bluebird\js\release\promise.js:483:18)
at new Promise (C:\Projects\gdpr\node_modules\bluebird\js\release\promise.js:79:10)
at Client_MySQL.acquireRawConnection (C:\Projects\gdpr\node_modules\knex\lib\dialects\mysql\index.js:104:12)
at Object.create (C:\Projects\gdpr\node_modules\knex\lib\client.js:239:16)
at Pool._createResource (C:\Projects\gdpr\node_modules\generic-pool\lib\generic-pool.js:354:17)
at Pool.dispense [as _dispense] (C:\Projects\gdpr\node_modules\generic-pool\lib\generic-pool.js:314:10)
at Pool.acquire (C:\Projects\gdpr\node_modules\generic-pool\lib\generic-pool.js:436:8)
at C:\Projects\gdpr\node_modules\knex\lib\client.js:289:19
at Promise._execute (C:\Projects\gdpr\node_modules\bluebird\js\release\debuggability.js:300:9)
at Promise._resolveFromExecutor (C:\Projects\gdpr\node_modules\bluebird\js\release\promise.js:483:18)
at new Promise (C:\Projects\gdpr\node_modules\bluebird\js\release\promise.js:79:10)
Process finished with exit code 0
or
#2
Unhandled rejection Error: ER_HANDSHAKE_ERROR: Bad handshake
at Handshake.Sequence._packetToError (C:\Projects\gdpr\node_modules\mysql\lib\protocol\sequences\Sequence.js:52:14)
at Handshake.ErrorPacket (C:\Projects\gdpr\node_modules\mysql\lib\protocol\sequences\Handshake.js:103:18)
at Protocol._parsePacket (C:\Projects\gdpr\node_modules\mysql\lib\protocol\Protocol.js:280:23)
at Parser.write (C:\Projects\gdpr\node_modules\mysql\lib\protocol\Parser.js:75:12)
at Protocol.write (C:\Projects\gdpr\node_modules\mysql\lib\protocol\Protocol.js:39:16)
at Socket.<anonymous> (C:\Projects\gdpr\node_modules\mysql\lib\Connection.js:103:28)
at emitOne (events.js:96:13)
at Socket.emit (events.js:191:7)
at readableAddChunk (_stream_readable.js:178:18)
at Socket.Readable.push (_stream_readable.js:136:10)
at TCP.onread (net.js:561:20)
--------------------
at Protocol._enqueue (C:\Projects\gdpr\node_modules\mysql\lib\protocol\Protocol.js:141:48)
at Protocol.handshake (C:\Projects\gdpr\node_modules\mysql\lib\protocol\Protocol.js:52:41)
at Connection.connect (C:\Projects\gdpr\node_modules\mysql\lib\Connection.js:130:18)
at C:\Projects\gdpr\node_modules\knex\lib\dialects\mysql\index.js:106:18
at Promise._execute (C:\Projects\gdpr\node_modules\bluebird\js\release\debuggability.js:300:9)
at Promise._resolveFromExecutor (C:\Projects\gdpr\node_modules\bluebird\js\release\promise.js:483:18)
at new Promise (C:\Projects\gdpr\node_modules\bluebird\js\release\promise.js:79:10)
at Client_MySQL.acquireRawConnection (C:\Projects\gdpr\node_modules\knex\lib\dialects\mysql\index.js:104:12)
at Object.create (C:\Projects\gdpr\node_modules\knex\lib\client.js:239:16)
at Pool._createResource (C:\Projects\gdpr\node_modules\generic-pool\lib\generic-pool.js:354:17)
at Pool.dispense [as _dispense] (C:\Projects\gdpr\node_modules\generic-pool\lib\generic-pool.js:314:10)
at Pool.acquire (C:\Projects\gdpr\node_modules\generic-pool\lib\generic-pool.js:436:8)
at C:\Projects\gdpr\node_modules\knex\lib\client.js:289:19
at Promise._execute (C:\Projects\gdpr\node_modules\bluebird\js\release\debuggability.js:300:9)
at Promise._resolveFromExecutor (C:\Projects\gdpr\node_modules\bluebird\js\release\promise.js:483:18)
at new Promise (C:\Projects\gdpr\node_modules\bluebird\js\release\promise.js:79:10)
Process finished with exit code 0
It seems to be a kind of incompatibility between Node and Azure's mysql server.
Did anyone experienced the same ?
The similar question: Connecting to Azure Database for MySQL in npm/mysql. As #Gary Liu pointed out, you may use pull 1730 for a workaround.
Please modify the package.json as:
"dependencies": {
"knex": "^0.13.0",
"mysql": "git://github.com/mysqljs/mysql#e3e123e9af7c0829a6c1417d911572a75b4a5f95"
}
then, run npm install to install this version.

Connecting to Azure Database for MySQL in npm/mysql

I am getting an error when connecting to Azure Database for MySQL with the mysql npm module (version 2.13.0):
Error:
/Users/sascha/Desktop/azure-mysql/index.js:19
if (error) throw error;
^
Error: ER_HANDSHAKE_ERROR: Bad handshake
at Handshake.Sequence._packetToError (/Users/sascha/Desktop/azure-mysql/node_modules/mysql/lib/protocol/sequences/Sequence.js:52:14)
at Handshake.ErrorPacket (/Users/sascha/Desktop/azure-mysql/node_modules/mysql/lib/protocol/sequences/Handshake.js:103:18)
at Protocol._parsePacket (/Users/sascha/Desktop/azure-mysql/node_modules/mysql/lib/protocol/Protocol.js:280:23)
at Parser.write (/Users/sascha/Desktop/azure-mysql/node_modules/mysql/lib/protocol/Parser.js:75:12)
at Protocol.write (/Users/sascha/Desktop/azure-mysql/node_modules/mysql/lib/protocol/Protocol.js:39:16)
at TLSSocket.ondata (_stream_readable.js:528:20)
at emitOne (events.js:77:13)
at TLSSocket.emit (events.js:169:7)
at readableAddChunk (_stream_readable.js:146:16)
at TLSSocket.Readable.push (_stream_readable.js:110:10)
--------------------
at Protocol._enqueue (/Users/sascha/Desktop/azure-mysql/node_modules/mysql/lib/protocol/Protocol.js:141:48)
at Protocol.handshake (/Users/sascha/Desktop/azure-mysql/node_modules/mysql/lib/protocol/Protocol.js:52:41)
at Connection.connect (/Users/sascha/Desktop/azure-mysql/node_modules/mysql/lib/Connection.js:130:18)
at Object.<anonymous> (/Users/sascha/Desktop/azure-mysql/index.js:16:12)
at Module._compile (module.js:435:26)
at Object.Module._extensions..js (module.js:442:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
at Module.runMain [as _onTimeout] (module.js:467:10)
at Timer.listOnTimeout (timers.js:92:15)
Code:
var mysql = require("mysql");
var fs = require("fs");
var connection = mysql.createConnection({
host: "host",
user: "user#db",
password: "pw",
database: "scheme",
port: 3306,
ssl: {
ca: fs.readFileSync(__dirname + "/cert.pem")
},
insecureAuth: true
});
connection.connect();
connection.query('SELECT 1 + 1 AS solution', function (error, results, fields) {
if (error) throw error;
console.log('The solution is: ', results[0].solution);
});
connection.end();
Connecting to the database via MySQL Workbench works fine.
This is a known issue as our engineers have a disscusion at https://social.msdn.microsoft.com/Forums/en-US/c8fedbcc-909d-41ce-8c72-0374f76fdf82/cannot-connect-from-nodejs?forum=AzureDatabaseforMySQL.
And currently, I have test the solution provided by #Bradley Grainger at Cannot connect Azure Web App - NodeJS to Azure Mysql. We also can see the gist at https://github.com/mysqljs/mysql/commit/e3e123e9af7c0829a6c1417d911572a75b4a5f95.
Currently, please modify the package.json as:
"dependencies": {
"mysql": "git://github.com/mysqljs/mysql#e3e123e9af7c0829a6c1417d911572a75b4a5f95"
},
then, run npm install to install this version.
It should work as expected.
Additionally, please wait for the maintainer merges this PR.