[英]Handling database environment configuration in Sails.js
The issue I have is related to the following quote from the official documentation : 我遇到的问题与官方文档中的以下引用有关:
Note If any connection to an adapter is used by a model, then all connections to that adapter will be loaded on sails.lift, whether or not models are actually using them.
注意如果模型使用了与适配器的任何连接,则无论模型是否实际使用它们,都将在sails.lift上加载与该适配器的所有连接。 In the example above, if a model was configured to use the localMysql connection, then both localMysql and remoteMysql would attempt to connect at run time.
在上面的示例中,如果模型配置为使用localMysql连接,则localMysql和remoteMysql都将尝试在运行时进行连接。 It is therefore good practice to split your connection configurations up by environment and save them to the appropriate environment-specific config files, or else comment out any connections that you don't want active.
因此,最好按环境拆分连接配置并将其保存到适当的特定于环境的配置文件中,否则请注释掉任何不希望处于活动状态的连接。
How can you configure the connection for a production server? 如何配置生产服务器的连接?
My connections.js file looks like this: 我的connections.js文件看起来像这样:
module.exports.connections = {
mongoDev: {
adapter: 'sails-mongo',
host: 'localhost',
port: 27017,
user: 'username',
password: 'password',
database: 'database'
},
mongoLive: {
adapter: 'sails-mongo',
host: 'host.mongolab.com',
port: 31681,
user: 'user',
password: 'password',
database: 'database'
}
};
And in my environment config files I've got: 在我的环境配置文件我有:
development.js development.js
module.exports = {
models: {
connection: 'mongoDev'
}
};
production.js production.js
module.exports = {
models: {
connection: 'mongoLive'
},
port: 3000,
};
This works on my local machine, because the production database server is on an external server. 这适用于我的本地计算机,因为生产数据库服务器位于外部服务器上。 On the production environment I'm getting the following error:
在生产环境中我收到以下错误:
[Error: failed to connect to [localhost:27017]]
It works if I remove the mongoDev object from my the connections object. 如果我从连接对象中删除mongoDev对象,它可以工作。
I've also tried using adaptors.js, but this only resulted in some deprecation errors. 我也尝试过使用adaptors.js,但这只会导致一些弃用错误。
$ sails -v
info: v0.9.9
I'm getting something different when running sails lift
: 在运行
sails lift
时,我得到了一些不同的东西:
info: Sails
info: v0.10.5
You want to save the actual connection definition in the either development.js or production.js and remove them from connections.js. 您希望将实际连接定义保存在development.js或production.js中,并从connections.js中删除它们。 It's a little non-intuitive.
这有点不直观。
development.js development.js
module.exports = {
connections : {
mongoDev: {
adapter: 'sails-mongo',
host: 'localhost',
port: 27017,
user: 'username',
password: 'password',
database: 'database'
}
},
models: {
connection: 'mongoDev'
}
};
production.js production.js
module.exports = {
connections : {
mongoLive: {
adapter: 'sails-mongo',
host: 'host.mongolab.com',
port: 31681,
user: 'user',
password: 'password',
database: 'database'
}
},
models: {
connection: 'mongoLive'
},
port: 3000,
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.