问题
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 ?
回答1:
I changed 'localhost' to '127.0.0.1' and it worked for me.
回答2:
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;
回答3:
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!
来源:https://stackoverflow.com/questions/27406734/sails-can-not-connect-to-mysql