linux下Mongodb集群搭建:分片+副本集
三台服务器 192.168.1.40/41/42
安装包 mongodb-linux-x86_64-amazon2-4.0.1.tgz
服务器40 | 服务器41 | 服务器42 |
mongos | mongos | mongos |
config server | config server | config server |
shard server1 主节点 | shard server1副节点 | shard server1仲裁 |
shard server2 仲裁 | shard server2 主节点 | shard server2 副节点 |
shard server3副节点 | shard server3仲裁 | shard server3主节点 |
端口分配:
mongos:28000
config:28001
shard1:28011
shard2:28012
shard3:28013
主要模块以及配置文件
1、config server 配置服务器
vi /usr/local/mongodb/conf/config.conf
40服务器配置文件
pidfilepath = /usr/local/mongodb/config/log/configsrv.pid
dbpath = /mydata/mongodb/config/data
logpath = /usr/local/mongodb/config/log/congigsrv.log
logappend = true bind_ip = 192.168.29.40
port =
fork = true #以守护进程的方式运行MongoDB,创建服务器进程 #declare this is a config db of a cluster;
configsvr = true #副本集名称
replSet=configs #设置最大连接数
maxConns=20000
41服务器配置文件
pidfilepath = /usr/local/mongodb/config/log/configsrv.pid
dbpath = /mydata/mongodb/config/data
logpath = /usr/local/mongodb/config/log/congigsrv.log
logappend = true bind_ip = 192.168.29.41
port =
fork = true #以守护进程的方式运行MongoDB,创建服务器进程 #declare this is a config db of a cluster;
configsvr = true #副本集名称
replSet=configs #设置最大连接数
maxConns=
42服务器配置文件
pidfilepath = /usr/local/mongodb/config/log/configsrv.pid
dbpath = /mydata/mongodb/config/data
logpath = /usr/local/mongodb/config/log/congigsrv.log
logappend = true bind_ip = 192.168.29.42
port =
fork = true #以守护进程的方式运行MongoDB,创建服务器进程 #declare this is a config db of a cluster;
configsvr = true #副本集名称
replSet=configs #设置最大连接数
maxConns=
启动三台服务器的config server
mongod -f /usr/local/mongodb/conf/config.conf 登录任意一台配置服务器,初始化配置副本集
#连接
mongo --port
#config变量
config = {
... _id : "configs",
... members : [
... {_id : , host : "192.168.1.40:28001" },
... {_id : , host : "192.168.1.41:28001" },
... {_id : , host : "192.168.1.42:28001" }
... ]
... } #初始化副本集
rs.initiate(config)
2 配置分片副本集(三台机器)
配置文件
vi /usr/local/mongodb/conf/shard1.conf
#配置文件内容
#——————————————–
pidfilepath = /usr/local/mongodb/shard1/log/shard1.pid
dbpath = /mydata/mongodb/shard1/data
logpath = /usr/local/mongodb/shard1/log/shard1.log
logappend = true bind_ip = 192.168.29.40
port =
fork = true #打开web监控
#httpinterface=true
#rest=true #副本集名称
replSet=shard1 #declare this is a shard db of a cluster;
shardsvr = true #设置最大连接数
maxConns=
#配置文件内容
#——————————————–
pidfilepath = /usr/local/mongodb/shard1/log/shard1.pid
dbpath = /mydata/mongodb/shard1/data
logpath = /usr/local/mongodb/shard1/log/shard1.log
logappend = true bind_ip = 192.168.29.41
port =
fork = true #打开web监控
#httpinterface=true
#rest=true #副本集名称
replSet=shard1 #declare this is a shard db of a cluster;
shardsvr = true #设置最大连接数
maxConns=20000
#配置文件内容
#——————————————–
pidfilepath = /usr/local/mongodb/shard1/log/shard1.pid
dbpath = /mydata/mongodb/shard1/data
logpath = /usr/local/mongodb/shard1/log/shard1.log
logappend = true bind_ip = 192.168.29.42
port = 28011
fork = true #打开web监控
#httpinterface=true
#rest=true #副本集名称
replSet=shard1 #declare this is a shard db of a cluster;
shardsvr = true #设置最大连接数
maxConns=20000
vi /usr/local/mongodb/conf/shard2.conf
vi /usr/local/mongodb/conf/shard2.conf (shard2和shard3就是上面配置文件相应地方改为2和3就可以了)
3、配置路由服务器 mongos
先启动配置服务器和分片服务器,后启动路由实例:(三台机器)
vi /usr/local/mongodb/conf/mongos.conf
#内容
pidfilepath = /usr/local/mongodb/mongos/log/mongos.pid
logpath = /usr/local/mongodb/mongos/log/mongos.log
logappend = true bind_ip = 0.0.0.0
port =
fork = true #监听的配置服务器,只能有1个或者3个 configs为配置服务器的副本集名字
configdb = configs/192.168.1.40:,192.168.1.41:,192.168.1.42: #设置最大连接数
maxConns=
启动三台服务器的mongos server
mongos -f /usr/local/mongodb/conf/mongos.conf
4、启用分片
目前搭建了mongodb配置服务器、路由服务器,各个分片服务器,不过应用程序连接到mongos路由服务器并不能使用分片机制,还需要在程序里设置分片配置,让分片生效。
登陆任意一台mongos
mongo --port 28000
#使用admin数据库
use admin
#串联路由服务器与分配副本集
sh.addShard("shard1/192.168.1.40:28011,192.168.1.41:28011,192.168.1.42:28011")
sh.addShard("shard2/192.168.1.40:28012,192.168.1.41:28012,192.168.1.42:28012")
sh.addShard("shard3/192.168.1.40:28013,192.168.1.41:28013,192.168.1.42:28013")
#查看集群状态
sh.status()
5、测试
目前配置服务、路由服务、分片服务、副本集服务都已经串联起来了,但我们的目的是希望插入数据,数据能够自动分片。连接在mongos上,准备让指定的数据库、指定的集合分片生效。
#指定testdb分片生效
db.runCommand( { enablesharding :"testdb"});
#指定数据库里需要分片的集合和片键
db.runCommand( { shardcollection : "testdb.table1",key : {id: } } ) 我们设置testdb的 table1 表需要分片,根据 id 自动分片到 shard1 ,shard2,shard3 上面去。要这样设置是因为不是所有mongodb 的数据库和表 都需要分片!
测试分片配置结果
mongo 127.0.0.1:
#使用testdb
use testdb;
#插入测试数据
for (var i = ; i <= ; i++)
db.table1.save({id:i,"test1":"testval1"});
#查看分片情况如下,部分无关信息省掉了
db.table1.stats(); {
"sharded" : true,
"ns" : "testdb.table1",
"count" : ,
"numExtents" : ,
"size" : ,
"storageSize" : ,
"totalIndexSize" : ,
"indexSizes" : {
"_id_" : ,
"id_1" :
},
"avgObjSize" : ,
"nindexes" : ,
"nchunks" : ,
"shards" : {
"shard1" : {
"ns" : "testdb.table1",
"count" : ,
"size" : ,
...
"ok" :
},
"shard2" : {
"ns" : "testdb.table1",
"count" : ,
"size" : ,
...
"ok" :
},
"shard3" : {
"ns" : "testdb.table1",
"count" :,
"size" : ,
...
"ok" :
}
},
"ok" :
}
6、后期运维
启动关闭
mongodb的启动顺序是,先启动配置服务器,在启动分片,最后启动mongos.
mongod -f /usr/local/mongodb/conf/config.conf
mongod -f /usr/local/mongodb/conf/shard1.conf
mongod -f /usr/local/mongodb/conf/shard2.conf
mongod -f /usr/local/mongodb/conf/shard3.conf
mongod -f /usr/local/mongodb/conf/mongos.conf
关闭时,直接killall杀掉所有进程
killall mongod
killall mongos
最新文章
- cent6.4使用
- Druid监控Mybatis不显示SQL问题
- scrollLeft的相关问题(js横向无缝滚动)
- 【原】js检测移动端横竖屏
- JavaScript实现字符串的contains函数
- Lost Cows(线段树 POJ2182)
- Lucky String
- 提交自己的插件包(package)
- Lua 简单的IO交互 和迷宫代码
- java websocket工具
- javascript isNaN
- gir配置
- Rxjava+Retrofit2+Okhttp3多文件上传(服务器端代码+客户端代码)
- ShellExecute函数简单说明
- Sublime 常用快捷键
- 如何使用LightningChart拖放功能进行数据转移 ?
- CRM权限管理
- File API简介
- PLSQL程序编写杂烦数据表信息编写批量排版
- (七十四)iOS8之前使socket可以后台运行的方法