MongoDB replica set :
A replica set in MongoDB is a group of mongod processes that maintain the same data set. Replica sets provide redundancy and high availability, and are the basis for all production deployments.
簡單的說典尾,復(fù)制集有多個MongoDB服務(wù)器肌毅,它們有相同的數(shù)據(jù)庫凳谦,如果一個服務(wù)器因?yàn)榫W(wǎng)絡(luò)故障或者停電DOWN了,其他服務(wù)器可以繼續(xù)提供服務(wù),這樣可以提高數(shù)據(jù)源的可靠性和健壯性淀衣。詳情見這里灯帮。
MongoDB復(fù)制集包括一個PRIMARY,其他的都是SECONDARY擎椰,只有PRIMARY才能接受讀寫操作支子。PRIMARY會記錄那些寫的操作到一個日志當(dāng)中,oplog达舒,其他SECONDARY會按照這個日志值朋,去操作他們的數(shù)據(jù)庫來保持?jǐn)?shù)據(jù)庫的一致。
搭建過程(在同一臺機(jī)子嘗試):
1.新建保存復(fù)制集的目錄
先生成一些目錄巩搏,用來保存數(shù)據(jù)庫相關(guān)文件(比如日志昨登、數(shù)據(jù)、配置文件)
最終搭建成這個樣子:
-mongo_repl
|-rs1
|-data
|-log
|-conf
|-journal
|-diagnostic.data
|-rs2
|-data
|-log
|-conf
|-journal
|-diagnostic.data
|-rs3
|-data
|-log
|-conf
|-journal
|-diagnostic.data
相關(guān)命令(在~/data/下新建)
cd ~
mkdir -p data/mongo_repl
cd data/mongo_repl
mkdir -p rs1/conf rs1/data rs1/log rs1/journal rs1/diagnostic.data
mkdir -p rs2/conf rs2/data rs2/log rs2/journal rs2/diagnostic.data
mkdir -p rs3/conf rs3/data rs3/log rs3/journal rs3/diagnostic.data
2.生成key file(用于復(fù)制集成員之間的驗(yàn)證)
Keyfiles use SCRAM-SHA-1 challenge and response authentication mechanism. The contents of the keyfiles serve as the shared password for the members. A key’s length must be between 6 and 1024 characters and may only contain characters in the base64 set.
生成命令:
cd ~/data/mongo_repl
openssl rand -base64 756 > repl_set.key
chmod 400 keyFile
3.設(shè)置配置文件
(1)設(shè)置rs1的配置文件
vi ~/data/mongo_repl/rs1/conf/mongod.conf
配置如下內(nèi)容:
systemLog:
destination: file
path: "~/data/mongo_repl/rs1/log/mongod.log"
logAppend: true
storage:
journal:
enabled: true
dbPath: "~/data/mongo_repl/rs1"
processManagement:
fork: true
pidFilePath: "~/data/mongo_repl/rs1/mongod.pid"
net:
bindIp: 0.0.0.0
port: 19017
maxIncomingConnections: 2000
setParameter:
enableLocalhostAuthBypass: false
replication:
replSetName: my_repl
security:
keyFile: "~/data/mongo_repl/repl_set.key"
# authorization: enabled
(2)設(shè)置rs2的配置文件
vi ~/data/mongo_repl/rs2/conf/mongod.conf
配置如下內(nèi)容:
systemLog:
destination: file
path: "~/data/mongo_repl/rs2/log/mongod.log"
logAppend: true
storage:
journal:
enabled: true
dbPath: "~/data/mongo_repl/rs2"
processManagement:
fork: true
pidFilePath: "~/data/mongo_repl/rs2/mongod.pid"
net:
bindIp: 0.0.0.0
port: 19018
maxIncomingConnections: 2000
setParameter:
enableLocalhostAuthBypass: false
replication:
replSetName: my_repl
security:
keyFile: "~/data/mongo_repl/repl_set.key"
# authorization: enabled
(3)設(shè)置rs3的配置文件
vi ~/data/mongo_repl/rs3/conf/mongod.conf
配置如下內(nèi)容:
systemLog:
destination: file
path: "~/data/mongo_repl/rs3/log/mongod.log"
logAppend: true
storage:
journal:
enabled: true
dbPath: "~/data/mongo_repl/rs3"
processManagement:
fork: true
pidFilePath: "~/data/mongo_repl/rs3/mongod.pid"
net:
bindIp: 0.0.0.0
port: 19019
maxIncomingConnections: 2000
setParameter:
enableLocalhostAuthBypass: false
replication:
replSetName: my_repl
security:
keyFile: "~/data/mongo_repl/repl_set.key"
# authorization: enabled
注意:security.authorization:enabled注釋掉了贯底,這樣進(jìn)去才可以進(jìn)行復(fù)制集初始化丰辣、新建用戶等操作。
4.啟動復(fù)制集三個成員的mongod進(jìn)程
mongod -f ~/data/mongo_repl/rs1/conf/mongod.conf
mongod -f ~/data/mongo_repl/rs2/conf/mongod.conf
mongod -f ~/data/mongo_repl/rs3/conf/mongod.conf
5.連接其中一個mongod,初始化復(fù)制集笙什。
mongo --port 19017
config = { _id: "my_repl", members: [{_id: 0, host: "機(jī)子IP地址:19017"}, {_id: 1, host: "機(jī)子IP地址:19018"}, {_id: 2, host: "機(jī)子IP地址:19019"}]}
rs.initiate(config)
rs.status(); #查看復(fù)制集狀態(tài)
6.創(chuàng)建相關(guān)用戶
use admin
db.createUser({user:"admin",pwd:"yourpassword",roles:[{role:"userAdminAnyDatabase",db:"admin"}]})
MongoDB規(guī)定飘哨,創(chuàng)建的第一個用戶一定要是在admin數(shù)據(jù)庫、并且角色是userAdminAnyDatabase琐凭。
創(chuàng)建完第一個角色芽隆,用這個角色登錄來創(chuàng)建其他角色。
use admin
db.auth("admin","yourpassword")
db.createUser({user:"clusterAdmin",pwd:"yourpassword",roles:[{role:"clusterAdmin",db:"admin"}]})
#這個角色可以進(jìn)行復(fù)制集的管理(添加成員统屈、查看狀態(tài)等等)胚吁。
7.最后
把3份配置文件的security.authorization:true注釋去掉,重啟復(fù)制集鸿吆。
mongod -f ~/data/mongo_repl/rs1/conf/mongod.conf
mongod -f ~/data/mongo_repl/rs2/conf/mongod.conf
mongod -f ~/data/mongo_repl/rs3/conf/mongod.conf
現(xiàn)在要登錄才能查看復(fù)制集狀態(tài)了囤采。