3

On a brand new sails projet, I've added:

localMysql: {
    adapter: 'sails-mysql',
    host: 'localhost',
    user: 'homestead',
    password: 'secret',
    database: 'sails'
},

on config.connections.js, and:

connection: 'localMysql',
migrate: 'alter'

on config.models.js. A user model is set on api/models/User.js:

// api/models/User.js
module.exports = {

    attributes: {
        appid: {
          type: 'string'
        },
        uid: {
            type: 'string'
        },
        hid: {
            type: 'string'
        },
        params: {
            type: 'string'
        }
    }
}

When running, sails lift, I obtain:

error: A hook (`orm`) failed to load!
error: Error (E_UNKNOWN) :: Encountered an unexpected error
: Could not connect to MySQL:
Error: connect ECONNREFUSED
    at afterwards (/home/vagrant/sails/track/node_modules/sails-mysql/lib/connections/spawn.js:75:13)
    at /home/vagrant/sails/track/node_modules/sails-mysql/lib/connections/spawn.js:40:7
    at Pool.<anonymous> (/home/vagrant/sails/track/node_modules/sails-mysql/node_modules/mysql/lib/Pool.js:47:16)
    at Handshake.Sequence.end (/home/vagrant/sails/track/node_modules/sails-mysql/node_modules/mysql/lib/protocol/sequences/Sequence.js:78:24)
    at Protocol.handleNetworkError (/home/vagrant/sails/track/node_modules/sails-mysql/node_modules/mysql/lib/protocol/Protocol.js:282:14)
    at PoolConnection.Connection._handleNetworkError (/home/vagrant/sails/track/node_modules/sails-mysql/node_modules/mysql/lib/Connection.js:303:18)
    at Socket.emit (events.js:95:17)
    at net.js:440:14
    at process._tickDomainCallback (node.js:463:13)

Meanwhile, on same folder I ran sails, I can successfully connect to mysql (sails database is currently empty) by:

mysql --user=homestead --password=secret --host=localhost --database=sails

Both sails and mysql are running within a vagrant box.

Any suggestion ?

  • Is MySQL running on the default port? If not, you'll have to set the `port` in the connection config as well. – sgress454 Dec 21 '14 at 08:14

5 Answers5

2

I changed 'localhost' to '127.0.0.1' and it worked for me.

1

Did you try this way?

https://sailsjs.com/documentation/reference/configuration/sails-config-datastores

    // config/datastores.js
    module.exports.datastores = {
      default: {
        adapter: require('sails-mysql'),
        url: 'mysql://root:squ1ddy@localhost:3306/my_dev_db_name',
      }
  
  };
0

Nothing wrong with your sails configuration. I copied the code and tested it at my end. Make sure you have all the necessary permissions on the database you are trying to access. Just being able to connect to a database does not mean you have all the necessary permissions. Try sails lift after executing the query below by logging in as root to mysql

GRANT ALL PRIVILEGES ON sails.* TO 'homestead'@'%' WITH GRANT OPTION;
Mandeep Singh
  • 7,674
  • 19
  • 62
  • 104
0

I was getting the same error. Then I just created a database in mysql with the name specified (i.e. sails ) in your case. And the server started successfully!

AnkurVyas
  • 259
  • 2
  • 12
0

I have stuck in the same problem with sails version 1.5.4 (with the connection is defined in file config/datastores.js and the default is something with: url: mysql://user:password@host:port/database',

Turns out the problem is that I created the database using phpmyadmin with the database authentication plugin is set using Caching sha2 authentication. Re-create this database with Native MySQL authenticationplugin solves the issue.

Zuzu Softman
  • 468
  • 4
  • 15