centos搭建mongodb叢集
開始前進入su模式,關閉防火牆systemctl stop firewalld.service 重啟生效:systemctl disable firewalld.service
以下配置檔案中id除非是0.0.0.0的,其他都需要根據自身情況來修改
下載:mongodb: curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.4.6.tgz
解壓:tar -xzvf mongodb-linux-x86_64-3.4.6.tgz -C /data/local/
改名:mv mongodb-linux-x86_64-3.4.6 mongodb
分別在三個機器建立以下檔案
mkdir -p /data/local/mongodb/conf
mkdir -p /data/local/mongodb/mongos/log
mkdir -p /data/local/mongodb/config/data
mkdir -p /data/local/mongodb/config/log
mkdir -p /data/local/mongodb/shard1/data
mkdir -p /data/local/mongodb/shard1/log
mkdir -p /data/local/mongodb/shard2/data
mkdir -p /data/local/mongodb/shard2/log
mkdir -p /data/local/mongodb/shard3/data
mkdir -p /data/local/mongodb/shard3/log
配置環境變數
# 內容
export MONGODB_HOME=/dev/local/mongodb
export PATH=$MONGODB_HOME/bin:$PATH
# 使立即生效
source /etc/profile
2、config server配置伺服器
新增配置檔案
命令:vi /data/local/mongodb/conf/config.conf
配置檔案內容如下
pidfilepath = /data/local/mongodb/config/log/configsrv.pid
dbpath = /data/local/mongodb/config/data
logpath = /data/local/mongodb/config/log/congigsrv.log
logappend = true
bind_ip = 0.0.0.0
port = 21000
fork = true
#declare this is a config db of a cluster;
configsvr = true
#副本集名稱
replSet=configs
#設定最大連線數
maxConns=20000
注:必須關閉防火牆
啟動三臺伺服器的config server
命令:mongod -f /data/local/mongodb/conf/config.conf
注:如果全域性變數沒執行可以到bin目錄下 ./mongod -f /data/local/mongodb/conf/config.conf
登入任意一臺配置伺服器,初始化配置副本集
命令:mongo --port 21000
設定config變數命令如下
config = {
... _id : "configs",
... members : [
... {_id : 0, host : "10.2.5.99:21000" },
... {_id : 1, host : "10.2.5.98:21000" },
... {_id : 2, host : "10.2.5.97:21000" }
... ]
... }
初始化副本集命令:rs.initiate(config)
3、配置分片副本集(三臺機)
vi /data/local/mongodb/conf/shard1.conf
第一個刀片副本集檔案內容如下:
pidfilepath = /data/local/mongodb/shard1/log/shard1.pid
dbpath = /data/local/mongodb/shard1/data
logpath = /data/local/mongodb/shard1/log/shard1.log
logappend = true
bind_ip = 0.0.0.0
port = 27001
fork = true
#開啟web監控
httpinterface=true
rest=true
#副本集名稱
replSet=shard1
#declare this is a shard db of a cluster;
shardsvr = true
#設定最大連線數
maxConns=20000
啟動三臺機器伺服器命令:./mongod -f /data/local/mongodb/conf/shard1.conf
登陸非arbiterOnly: true ID的任意機器啟動以下命令 (如以下id中24.175不能啟動)
命令:mongo --port 27001
命令:use admin
命令:
config = {
... _id : "shard1",
... members : [
... {_id : 0, host : "10.2.5.99:27001" },
... {_id : 1, host : "10.2.5.98:27001" },
... {_id : 2, host : "10.2.5.97:27001" , arbiterOnly: true }
... ]
... }
初始化副本集命令:rs.initiate(config);
開啟第二個刀片副本集conf:vi /data/local/mongodb/conf/shard2.conf
第二個刀片副本集配置檔案如下:
pidfilepath = /data/local/mongodb/shard2/log/shard2.pid
dbpath = /data/local/mongodb/shard2/data
logpath = /data/local/mongodb/shard2/log/shard2.log
logappend = true
bind_ip = 0.0.0.0
port = 27002
fork = true
#開啟web監控
httpinterface=true
rest=true
#副本集名稱
replSet=shard2
#declare this is a shard db of a cluster;
shardsvr = true
#設定最大連線數
maxConns=20000
啟動三個伺服器:./mongod -f /data/local/mongodb/conf/shard2.conf
登陸非arbiterOnly: true的任意機子初始化初始化副本
命令:mongo --port 27002
命令:use admin
命令:
config = {
... _id : "shard2",
... members : [
... {_id : 0, host : "10.2.5.99:27002" , arbiterOnly: true },
... {_id : 1, host : "10.2.5.98:27002" },
... {_id : 2, host : "10.2.5.97:27002" }
... ]
... }
命令:rs.initiate(config);
設定第三個刀片副本集
命令:vi /data/local/mongodb/conf/shard3.conf
第三個副本集配置檔案內容
pidfilepath = /data/local/mongodb/shard3/log/shard3.pid
dbpath = /data/local/mongodb/shard3/data
logpath = /data/local/mongodb/shard3/log/shard3.log
logappend = true
bind_ip = 0.0.0.0
port = 27003
fork = true
#開啟web監控
httpinterface=true
rest=true
#副本集名稱
replSet=shard3
#declare this is a shard db of a cluster;
shardsvr = true
#設定最大連線數
maxConns=20000
啟動三臺伺服器:./mongod -f /data/local/mongodb/conf/shard3.conf
登陸非arbiterOnly: true ID的機子初始化副本集
命令:mongo --port 27003
命令:use admin
命令:
config = {
... _id : "shard3",
... members : [
... {_id : 0, host : "10.2.5.99:27003" },
... {_id : 1, host : "10.2.5.98:27003" , arbiterOnly: true},
... {_id : 2, host : "10.2.5.97:27003" }
... ]
... }
初始化命令:rs.initiate(config);
4、配置路由伺服器 mongos
三臺機開啟:vi /data/local/mongodb/conf/mongos.conf
配置檔案內容:
pidfilepath = /data/local/mongodb/mongos/log/mongos.pid
logpath = /data/local/mongodb/mongos/log/mongos.log
logappend = true
bind_ip = 0.0.0.0
port = 20000
fork = true
#監聽的配置伺服器,只能有1個或者3個 configs為配置伺服器的副本集名字
configdb = configs/10.2.5.99:21000,10.2.5.98:21000,10.2.5.97:21000
#設定最大連線數
maxConns=20000
啟動三臺伺服器的mongos server命令: ./mongos -f /data/local/mongodb/conf/mongos.conf
5、啟動分片
登陸任意一臺mongos
命令:mongo --port 20000
命令:use admin
配置檔案內容:
sh.addShard("shard1/10.2.5.99:27001,10.2.5.99:27001,10.2.5.99:27001")
sh.addShard("shard2/10.2.5.98:27002,10.2.5.98:27002,10.2.5.98:27002")
sh.addShard("shard3/10.2.5.97:27003,10.2.5.97:27003,10.2.5.97:27003")
檢視狀態命令:sh.status()
分片的的的資料平衡時間設定:
db.settings.update({ _id : "balancer" }, { $set : { activeWindow : { start : "23:00", stop : "6:00" } } }, true )
啟動分片方法
use admin
db.runCommand( { enablesharding :"MongoFile"}); 注:其中 MongoFile為庫名,table為表名
建立片鍵
命令:db.table1.ensureIndex({"id":1})
db.runCommand( { shardcollection : "MongoFile.table",key : {id: 1} } )
---搭建成功---
測試方法
1.進入mongoDB
命令為:mongo --port 20000
2.新建資料庫且分片
命令:use 502
switched to db 502
命令:sh.enableSharding("502")
3.建立索引
命令:db.table1.ensureIndex({"id":1})
4.表分片
命令:sh.shardCollection("502.table1",{"id":1})
查看錶狀態:
<pre name="code" class="plain">mongos> sh.status()
5.儲存資料
命令:for(var i=1;i<=1000000;i++){
... db.table1.save({"id":i,"x":Math.random(),"name":"xubo","time":"20150819","ops":"testinserttimes"});
... }
WriteResult({ "nInserted" : 1 })
6.檢視每個分片資料總數
命令: db.table1.stats()
往庫插資料
for (var i = 1; i <= 10000; i++) db.table1.save({id:i,"test1":"testval1"});
啟動mongodb服務
mongod -f /data/local/mongodb/conf/config.conf
mongod -f /data/local/mongodb/conf/shard1.conf
mongod -f /data/local/mongodb/conf/shard2.conf
mongod -f /data/local/mongodb/conf/shard3.conf
mongos -f /data/local/mongodb/conf/mongos.conf
關閉mongodb服務
./mongo --port 20000
use admin
db.shutdownServer()
=====================================
重啟庫
cd /home/local/mongodb/bin/
./mongod -f /home/local/mongodb/conf/config.conf
./mongod -f /home/local/mongodb/conf/shard1.conf
./mongod -f /home/local/mongodb/conf/shard2.conf
./mongod -f /home/local/mongodb/conf/shard3.conf
./mongos -f /home/local/mongodb/conf/mongos.conf
db.getProfilingLevel()
db.setProfilingLevel(1,200)
db.system.profile.find().limit(10).sort({ts:-1}).pretty()
db.system.profile.find({op: {$ne:'command'}}).pretty()
db.system.profile.find({millis:{$gt:5}}).pretty()
db.getCollection('result_1').find({}).explain("executionStats")
db.getCollection('result_1').find({'state':'0'}).explain("executionStats")
取資料測試語句
db.filter_task_1.find({state:'0', task_categories_id:'1'}).limit(50).explain("executionStats")
刪除複合索引
db.abc.dropIndex("idx_c") #刪除索引,指定"name"
建立複合索引
db.collections.ensureIndex({...})