sails-mysql: ER_NO_DB_ERROR: No database selected - mysql

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.

Related

how do i connect to a remote mysql database using node?

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.

what is 'node:6856' warnig?

This error came up when I tried running server. what should I do?
(node:6856) DeprecationWarning: current URL string parser is
deprecated, and will be removed in a future version. To use the new
parser, pass option { useNewUrlParser: true } to MongoClient.connect.
Server running on port ${port} { MongoNetworkError: failed to connect
to server [ds263172.mlab.com:63172] on first connect
[MongoNetworkError: connect ETIMEDOUT 54.171.245.223:63172] at
Pool. (D:\Learning &
Interest\Computer\Web\projects\devconnector\node_modules\mongodb-core\lib\topologies\server.js:564:11)
at emitOne (events.js:116:13) at Pool.emit (events.js:211:7) at
Connection. (D:\Learning &
Interest\Computer\Web\projects\devconnector\node_modules\mongodb-core\lib\connection\pool.js:317:12)
at Object.onceWrapper (events.js:317:30) at emitTwo (events.js:126:13)
at Connection.emit (events.js:214:7) at Socket.
(D:\Learning &
Interest\Computer\Web\projects\devconnector\node_modules\mongodb-core\lib\connection\connection.js:246:50)
at Object.onceWrapper (events.js:315:30) at emitOne (events.js:116:13)
at Socket.emit (events.js:211:7) at emitErrorNT
(internal/streams/destroy.js:66:8) at _combinedTickCallback
(internal/process/next_tick.js:139:11) at process._tickCallback
(internal/process/next_tick.js:181:9) name: 'MongoNetworkError',
errorLabels: [ 'TransientTransactionError' ],
[Symbol(mongoErrorContextSymbol)]: {} }
On your MongoClient.connect(...) statement add {useNewParser: true}. Your connect statement should look something like this:
MongoClient.connect("mongodb://localhost:27017/YourDB", { useNewUrlParser: true })
As the warning suggests you need to use the updated URL parser
USE this for Reference

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.

"mysql_clear_password" plugin error when using knex but not when using mysql2

I am trying to connect to MySQL server using the mysql_clear_password plugin. I set up the connection configuration for node-mysql2 as follows:
const connectionConfig = {
host: rwConfig.host,
user: rwConfig.user,
database: rwConfig.database,
ssl: {
ca: sslContent
},
password
}
Then to support the mysql_clear_password plugin I added the following (link of the reference i used: https://github.com/sidorares/node-mysql2/issues/438#issuecomment-255343793):
connectionConfig.authSwitchHandler = (data, cb) => {
console.log('In auth switch handler');
if (data.pluginName === 'mysql_clear_password') {
console.log(data);
console.log(data.pluginData.toString('utf8'));
console.log('In mysql clear password');
var tmppassword = connectionConfig.password + '\0';
var buffer = Buffer.from(tmppassword, 'base64');
cb(null, buffer);
}
};
This works when I attempt to connect to my database.
Now I try to do something similar using knexjs. I use the following configuration object:
const knexConfig = {
client: 'mysql2',
connection: connectionConfig,
pool: {
min: 0,
max: 20
},
acquireConnectionTimeout: 10000
};
The connectionConfig I passed in as the value for connection is the same object I used for connecting with node-mysql2. Then I create a knex connection:
const rwConnection = knex(knexConfig);
This for some reason throws this error:
{ Error: Client does not support authentication protocol requested by server; consider upgrading MySQL client
at Packet.asError (node_modules/mysql2/lib/packets/packet.js:703:13)
at ClientHandshake.Command.execute (node_modules/mysql2/lib/commands/command.js:28:22)
at Connection.handlePacket (node_modules/mysql2/lib/connection.js:515:28)
at PacketParser.onPacket (node_modules/mysql2/lib/connection.js:94:16)
at PacketParser.executeStart (node_modules/mysql2/lib/packet_parser.js:77:14)
at TLSSocket.<anonymous> (node_modules/mysql2/lib/connection.js:386:31)
at emitOne (events.js:96:13)
at TLSSocket.emit (events.js:188:7)
at readableAddChunk (_stream_readable.js:176:18)
at TLSSocket.Readable.push (_stream_readable.js:134:10)
at TLSWrap.onread (net.js:547:20)
code: 'ER_NOT_SUPPORTED_AUTH_MODE',
errno: 1251,
sqlState: '#08004' }
I don't know why this is giving me the error. In the knex documentation (http://knexjs.org/) it says:
The connection options are passed directly to the appropriate database client to create the connection, and may be either an object, or a connection string
Based on that I thought it would just pass the configuration through and make the connection using node-mysql2. Any help would be appreciated.
I figured out the issue and it works now. Knex was not passing on all the attributes to the msyql2 client.
Here is what I did for anyone else who might run into this problem. I modified the configurations that knex was passing to mysql2 in the node_modules/knex/lib/dialect/mysql2 and added authSwitchHandler to the configOptions array.

node-webkit MySQL connection Error - ER_HANDSHAKE_ERROR: Bad handshake

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...