mongodb複製集部署文件
一、安裝SNMP(新版mongodb需要此依賴安裝)
安裝snmp服務需要的rpm包:
perl-Data-Dumper-2.145-3.el7.x86_64.rpm
net-snmp-5.7.2-28.el7_4.1.x86_64.rpm
net-snmp-agent-libs-5.7.2-28.el7_4.1.x86_64.rpm
net-snmp-libs-5.7.2-28.el7_4.1.x86_64.rpm
安裝步驟:
(1)將這個幾rpm包放到任意目錄下,進入到這個目錄,執行命令 rpm -ivh net-snmp*.rpm --force
例如,將這幾個
rpm -ivh net-snmp*.rpm --force 安裝完成之後,這幾個rpm包就可以刪掉了。
(2)修改配置檔案(/etc/snmp/snmpd.conf),大家可以參考http://www.linuxde.net/2013/02/12269.html對snmp進行符合自己要求的配置,如果你嫌麻煩,可以直接用我配置好的snmpd.conf覆蓋過去,基本能滿足我們日常開發需求,步驟就是把壓縮包裡的snmpd.conf
(3)最後啟動服務,執行命令 systemctl start snmpd
(4)測試驗證SNMP配置的正確性,執行命令
snmpwalk -v 1 192.168.0.200 -c public system
(注意,192.168.0.200更改為自己安裝snmp服務的那臺機器地址)
表明安裝成功!!!
二、mongodb配置,順序一定不能錯
建立相關目錄
新增mongodb環境變數
echo 'export PATH=/usr/local/mongodb/bin:$PATH' >> /etc/profile
source /etc/profile
[[email protected] mongodb4.0.2]# mkdir data logs # 資料目錄和日誌目錄
新建配置檔案(兩個節點的配置一下只有存資料的目錄不同)
[[email protected] mongodb4.0.2]# cat mongodb.conf
logpath=/usr/local/mongodb/logs/mongod.log # 日誌路徑
logappend=true # 是否日誌追加
fork=true # 後臺啟動
port=27017 # 埠
dbpath=/usr/local/mongodb/data/mongodb_bak/mongodb_bak_now # 資料目錄
pidfilepath=/usr/local/mongodb/data/mongod.pid # pid
oplogSize=4096 # 設定oplog的大小
maxConns=640000 # 最大同時連線數 預設2000
directoryperdb=true # 設定每個資料庫將被儲存在一個單獨的目錄
bind_ip=0.0.0.0 # 監聽 0.0.0.0 代表可以被訪問連線
啟動主節點
[[email protected] mongodb4.0.2]#./bin/mongod -f mongodb.conf # 主
進入主節點並新建root賬號
[[email protected] mongodb4.0.2]#mongo
MongoDB shell version v4.0.2
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 4.0.2> db.createUser({ user: 'root', pwd: 'test123', roles: [ { role:"root", db: "admin" } ] });
Successfully added user: {
"user" : "root",
"roles" : [
{
"role" : "root",
"db" : "admin"
}
]}>
停止主節點
[[email protected] mongodb4.0.2]#./bin/mongod -f mongodb.conf # 主
啟用密碼認證和keyfile證書驗證
把主節點上生成的energy-rc.key傳到從節點上,配置同主節點一樣(記得對傳過去的檔案也得同樣賦許可權)
# 生成keyfiles檔案[[email protected] mongodb4.0.2]#openssl rand -base64 756 > energy-rc.key
# 設定只讀許可權[[email protected] mongodb4.0.2]#chmod 400 energy-rc.key
# 配置檔案中新增如下[[email protected] mongodb4.0.2]#cat mongodb.conf
auth=true #開啟認證
keyFile=/root/mongodb4.0.2/energy-rc.key #啟用key驗證
replSet=rs0
啟動主從節點
[[email protected] mongodb4.0.2]#./bin/mongod -f mongodb.conf # 主[[email protected] mongodb4.0.2]#./bin/mongod -f mongodb.conf # 從
進入主節點新增副本集節點
2018-09-05T13:26:02.859+0800 I CONTROL [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
about to fork child process, waiting until server is ready for connections.
forked process: 6290
child process started successfully, parent exiting[[email protected] mongodb4.0.2]# ./bin/mongo
MongoDB shell version v4.0.2
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 4.0.2
> use admin
switched to db admin> db.auth('root','test123')1
# 檢視複製集狀態> rs.status(){
"ok" : 0,
"errmsg" : "no replset config has been received",
"code" : 94,
"codeName" : "NotYetInitialized"}
# 定義配置資訊,把兩臺節點加進來> var cfg={_id:'rs0',members:[{_id:0,host:'10.90.2.105:27017'},{_id:1,host:'10.90.2.103:27017'}]}
# 初始化複製集> rs.initiate(cfg){ "ok" : 1 }
# 檢視配置資訊
rs0:SECONDARY> rs.conf(){
"_id" : "rs0",
"version" : 1,
"protocolVersion" : NumberLong(1),
"writeConcernMajorityJournalDefault" : true,
"members" : [
{
"_id" : 0,
"host" : "10.90.2.105:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 1,
"host" : "10.90.2.103:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatIntervalMillis" : 2000,
"heartbeatTimeoutSecs" : 10,
"electionTimeoutMillis" : 10000,
"catchUpTimeoutMillis" : -1,
"catchUpTakeoverDelayMillis" : 30000,
"getLastErrorModes" : {
},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
},
"replicaSetId" : ObjectId("5b8f65bd5eb3d94f12e22e96")
}}
rs0:SECONDARY> rs.status(){
"set" : "rs0",
"date" : ISODate("2018-09-05T05:13:09.326Z"),
"myState" : 2,
"term" : NumberLong(0),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"appliedOpTime" : {
"ts" : Timestamp(1536124349, 1),
"t" : NumberLong(-1)
},
"durableOpTime" : {
"ts" : Timestamp(1536124349, 1),
"t" : NumberLong(-1)
}
},
"lastStableCheckpointTimestamp" : Timestamp(0, 0),
"members" : [
{
"_id" : 0,
"name" : "10.90.2.105:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 191,
"optime" : {
"ts" : Timestamp(1536124349, 1),
"t" : NumberLong(-1)
},
"optimeDate" : ISODate("2018-09-05T05:12:29Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "could not find member to sync from",
"configVersion" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "10.90.2.103:27017",
"health" : 1,
"state" : 0,
"stateStr" : "STARTUP",
"uptime" : 39,
"optime" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"optimeDurable" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"optimeDate" : ISODate("1970-01-01T00:00:00Z"),
"optimeDurableDate" : ISODate("1970-01-01T00:00:00Z"),
"lastHeartbeat" : ISODate("2018-09-05T05:13:09.261Z"),
"lastHeartbeatRecv" : ISODate("1970-01-01T00:00:00Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : -2
}
],
"ok" : 1}
rs0:SECONDARY>
現在退出連線重新連線下主庫,主節點的狀態就變成PRIMARY
[[email protected] mongodb4.0.2]#./bin/mongod --shutdown -f mongodb.conf # 主[[email protected] mongodb4.0.2]#./bin/mongod -f mongodb.conf # 主[[email protected] mongodb4.0.2]# ./bin/mongo
MongoDB shell version v4.0.2
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 4.0.2
# 需要認證才能檢視
rs0:SECONDARY> show dbs;2018-09-05T13:26:33.214+0800 E QUERY [js] Error: listDatabases failed:{
"operationTime" : Timestamp(1536125178, 1),
"ok" : 0,
"errmsg" : "command listDatabases requires authentication",
"code" : 13,
"codeName" : "Unauthorized",
"$clusterTime" : {
"clusterTime" : Timestamp(1536125178, 1),
"signature" : {
"hash" : BinData(0,"IszQmtleFm3uVO+Uc8luktG9uYM="),
"keyId" : NumberLong("6597607397777211393")
}
}} :
[email protected]/mongo/shell/utils.js:25:13
[email protected]/mongo/shell/mongo.js:67:1
[email protected]/mongo/shell/utils.js:876:19
[email protected]/mongo/shell/utils.js:766:15
@(shellhelp2):1:1
# 認證
rs0:PRIMARY> use admin
switched to db admin
rs0:PRIMARY> db.auth('root','test123')1
# 可以看到,當前節點為主了
rs0:PRIMARY> db.isMaster();{
"hosts" : [
"10.90.2.105:27017",
"10.90.2.103:27017"
],
"setName" : "rs0",
"setVersion" : 1,
"ismaster" : true,
"secondary" : false,
"primary" : "10.90.2.105:27017",
"me" : "10.90.2.105:27017",
"electionId" : ObjectId("7fffffff0000000000000001"),
"lastWrite" : {
"opTime" : {
"ts" : Timestamp(1536125327, 1),
"t" : NumberLong(1)
},
"lastWriteDate" : ISODate("2018-09-05T05:28:47Z"),
"majorityOpTime" : {
"ts" : Timestamp(1536125327, 1),
"t" : NumberLong(1)
},
"majorityWriteDate" : ISODate("2018-09-05T05:28:47Z")
},
"maxBsonObjectSize" : 16777216,
"maxMessageSizeBytes" : 48000000,
"maxWriteBatchSize" : 100000,
"localTime" : ISODate("2018-09-05T05:28:57.348Z"),
"logicalSessionTimeoutMinutes" : 30,
"minWireVersion" : 0,
"maxWireVersion" : 7,
"readOnly" : false,
"ok" : 1,
"operationTime" : Timestamp(1536125327, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1536125327, 1),
"signature" : {
"hash" : BinData(0,"DUnksVz3DkGjfXNiuFbVDPRjtgg="),
"keyId" : NumberLong("6597607397777211393")
}
}}
現在副本集主從狀態已經建立了,接下來就測試下資料能不能正常同步
測試同步
在主上插入一條資料
rs0:PRIMARY> use pay
switched to db pay
rs0:PRIMARY> db.pay.insert({"_id":"1"})WriteResult({ "nInserted" : 1 })
rs0:PRIMARY>
然後在從節點上檢視是否已同步
rs0:SECONDARY> use admin
switched to db admin
rs0:SECONDARY> db.auth('root','test123')
1
rs0:SECONDARY> show dbs;
2018-09-05T13:31:49.563+0800 E QUERY [js] Error: listDatabases failed:{
"operationTime" : Timestamp(1536125517, 1),
"ok" : 0,
"errmsg" : "not master and slaveOk=false",
"code" : 13435,
"codeName" : "NotMasterNoSlaveOk",
"$clusterTime" : {
"clusterTime" : Timestamp(1536125517, 1),
"signature" : {
"hash" : BinData(0,"KIttQEB/e0mzlQvqPJzeLWZgREM="),
"keyId" : NumberLong("6597607397777211393")
}
}} :
[email protected]/mongo/shell/utils.js:25:13
[email protected]/mongo/shell/mongo.js:67:1
[email protected]/mongo/shell/utils.js:876:19
[email protected]/mongo/shell/utils.js:766:15
@(shellhelp2):1:1
# 預設因為SECONDARY是不允許讀寫的,如果非要解決,方法如下:
rs0:SECONDARY> db.getMongo().setSlaveOk();
# 可以看到資料已經同步過來了
rs0:SECONDARY> show dbs;
admin 0.000GB
config 0.000GB
local 0.000GB
pay 0.000GB
rs0:SECONDARY>use pay
rs0:SECONDARY> db.pay.find().count()
1
rs0:SECONDARY> db.pay.find(){ "_id" : "1" }
三、mongodb配置xml檔案
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation="http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd">
<mongo:mongo-client id="mongofs" replica-set="${mongodb.host}:${mongodb.port},${mongodb.host2}:${mongodb.port}" credentials="${mongodb.user}:${mongodb.pwd}@${mongodb.defaultDbName}">
<mongo:client-options connections-per-host="8"
threads-allowed-to-block-for-connection-multiplier="4"
connect-timeout="1000"
read-preference="primaryPreferred"
max-wait-time="1500"
socket-keep-alive="true"
socket-timeout="1500"
write-concern="NORMAL"/>
</mongo:mongo-client>
<mongo:db-factory id="mongofsDbFactory" dbname="pay" mongo-ref="mongofs"/>
<mongo:mapping-converter id="mongofsConverter" db-factory-ref="mongofsDbFactory"/>
<bean id="gridFsTemplate" class="com.silverdata.smart.mongodb.gridfs.GenericGridFsTemplate">
<constructor-arg ref="mongofsDbFactory"/>
<constructor-arg ref="mongofsConverter"/>
</bean>
</beans>