I am using Node-Webkit for creating desktop application using NodeJS.
In this i need to get data from MySQL database. For this am using node-mysql module and the following code is used to connect MySQL server using node-mysql.
var mysql_con = require('mysql');
var connection = mysql_con.createConnection({
host : input.host,
user : input.username,
password : input.password,
database : input.database
});
connection.connect(function(err) {
if (err) {
console.log('Error : '+err.stack);
}
console.log('Success');
});
if i run in the command line of my Windows machine, connection has been established and Success has been printed in console. But if i pack and run the code in the node-webkit's nw.exe its giving the following error in web console
Error : Error: ER_HANDSHAKE_ERROR: Bad handshake
at Handshake.Sequence._packetToError (C:\DOCUME~1\admin\LOCALS~1\Temp\nw4480_11607\node_modules\mysql\lib\protocol\sequences\Sequence.js:48:14)
at Handshake.ErrorPacket (C:\DOCUME~1\admin\LOCALS~1\Temp\nw4480_11607\node_modules\mysql\lib\protocol\sequences\Handshake.js:101:18)
at Protocol._parsePacket (C:\DOCUME~1\admin\LOCALS~1\Temp\nw4480_11607\node_modules\mysql\lib\protocol\Protocol.js:270:23)
at Parser.write (C:\DOCUME~1\admin\LOCALS~1\Temp\nw4480_11607\node_modules\mysql\lib\protocol\Parser.js:77:12)
at Protocol.write (C:\DOCUME~1\admin\LOCALS~1\Temp\nw4480_11607\node_modules\mysql\lib\protocol\Protocol.js:39:16)
at Socket.<anonymous> (C:\DOCUME~1\admin\LOCALS~1\Temp\nw4480_11607\node_modules\mysql\lib\Connection.js:82:28)
at Socket.EventEmitter.emit (events.js:104:17)
at readableAddChunk (_stream_readable.js:156:16)
at Socket.Readable.push (_stream_readable.js:123:10)
at TCP.onread (net.js:520:20)
--------------------
at Protocol._enqueue (C:\DOCUME~1\admin\LOCALS~1\Temp\nw4480_11607\node_modules\mysql\lib\protocol\Protocol.js:135:48)
at Protocol.handshake (C:\DOCUME~1\admin\LOCALS~1\Temp\nw4480_11607\node_modules\mysql\lib\protocol\Protocol.js:52:41)
at Connection.connect (C:\DOCUME~1\admin\LOCALS~1\Temp\nw4480_11607\node_modules\mysql\lib\Connection.js:108:18)
at Object.mysql.testConnection
Kindly suggest me solution to establish the MySQL connection through node-webkit.
i had the same problem... and downgrading node webkit to the version 0.8.6 did the trick...
i hope this help you...
Related
i have a node application that is duplicated both on the server (digital ocean) as well as my local machine. im trying to configure the local version so that i can use it as a 'sandbox' environment to make changes and edits before i push the update to the live application on the server.
the databaseconnection.js file contains the following.
const mysql = require('mysql');
function getConnection() {
return mysql.createPool({
connectionLimit: 100,
host: 'ipaddress',
port: 3306,
user: 'forge',
password: 'password',
database: 'mydatabase',
multipleStatements: true
});
}
module.exports = getConnection();
when i access the node application that is stored on the DO server, everything run fine, however, when i try to run it from my localhost environment, i get the following error.
Error: connect ETIMEDOUT
at PoolConnection.Connection._handleConnectTimeout (C:\MAMP\htdocs\WIGHTcloudDEV\node_modules\mysql\lib\Connection.js:409:13)
at Object.onceWrapper (events.js:286:20)
at Socket.emit (events.js:198:13)
at Socket._onTimeout (net.js:442:8)
at ontimeout (timers.js:436:11)
at tryOnTimeout (timers.js:300:5)
at listOnTimeout (timers.js:263:5)
at Timer.processTimers (timers.js:223:10)
--------------------
at Protocol._enqueue (C:\MAMP\htdocs\WIGHTcloudDEV\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at Protocol.handshake (C:\MAMP\htdocs\WIGHTcloudDEV\node_modules\mysql\lib\protocol\Protocol.js:51:23)
at PoolConnection.connect (C:\MAMP\htdocs\WIGHTcloudDEV\node_modules\mysql\lib\Connection.js:116:18)
at Pool.getConnection (C:\MAMP\htdocs\WIGHTcloudDEV\node_modules\mysql\lib\Pool.js:48:16)
at Pool.query (C:\MAMP\htdocs\WIGHTcloudDEV\node_modules\mysql\lib\Pool.js:202:8)
at Strategy.passport.use.OutlookStrategy [as _verify] (C:\MAMP\htdocs\WIGHTcloudDEV\config\passport-setup.js:58:18)
at C:\MAMP\htdocs\WIGHTcloudDEV\node_modules\passport-oauth2\lib\strategy.js:202:24
at Request._callback (C:\MAMP\htdocs\WIGHTcloudDEV\node_modules\passport-outlook\lib\strategy.js:157:5)
at Request.self.callback (C:\MAMP\htdocs\WIGHTcloudDEV\node_modules\request\request.js:185:22)
at Request.emit (events.js:198:13)
i slightly remember doing something with either ssh2 or tunnel forwarder in the past that pertained to this issue but i cant find any of my old files nor can i find anything online that clearly shows what im missing.
I'm trying to connect a Node.js app with a MySQL database which works for another app really well on a remote server with this code:
var mysql = require('mysql');
var con = mysql.createConnection({
host: "www.<dnw>.com",
port: <correct port number>,
user: "<correct Username>",
password: "<correct password>",
database: "<correct databasename>"
});
con.connect(function(err) {
if (err)
{
throw err;
}
});
con.end();
On execution I get this as a error message
/home/akiku/node/dnwbot/main.js:49
throw err;
^
Error: connect ECONNREFUSED 85.25.34.68: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 (/home/akiku/node/dnwbot/node_modules/mysql/lib/protocol/Protocol.js:145:48)
at Protocol.handshake (/home/akiku/node/dnwbot/node_modules/mysql/lib/protocol/Protocol.js:52:23)
at Connection.connect (/home/akiku/node/dnwbot/node_modules/mysql/lib/Connection.js:130:18)
at Object.<anonymous> (/home/akiku/node/dnwbot/main.js:46:9)
at promises.push.Promise (/home/akiku/node/dnwbot/node_modules/telebot/lib/telebot.js:439:29)
at new Promise (<anonymous>)
at TeleBot.event (/home/akiku/node/dnwbot/node_modules/telebot/lib/telebot.js:432:32)
at promise.then (/home/akiku/node/dnwbot/node_modules/telebot/lib/updates.js:92:33)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
What I thought it could be:
I checked the port and the host multiple times via SQL Query in phpmyAdmin. They are correct.
And I enabled in Plesk that ANY host can access the database.
The username and password is also correct.
What it should do:
Obviously just connect to the database. Nothing more.
Do you habe any clues what it could be?
Check you have access to remote server database.Try to connect using workbench or any other tool
I finally found the problem: The serveradmin blocked the port on a deeper server level which can't be accessed by Plesk.
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.
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.
When trying to use sails-mysql I get an ER_NO_DB_ERROR: No database selected exception.
Even though I followed all the instructions I was able to find as closely as possible. I also looked into related issues:
Sailsjs - How to use mySQL
https://github.com/balderdashy/sails/issues/632
http://dustinbolton.com/error-er_no_db_error-no-database-selected/
Nothing seemed to help so far.
This is what I am doing:
I started out with a fresh project:
sails new sql-test
cd sql-test
Installed sails-mysql
sudo npm install sails-mysql
I changed the config:
// config/adapters.js
module.exports.adapters = {
'default': 'mysql',
mysql: {
module : 'sails-mysql',
host : 'localhost',
port : 3306,
user : 'root',
password : 'superSecret',
database : 'testDB'
}
};
Created a Model:
// api/models/User.js
module.exports = {
attributes: {
name: 'string'
}
};
And when I try to run it from the project's root:
sails lift
I get the following:
Logic error in mySQL ORM.
{ [Error: ER_NO_DB_ERROR: No database selected] code: 'ER_NO_DB_ERROR', index: 0 }
error: Hook failed to load: orm (Error: ER_NO_DB_ERROR: No database selected)
error: Error encountered while loading Sails core!
error: Error: ER_NO_DB_ERROR: No database selected
at Query.Sequence._packetToError (/home/tster/Documents/sandbox/sql-test/node_modules/sails-mysql/node_modules/mysql/lib/protocol/sequences/Sequence.js:32:14)
at Query.ErrorPacket (/home/tster/Documents/sandbox/sql-test/node_modules/sails-mysql/node_modules/mysql/lib/protocol/sequences/Query.js:82:18)
at Protocol._parsePacket (/home/tster/Documents/sandbox/sql-test/node_modules/sails-mysql/node_modules/mysql/lib/protocol/Protocol.js:172:24)
at Parser.write (/home/tster/Documents/sandbox/sql-test/node_modules/sails-mysql/node_modules/mysql/lib/protocol/Parser.js:62:12)
at Protocol.write (/home/tster/Documents/sandbox/sql-test/node_modules/sails-mysql/node_modules/mysql/lib/protocol/Protocol.js:37:16)
at Socket.ondata (stream.js:51:26)
at Socket.EventEmitter.emit (events.js:117:20)
at Socket.<anonymous> (_stream_readable.js:746:14)
at Socket.EventEmitter.emit (events.js:92:17)
at emitReadable_ (_stream_readable.js:408:10)
at emitReadable (_stream_readable.js:404:5)
at readableAddChunk (_stream_readable.js:165:9)
at Socket.Readable.push (_stream_readable.js:127:10)
at TCP.onread (net.js:526:21)
--------------------
at Query.Sequence (/home/tster/Documents/sandbox/sql-test/node_modules/sails-mysql/node_modules/mysql/lib/protocol/sequences/Sequence.js:15:20)
at new Query (/home/tster/Documents/sandbox/sql-test/node_modules/sails-mysql/node_modules/mysql/lib/protocol/sequences/Query.js:12:12)
at Function.Connection.createQuery (/home/tster/Documents/sandbox/sql-test/node_modules/sails-mysql/node_modules/mysql/lib/Connection.js:48:10)
at Connection.query (/home/tster/Documents/sandbox/sql-test/node_modules/sails-mysql/node_modules/mysql/lib/Connection.js:100:26)
at __DESCRIBE__ (/home/tster/Documents/sandbox/sql-test/node_modules/sails-mysql/lib/adapter.js:121:20)
at afterwards (/home/tster/Documents/sandbox/sql-test/node_modules/sails-mysql/lib/adapter.js:571:7)
at Handshake._callback (/home/tster/Documents/sandbox/sql-test/node_modules/sails-mysql/lib/adapter.js:549:9)
at Handshake.Sequence.end (/home/tster/Documents/sandbox/sql-test/node_modules/sails-mysql/node_modules/mysql/lib/protocol/sequences/Sequence.js:66:24)
at Handshake.Sequence.OkPacket (/home/tster/Documents/sandbox/sql-test/node_modules/sails-mysql/node_modules/mysql/lib/protocol/sequences/Sequence.js:75:8)
at Protocol._parsePacket (/home/tster/Documents/sandbox/sql-test/node_modules/sails-mysql/node_modules/mysql/lib/protocol/Protocol.js:172:24)
at Parser.write (/home/tster/Documents/sandbox/sql-test/node_modules/sails-mysql/node_modules/mysql/lib/protocol/Parser.js:62:12)
at Protocol.write (/home/tster/Documents/sandbox/sql-test/node_modules/sails-mysql/node_modules/mysql/lib/protocol/Protocol.js:37:16)
at Socket.ondata (stream.js:51:26)
at Socket.EventEmitter.emit (events.js:117:20)
at Socket.<anonymous> (_stream_readable.js:746:14)
at Socket.EventEmitter.emit (events.js:92:17)
at emitReadable_ (_stream_readable.js:408:10)
at emitReadable (_stream_readable.js:404:5)
at readableAddChunk (_stream_readable.js:165:9)
at Socket.Readable.push (_stream_readable.js:127:10)
at TCP.onread (net.js:526:21)
Additional Information:
sails v0.9.13
sails-mysql (v0.9.9)
mysql v14.14 Distrib 5.5.34, for debian-linux-gnu (x86_64) using readline 6.2
I can connect the the database via command line.
I am able to connect and query the Database when using node-mysql
Could anybody give me some advice? Am I missing something? Anything else I should check out?
I just hit this same problem. In order to get it to work, I had to add the adapter configuration information to my model, e.g. /api/models/User.js:
module.exports = {
adapter: 'mysql',
config: {
host: 'localhost',
user: 'user',
port:'3306',
// Psst.. You can put your password in config/local.js instead
// so you don't inadvertently push it up if you're using version control
password: 'secret',
database: 'sailstest'
},
attributes: {
firstName: 'STRING'
}
};
See the docs:
http://sailsjs.org/#!documentation/models
I also had to add /api/controllers/UserController.js manually, as the generate function did not add it on this app, although it did add it on the previous test app I made.
UPDATE:
Install the Sails.js beta to get better functionality.
npm install sails#beta -g
Not only does the config actually work as expected (under config/connections.js), the ORM features support associations, which are not supported in the 0.9 release.
I know this question already has an answer, but it's more of a workaround. This is what fixed the same issue for me.
Changing the default adapters.js file mysql adapter object from myLocalMySQLDatabase to simply mysql seemed to fix it for me.
Hope this helps.
You may get this error when the .env file is not in the same directory.
You can check what the error is with the following code:
const result = require('dotenv').config();
if (result.error) {
console.log(result.error)
}
console.log(result.parsed)
You must take care about data type.
if you have module like this
module.exports = {
attributes: {
name: 'string'
}
};
In database under modal name table field name must be varchar.
My database field is set to text and I getting this error , when change it to varchar everything work.