架構(三)MongoDB安裝配置以及叢集搭建

來源:互聯網
上載者:User

標籤:原來   3.3   mon   sed   設定   status   interval   image   show   

一 安裝1.1 下載MongoDB

我個人不太喜歡用wget url, 之前出現過wget下載的包有問題的情況

https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.4.7.tgz
1.2 上傳伺服器並解壓
tar -zxvf mongodb-linux-x86_64-3.4.7.tgz 
1.3 移動到/data/program/software/mongodb
mv mongodb-linux-x86_64-3.4.7/ /data/program/software/mongodb
1.4 進入到mongodb目錄下
mkdir dbmkdir logs
1.5 進入到bin目錄下

建立設定檔:touch mongodb.conf

dbpath=/data/program/software/mongodb/dblogpath=/data/program/software/mongodb/logs/mongodb.logport=27017fork=truenohttpinterface=true
1.6 啟動

分別在三台機器上啟動

/data/program/software/mongodb/bin/mongod --replSet repset -f /data/program/software/mongodb/bin/mongodb.conf
1.7 查看是否啟動成功
ps -ef |grep mongodb
1.8 建立叢集1.8.1 登入用戶端

選擇以上任意一台伺服器登入用戶端

/data/program/software/mongodb/bin/mongo
1.8.2 切換到admin資料庫
use admin
1.8.3 建立叢集

定義複本集組態變數,這裡的_id:"repset"和上面命理參數--replSet repset保持一致

config={_id:"repset", members:[{_id:0,host:"132.232.47.139"},{_id:1,host:"45.40.199.46"},{_id:2,host:"132.232.40.31"}]}

輸入以上變數後輸入下面內容說明沒問題

rs.initiate(config)

執行上面語句,如果輸出下面內容說明沒問題

1.8.5 查看叢集狀態
rs.status()
{"set" : "repset","date" : ISODate("2018-08-05T12:20:02.096Z"),"myState" : 1,"term" : NumberLong(1),"heartbeatIntervalMillis" : NumberLong(2000),"optimes" : {"lastCommittedOpTime" : {"ts" : Timestamp(1533471594, 1),"t" : NumberLong(1)},"appliedOpTime" : {"ts" : Timestamp(1533471594, 1),"t" : NumberLong(1)},"durableOpTime" : {"ts" : Timestamp(1533471594, 1),"t" : NumberLong(1)}},"members" : [{"_id" : 0,"name" : "132.232.47.139:27017","health" : 1,"state" : 1,"stateStr" : "PRIMARY","uptime" : 4360,"optime" : {"ts" : Timestamp(1533471594, 1),"t" : NumberLong(1)},"optimeDate" : ISODate("2018-08-05T12:19:54Z"),"infoMessage" : "could not find member to sync from","electionTime" : Timestamp(1533471513, 1),"electionDate" : ISODate("2018-08-05T12:18:33Z"),"configVersion" : 1,"self" : true},{"_id" : 1,"name" : "45.40.199.46:27017","health" : 1,"state" : 2,"stateStr" : "SECONDARY","uptime" : 100,"optime" : {"ts" : Timestamp(1533471594, 1),"t" : NumberLong(1)},"optimeDurable" : {"ts" : Timestamp(1533471594, 1),"t" : NumberLong(1)},"optimeDate" : ISODate("2018-08-05T12:19:54Z"),"optimeDurableDate" : ISODate("2018-08-05T12:19:54Z"),"lastHeartbeat" : ISODate("2018-08-05T12:20:01.211Z"),"lastHeartbeatRecv" : ISODate("2018-08-05T12:20:01.767Z"),"pingMs" : NumberLong(0),"syncingTo" : "132.232.47.139:27017","configVersion" : 1},{"_id" : 2,"name" : "132.232.40.31:27017","health" : 1,"state" : 2,"stateStr" : "SECONDARY","uptime" : 100,"optime" : {"ts" : Timestamp(1533471594, 1),"t" : NumberLong(1)},"optimeDurable" : {"ts" : Timestamp(1533471594, 1),"t" : NumberLong(1)},"optimeDate" : ISODate("2018-08-05T12:19:54Z"),"optimeDurableDate" : ISODate("2018-08-05T12:19:54Z"),"lastHeartbeat" : ISODate("2018-08-05T12:20:01.211Z"),"lastHeartbeatRecv" : ISODate("2018-08-05T12:20:01.769Z"),"pingMs" : NumberLong(0),"syncingTo" : "132.232.47.139:27017","configVersion" : 1}],"ok" : 1}
1.8.6 測試叢集功能a 主節點伺服器登入用戶端
./mongo
b 串連test資料庫
use test
c 往testdb表中插入資料
db.testdb.insert({"test1":"testval1"})
d 串連到副本節點用戶端
./mongo 45.40.199.46:27017
e 使用test資料庫
use test
f 查詢表格
show tables

如果出現以下報錯

mongodb預設是從主節點讀寫資料,副本節點上不允許讀,設定副本節點可讀

g 設定讀許可權
db.getMongo().setSlaveOk()
h 查詢在主節點上插入的資料
db.testdb.find()

