https://www.mongodb.com/docs/manual/tutorial/deploy-replica-set/
前提
OS : Rocky Linux 9.1
MongoDB version : 6.0.3
mmm191 : MongoDB primary
mmm192 : MongoDB secondary
mmm193 : MongoDB secondary
-- 1. MongoDBインストール[mmm191,mmm192,mmm193で実施]
vim /etc/yum.repos.d/mongodb-org-6.0.repo
[mongodb-org-6.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/6.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-6.0.asc
dnf install -y mongodb-org
systemctl enable mongod
systemctl restart mongod
systemctl status mongod
mongod --version
-- 2. bind設定[mmm191,mmm192,mmm193で実施]
vim /etc/mongod.conf
bindIp: 0.0.0.0
replication:
replSetName: "rs01"
oplogSizeMB: 1024
systemctl restart mongod
ss -ano | grep 27017
-- 3. Replication設定[mmm191で実施]
mongosh
rs.initiate(
{
_id: "rs01",
members: [
{ _id: 0, host: "192.168.137.191:27017", priority : 4 },
{ _id: 1, host: "192.168.137.192:27017", priority : 2 },
{ _id: 2, host: "192.168.137.193:27017", priority : 1 }
]
}
)
rs.conf()
rs.status();
db.tab1.insertOne( { col1: "val1"} )
db.tab1.find()
※レプリカで読み取りするためには接続後、下記コマンド実行必要
db.getMongo().setReadPref("primaryPreferred")
-- 4. 切り替わり確認[mmm191,mmm192,mmm193で実施]
-- mmm191サービス停止
systemctl stop mongod
mongosh
rs.status();
db.tab1.insertOne( { col1: "val2"} )
db.tab1.find()
-- 5. 切り戻し確認[mmm191,mmm192,mmm193で実施]
-- mmm191サービス起動
systemctl start mongod
自動で元のマスタに戻る
mongosh
rs.status();
db.tab1.insertOne( { col1: "val3"} )
db.tab1.find()