目录
说明
配置说明
gitee
3台机器
端口
安装
下载
解压文件
创建文件夹
目录结构
#按顺序启动
shard集群配置
#配置复制集 shard1,注意ip和端口
#初始化副本集配置
#配置复制集 shard2
#配置复制集 shard3
config集群配置
#启动各个节点:
#创建config集群
mongos节点配置
#启动mongos
#登录到mongos
#添加 分片集群
#查看分片状态
#查看集群状态
使用
#创建分片,启动
开机启动脚本
说明
本文参考了
/clsn/p/8214345.html#auto_id_42
/tech/arch/mongodb_shard1.html
/aafeiyang/article/details/82109662
/vansky/p/9640268.html
配置说明
gitee
配置文件在gitee上
/DDDD/mongodb_settings
3台机器
jiba01 10.10.10.100
jiba0210.10.10.101
jiba0310.10.10.102
端口
mongos端口 21000config集群端口 22000 集群名字(replSetName)configReplSet3个分片集群端口shard1:23001 集群名字(replSetName)shard1shard2:23002集群名字(replSetName)shard2shard3:23003集群名字(replSetName)shard3安装
下载
#下载[root@jiba01 files]# wget /linux/mongodb-linux-x86_64-4.0.10.tgz---07-09 04:38:48-- /linux/mongodb-linux-x86_64-4.0.10.tgzResolving ()... 13.249.236.15, 13.249.236.35, 13.249.236.77, ...Connecting to ()|13.249.236.15|:443... connected.HTTP request sent, awaiting response... 200 OKLength: 84996443 (81M) [application/x-gzip]Saving to: ‘mongodb-linux-x86_64-4.0.10.tgz’100%[===========================================================================>] 84,996,443 2.95MB/s in 22s-07-09 04:39:13 (3.75 MB/s) - ‘mongodb-linux-x86_64-4.0.10.tgz’ saved [84996443/84996443]
解压文件
[root@jiba01 usr]# tar -zxvf mongodb-linux-x86_64-4.0.10.tgzmongodb-linux-x86_64-4.0.10/THIRD-PARTY-NOTICES.gotoolsmongodb-linux-x86_64-4.0.10/READMEmongodb-linux-x86_64-4.0.10/THIRD-PARTY-NOTICESmongodb-linux-x86_64-4.0.10/MPL-2mongodb-linux-x86_64-4.0.10/LICENSE-Community.txtmongodb-linux-x86_64-4.0.10/bin/mongodumpmongodb-linux-x86_64-4.0.10/bin/mongorestoremongodb-linux-x86_64-4.0.10/bin/mongoexportmongodb-linux-x86_64-4.0.10/bin/mongoimportmongodb-linux-x86_64-4.0.10/bin/mongostatmongodb-linux-x86_64-4.0.10/bin/mongotopmongodb-linux-x86_64-4.0.10/bin/bsondumpmongodb-linux-x86_64-4.0.10/bin/mongofilesmongodb-linux-x86_64-4.0.10/bin/mongoreplaymongodb-linux-x86_64-4.0.10/bin/mongodmongodb-linux-x86_64-4.0.10/bin/mongosmongodb-linux-x86_64-4.0.10/bin/mongomongodb-linux-x86_64-4.0.10/bin/install_compass
创建文件夹
#创建文件夹[root@jiba01 mongodb]# mkdir -p /usr/mongodb/conf[root@jiba01 mongodb]# mkdir -p /usr/mongodb/mongos/log[root@jiba01 mongodb]# mkdir -p /usr/mongodb/config/data[root@jiba01 mongodb]# mkdir -p /usr/mongodb/config/log[root@jiba01 mongodb]# mkdir -p /usr/mongodb/shard1/data[root@jiba01 mongodb]# mkdir -p /usr/mongodb/shard1/log[root@jiba01 mongodb]# mkdir -p /usr/mongodb/shard2/data[root@jiba01 mongodb]# mkdir -p /usr/mongodb/shard2/log[root@jiba01 mongodb]# mkdir -p /usr/mongodb/shard3/data[root@jiba01 mongodb]# mkdir -p /usr/mongodb/shard3/log
目录结构
#按顺序启动
#按顺序启动mongod -f /usr/mongodb/conf/shard1.conf &mongod -f /usr/mongodb/conf/shard2.conf &mongod -f /usr/mongodb/conf/shard3.conf &
#启动的结果
[root@jiba01 conf]# about to fork child process, waiting until server is ready for connections.forked process: 1466child process started successfully, parent exiting
shard集群配置
#连接集群 创建shard1,此处可以随意登录3个机器中的一个,但是需要保证port是shard1的port,此处用的是23001(shard1-23001 shard2-23002 shard3-23003)
mongo --host 10.10.10.100 --port 23001 admin
#配置复制集 shard1,注意ip和端口
config = {_id: 'shard1', members: [{_id: 0, host: '10.10.10.100:23001'},{_id: 1, host: '10.10.10.101:23001'},{_id: 2, host: '10.10.10.102:23001',"arbiterOnly":true}]}
#返回信息
{"_id" : "shard1","members" : [{"_id" : 0,"host" : "10.10.10.100:23001"},{"_id" : 1,"host" : "10.10.10.101:23001"},{"_id" : 2,"host" : "10.10.10.102:23001","arbiterOnly" : true}]}
#初始化副本集配置
#返回信息 "ok" : 1 是成功,其他的需要看报错进行处理
#返回信息 "ok" : 1 是成功,其他的需要看报错进行处理{"ok" : 1,"operationTime" : Timestamp(1562623465, 1),"$clusterTime" : {"clusterTime" : Timestamp(1562623465, 1),"signature" : {"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),"keyId" : NumberLong(0)}}}shard1:SECONDARY>
#配置复制集 shard2
#退出重新登录
mongo --host 10.10.10.100 --port 23002 admin
config = {_id: 'shard2', members: [{_id: 0, host: '10.10.10.100:23002'},{_id: 1, host: '10.10.10.101:23002'},{_id: 2, host: '10.10.10.102:23002',"arbiterOnly":true}]} rs.initiate(config);#返回结果一样,不再展示
#配置复制集 shard3
#退出重新登录
mongo --host 10.10.10.100 --port 23003 admin
config = {_id: 'shard3', members: [{_id: 0, host: '10.10.10.100:23003'},{_id: 1, host: '10.10.10.101:23003'},{_id: 2, host: '10.10.10.102:23003',"arbiterOnly":true}]} rs.initiate(config);
#shard集群全部配置完毕
config集群配置
#启动各个节点:
3个机器 ###每个机器启动一个###
mongod -f /usr/mongodb/conf/config.conf
#然后登录其中一个节点 创建config集群,注意端口号
mongo --host 10.10.10.100 --port 22000 admin
#创建config集群
注意 集群的名字 _id需要和配置文件中相同 configReplSet
config = {_id: 'configReplSet', members: [{_id: 0, host: '10.10.10.100:22000'},{_id: 1, host: '10.10.10.101:22000'},{_id: 2, host: '10.10.10.102:22000'}]} rs.initiate(config);
#config创建完毕
mongos节点配置
#启动mongos
在每个机器上 都启动mongos 此处是mongos命令
mongos -f /usr/mongodb/conf/mongos.conf
#登录到mongos
mongo --host 10.10.10.100 --port 21000 admin
#添加 分片集群
db.runCommand( { addshard : "shard1/10.10.10.100:23001,10.10.10.101:23001,10.10.10.102:23001",name:"shard1"} );db.runCommand( { addshard : "shard2/10.10.10.100:23002,10.10.10.101:23002,10.10.10.102:23002",name:"shard2"} );db.runCommand( { addshard : "shard3/10.10.10.100:23003,10.10.10.101:23003,10.10.10.102:23003",name:"shard3"} );
#查看分片状态
#10.10.10.102上的全是仲裁节点,所以不会显示
db.runCommand( { listshards : 1 } )#返回结果mongos> db.runCommand( { listshards : 1 } ){"shards" : [{"_id" : "shard1","host" : "shard1/10.10.10.100:23001,10.10.10.101:23001","state" : 1},{"_id" : "shard2","host" : "shard2/10.10.10.100:23002,10.10.10.101:23002","state" : 1},{"_id" : "shard3","host" : "shard3/10.10.10.100:23003,10.10.10.101:23003","state" : 1}],"ok" : 1,"operationTime" : Timestamp(1562628088, 1),"$clusterTime" : {"clusterTime" : Timestamp(1562628088, 1),"signature" : {"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),"keyId" : NumberLong(0)}}}
#查看集群状态
#返回结果--- Sharding Status ---sharding version: {"_id" : 1,"minCompatibleVersion" : 5,"currentVersion" : 6,"clusterId" : ObjectId("5d23ca90f263273c0ac89699")}shards:{ "_id" : "shard1", "host" : "shard1/10.10.10.100:23001,10.10.10.101:23001", "state" : 1 }{ "_id" : "shard2", "host" : "shard2/10.10.10.100:23002,10.10.10.101:23002", "state" : 1 }{ "_id" : "shard3", "host" : "shard3/10.10.10.100:23003,10.10.10.101:23003", "state" : 1 }active mongoses:"4.0.10" : 3autosplit:Currently enabled: yesbalancer:Currently enabled: yesCurrently running: noFailed balancer rounds in last 5 attempts: 0Migration Results for the last 24 hours:No recent migrationsdatabases:{ "_id" : "config", "primary" : "config", "partitioned" : true }config.system.sessionsshard key: { "_id" : 1 }unique: falsebalancing: truechunks:shard1 1{ "_id" : { "$minKey" : 1 } } -->> { "_id" : { "$maxKey" : 1 } } on : shard1 Timestamp(1, 0)
######################搭建完毕############################
使用
#创建分片,启动
#登录任意一个mongos节点
mongo --host 10.10.10.100 --port 21000
#newTable是新的数据库名字
#newTable是新的数据库名字mongos> db.runCommand({enablesharding:'newTable'});
#返回信息{"ok" : 1,"operationTime" : Timestamp(1562628535, 5),"$clusterTime" : {"clusterTime" : Timestamp(1562628535, 5),"signature" : {"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),"keyId" : NumberLong(0)}}}
#指定数据库里需要分片的集合和片键,用admin登录
mongo --host 10.10.10.100 --port 21000 admin
#newTable的table1表需要分片 根据 id 自动分片到 shard1 shard2 shard3 上面去
#要这样设置是因为不是所有mongodb 的数据库和表 都需要分片
db.runCommand( { shardcollection : "newTable.table1",key : {id: 1} } )
#以后登录可以直接登录 mongos 而不用登录各个分片的节点
开机启动脚本
mongod -f /usr/mongodb/conf/shard1.conf &mongod -f /usr/mongodb/conf/shard2.conf &mongod -f /usr/mongodb/conf/shard3.conf &mongod -f /usr/mongodb/conf/config.conf &mongos -f /usr/mongodb/conf/mongos.conf &
#登录mongos命令 21000是mongo的端口
mongo --host 10.10.10.100 --port 21000
#然后选择不同的数据库
use newTable
完毕
文件路径如图