Sails无法连接到mySql

时间:2014-12-10 17:02:53

标签: mysql sails.js

在一个全新的风帆项目中,我添加了:

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

在config.connections.js上,并且:

connection: 'localMysql',
migrate: 'alter'

在config.models.js上。在api / models / User.js上设置用户模型:

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

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

跑步时,帆抬起,我获得:

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)

同时,在同一个文件夹上我运行了帆,我可以通过以下方式成功连接到mysql(sails数据库当前为空):

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

sails和mysql都在一个流浪盒中运行。

有什么建议吗?

4 个答案:

答案 0 :(得分:2)

我改变了#localhost&#39;至&#39; 127.0.0.1&#39;它对我有用。

答案 1 :(得分:1)

您尝试过这种方式吗?

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',
      }
  
  };

答案 2 :(得分:0)

您的风帆配置没有任何问题。我复制了代码并在我的最后测试了它。确保您对要尝试访问的数据库拥有所有必要的权限。只是能够连接到数据库并不意味着您拥有所有必要的权限。执行以下查询后尝试sails lift,以root用户身份登录到mysql

GRANT ALL PRIVILEGES ON sails.* TO 'homestead'@'%' WITH GRANT OPTION;

答案 3 :(得分:0)

我得到了同样的错误。然后我在mysql中创建了一个数据库,其中包含您指定的名称(即sails)。服务器启动成功!