1、启动 config server 服务
/chj/data/mongodb/chj_db/bin/mongod -f /chj/data/mongodb/chj_db/conf/config.conf
2、初始化config server集群
#登录其中一个config server节点 /chj/data/mongodb/chj_db/bin/mongo --port 27100 #配置集群 config = { _id:"repl_configsvr",members:[ {_id:0,host:"172.21.244.101:27100"}, {_id:1,host:"172.21.244.102:27100"}, {_id:2,host:"172.21.244.94:27100"}] } #初始化集群 rs.initiate(config) PS: 结果输出如下,说明集群初始化成功,可以通过rs.status()命令查看集群状态 { "ok" : 1, "$gleStats" : { "lastOpTime" : Timestamp(1557538260, 1), "electionId" : ObjectId("000000000000000000000000") }, "lastCommittedOpTime" : Timestamp(0, 0) }
3、启动存储节点服务
/chj/data/mongodb/chj_db/bin/mongod -f /chj/data/mongodb/chj_db/conf/shard1.conf
4、初始化存储集群
#登录你希望是主节点的服务器 /chj/data/mongodb/chj_db/bin/mongo --port 27101 #配置集群 config = { _id:"shard1",members:[ {_id:0,host:"172.21.244.101:27101"}, {_id:1,host:"172.21.244.102:27101"},{_id:2,host:"172.21.244.94:27101",arbiterOnly:true}] } #初始化集群 rs.initiate(config) PS: 结果输出如下,说明集群初始化成功,可以通过rs.status()命令查看集群状态 { "ok" : 1 }
5、添加存储集群的管理账号
登录主节点
/chj/data/mongodb/chj_db/bin/mongo --port 27101 use admin db.createUser( { user: "root", pwd: "123456", roles: [ { role: "root", db: "admin" } ] } )
6、启动mongos 服务
/chj/data/mongodb/chj_db/bin/mongos -f /chj/data/mongodb/chj_db/conf/mongos.conf
7、添加config server的管理账号
登录任意一个mongos节点
/chj/data/mongodb/chj_db/bin/mongo --port 27000 use admin db.createUser( { user: "root", pwd: "123456", roles: [ { role: "root", db: "admin" } ] } )
8、把存储节点添加到mongos
登录任意一个mongos节点(如果是在上一步的窗口,需要退出重新登录)
/chj/data/mongodb/chj_db/bin/mongo --port 27000 use admin db.auth('root','123456') #添加分片 sh.addShard('shard1/172.21.244.101:27101,172.21.244.102:27101,172.21.244.94:27101') #查看分片状态 sh.status()
四、交付业务方
1、建立应用账号
登录任意一个mongos节点 /chj/data/mongodb/chj_db/bin/mongo --port 27000 use admin db.auth('root','123456') #切到业务数据库 use chj_db #建立读写账号 db.createUser( { user: "chj_db_rw", pwd: "123456", roles: [ { role: "readWrite", db: "chj_db" }, { role: "dbOwner", db: "chj_db" } ] } ) #建立只读账号(根据业务需求确认是否需要) db.createUser( { user: "chj_db_r", pwd: "123456", roles: [ { role: "read", db: "chj_db" } ] } )
2、交付开发人员信息
连接地址:172.21.244.101:27000,172.21.244.102:27000,172.21.244.94:27000 库名:chj_db 账号:chj_db_rw 密码:123456
五、数据库启用分片
如果后期业务量大,需要开启分片,配置如下
#指定需要分片的数据库 mongos> sh.enableSharding("chj_db") { "ok" : 1, "operationTime" : Timestamp(1557546835, 3), "$clusterTime" : { "clusterTime" : Timestamp(1557546835, 3), "signature" : { "hash" : BinData(0,"bkrrr8Kxrr9j9udrDc/hURHld38="), "keyId" : NumberLong("6689575940508352541") } } } #在chj_db数据库和users集合中创建了name和age为升序的片键 mongos> sh.shardCollection("chj_db.users",{name:1,age:1}) { "collectionsharded" : "chj_db.users", "collectionUUID" : UUID("59c0b99f-efff-4132-b489-f6c7e3d98f42"), "ok" : 1, "operationTime" : Timestamp(1557546861, 12), "$clusterTime" : { "clusterTime" : Timestamp(1557546861, 12), "signature" : { "hash" : BinData(0,"UBB1A/YODnmXwG5eAhgNLcKVzug="), "keyId" : NumberLong("6689575940508352541") } } } #查看分片情况 mongos> sh.status() --- Sharding Status --- sharding version: { "_id" : 1, "minCompatibleVersion" : 5, "currentVersion" : 6, "clusterId" : ObjectId("5cd625e0da695346d740f749") } shards: { "_id" : "shard1", "host" : "shard1/172.21.244.101:27101,172.21.244.102:27101", "state" : 1 } active mongoses: "4.0.4-62-g7e345a7" : 3 autosplit: Currently enabled: yes balancer: Currently enabled: yes Currently running: no Failed balancer rounds in last 5 attempts: 0 Migration Results for the last 24 hours: No recent migrations databases: { "_id" : "chj_db", "primary" : "shard1", "partitioned" : true, "version" : { "uuid" : UUID("82088bc7-7b98-4033-843d-7058d8d959f6"), "lastMod" : 1 } } chj_db.users shard key: { "name" : 1, "age" : 1 } unique: false balancing: true chunks: shard1 1 { "name" : { "$minKey" : 1 }, "age" : { "$minKey" : 1 } } -->> { "name" : { "$maxKey" : 1 }, "age" : { "$maxKey" : 1 } } on : shard1 Timestamp(1, 0) { "_id" : "config", "primary" : "config", "partitioned" : true } config.system.sessions shard key: { "_id" : 1 } unique: false balancing: true chunks: shard1 1 { "_id" : { "$minKey" : 1 } } -->> { "_id" : { "$maxKey" : 1 } } on : shard1 Timestamp(1, 0)