i 測試叢集恢複功能1 將主節點服務停掉後在登入其他節點用戶端查看叢集狀態
{"set" : "repset","date" : ISODate("2018-08-05T13:15:05.136Z"),"myState" : 1,"term" : NumberLong(2),"heartbeatIntervalMillis" : NumberLong(2000),"optimes" : {"lastCommittedOpTime" : {"ts" : Timestamp(1533474896, 1),"t" : NumberLong(2)},"appliedOpTime" : {"ts" : Timestamp(1533474896, 1),"t" : NumberLong(2)},"durableOpTime" : {"ts" : Timestamp(1533474896, 1),"t" : NumberLong(2)}},"members" : [{"_id" : 0,"name" : "132.232.47.139:27017","health" : 0,"state" : 8,"stateStr" : "(not reachable/healthy)","uptime" : 0,"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-08-05T13:15:03.341Z"),"lastHeartbeatRecv" : ISODate("2018-08-05T13:12:24.585Z"),"pingMs" : NumberLong(0),"lastHeartbeatMessage" : "Connection refused","configVersion" : -1},{"_id" : 1,"name" : "45.40.199.46:27017","health" : 1,"state" : 1,"stateStr" : "PRIMARY","uptime" : 7653,"optime" : {"ts" : Timestamp(1533474896, 1),"t" : NumberLong(2)},"optimeDate" : ISODate("2018-08-05T13:14:56Z"),"electionTime" : Timestamp(1533474755, 1),"electionDate" : ISODate("2018-08-05T13:12:35Z"),"configVersion" : 1,"self" : true},{"_id" : 2,"name" : "132.232.40.31:27017","health" : 1,"state" : 2,"stateStr" : "SECONDARY","uptime" : 3401,"optime" : {"ts" : Timestamp(1533474896, 1),"t" : NumberLong(2)},"optimeDurable" : {"ts" : Timestamp(1533474896, 1),"t" : NumberLong(2)},"optimeDate" : ISODate("2018-08-05T13:14:56Z"),"optimeDurableDate" : ISODate("2018-08-05T13:14:56Z"),"lastHeartbeat" : ISODate("2018-08-05T13:15:03.282Z"),"lastHeartbeatRecv" : ISODate("2018-08-05T13:15:03.165Z"),"pingMs" : NumberLong(0),"syncingTo" : "45.40.199.46:27017","configVersion" : 1}],"ok" : 1}

結論:_id=1的服務現在變成了主節點,_id=0的服務現在不可用了

2 再次將原來的132.232.47.139這台機器啟動起來
{"set" : "repset","date" : ISODate("2018-08-05T13:19:08.073Z"),"myState" : 1,"term" : NumberLong(2),"heartbeatIntervalMillis" : NumberLong(2000),"optimes" : {"lastCommittedOpTime" : {"ts" : Timestamp(1533475146, 1),"t" : NumberLong(2)},"appliedOpTime" : {"ts" : Timestamp(1533475146, 1),"t" : NumberLong(2)},"durableOpTime" : {"ts" : Timestamp(1533475146, 1),"t" : NumberLong(2)}},"members" : [{"_id" : 0,"name" : "132.232.47.139:27017","health" : 1,"state" : 2,"stateStr" : "SECONDARY","uptime" : 4,"optime" : {"ts" : Timestamp(1533474744, 1),"t" : NumberLong(1)},"optimeDurable" : {"ts" : Timestamp(1533474744, 1),"t" : NumberLong(1)},"optimeDate" : ISODate("2018-08-05T13:12:24Z"),"optimeDurableDate" : ISODate("2018-08-05T13:12:24Z"),"lastHeartbeat" : ISODate("2018-08-05T13:19:07.545Z"),"lastHeartbeatRecv" : ISODate("2018-08-05T13:19:03.326Z"),"pingMs" : NumberLong(0),"configVersion" : 1},{"_id" : 1,"name" : "45.40.199.46:27017","health" : 1,"state" : 1,"stateStr" : "PRIMARY","uptime" : 7896,"optime" : {"ts" : Timestamp(1533475146, 1),"t" : NumberLong(2)},"optimeDate" : ISODate("2018-08-05T13:19:06Z"),"electionTime" : Timestamp(1533474755, 1),"electionDate" : ISODate("2018-08-05T13:12:35Z"),"configVersion" : 1,"self" : true},{"_id" : 2,"name" : "132.232.40.31:27017","health" : 1,"state" : 2,"stateStr" : "SECONDARY","uptime" : 3644,"optime" : {"ts" : Timestamp(1533475146, 1),"t" : NumberLong(2)},"optimeDurable" : {"ts" : Timestamp(1533475146, 1),"t" : NumberLong(2)},"optimeDate" : ISODate("2018-08-05T13:19:06Z"),"optimeDurableDate" : ISODate("2018-08-05T13:19:06Z"),"lastHeartbeat" : ISODate("2018-08-05T13:19:07.360Z"),"lastHeartbeatRecv" : ISODate("2018-08-05T13:19:07.255Z"),"pingMs" : NumberLong(0),"syncingTo" : "45.40.199.46:27017","configVersion" : 1}],"ok" : 1}

結論:_id=1的這個服務還是主節點,_id=0的服務變成了副本節點

 

架構(三)MongoDB安裝配置以及叢集搭建

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.