Replication
MongoDB:如何將獨立集轉換為副本集
我是 MongoDB 的新手,並且搜尋了幾個小時如何將我的 mongo 獨立實例提升為副本集。我的實際環境是一台 ubuntu 伺服器
root@UMONGO1:/home/msc# lsb_release -a Distributor ID: Ubuntu Description: Ubuntu 20.04.3 LTS Release: 20.04 Codename: focal
在埠 27017 上本地執行獨立的 mongodb 實例:
root@UMONGO1:/var/lib# systemctl status mongod ● mongod.service - MongoDB Database Server Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled) Active: active (running) since Sat 2022-01-01 15:46:52 CET; 10min ago Docs: https://docs.mongodb.org/manual Main PID: 964 (mongod) Memory: 222.6M CGroup: /system.slice/mongod.service └─964 /usr/bin/mongod --config /etc/mongod.conf # mongod.conf # for documentation of all options, see: # http://docs.mongodb.org/manual/reference/configuration-options/ # Where and how to store data. storage: dbPath: /var/lib/mongodb journal: enabled: true # engine: # wiredTiger: # where to write logging data. systemLog: destination: file logAppend: true path: /var/log/mongodb/mongod.log # network interfaces net: port: 27017 bindIp: 127.0.0.1 # how the process runs processManagement: timeZoneInfo: /usr/share/zoneinfo #security: #operationProfiling: #replication: #sharding: ## Enterprise-Only Options: #auditLog: #snmp:
我從這裡導入了一個數據庫目標是再創建兩台伺服器並在它們上面複製數據庫。
參考文件:
將獨立集轉換為副本集我對必須使用的兩個參數 –port 和 –dbpath 感到困惑。如果我想推廣我的實際實例,我應該使用現有的數據庫路徑和埠,對嗎?但無論我在嘗試什麼,它都是失敗的。
使用另一個埠:
mongod --port 27017 --dbpath /var/lib/mongodb/ --replSet rs0 "ctx":"initandlisten","msg":"Error setting up listener","attr":{"error":{"code":9001,"codeName":"SocketException","errmsg":"Address already in use"}}} mongod --port 27018 --dbpath /var/lib/mongodb/ --replSet rs0 BException in initAndListen, terminating","attr":{"error":"DBPathInUse: Unable to lock the lock file: /var/lib/mongodb/mongod.lock (Resource temporarily unavailable). Another mongod instance is already running on the /var/lib/mongodb/ directory"}}
使用新目錄:
mongod --port 27017 --dbpath /media/msc/MONGO/rs/ --replSet rs0 "replication":{"replSet":"rs0"},"storage":{"dbPath":"/media/msc/MONGO/rs/"}}}} {"t":{"$date":"2021-12-31T16:55:28.885+01:00"},"s":"E", "c":"CONTROL", "id":20568, "ctx":"initandlisten","msg":"Error setting up listener","attr":{"error":{"code":9001,"codeName":"SocketException","errmsg":"Address already in use"}}} mongod --port 27018 --dbpath /media/msc/MONGO/rs/ --replSet rs0 {"t":{"$date":"2021-12-31T16:57:11.762+01:00"},"s":"I", "c":"-", "id":4939300, "ctx":"monitoring-keys-for-HMAC","msg":"Failed to refresh key cache","attr":{"error":"NotYetInitialized: Cannot use non-local read concern until replica set is finished initializing.","nextWakeupMillis":2000}}
而這種等待“nextWakeupMillis”永遠不會停止。
您能否提供有關正確配置和問題所在的幫助?
編輯:2022 年 1 月 3 日
現在我可以使用以下方式推廣我的實例:
mongod --port 27017 --dbpath /var/lib/mongodb/ --replSet rs0
然後打開一個新的shell並發送
rs.initiate()
但是為什麼服務不會以這個新配置啟動呢?
● mongod.service - MongoDB Database Server Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled) Active: failed (Result: exit-code) since Mon 2022-01-03 11:39:51 CET; 1min 12s ago Docs: https://docs.mongodb.org/manual Process: 904 ExecStart=/usr/bin/mongod --config /etc/mongod.conf (code=exited, status=14) Main PID: 904 (code=exited, status=14) janv. 03 11:39:35 UMONGO1 systemd[1]: Started MongoDB Database Server. janv. 03 11:39:51 UMONGO1 systemd[1]: mongod.service: Main process exited, code=exited, status=14/n/a janv. 03 11:39:51 UMONGO1 systemd[1]: mongod.service: Failed with result 'exit-code'.
此外,如果我重新啟動伺服器,我必須重複這些操作。有沒有辦法修復配置並啟動 mongod 服務?
如果您使用 localhost 127.0.0.1 地址並希望擁有副本集,那麼所有(不同的)副本(mongoD 程序)必須存在於不同的埠並且具有不同的數據目錄(storage.dbPath),因為它們位於同一台機器上。所以,rs0 使用 f.ex。27007,rs1 使用 f.ex。27017,rs2 使用 f.ex。27027 然後目錄是 f.ex。/var/lib/mongodb/rs0, /var/lib/mongodb/rs1…
但是,如果您使用多台機器建構副本集,那麼
- 不能使用localhost地址,需要綁定網卡IP
- 每個節點的埠可以是 27017
對於初學者來說,當 MongoDB 文件沒有明確強調配置文件設置和命令行選項時,通常會感到困惑。
當您將 MongoDB 作為服務執行時,您必須
/etc/mongod.conf
像這樣編輯配置文件# mongod.conf # Where and how to store data. storage: dbPath: /var/lib/mongodb journal: enabled: true # where to write logging data. systemLog: destination: file logAppend: true path: /var/log/mongodb/mongod.log # network interfaces net: port: 27017 bindIp: 127.0.0.1 # how the process runs processManagement: timeZoneInfo: /usr/share/zoneinfo ### THIS ONE ### replication: replSetName: rs0