您好,登錄后才能下訂單哦!
# 啟動(dòng)時(shí)需要使用非root用戶,所有創(chuàng)建一個(gè)mongo用戶:
useradd mongo
# 為mongo用戶添加密碼:
echo 123456 | passwd --stdin mongo
# 將mongo添加到sudoers
echo "mongo ALL = (root) NOPASSWD:ALL" | tee /etc/sudoers.d/mongo
chmod 0440 /etc/sudoers.d/mongo
#解決sudo: sorry, you must have a tty to run sudo問題,在/etc/sudoer注釋掉 Default requiretty 一行
sudo sed -i 's/Defaults requiretty/Defaults:chiansun !requiretty/' /etc/sudoers
# 創(chuàng)建一個(gè)mongo目錄
mkdir /mongo
# 給相應(yīng)的目錄添加權(quán)限
chown -R mongo:mongo /mongo
# 配置mongo的yum源
cat >> /etc/yum.repos.d/mongodb-org-4.0.repo << EOF
[mongodb-org-4.0]
name=MongoDB Repository
baseurl=http://repo.mongodb.org/yum/redhat/\$releasever/mongodb-org/4.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc
EOF
# 關(guān)閉selinux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
setenforce 0
# 關(guān)閉防火墻
systemctl disable firewalld
systemctl stop firewalld
192.168.33.14 node-1
192.168.33.15 node-2
192.168.33.16 node-3
node-1 node-2 node-3
mongos mongos mongos 路由服務(wù)器,尋址
config config config 配置服務(wù)器,保存配置
shard1主 shard2主 shard3主 分片:保存數(shù)據(jù)
shard2從 shard3從 shard1從 副本集:備份數(shù)據(jù),可以配置讀寫分離(主負(fù)責(zé)寫,從負(fù)責(zé)同步數(shù)據(jù)和讀)
shard3從 shard1從 shard2從
# 分別在多臺(tái)機(jī)器上使用mongo用戶登錄
sudo yum install -y mongodb-org
# 分別在多臺(tái)機(jī)器上創(chuàng)建mongo config server對(duì)應(yīng)的目錄
mkdir -p /mongo/config/{log,data,run}
# 分別在多臺(tái)機(jī)器上修改config server的配置文件
cat > /mongo/config/mongod.conf << EOF
systemLog:
destination: file
logAppend: true
path: /mongo/config/log/mongod.log
storage:
dbPath: /mongo/config/data
journal:
enabled: true
processManagement:
fork: true
pidFilePath: /mongo/config/run/mongod.pid
net:
port: 27100
bindIp: 0.0.0.0
replication:
replSetName: config
sharding:
clusterRole: configsvr
EOF
# 啟動(dòng)所有的mongo config server服務(wù)
mongod --config /mongo/config/mongod.conf
# 登錄任意一臺(tái)配置服務(wù)器,初始化配置副本集
mongo --port 27100
# 創(chuàng)建配置
# id名要和replSetName名保持一致
config = {
_id : "config",
members : [
{_id : 0, host : "192.168.33.14:27100" },
{_id : 1, host : "192.168.33.15:27100" },
{_id : 2, host : "192.168.33.16:27100" }
]
}
# 初始化副本集配置
rs.initiate(config)
# 查看分區(qū)狀態(tài)
rs.status()
# 注意:其中,"_id" : "config"應(yīng)與配置文件中配置的 replicaction.replSetName 一致,"members" 中的 "host" 為三個(gè)節(jié)點(diǎn)的ip和port
# 修改mongo shard1 server的配置文件
mkdir -p /mongo/shard1/{log,data,run}
# 分別在多臺(tái)機(jī)器上修改shard1 server的配置文件
cat > /mongo/shard1/mongod.conf << EOF
systemLog:
destination: file
logAppend: true
path: /mongo/shard1/log/mongod.log
storage:
dbPath: /mongo/shard1/data
journal:
enabled: true
processManagement:
fork: true
pidFilePath: /mongo/shard1/run/mongod.pid
net:
port: 27001
bindIp: 0.0.0.0
replication:
replSetName: shard1
sharding:
clusterRole: shardsvr
EOF
# 啟動(dòng)所有的shard1 server
mongod --config /mongo/shard1/mongod.conf
# 登陸任意一臺(tái)shard1服務(wù)器,初始化副本集
mongo --port 27001
# 使用admin數(shù)據(jù)庫
use admin
# 定義副本集配置
config = {
_id : "shard1",
members : [
{_id : 0, host : "192.168.33.14:27001" },
{_id : 1, host : "192.168.33.15:27001" },
{_id : 2, host : "192.168.33.16:27001" }
]
}
# 初始化副本集配置
rs.initiate(config);
# 查看分區(qū)狀態(tài)
rs.status()
# 修改mongo shard2 server的配置文件
mkdir -p /mongo/shard2/{log,data,run}
# 分別在多臺(tái)機(jī)器上修改shard2 server的配置文件
cat > /mongo/shard2/mongod.conf << EOF
systemLog:
destination: file
logAppend: true
path: /mongo/shard2/log/mongod.log
storage:
dbPath: /mongo/shard2/data
journal:
enabled: true
processManagement:
fork: true
pidFilePath: /mongo/shard2/run/mongod.pid
net:
port: 27002
bindIp: 0.0.0.0
replication:
replSetName: shard2
sharding:
clusterRole: shardsvr
EOF
# 啟動(dòng)所有的shard2 server
mongod --config /mongo/shard2/mongod.conf
# 登陸任意一臺(tái)shard2服務(wù)器,初始化副本集
mongo --port 27002
# 使用admin數(shù)據(jù)庫
use admin
# 定義副本集配置
config = {
_id : "shard2",
members : [
{_id : 0, host : "192.168.33.14:27002" },
{_id : 1, host : "192.168.33.15:27002" },
{_id : 2, host : "192.168.33.16:27002" }
]
}
# 初始化副本集配置
rs.initiate(config)
# 查看分區(qū)狀態(tài)
rs.status()
# 修改mongo shard3 server的配置文件
mkdir -p /mongo/shard3/{log,data,run}
# 分別在多臺(tái)機(jī)器上修改shard3 server的配置文件
cat > /mongo/shard3/mongod.conf << EOF
systemLog:
destination: file
logAppend: true
path: /mongo/shard3/log/mongod.log
storage:
dbPath: /mongo/shard3/data
journal:
enabled: true
processManagement:
fork: true
pidFilePath: /mongo/shard3/run/mongod.pid
net:
port: 27003
bindIp: 0.0.0.0
replication:
replSetName: shard3
sharding:
clusterRole: shardsvr
EOF
# 啟動(dòng)所有的shard3 server
mongod --config /mongo/shard3/mongod.conf
# 登陸任意一臺(tái)的shard3服務(wù)器,初始化副本集
mongo --port 27003
# 使用admin數(shù)據(jù)庫
use admin
# 定義副本集配置
config = {
_id : "shard3",
members : [
{_id : 0, host : "192.168.33.14:27003" },
{_id : 1, host : "192.168.33.15:27003" },
{_id : 2, host : "192.168.33.16:27003" }
]
}
# 初始化副本集配置
rs.initiate(config)
# 查看分區(qū)狀態(tài)
rs.status()
##### 注意:?jiǎn)?dòng)mongos是守候進(jìn)程是因?yàn)?mongo/mongos/mongod.conf缺少了fork: true這個(gè)配置#######
------------------------------------------------------------------------------------------
mkdir -p /mongo/mongos/{log,data,run}
# 添加mongs的配置文件
cat > /mongo/mongos/mongod.conf << EOF
systemLog:
destination: file
logAppend: true
path: /mongo/mongos/log/mongod.log
processManagement:
fork: true
pidFilePath: /mongo/mongos/run/mongod.pid
net:
port: 27200
bindIp: 0.0.0.0
sharding:
configDB: config/192.168.33.14:27100,192.168.33.15:27100,192.168.33.16:27100
EOF
# 注意,這里configDB后面的config要與配置服務(wù)器的_id保持一致
# 啟動(dòng)路由服務(wù)器
mongos --config /mongo/mongos/mongod.conf
# 登錄其中的一臺(tái)路由節(jié)點(diǎn),手動(dòng)啟用分片
mongo --port 27200
# 添加分片到mongos
sh.addShard("shard1/192.168.33.14:27001,192.168.33.15:27001,192.168.33.16:27001")
sh.addShard("shard2/192.168.33.15:27002,192.168.33.16:27002,192.168.33.14:27002")
sh.addShard("shard3/192.168.33.16:27003,192.168.33.14:27003,192.168.33.15:27003")
# 設(shè)置slave可讀
rs.slaveOk()
#沒有分片是因?yàn)闆]有開啟分片規(guī)則
# 對(duì)bike這個(gè)數(shù)據(jù)庫開啟分片功能
use admin
db.runCommand({"enablesharding":"bike"})
# 對(duì)bike數(shù)據(jù)庫下的users集合按id的hash進(jìn)行分片
db.runCommand({"shardcollection":"bike.users","key":{_id:'hashed'}})
# 啟動(dòng)所有的config server
mongod --config /mongo/config/mongod.conf
# 啟動(dòng)所有的shard1
mongod --config /mongo/shard1/mongod.conf
# 啟動(dòng)所有的shard2
mongod --config /mongo/shard2/mongod.conf
# 啟動(dòng)所有的shard3
mongod --config /mongo/shard3/mongod.conf
# 啟動(dòng)所有的mongos
mongos --config /mongo/mongos/mongod.conf
# 關(guān)閉服務(wù)
mongod --shutdown --dbpath /mongo/shard3/data
mongod --shutdown --dbpath /mongo/shard2/data
mongod --shutdown --dbpath /mongo/shard1/data
mongod --shutdown --dbpath /mongo/config/data
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。