1.创建数据库将会存放的文件夹
md E:\study\ducument\technology\2020\mongodb\res1
md E:\study\ducument\technology\2020\mongodb\res2
md E:\study\ducument\technology\2020\mongodb\res3
2.创建对应文件夹的数据库:
start mongod --bind_ip localhost --dbpath E:\study\ducument\technology\2020\mongodb\res1 --port 20001 --replSet myrs
start mongod --bind_ip localhost --dbpath E:\study\ducument\technology\2020\mongodb\res2 --port 20002 --replSet myrs
start mongod --bind_ip localhost --dbpath E:\study\ducument\technology\2020\mongodb\res3 --port 20003 --replSet myrs
3.将rest1端口为20001作为主要数据库
mongo localhost:20001
> rs.initiate();
{
"info2" : "no configuration specified. Using a default
configuration for the set",
"me" : "hostname:20001",
"ok" : 1,
"operationTime" : Timestamp(1538362864, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1538362864, 1),
"signature" : {
Figure 5-8. Connect to mongo instance on port 20001
Chapter 5 repliCation and Sharding134
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
4.将20002和20003作为20001的次要数据库
myrs:SECONDARY> rs.add("localhost:20002");
rs.add("localhost:20003"); // to add secondary
5.在主要数据库20001中可以看到自己的数据库状态
rs.status()
6.在主要数据库中加入数据
db.employee.insert({_id:10001,name:'Subhashini'});
db.employee.insert({_id:10002,name:'Shobana'});
7.次要数据库如果要查看数据,则先执行这个
rs.slaveOk()
db.employee.find()
8.次要数据库不能添加数据
=====Sharding=====创建分区数据库
1.创建以下文件夹用于分区shard1、shard2、shard3存入数据库
shard1
md E:\study\ducument\technology\2020\mongodb\shard_data\shard1\data1
md E:\study\ducument\technology\2020\mongodb\shard_data\shard1\data2
md E:\study\ducument\technology\2020\mongodb\shard_data\shard1\data3
shard2
md E:\study\ducument\technology\2020\mongodb\shard_data\shard2\data1
md E:\study\ducument\technology\2020\mongodb\shard_data\shard2\data2
md E:\study\ducument\technology\2020\mongodb\shard_data\shard2\data3
shard3
md E:\study\ducument\technology\2020\mongodb\shard_data\shard3\data1
md E:\study\ducument\technology\2020\mongodb\shard_data\shard3\data2
md E:\study\ducument\technology\2020\mongodb\shard_data\shard3\data3
2.以下命令来创建分区shard1、shard2、shard3的数据库
shard1
start mongod.exe --shardsvr --port 26017 --dbpath "E:\study\ducument\technology\2020\mongodb\shard_data\shard1\data1" --replSet shard1_replset
start mongod.exe --shardsvr --port 26117 --dbpath "E:\study\ducument\technology\2020\mongodb\shard_data\shard1\data2" --replSet shard1_replset
start mongod.exe --shardsvr --port 26217 --dbpath "E:\study\ducument\technology\2020\mongodb\shard_data\shard1\data3" --replSet shard1_replset
shard2
start mongod.exe --shardsvr --port 28017 --dbpath "E:\study\ducument\technology\2020\mongodb\shard_data\shard2\data1" --replSet shard2_replset
start mongod.exe --shardsvr --port 28117 --dbpath "E:\study\ducument\technology\2020\mongodb\shard_data\shard2\data2" --replSet shard2_replset
start mongod.exe --shardsvr --port 28217 --dbpath "E:\study\ducument\technology\2020\mongodb\shard_data\shard2\data3" --replSet shard2_replset
shard3
start mongod.exe --shardsvr --port 29017 --dbpath "E:\study\ducument\technology\2020\mongodb\shard_data\shard3\data1" --replSet shard3_replset
start mongod.exe --shardsvr --port 29117 --dbpath "E:\study\ducument\technology\2020\mongodb\shard_data\shard3\data2" --replSet shard3_replset
start mongod.exe --shardsvr --port 29217 --dbpath "E:\study\ducument\technology\2020\mongodb\shard_data\shard3\data3" --replSet shard3_replset
3.指定每个分区中数据库的主从数据库
mongo.exe localhost:26017
rs.initiate({_id: "shard1_replset",members: [{ _id : 0, host:"localhost:26017" },{ _id : 1, host:"localhost:26117" },{ _id : 2, host:"localhost:26217" }]})
mongo.exe localhost:28017
rs.initiate({_id: "shard2_replset",members: [{ _id : 0, host:"localhost:28017" },{ _id : 1, host:"localhost:28117" },{ _id : 2, host:"localhost:28217" }] } )
mongo.exe localhost:29017
rs.initiate({_id: "shard3_replset",members: [{ _id : 0, host:"localhost:29017" },{ _id : 1, host:"localhost:29117" },{ _id : 2, host:"localhost:29217" }] } )
4.创建配置服务的文件夹用于存放服务
md E:\study\ducument\technology\2020\mongodb\shard_data\config_server1\data1
md E:\study\ducument\technology\2020\mongodb\shard_data\config_server1\data2
md E:\study\ducument\technology\2020\mongodb\shard_data\config_server1\data3
5.创建服务下的数据库
start mongod.exe --configsvr --port 47017 --dbpath "E:\study\ducument\technology\2020\mongodb\shard_data\config_server1\data1" --replSet configserver1_replset
start mongod.exe --configsvr --port 47117 --dbpath "E:\study\ducument\technology\2020\mongodb\shard_data\config_server1\data2" --replSet configserver1_replset
start mongod.exe --configsvr --port 47217 --dbpath "E:\study\ducument\technology\2020\mongodb\shard_data\config_server1\data3" --replSet configserver1_replset
6.配置服务下数据库的主从关系
mongo.exe localhost:47017
rs.initiate({_id: "configserver1_replset",configsvr: true,members: [{ _id : 0, host : "localhost:47017" },{ _id : 1, host : "localhost:47117" },{ _id : 2, host : "localhost:47217" }] } )
7.将配置服务下有数据库集群到一个端口,即开启服务
start mongos.exe --configdb configserver1_replset/localhost:47017,localhost:47117,localhost:47217 --port 1000
8.将分区下的数据库加入服务中
mongo.exe localhost:1000
sh.addShard("shard1_replset/localhost:26017,localhost:26117,localhost:26217")
sh.addShard("shard2_replset/localhost:28017,localhost:28117,localhost:28217")
sh.addShard("shard3_replset/localhost:29017,localhost:29117,localhost:29217")
9.在服务下创建集合
使用demos分区:sh.enableSharding("demos")
创建集合:sh.shardCollection("demos.users",{"id":1})
初始数据:use demos; for(var i=0;i<10000;i++){db.users.insert({id: Math.random(),count:i, date: new Date()})}
查看数量:db.users.count() --10000
查看状态:sh.status()
结论:user的数据分部到shard1和shard2
10.使用hashed来分区
10-1:sh.enableSharding("sample")
10-2:sh.shardCollection("sample.users",{"id":"hashed"})