MongoDB 分片集群搭建
目錄
說明
配置說明
gitee
3臺機器
端口
安裝
下載
解壓文件
創建文件夾
目錄結構
#按順序啟動
#配置復制集 shard1,注意ip和端口
#初始化副本集配置
#配置復制集 shard2
#配置復制集 shard3
config集群配置
#啟動各個節點:
#創建config集群?
mongos節點配置
#啟動mongos
#登錄到mongos
#添加 分片集群
#查看分片狀態?
#查看集群狀態
使用
#創建分片,啟動
開機啟動腳本
說明
本文參考了?
https://www.cnblogs.com/clsn/p/8214345.html#auto_id_42
http://www.lanceyan.com/tech/arch/mongodb_shard1.html
https://blog.csdn.net/aafeiyang/article/details/82109662
https://www.cnblogs.com/vansky/p/9640268.html
配置說明
gitee
配置文件在gitee上
https://gitee.com/DDDD/mongodb_settings
3臺機器
jiba01 10.10.10.100??
jiba02?10.10.10.101
jiba03?10.10.10.102
端口
- mongos端口? 21000
- config集群端口? 22000? ? 集群名字?(replSetName)configReplSet
- 3個分片集群端口?
- shard1:23001 集群名字(replSetName)shard1
- shard2:23002?集群名字(replSetName)shard2
- shard3:23003?集群名字(replSetName)shard3
安裝
下載
#下載 [root@jiba01 files]# wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-4.0.10.tgz --2019-07-09 04:38:48-- https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-4.0.10.tgz Resolving fastdl.mongodb.org (fastdl.mongodb.org)... 13.249.236.15, 13.249.236.35, 13.249.236.77, ... Connecting to fastdl.mongodb.org (fastdl.mongodb.org)|13.249.236.15|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 84996443 (81M) [application/x-gzip] Saving to: ‘mongodb-linux-x86_64-4.0.10.tgz’100%[===========================================================================>] 84,996,443 2.95MB/s in 22s2019-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.tgz mongodb-linux-x86_64-4.0.10/THIRD-PARTY-NOTICES.gotools mongodb-linux-x86_64-4.0.10/README mongodb-linux-x86_64-4.0.10/THIRD-PARTY-NOTICES mongodb-linux-x86_64-4.0.10/MPL-2 mongodb-linux-x86_64-4.0.10/LICENSE-Community.txt mongodb-linux-x86_64-4.0.10/bin/mongodump mongodb-linux-x86_64-4.0.10/bin/mongorestore mongodb-linux-x86_64-4.0.10/bin/mongoexport mongodb-linux-x86_64-4.0.10/bin/mongoimport mongodb-linux-x86_64-4.0.10/bin/mongostat mongodb-linux-x86_64-4.0.10/bin/mongotop mongodb-linux-x86_64-4.0.10/bin/bsondump mongodb-linux-x86_64-4.0.10/bin/mongofiles mongodb-linux-x86_64-4.0.10/bin/mongoreplay mongodb-linux-x86_64-4.0.10/bin/mongod mongodb-linux-x86_64-4.0.10/bin/mongos mongodb-linux-x86_64-4.0.10/bin/mongo mongodb-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: 1466 child 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
#退出重新登錄
#配置復制集 shard3
#退出重新登錄
#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上的全是仲裁節點,所以不會顯示
#查看集群狀態
#返回結果 --- 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節點
#newTable是新的數據庫名字
?
#指定數據庫里需要分片的集合和片鍵,用admin登錄
mongo --host 10.10.10.100 --port 21000 admin#newTable的table1表需要分片 根據 id 自動分片到 shard1 shard2 shard3 上面去
#要這樣設置是因為不是所有mongodb 的數據庫和表 都需要分片
#以后登錄可以直接登錄 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完畢
文件路徑如圖
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
總結
以上是生活随笔為你收集整理的MongoDB 分片集群搭建的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python运行系统找不到指定文件_系统
- 下一篇: JAVA 字符串转数组并且进行遍历