# 連接(connections)
> 原文:[Connections](http://mongoosejs.com/docs/connections.html)
## Connections
我們可以通過利用`mongoose.connect()`方法連接MongoDB 。
```
mongoose.connect('mongodb://localhost/myapp');
```
這是最需要的在連接 myapp數據庫運行在默認端口(27017)上。如果本地連接失敗,然后嘗試用127.0.0.1不是localhost。有時可能會出現問題,當本地主機名已更改。
我們還可以視你的環境而定在URI指定幾個參數:
```
mongoose.connect('mongodb://username:password@host:port/database?options...');
```
查看 [mongodb connection string spec](http://docs.mongodb.org/manual/reference/connection-string/) 了解更多。
### 選項
該連接方法還接受一個選項對象,該對象將被傳遞給底層驅動程序。這里包含的所有選項優先于連接字符串中傳遞的選項。
```
mongoose.connect(uri, options);
```
以下是可用的選項鍵:
- 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);
```
**注意:** 服務器選項auto\_reconnect默認為真的可以重寫。數據庫選項`forceserverobjectid`設置為false將不能被重寫。
有關可用選項的更多信息,見[driver](https://github.com/mongodb/node-mongodb-native)。
注意:如果auto\_reconnect設置成on,mongoose會放棄試圖恢復一定數量的失敗后。設置[`server.reconnectTries`和`server.reconnectInterval options`選項](http://mongodb.github.io/node-mongodb-native/2.1/api/Server.html)增加mongoose嘗試重新連接的次數。
```
// Good way to make sure mongoose never stops trying to reconnect
mongoose.connect(uri, { server: { reconnectTries: Number.MAX_VALUE } });
```
### 連接字符串選項
mongoose支持以下的連接字符串選項。
- [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)
### 關于keepAlive
> 對于長時間運行的應用程序,它往往是謹慎開啟keepAlive數毫秒。沒有它,在一段時間后,你可能會開始看到“連接關閉”的錯誤,似乎沒有理由。如果是這樣的話,讀了這些之后,你可能會決定啟用KeepAlive:
```
options.server.socketOptions = options.replset.socketOptions = { keepAlive: 120 };
mongoose.connect(uri, options);
```
### 復制集連接
用同樣方法連接到一個復制集但是通過逗號分隔uris的列表。
```
mongoose.connect('mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]' [, options]);
```
### 多mongos的支持
高可用性在多mongoss情況下也支持。通過你的mongos實例的連接字符串和設置mongos選項為true:
```
mongoose.connect('mongodb://mongosA:27501,mongosB:27501', { mongos: true }, cb);
```
### 多個連接
到目前為止,我們已經看到了如何連接到使用Mongoose默認連接MongoDB。有時我們可能需要多個連接到Mongo,各有不同的讀/寫設置,或者只是不同的數據庫為例。在這些情況下,我們可以利用`mongoose.createConnection()`接受所有已經討論的爭論并返回你一個新的連接。
```
var conn = mongoose.createConnection('mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]', options);
```
此連接對象,然后用于創建和檢索模型。模型總是局限于單個連接。
### 連接池
每個連接,使用`mongoose.connect`或`mongoose.createconnection`創造所有的內部配置連接池的默認大小為5的支持 。使用您的連接選項調整池大小:
```
// 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);
```
### 下一步
現在我們已經掌握了connections,讓我們看看我們如何能將我們的功能的碎片變成可重用的并共享[插件](http://mongoosejs.com/docs/plugins.html)。