replicaset은 홀수의 노드가 필요함. secondary(슬레이브)에서 몽고 프로세스를 하나 더 실행한다.
replication:
oplogSizeMB: 10240
replSetName: "rs01"
replication:
oplogSizeMB: 10240
replSetName: "rs01"
기존 mongod.conf 파일 카피 후 새로만든 arbiter 디렉터리로 경로를 바꾼다.
path: "/astron/service/app/arbiter/log/mongod.log"
dbPath: "/astron/service/app/arbiter/mongo"
pidFilePath: "/astron/service/app/arbiter/run/mongod.pid"
port: 27999
정상구동 확인
root@db2 arbiter]# mkdir log mongo run
[root@db2 arbiter]# ls
arbiter.conf log mongo run
[root@db2 arbiter]# mongod -f ./arbiter.conf
about to fork child process, waiting until server is ready for connections.
forked process: 3427
child process started successfully, parent exiting
[root@db2 arbiter]#
[root@db2 arbiter]# ps -ef | grep mongo
root 3427 1 3 16:32 ? 00:00:00 mongod -f ./arbiter.conf
root 3477 28532 0 16:32 pts/0 00:00:00 grep --color=auto mongo
[root@db2 arbiter]# mongo --port 27999
MongoDB shell version v4.2.11
connecting to: mongodb://127.0.0.1:27999/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("806ef731-993b-4652-bda4-f3c1aa1000ee") }
MongoDB server version: 4.2.11
Welcome to the MongoDB shell.
[root@db2 arbiter]# mongod --shutdown -f ./arbiter.conf
killing process with pid: 3427
mongo 혹은 mongo localhost:27017/admin
접속 후 아래 입력
rs.initiate(
{
_id: "rs01",
version: 1,
members: [
{ _id: 0, host : "192.168.218.9:27017", priority: 100 },
{ _id: 1, host : "192.168.218.11:27017" }
]
}
)
# 성공
{
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1657009455, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1657009455, 1)
}
이후 상태 확인
rs.status()
primary, secondary 각각 prompt가 바뀐다.
# primary에서 실행
rs01:PRIMARY>
# secondary에서 실행
rs01:SECONDARY>
rs.addArb("192.168.218.11:27999")
# 성공
{
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1657009654, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1657009654, 1)
}
rs가 구성됐다면 아래 커맨드를 쳤을 때 rs의 구성 정보가 나온다.
rs.state()
만약 인증이 필요하다는 에러가 나면 conf파일의 security: authorization: 값 주석처리후 mongo 재기동
rs.status()도 security 설정돼있으면 사용 불가.
{
"ok" : 0,
"errmsg" : "command replSetInitiate requires authentication",
"code" : 13,
"codeName" : "Unauthorized"
}
primary
rs01:PRIMARY> use test
switched to db test
rs01:PRIMARY> db.col1.insert({"test" : "ok"})
WriteResult({ "nInserted" : 1 })
rs01:PRIMARY> db.col1.find()
{ "_id" : ObjectId("62c3fc754fdb6d26e3eba608"), "test" : "ok" }
secondary
rs01:SECONDARY> rs.slaveOk()
WARNING: slaveOk() is deprecated and may be removed in the next major release. Please use secondaryOk() instead.
rs01:SECONDARY> db.col1.find()
{ "_id" : ObjectId("62c3fc754fdb6d26e3eba608"), "test" : "ok" }
성공
secondary
rs01:SECONDARY> db.col1.find()
{ "_id" : ObjectId("62c3fc754fdb6d26e3eba608"), "test" : "ok" }
rs01:SECONDARY> ^C
bye
[root@db2 arbiter]# mongo
MongoDB shell version v4.2.11
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("fee0a7e6-0d2e-4f13-bc23-82247d07b604") }
MongoDB server version: 4.2.11
rs01:PRIMARY> db.col1.find()
{ "_id" : ObjectId("62c3fc754fdb6d26e3eba608"), "test" : "ok" }
rs01:PRIMARY>
성공