`
fantaxy025025
  • 浏览: 1278943 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类

关于MONGODB最大连接数的查看与修改

 
阅读更多

 

关于MONGODB最大连接数的查看与修改

在Linux平台下,无论是64位或者32位的MongoDB默认最大连接数都是819,WIN平台不知道,估计也没有人在 WIN平台下使用MongoDB做生产环境

[root@DELL113 mongodb-linux-i686-2.4.1]# mongo admin -u root -p password
MongoDB shell version: 2.4.1
connecting to: 192.168.6.42/admin
> db.serverStatus().connections
{ "current" : 1, "available" : 818, "totalCreated" : NumberLong(1) }


途中available显示818少了一个,表示空闲的。current表示已经占用了的连接数,两数一加就等于819,如果我现在在连接一个,那么available就是817,current就是2

[root@DELL113 mongodb-linux-i686-2.4.1]# ./bin/mongo 192.168.6.42
MongoDB shell version: 2.4.1
connecting to: 192.168.6.42/test
> db.serverStatus().connections
"current" : 1, "available" : 818, "totalCreated" : NumberLong(1) }
> db.serverStatus().connections
"current" : 2, "available" : 817, "totalCreated" : NumberLong(2) }


819个连接数对于一般的站点我认为已经够用,并且都是现连现取现断。但这个连接数也可以修改,只要在启动的时候加入--maxConns即可
服务器启动

[root@lee mongodb-linux-x86_64-2.4.1]# ./bin/mongod --dbpath=/root/db --maxConns=2000
Wed Apr 3 11:06:21.905 [initandlisten] MongoDB starting : pid=2812 port=27017 dbpath=/root/db 64-bit host=lee
Wed Apr 3 11:06:21.957 [initandlisten] db version v2.4.1
Wed Apr 3 11:06:21.957 [initandlisten] git version: 1560959e9ce11a693be8b4d0d160d633eee75110
Wed Apr 3 11:06:21.957 [initandlisten] build info: Linux ip-10-2-29-40 2.6.21.7-2.ec2.v1.2.fc8xen #1 SMP Fri Nov 20 17:48:28 EST 2009 x86_64 BOOST_LIB_VERSION=1_49
Wed Apr 3 11:06:21.957 [initandlisten] allocator: tcmalloc
Wed Apr 3 11:06:21.957 [initandlisten] options: { dbpath: "/root/db", maxConns: 2000 }
Wed Apr 3 11:06:21.982 [initandlisten] journal dir=/root/db/journal
Wed Apr 3 11:06:21.982 [initandlisten] recover : no journal files present, no recovery needed
Wed Apr 3 11:06:22.297 [initandlisten] preallocateIsFaster=true 2.62
Wed Apr 3 11:06:22.717 [initandlisten] --maxConns too high, can only handle 819
Wed Apr 3 11:06:22.724 [initandlisten] waiting for connections on port 27017
Wed Apr 3 11:06:22.725 [websvr] admin web console waiting for connections on port 28017
Wed Apr 3 11:06:25.126 [initandlisten] connection accepted from 192.168.4.86:53917 #1 (1 connection now open)


查询最大连接数

[root@DELL113 mongodb-linux-i686-2.4.1]# ./bin/mongo 192.168.6.42
MongoDB shell version: 2.4.1
connecting to: 192.168.6.42/test
> db.serverStatus().connections
"current" : 1, "available" : 818, "totalCreated" : NumberLong(1) }


发现还是819?其实是Linux默认进程能打开最大文件数有关,可以通过ulimit 解决

[root@lee mongodb-linux-x86_64-2.4.1]# ulimit -n 2500
[root@lee mongodb-linux-x86_64-2.4.1]# ./bin/mongod --dbpath=/root/db --maxConns=2000
Wed Apr 3 11:11:07.013 [initandlisten] MongoDB starting : pid=2930 port=27017 dbpath=/root/db 64-bit host=lee
Wed Apr 3 11:11:07.013 [initandlisten] db version v2.4.1
Wed Apr 3 11:11:07.013 [initandlisten] git version: 1560959e9ce11a693be8b4d0d160d633eee75110
Wed Apr 3 11:11:07.013 [initandlisten] build info: Linux ip-10-2-29-40 2.6.21.7-2.ec2.v1.2.fc8xen #1 SMP Fri Nov 20 17:48:28 EST 2009 x86_64 BOOST_LIB_VERSION=1_49
Wed Apr 3 11:11:07.013 [initandlisten] allocator: tcmalloc
Wed Apr 3 11:11:07.013 [initandlisten] options: { dbpath: "/root/db", maxConns: 2000 }
Wed Apr 3 11:11:07.031 [initandlisten] journal dir=/root/db/journal
Wed Apr 3 11:11:07.031 [initandlisten] recover : no journal files present, no recovery needed
Wed Apr 3 11:11:07.170 [initandlisten] waiting for connections on port 27017
Wed Apr 3 11:11:07.171 [websvr] admin web console waiting for connections on port 28017
Wed Apr 3 11:11:10.076 [initandlisten] connection accepted from 192.168.4.86:53161 #1 (1 connection now open)


再查看最大连接数,搞定

[root@DELL113 mongodb-linux-i686-2.4.1]# ./bin/mongo 192.168.6.42
MongoDB shell version: 2.4.1
connecting to: 192.168.6.42/test
> db.serverStatus().connections
"current" : 1, "available" : 1999, "totalCreated" : NumberLong(1) }


关于ulimit的更多知识大家可以去网上检索检索

客户端程序通常是通过DRIVER来链接,由于每次建立链接的成本都挺高,因此都用链接池来实现,SPRING DATA MONGODB中是如下配置

mongo.dbname=cms
#线程池的大小
mongo.connectionsPerHost=100
#这个*mongo.connectionsPerHost则是如果链接数大于100的等待xttk数
mongo.threadsAllowedToBlockForConnectionMultiplier=4
#等待线程的等待时间
mongo.maxWaitTime=1500
mongo.socketTimeout=1500
mongo.connectTimeout=1000
mongo.autoConnectRetry=true
mongo.socketKeepAlive=true
mongo.slaveOk=true



  • autoConnectRetry simply means the driver will automatically attempt to reconnect to the server(s) after unexpected disconnects. In production environments you usually want this set to true.

  • connectionsPerHost are the amount of physical connections a single Mongo instance (it's singleton so you usually have one per application) can establish to a mongod/mongos process. At time of writing the java driver will establish this amount of connections eventually even if the actual query throughput is low (in order words you will see the "conn" statistic in mongostat rise until it hits this number per app server).

    There is no need to set this higher than 100 in most cases but this setting is one of those "test it and see" things. Do note that you will have to make sure you set this low enough so that the total amount of connections to your server do not exceed

    db.serverStatus().connections.available

    In production we currently have this at 40.

  • connectTimeout. As the name suggest number of milliseconds the driver will wait before a connection attempt is aborted. Set timeout to something long (15-30 seconds) unless there's a realistic, expected chance this will be in the way of otherwise succesful connection attempts. Normally if a connection attempt takes longer than a couple of seconds your network infrastructure isn't capable of high throughput.

  • maxWaitTime. Number of ms a thread will wait for a connection to become available on the connection pool, and raises an exception if this does not happen in time. Keep default.

  • socketTimeout. Standard socket timeout value. Set to 60 seconds (60000).

  • threadsAllowedToBlockForConnectionMultiplier. Multiplier for connectionsPerHost that denotes the number of threads that are allowed to wait for connections to become available if the pool is currently exhausted. This is the setting that will cause the "com.mongodb.DBPortPool$SemaphoresOut: Out of semaphores to get db connection" exception. It will throw this exception once this thread queue exceeds the threadsAllowedToBlockForConnectionMultiplier value. For example, if the connectionsPerHost is 10 and this value is 5 up to 50 threads can block before the aforementioned exception is thrown.

    If you expect big peaks in throughput that could cause large queues temporarily increase this value. We have it at 1500 at the moment for exactly that reason. If your query load consistently outpaces the server you should just improve your hardware/scaling situation accordingly.

  • readPreference(UPDATED, 2.8+) Used to determine the default read preference and replaces "slaveOk". Set up a ReadPreference through one of the class factory method. A full description of the most common settings can be found at the end of this post

  • w(UPDATED, 2.6+) This value determines the "safety" of the write. When this value is -1 the write will not report any errors regardless of network or database errors. WriteConcern.NONE is the appropriate predefined WriteConcern for this. If w is 0 then network errors will make the write fail but mongo errors will not. This is typically referred to as "fire and forget" writes and should be used when performance is more important than consistency and durability. Use WriteConcern.NORMAL for this mode.

    If you set w to 1 or higher the write is considered safe. Safe writes perform the write and follow it up by a request to the server to make sure the write succeeded or retrieve an error value if it did not (in other words, it sends a getLastError() command after you write). Note that until this getLastError() command is completed the connection is reserved. As a result of that and the additional command the throughput will be signficantly lower than writes with w <= 0. With a w value of exactly 1 MongoDB guarantees the write succeeded (or verifiably failed) on the instance you sent the write to.

    In the case of replica sets you can use higher values for w whcih tell MongoDB to send the write to at least "w" members of the replica set before returning (or more accurately, wait for the replication of your write to "w" members). You can also set w to the string "majority" which tells MongoDB to perform the write to the majority of replica set members (WriteConcern.MAJORITY). Typicall you should set this to 1 unless you need raw performance (-1 or 0) or replicated writes (>1). Values higher than 1 have a considerable impact on write throughput.

  • fsync. Durability option that forces mongo to flush to disk after each write when enabled. I've never had any durability issues related to a write backlog so we have this on false (the default) in production.

  • j *(NEW 2.7+)*. Boolean that when set to true forces MongoDB to wait for a successful journaling group commit before returning. If you have journaling enabled you can enable this for additional durability. Refer to http://www.mongodb.org/display/DOCS/Journaling to see what journaling gets you (and thus why you might want to enable this flag).

ReadPreference The ReadPreference class allows you to configure to what mongod instances queries are routed if you are working with replica sets. The following options are available :

  • ReadPreference.primary() : All reads go to the repset primary member only. Use this if you require all queries to return consistent (the most recently written) data. This is the default.

  • ReadPreference.primaryPreferred() : All reads go to the repset primary member if possible but may query secondary members if the primary node is not available. As such if the primary becomes unavailable reads become eventually consistent, but only if the primary is unavailable.

  • ReadPreference.secondary() : All reads go to secondary repset members and the primary member is used for writes only. Use this only if you can live with eventually consistent reads. Additional repset members can be used to scale up read performance although there are limits to the amount of (voting) members a repset can have.

  • ReadPreference.secondaryPreferred() : All reads go to secondary repset members if any of them are available. The primary member is used exclusively for writes unless all secondary members become unavailable. Other than the fallback to the primary member for reads this is the same as ReadPreference.secondary().

  • ReadPreference.nearest() : Reads go to the nearest repset member available to the database client. Use only if eventually consistent reads are acceptable. The nearest member is the member with the lowest latency between the client and the various repset members. Since busy members will eventually have higher latencies this should also automatically balance read load although in my experience secondary(Preferred) seems to do so better if member latencies are relatively consistent.

Note : All of the above have tag enabled versions of the same method which return TaggableReadPreference instances instead. A full description of replica set tags can be found here :Replica Set Tags



参考网址:
http://api.mongodb.org/java/2.10.1/com/mongodb/MongoClientOptions.Builder.html#connectionsPerHost(int)
https://github.com/spring-projects/spring-data-mongodb/blob/master/spring-data-mongodb/src/main/resources/org/springframework/data/mongodb/config/spring-mongo-1.5.xsd

posted on 2015-01-06 22:10 paulwong 阅读(2000) 评论(0)  编辑  收藏 所属分类: 性能优MONGODB

 

 

++

=

=

=s=

=-s

s

 

分享到:
评论

相关推荐

    docker下部署mongodb

    详细介绍了在docker下部署mongodb的方法以及启动命令及设置容器开机自启动的命令。文章末还介绍了“当使用docker容器的时候有可能出现以下情况:IPv4转发已禁用”解决方法。

    Mongodb in Mycat指南

    其中,`name`属性用于唯一标识该连接配置,`maxCon`和`minCon`分别定义了连接池的最大和最小连接数,`balance`和`writeType`用于控制写操作的负载均衡策略。 - **配置表** 在`&lt;schema&gt;`节点中为MongoDB表指定配置...

    MongoDB介绍与使用文档

    ### MongoDB介绍与使用文档 #### mongoDB概述 MongoDB是一个介于关系数据库和非关系数据库之间的产品,它属于NoSQL数据库的一种,具有高度灵活性和强大的查询能力。与传统的关系型数据库相比,MongoDB采用了面向...

    mongodb复制集

    7. `--maxConns arg`: 最大连接数,默认1000000。 8. `--logpath arg`: 日志文件路径。 9. `--pidfilepath arg`: PID文件路径,用于记录进程ID。 10. `--keyFile arg`: 分片验证时使用的关键文件路径。 11. `--...

    mongodb优化

    在 MongoDB 命令行中,可以通过执行 `db.adminCommand({setParameter:1,internalQueryExecMaxBlockingSortBytes:335544320})` 来增加内部查询最大阻塞排序字节数,这有助于处理更大规模的数据排序。 慢查询是性能...

    MongoDB启动配置详解.pdf

    在本文中,我们将详细了解MongoDB的启动配置,参数说明,修改服务器的最大连接数,并介绍如何完成开启自启动的方法。 MongoDB配置及参数说明 在MongoDB的安装目录中,有几个子目录,bin下面是可执行文件,包括...

    mongodb3.2集群配置

    - `maxIncomingConnections: 65536`:最大允许的传入连接数。 - `wireObjectCheck: true`:开启对文档的严格检查。 4. **replication**: - `replSetName: shard_1_ReplSet`:指定复制集名称。 - `oplogSizeMB:...

    MongoDB 参考文档

    本参考文档旨在深入解析MongoDB的使用方法,涵盖其基本操作到高级功能,是学习与掌握MongoDB不可或缺的资料。 #### 关于MongoDB文档 在文档的第一部分“关于MongoDB文档”中,详细介绍了MongoDB文档的结构、版权、...

    mongodb基础学习

    - **最大连接数**:控制最大连接数,减少线程切换带来的性能损耗。 - **索引构建选项**:关闭索引构建重试选项`indexBuildRetry:false`,提高构建效率。 #### 配置示例 以下是一个简单的`mongod.cnf`配置文件示例:...

    解决MongoDB占用内存过大频繁死机的方法详解

    可以调整`maxConns`参数来限制最大同时连接数。默认值为1000000,你可以根据实际情况适当降低这个数值。 3. **优化查询和索引**: 不恰当的查询和缺乏合适索引可能导致MongoDB在处理数据时占用大量内存。审查查询...

    Ubuntu系统中安装MongoDB及其启动命令mongod的教程

    其中,`-journal`开启日志功能,`-maxConns=2400`设置最大连接数为2400,`-rest`允许通过REST API访问MongoDB。 5. **调整系统最大文件打开数**: 默认情况下,Ubuntu系统的最大文件打开数可能不足,可以使用`...

    Mongodb 启动命令mongod参数说明(中文翻译)

    19. **--maxConns arg**:设置最大并发连接数,默认为 2000。 20. **--noauth**:禁用身份验证,不建议在生产环境中使用。 21. **--nohttpinterface**:关闭 HTTP 接口,防止未经授权的访问。 22. **--noprealloc...

Global site tag (gtag.js) - Google Analytics