mongodb速览
因公司需要,希望将日志类型的数据往mongodb上迁移,所以对mongodb相关知识体系做了一次快速梳理。
成果见PPT《mongodb速览》,下面仅记录搭建mongodb集群的关键步骤。
搭建步骤
config server
因为config server本身也是基于Replica set做高可用的,所以准备3个目录config0,config1,config2,让它们组成一个replica set。
配置分别如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
processManagement: fork: true pidFilePath: /data/mongodb/config0/mongod.pid net: bindIp: 127.0.0.1 port: 27000 storage: dbPath: /data/mongodb/config0/data systemLog: destination: file path: "/data/mongodb/config0/mongod.log" logAppend: true storage: journal: enabled: true sharding: clusterRole: configsvr replication: replSetName: csRS |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
processManagement: fork: true pidFilePath: /data/mongodb/config1/mongod.pid net: bindIp: 127.0.0.1 port: 27001 storage: dbPath: /data/mongodb/config1/data systemLog: destination: file path: "/data/mongodb/config1/mongod.log" logAppend: true storage: journal: enabled: true sharding: clusterRole: configsvr replication: replSetName: csRS |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
processManagement: fork: true pidFilePath: /data/mongodb/config2/mongod.pid net: bindIp: 127.0.0.1 port: 27002 storage: dbPath: /data/mongodb/config2/data systemLog: destination: file path: "/data/mongodb/config2/mongod.log" logAppend: true storage: journal: enabled: true sharding: clusterRole: configsvr replication: replSetName: csRS |
唯一值得注意的就是replica set的名字是csRS。
启动config0的命令:
config0/bin/mongod -f config0/mongod.conf
三个节点全部启动后,可以利用mongo shell客户端连接任意一个config server:
bin/mongo localhost:27000
通过如下命令,配置整个replica set:
1 2 3 4 5 6 7 8 9 10 11 12 |
rs.initiate( { _id: "csRS", version: 1, configsvr: true, members: [ { _id: 0, host : "localhost:27000" }, { _id: 1, host : "localhost:27001" }, { _id: 2, host : "localhost:27002" } ] } ) |
mongos
这里只搭建一个,mongos作为代理节点需要从config server获取数据库元信息,其配置如下:
1 2 3 4 5 6 7 8 9 10 11 12 |
processManagement: fork: true pidFilePath: /data/mongodb/mongos0/mongod.pid net: bindIp: 127.0.0.1 port: 28000 systemLog: destination: file path: "/data/mongodb/mongos0/mongos.log" logAppend: true sharding: configDB: csRS/127.0.0.1:27000,127.0.0.1:27001,127.0.0.1:27002 |
主要配置为configDB,指定了config server的replica set地址。
启动mongos的命令:
bin/mongos -f ./mongos.conf
shard
我准备搭建2个replica set,也就是2个shard。
其中shard0由shard0-0,shard0-1,shard0-2组成,配置分别如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
processManagement: fork: true pidFilePath: /data/mongodb/shard0-0/mongod.pid net: bindIp: 127.0.0.1 port: 29000 storage: dbPath: /data/mongodb/shard0-0/data systemLog: destination: file path: "/data/mongodb/shard0-0/mongod.log" logAppend: true storage: journal: enabled: true sharding: clusterRole: shardsvr replication: replSetName: shard0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
processManagement: fork: true pidFilePath: /data/mongodb/shard0-1/mongod.pid net: bindIp: 127.0.0.1 port: 29001 storage: dbPath: /data/mongodb/shard0-1/data systemLog: destination: file path: "/data/mongodb/shard0-1/mongod.log" logAppend: true storage: journal: enabled: true sharding: clusterRole: shardsvr replication: replSetName: shard0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
processManagement: fork: true pidFilePath: /data/mongodb/shard0-2/mongod.pid net: bindIp: 127.0.0.1 port: 29002 storage: dbPath: /data/mongodb/shard0-2/data systemLog: destination: file path: "/data/mongodb/shard0-2/mongod.log" logAppend: true storage: journal: enabled: true sharding: clusterRole: shardsvr replication: replSetName: shard0 |
主要区别在于clusterRole角色是shardsvr。
启动命令:
./bin/mongod -f mongod.conf
通过mongo shell连接任意节点,配置replica set:
1 2 3 4 5 6 7 8 9 10 11 |
rs.initiate( { _id: "shard0", version: 1, members: [ { _id: 0, host : "localhost:29000" }, { _id: 1, host : "localhost:29001" }, { _id: 2, host : "localhost:29002" } ] } ) |
目前config server尚不知道该shard的存在,需要连接mongos(bin/mongo localhost:27000)来间接的配置一下:
1 |
sh.addShard("shard0/localhost:29000,localhost:29001,localhost:29002") |
另外一个shard如法炮制。
结束
上面就是分布式Mongodb的全部搭建步骤了,云服务一般都提供mongodb,很少需要我们亲自搭建,主要是为了加强对mongodb的熟悉度。
如果文章帮助您解决了工作难题,您可以帮我点击屏幕上的任意广告,或者赞助少量费用来支持我的持续创作,谢谢~
