下载
MongoDB 官方下载地址:https://www.mongodb.com/download-center#community
下载后解压到app目录下
tar -zxvf mongodb-linux-x86_64-rhel70-4.4.3.tgz -C /app
修改文件夹名称
mv mongodb-linux-x86_64-rhel70-4.4.3 mongodb-4.4.3
进入根目录
mkdir log
mkdir data
vim conf/mongo.conf
配置
logpath=/app/mongodb-4.4.3/log/mongo.log # 日志文件
dbpath =/app/mongodb-4.4.3/data # 数据文件
#错误日志采用追加模式
logappend=true
#启用日志文件,默认启用
journal=true
#这个选项可以过滤掉一些无用的日志信息,若需要调试使用请设置为false
quiet=true
#端口号 默认为27017
port=27017
#允许远程访问
bind_ip=0.0.0.0
#开启子进程
fork=true
#开启认证,必选先添加用户, 如果不需要验证这里改成false
auth=true
~
启动mongo 并设置密码
mongo
MongoDB shell version v4.4.3
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("2eeb129e-26f3-418c-afd9-72867bf20118") }
MongoDB server version: 4.4.3
> use admin
switched to db admin
> db.createUser({user:"admin",pwd:"password",roles:["root"]})
Successfully added user: { "user" : "admin", "roles" : [ "root" ] }
下次登录后 如果不验证 则数据是查不到的
#验证语句
db.auth('root','123456')
至此 mongo安装完成 下面搞mongo分片
mongo分片介绍
① 配置服务器。是一个独立的mongod进程,保存集群和分片的元数据,即各分片包含了哪些数据的信息。最先开始建立,启用日志功能。像启动普通的mongod一样启动配置服务器,指定configsvr选项。不需要太多的空间和资源,配置服务器的1KB空间相当于真是数据的200MB。保存的只是数据的分布表。当服务不可用,则变成只读,无法分块、迁移数据。
② 路由服务器。即mongos,起到一个路由的功能,供程序连接。本身不保存数据,在启动时从配置服务器加载集群信息,开启mongos进程需要知道配置服务器的地址,指定configdb选项。
③ 分片服务器。是一个独立普通的mongod进程,保存数据信息。可以是一个副本集也可以是单独的一台服务器。
在部署之前先明白片键的意义,一个好的片键对分片至关重要。片键必须是一个索引,数据根据这个片键进行拆分分散。通过sh.shardCollection加会自动创建索引。一个自增的片键对写入和数据均匀分布就不是很好,因为自增的片键总会在一个分片上写入,后续达到某个阀值可能会写到别的分片。但是按照片键查询会非常高效。随机片键对数据的均匀分布效果很好。注意尽量避免在多个分片上进行查询。在所有分片上查询,mongos会对结果进行归并排序。
mongo分片实施 资源有限在一台机器上搞一下
1)配置服务器。配置服务器必须开启1个或则3个,开启2个则会报错: (开启3个,Port:20000、20001、20002 一个配置服务器=一个副本集=三个节点)
在其根目录下新建文件夹
[[email protected] mongodb-4.4.3]# mkdir configServer
[[email protected] mongodb-4.4.3]# cd ./configServer
[[email protected] configServer]# mkdir configserver20000
[[email protected] configServer]# mkdir configserver20001
[[email protected] configServer]# mkdir configserver20002
[[email protected] configServer]# cd configServer20000
[[email protected] configServer20000]# mkdir data
[[email protected] configServer20000]# mkdir log
[[email protected] configServer20000]# cp /app/mongodb-4.4.3/conf/mongo.conf ./mongo.conf
[[email protected] configServer20000]# 其他节点做一样的操作
配置节点的配置文件 其他节点修改端口 和文件路径
logpath=/app/mongodb-4.4.3/configServer/configServer20000/log/mongo.log # 日志文件
dbpath =/app/mongodb-4.4.3/configServer/configServer20000/data # 数据文件
#错误日志采用追加模式
logappend=true
#启用日志文件,默认启用
journal=true
#这个选项可以过滤掉一些无用的日志信息,若需要调试使用请设置为false
quiet=true
#端口号 默认为27017
port=20000
#允许远程访问
bind_ip=0.0.0.0
#开启子进程
fork=true
#开启认证,必选先添加用户,先不开启(不用验证账号密码)
auth=false
#最大连接数
maxConns=20000
#复制集名称
replSet=myShardConfigs
configsvr=true
启动配置服务器
./mongod -f ../configServer/configServer20000/mongo.conf
./mongod -f ../configServer/configServer20001/mongo.conf
./mongod -f ../configServer/configServer20002/mongo.conf
连接到某个节点 配置副本集(连接三个节点任意一个)
mongo --port 20000 //任意选择一台进入
config={_id:"myShardConfigs",configsvr:true,members:[{_id:0,host:"127.0.0.1:20000"},{_id:1,host:"127.0.0.1:20001"},{_id:2,host:"127.0.0.1:20002"}]} //创建复制集
rs.initiate(config) //初始化复制集
配置服务器 操作完毕
2)分片服务器 两个分片服务器(两个副本集) 每个分片3个节点
本文没有使用仲裁节点 为什么请看https://www.zhihu.com/question/27648448/answer/99091787
[[email protected] shareServer]# mkdir shareServer1-21000
[[email protected] shareServer]# mkdir shareServer1-21001-replSet1
[[email protected] shareServer]# mkdir shareServer1-21002-replSet2
[[email protected]1cf6pm8o3dv75jmev9Z shareServer]# mkdir shareServer2-21005
[[email protected] shareServer]# mkdir shareServer2-21006-replSet1
[[email protected] shareServer]# mkdir shareServer2-21007-replSet2
[[email protected] shareServer]#
分片节点的配置 其他节点修改路径和端口和集群名称
logpath=/app/mongodb-4.4.3/shareServer/shareServer1-21000/log/mongo.log # 日志文件
dbpath =/app/mongodb-4.4.3/shareServer/shareServer1-21000/data # 数据文件
#错误日志采用追加模式
logappend=true
#启用日志文件,默认启用
journal=true
#这个选项可以过滤掉一些无用的日志信息,若需要调试使用请设置为false
quiet=true
#端口号 默认为27017
port=21000
#允许远程访问
bind_ip=0.0.0.0
#开启子进程
fork=true
#开启认证,必选先添加用户,先不开启(不用验证账号密码)
auth=false
#最大连接数
maxConns=20000
#复制集名称
replSet=share1
# 声明这是一个集群的分片
shardsvr=true
启动 六个节点
./mongod -f ../shareServer/shareServer1-21000/mongo.conf
./mongod -f ../shareServer/shareServer1-21001-replSet1/mongo.conf
./mongod -f ../shareServer/shareServer1-21002-replSet2/mongo.conf
./mongod -f ../shareServer/shareServer2-21005/mongo.conf
./mongod -f ../shareServer/shareServer2-21006-replSet1/mongo.conf
./mongod -f ../shareServer/shareServer2-21007-replSet2/mongo.conf
连接到某个节点 初始化复制集(21000、21001、21002为share1 ,21005、21006、21007为share2 配置的时候前三个连接任意一个配置 后三个任意一个配置 )
注意:如果没有仲裁节点可以三个节点任意一台去初始化, 如果有仲裁节点只能去不是仲裁节点的另外两台去初始化。
share1
./mongo --port 21000
rsconf={ _id:"share1",members:[ {_id:0,host:"127.0.0.1:21000"}, {_id:1,host:"127.0.0.1:21001"}, {_id:2,host:"127.0.0.1:21002"}]}
rs.initiate(rsconf);
share2
./mongo --port 21005
rsconf={ _id:"share2",members:[ {_id:0,host:"127.0.0.1:21005"}, {_id:1,host:"127.0.0.1:21006"}, {_id:2,host:"127.0.0.1:21007"}]}
rs.initiate(rsconf);
3)路由服务器 (这里搞两个路由服务器 27017、27018)
[[email protected] mongodb-4.4.3]# cd routerServer/
[[email protected] routerServer]# mkdir router-27017
[[email protected] routerServer]# mkdir router-27018
config
logpath=/app/mongodb-4.4.3/routerServer/router-27017/log/mongo.log # 日志文件
pidfilepath =/app/mongodb-4.4.3/routerServer/router-27017/log/mongos.pid
#错误日志采用追加模式
logappend=true
#这个选项可以过滤掉一些无用的日志信息,若需要调试使用请设置为false
quiet=true
#端口号 默认为27017
port=27017
#允许远程访问
bind_ip=0.0.0.0
#开启子进程
fork=true
#最大连接数
maxConns=20000
#配置服务器
configdb =myShardConfigs/127.0.0.1:20000,127.0.0.1:20001,127.0.0.1:20002
启动路由(这里启动27017 没有启动27018 如果使用两个路由代码中要配置两个路由的路径)
./mongos -f ../routerServer/router-27017/mongo.conf
[[email protected] bin]# ps -ef |grep mong
root 4697 1 0 11:28 ? 00:03:16 ./mongod -f ../shareServer/shareServer1-21000/mongo.conf
root 4740 1 0 11:28 ? 00:02:46 ./mongod -f ../shareServer/shareServer1-21001-replSet1/mongo.conf
root 4783 1 0 11:28 ? 00:02:47 ./mongod -f ../shareServer/shareServer1-21002-replSet2/mongo.conf
root 4831 1 0 11:29 ? 00:03:16 ./mongod -f ../shareServer/shareServer2-21005/mongo.conf
root 4874 1 0 11:29 ? 00:02:49 ./mongod -f ../shareServer/shareServer2-21006-replSet1/mongo.conf
root 6136 1 0 14:07 ? 00:01:23 ./mongod -f ../shareServer/shareServer2-21007-replSet2/mongo.conf
root 11428 1 1 17:47 ? 00:00:03 ./mongod -f ../configServer/configServer20000/mongo.conf
root 11480 1 1 17:47 ? 00:00:02 ./mongod -f ../configServer/configServer20001/mongo.conf
root 11531 1 1 17:47 ? 00:00:02 ./mongod -f ../configServer/configServer20002/mongo.conf
root 11740 1 0 17:50 ? 00:00:00 ./mongos -f ../routerServer/router-27017/mongo.conf
root 11779 7334 0 17:51 pts/2 00:00:00 grep --color=auto mong
[[email protected] bin]#
4)此时登录路由服务器查看状态 这是还没有添加分片服务器的状态
mongo --port 27017
mongos> sh.status();
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("600559990c4072b92c72da2f")
}
shards:
active mongoses:
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" : "config", "primary" : "config", "partitioned" : true }
mongos>
手动加入分片1和分片2
admin
mongos> sh.addShard("share1/127.0.0.1:21000,127.0.0.1:21001,127.0.0.1:21002");
{
"shardAdded" : "share1",
"ok" : 1,
"operationTime" : Timestamp(1610964029, 4),
"$clusterTime" : {
"clusterTime" : Timestamp(1610964029, 4),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
mongos> sh.addShard("share2/127.0.0.1:21005,127.0.0.1:21006,127.0.0.1:21007");
{
"shardAdded" : "share2",
"ok" : 1,
"operationTime" : Timestamp(1610964053, 2),
"$clusterTime" : {
"clusterTime" : Timestamp(1610964054, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
mongos> sh.status();
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("600559990c4072b92c72da2f")
}
shards:
{ "_id" : "share1", "host" : "share1/127.0.0.1:21000,127.0.0.1:21001,127.0.0.1:21002", "state" : 1 }
{ "_id" : "share2", "host" : "share2/127.0.0.1:21005,127.0.0.1:21006,127.0.0.1:21007", "state" : 1 }
active mongoses:
"4.4.3" : 1
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" : "config", "primary" : "config", "partitioned" : true }
mongos>
rs.isMaster(); # 查看当前是否是主节点
5)实验
修改块的大小方便测试 默认是64MB,取值范围是1 MB 到 1024 MB.
mongos> use config
switched to db config
mongos> db.settings.save({"_id":"chunksize","value":1}) //设置块大小为1M是方便实验,不然就需要插入海量数据
WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : "chunksize" })
新建一个mydb数据库 并新建一个集合为user 并插入数据启用分片
use mydb;
mongos> for(i=1;i<=50000;i++){db.user.insert({"id":i,"name":"jack"+i})} //插入数据
mongos> sh.enableSharding("mydb");//给mydb数据库开启分片
{
"ok" : 1,
"operationTime" : Timestamp(1610966409, 3),
"$clusterTime" : {
"clusterTime" : Timestamp(1610966409, 4),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
mongos>db.user.createIndex({"id":1}) //给id添加索引
mongos> sh.shardCollection("mydb.user",{"id":1})
mongos> sh.shardCollection("mydb.user",{"id":1});//以id为片键将user集合分片
{
"collectionsharded" : "mydb.user",
"collectionUUID" : UUID("66006f80-67a4-4dc4-8913-38d5d2274256"),
"ok" : 1,
"operationTime" : Timestamp(1610967475, 5),
"$clusterTime" : {
"clusterTime" : Timestamp(1610967475, 5),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
mongos>
查看分片状态
mongos> sh.status();
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("600559990c4072b92c72da2f")
}
shards:
{ "_id" : "share1", "host" : "share1/127.0.0.1:21000,127.0.0.1:21001,127.0.0.1:21002", "state" : 1 }
{ "_id" : "share2", "host" : "share2/127.0.0.1:21005,127.0.0.1:21006,127.0.0.1:21007", "state" : 1 }
active mongoses:
"4.4.3" : 1
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:
514 : Success
databases:
{ "_id" : "config", "primary" : "config", "partitioned" : true }
config.system.sessions
shard key: { "_id" : 1 }
unique: false
balancing: true
chunks:
share1 512
share2 512
too many chunks to print, use verbose if you want to force print
{ "_id" : "mydb", "primary" : "share2", "partitioned" : true, "version" : { "uuid" : UUID("99f1c4c0-5440-4971-9767-12df46b379bc"), "lastMod" : 1 } }
mydb.user
shard key: { "id" : 1 }
unique: false
balancing: true
chunks:
share1 2
share2 2
{ "id" : { "$minKey" : 1 } } -->> { "id" : 2 } on : share1 Timestamp(2, 0)
{ "id" : 2 } -->> { "id" : 20604 } on : share2 Timestamp(3, 1)
{ "id" : 20604 } -->> { "id" : 31010 } on : share2 Timestamp(2, 2)
{ "id" : 31010 } -->> { "id" : { "$maxKey" : 1 } } on : share1 Timestamp(3, 0)
mongos>
此时如果你有share3 并将其sh.shard(xxx)服务器又对数据进行重新分片,当你再次移除一个分片服务器,此时又会对数据再次进行分片处理,MongoDB对数据的处理非常灵活
最终验证:
结果 确实数据分片了 而且每个分片的副本集数据一致
即 21000、21002、21003 数据量一致
21005、21006、21007数据量一致
share1+share2=总数
参考:https://www.imooc.com/article/277085?block_id=tuijian_wz
https://blog.51cto.com/13643643/2148825#h10
https://www.jianshu.com/p/918afbb6a8c5
https://www.jianshu.com/p/b5a7d13e1391
https://blog.51cto.com/13728740/2175905 //mongo的一些复制集实操