mongoose多个连接_连接(connections)
# 連接(connections)
> 原文:[Connections](http://mongoosejs.com/docs/connections.html)
## Connections
我們可以通過利用`mongoose.connect()`方法連接MongoDB 。
```
mongoose.connect('mongodb://localhost/myapp');
```
這是最需要的在連接 myapp數(shù)據(jù)庫(kù)運(yùn)行在默認(rèn)端口(27017)上。如果本地連接失敗,然后嘗試用127.0.0.1不是localhost。有時(shí)可能會(huì)出現(xiàn)問題,當(dāng)本地主機(jī)名已更改。
我們還可以視你的環(huán)境而定在URI指定幾個(gè)參數(shù):
```
mongoose.connect('mongodb://username:password@host:port/database?options...');
```
查看 [mongodb connection string spec](http://docs.mongodb.org/manual/reference/connection-string/) 了解更多。
### 選項(xiàng)
該連接方法還接受一個(gè)選項(xiàng)對(duì)象,該對(duì)象將被傳遞給底層驅(qū)動(dòng)程序。這里包含的所有選項(xiàng)優(yōu)先于連接字符串中傳遞的選項(xiàng)。
```
mongoose.connect(uri, options);
```
以下是可用的選項(xiàng)鍵:
- db - passed to the [underlying driver's db instance](http://mongodb.github.io/node-mongodb-native/2.1/api/Db.html)
- server - passed to the [underlying driver's server instance(s)](http://mongodb.github.io/node-mongodb-native/2.1/api/Server.html)
- replset - passed to the [underlying driver's ReplSet instance](http://mongodb.github.io/node-mongodb-native/2.1/api/ReplSet.html)
- user - username for authentication (if not specified in uri)
- pass - password for authentication (if not specified in uri)
- auth - options for authentication
- mongos - passed to the [underlying driver's mongos options](http://mongodb.github.io/node-mongodb-native/2.1/api/Mongos.html)
- promiseLibrary - sets the [underlying driver's promise library](http://mongodb.github.io/node-mongodb-native/2.1/api/MongoClient.html)
例子:
```
var options = {
db: { native_parser: true },
server: { poolSize: 5 },
replset: { rs_name: 'myReplicaSetName' },
user: 'myUserName',
pass: 'myPassword'
}
mongoose.connect(uri, options);
```
**注意:** 服務(wù)器選項(xiàng)auto\_reconnect默認(rèn)為真的可以重寫。數(shù)據(jù)庫(kù)選項(xiàng)`forceserverobjectid`設(shè)置為false將不能被重寫。
有關(guān)可用選項(xiàng)的更多信息,見[driver](https://github.com/mongodb/node-mongodb-native)。
注意:如果auto\_reconnect設(shè)置成on,mongoose會(huì)放棄試圖恢復(fù)一定數(shù)量的失敗后。設(shè)置[`server.reconnectTries`和`server.reconnectInterval options`選項(xiàng)](http://mongodb.github.io/node-mongodb-native/2.1/api/Server.html)增加mongoose嘗試重新連接的次數(shù)。
```
// Good way to make sure mongoose never stops trying to reconnect
mongoose.connect(uri, { server: { reconnectTries: Number.MAX_VALUE } });
```
### 連接字符串選項(xiàng)
mongoose支持以下的連接字符串選項(xiàng)。
- [ssl](http://mongodb.github.io/node-mongodb-native/2.1/api/Server.html)
- [poolSize](http://mongodb.github.io/node-mongodb-native/2.1/api/Server.html)
- [autoReconnect](http://mongodb.github.io/node-mongodb-native/2.1/api/Server.html)
- [socketTimeoutMS](http://mongodb.github.io/node-mongodb-native/2.1/api/Server.html)
- [connectTimeoutMS](http://mongodb.github.io/node-mongodb-native/2.1/api/Server.html)
- [authSource](http://mongodb.github.io/node-mongodb-native/2.1/api/Db.html)
- [retries](http://mongodb.github.io/node-mongodb-native/2.1/api/Server.html)
- [reconnectWait](http://mongodb.github.io/node-mongodb-native/2.1/api/Server.html)
- [rs\_name](http://mongodb.github.io/node-mongodb-native/2.1/api/ReplSet.html)
- [replicaSet](http://mongodb.github.io/node-mongodb-native/2.1/api/ReplSet.html)
- [nativeParser](http://mongodb.github.io/node-mongodb-native/2.1/api/Db.html)
- [w](http://mongodb.github.io/node-mongodb-native/2.1/api/Db.html)
- [journal](http://mongodb.github.io/node-mongodb-native/2.1/api/Db.html)
- [wtimeoutMS](http://mongodb.github.io/node-mongodb-native/2.1/api/Db.html)
- [readPreference](http://mongodb.github.io/node-mongodb-native/2.1/api/Db.html)
- [readPreferenceTags](http://mongodb.github.io/node-mongodb-native/2.1/api/Db.html)
- [sslValidate](http://mongodb.github.io/node-mongodb-native/2.1/api/Server.html)
### 關(guān)于keepAlive
> 對(duì)于長(zhǎng)時(shí)間運(yùn)行的應(yīng)用程序,它往往是謹(jǐn)慎開啟keepAlive數(shù)毫秒。沒有它,在一段時(shí)間后,你可能會(huì)開始看到“連接關(guān)閉”的錯(cuò)誤,似乎沒有理由。如果是這樣的話,讀了這些之后,你可能會(huì)決定啟用KeepAlive:
```
options.server.socketOptions = options.replset.socketOptions = { keepAlive: 120 };
mongoose.connect(uri, options);
```
### 復(fù)制集連接
用同樣方法連接到一個(gè)復(fù)制集但是通過逗號(hào)分隔uris的列表。
```
mongoose.connect('mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]' [, options]);
```
### 多mongos的支持
高可用性在多mongoss情況下也支持。通過你的mongos實(shí)例的連接字符串和設(shè)置mongos選項(xiàng)為true:
```
mongoose.connect('mongodb://mongosA:27501,mongosB:27501', { mongos: true }, cb);
```
### 多個(gè)連接
到目前為止,我們已經(jīng)看到了如何連接到使用Mongoose默認(rèn)連接MongoDB。有時(shí)我們可能需要多個(gè)連接到Mongo,各有不同的讀/寫設(shè)置,或者只是不同的數(shù)據(jù)庫(kù)為例。在這些情況下,我們可以利用`mongoose.createConnection()`接受所有已經(jīng)討論的爭(zhēng)論并返回你一個(gè)新的連接。
```
var conn = mongoose.createConnection('mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]', options);
```
此連接對(duì)象,然后用于創(chuàng)建和檢索模型。模型總是局限于單個(gè)連接。
### 連接池
每個(gè)連接,使用`mongoose.connect`或`mongoose.createconnection`創(chuàng)造所有的內(nèi)部配置連接池的默認(rèn)大小為5的支持 。使用您的連接選項(xiàng)調(diào)整池大小:
```
// single server
var uri = 'mongodb://localhost/test';
mongoose.createConnection(uri, { server: { poolSize: 4 }});
// for a replica set
mongoose.createConnection(uri, { replset: { poolSize: 4 }});
// passing the option in the URI works with single or replica sets
var uri = 'mongodb://localhost/test?poolSize=4';
mongoose.createConnection(uri);
```
### 下一步
現(xiàn)在我們已經(jīng)掌握了connections,讓我們看看我們?nèi)绾文軐⑽覀兊墓δ艿乃槠兂煽芍赜玫牟⒐蚕韀插件](http://mongoosejs.com/docs/plugins.html)。
總結(jié)
以上是生活随笔為你收集整理的mongoose多个连接_连接(connections)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: go读取excel_Excelize 2
- 下一篇: mysql 回滚失败_浅析Mysql 数