sails-mysql: ER_NO_DB_ERROR: No database selected

烈酒焚心 提交于 2019-12-13 19:09:24

问题


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?


回答1:


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.




回答2:


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.




回答3:


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)



回答4:


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.



来源:https://stackoverflow.com/questions/22548976/sails-mysql-er-no-db-error-no-database-selected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!