MongoDB+dockerdocker 配置 mongodb 複製集

來源:互聯網
上載者:User
dockerfile:
#base imagesFROM ubuntu:latestMAINTAINER yao.3.li@uconn.edu#install mongodbRUN apt-get updateRUN apt-get -y install curlRUN curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.0.6.tgzRUN tar -zxvf mongodb-linux-x86_64-3.0.6.tgzRUN mv mongodb-linux-x86_64-3.0.6/ /usr/local/mongodbRUN export PATH=/usr/local/mongodb/bin:$PATHADD ./run.sh /run.shRUN chmod 777 /run.sh#database directoryRUN mkdir -p /data/dbRUN mkdir -p /data/logRUN touch /data/log/r.logRUN mkdir -p /data/config#portEXPOSE 27017ENTRYPOINT ["/run.sh"]

docker-compose.yml:
version: '3.2'services:  primary:    image: mongodb    ports:      - "27017:27017"    environment:      - role=prime    volumes:      - ./config/r0.conf:/data/config/c.conf      - ./runPrime.sh:/runPrime.sh  secondary1:    image: mongodb    ports:      - "27018:27017"    environment:       - role=secondary    volumes:      - ./config/r1.conf:/data/config/c.conf  secondary2:    image: mongodb    ports:      - "27019:27017"    environment:       - role=secondary    volumes:      - ./config/r1.conf:/data/config/c.conf

r0.conf與r1.conf暫時一樣:
#資料檔案夾dbpath=/data/db#記錄檔夾,如果以後台方式運行,必須指定logpathlogpath=/data/log/r.log#以追加而非覆蓋方式寫入日誌logappend=true#連接埠port=27017#以後台方式運行  #fork=true 
run.sh:
#!/bin/bash/usr/local/mongodb/bin/mongod -f /data/config/c.conf --replSet "rs0"

runPrime.sh:
#!/bin/bashmongo localhost:27017/admin conf.js
conf.js:
config={_id:"rs0", members:[{_id:0,host:"primary",priority:1}, {_id:1,host:"secondary1",priority:0}, {_id:2,host:"secondary2",priority:0}]}rs.initiate(config)rs.conf()rs.add("secondary1")rs.add("secondary2")

運行過程: 先構建鏡像:
docker build -t mongodb .
啟動容器:
docker-compose up -d
登入主庫,使用指令碼配置,也即運行runPrime.sh來配置複製集:
./runPrime.sh

之後登入即可驗證:
mongo --port 27017


提示符帶有了primary,說明配置成功。 當然可以進一步在主庫寫,從庫讀,但是需要在從庫中使用:
db.setSlaveOk()

才能讀。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.