44665

sails-mysql: ER_NO_DB_ERROR: No database selected

Question:

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:

<ul><li><a href="https://stackoverflow.com/questions/20521525/sailsjs-how-to-use-mysql" rel="nofollow">Sailsjs - How to use mySQL</a></li> <li><a href="https://github.com/balderdashy/sails/issues/632" rel="nofollow">https://github.com/balderdashy/sails/issues/632</a></li> <li><a href="http://dustinbolton.com/error-er_no_db_error-no-database-selected/" rel="nofollow">http://dustinbolton.com/error-er_no_db_error-no-database-selected/</a></li> </ul>

Nothing seemed to help so far.

<strong>This is what I am doing:</strong>

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) <hr /><h2>Additional Information:</h2> <ul><li><strong>sails</strong> v0.9.13</li> <li><strong>sails-mysql</strong> (v0.9.9) </li> <li><strong>mysql</strong> v14.14 Distrib 5.5.34, for debian-linux-gnu (x86_64) using readline 6.2</li> <li>I can connect the the database via command line.</li> <li>I am able to connect and query the Database when using <a href="https://github.com/felixge/node-mysql" rel="nofollow">node-mysql</a></li> </ul>

Could anybody give me some advice? Am I missing something? Anything else I should check out?

Answer1:

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:

<a href="http://sailsjs.org/#!documentation/models" rel="nofollow">http://sailsjs.org/#!documentation/models</a>

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.

Answer2:

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.

Answer3:

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)

Answer4:

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.

Recommend

  • sailsjs policy causing Can't set headers after they are sent error
  • Sails.JS + MySQL: Unknown column 'NaN' in 'where clause'
  • Sails socket authorization (using passport)
  • Cannot make a static reference to the non-static method sendEmptyMessage(int) from the type Handler
  • Sails JS complex many to many association
  • Communicate between asyncio protocol/servers
  • XCB equivalent of XkbSetDetectableAutoRepeat
  • file_get_contents with https requests via proxy
  • reset the XMPP connection if the page gets refreshed
  • Asynchronous loading of images
  • Why are Pickle files in Pickle protocol 4 twice as large as those in protocol 3 without having any g
  • SQL server command to create server alias?
  • How to style GCKUIMiniMediaControlsViewController in iOS?
  • using pinentry-tty in a bash script (like read)
  • SOCKS in C/C++ or another language?
  • Get last insert id of Postgresql
  • Hadoop shuffle uses which protocol?
  • Is it possible to generate a unique numeric value for each row in an iSeries table without looping?
  • PHP problems with current url
  • PHP: Get HTTP Protocol Version (HTTP/1.1 vs HTTP/2)
  • UIImagePickerControllerDelegate Methods Not Called When Delegate Not UIViewController
  • C++ - Is destructor called when a vector holds objects?
  • Base64 as method of sanitizing user input for Mysql
  • Grunt watch Running “watch” task Waiting
  • Relative paths. baseUrl and paths not working on ionic2 - angular2
  • Installing Perl6 and Panda on Ubuntu 15.10. Problems with bootstrap.pl
  • Can my PDF ping my server when it is opened?
  • Angular2 component view does not update on value change via method
  • CakePHP ACL tutorial initDB function warnings
  • error importing numpy
  • Can you perform a UNION without a subquery in SQLAlchemy?
  • Very simple C++ DLL that can be called from .net
  • Android full screen on only one activity?
  • PostgreSQL Query without WHERE only ORDER BY and LIMIT doesn't use index
  • Accessing IRQ description array within a module and displaying action names
  • Display Images one by one with next and previous functionality
  • SetUp method failed while running tests from teamcity
  • SQL merge duplicate rows and join values that are different
  • Understanding cpu registers
  • Why joiner is not used after Sequence generator or Update statergy