I am new to MongoDB and searching for several hours how to promote my mongo standalone instance into a replicaset. My actual environnement is one ubuntu server
root@UMONGO1:/home/msc# lsb_release -a
Distributor ID: Ubuntu
Description: Ubuntu 20.04.3 LTS
Release: 20.04
Codename: focal
running a standalone mongodb instance localy on port 27017:
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:
I have imported a database from here The goal is to create two more servers and replicate the dbs on them.
Reffering to the documentation:
Convert a Standalone to a Replica Set I am confused about the two parameters --port and --dbpath that I have to use. If I want to promote my actual instance I should use the existing database path and port, right ? But whatever I am trying it's a failure.
Using another port:
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"}}
Using a new 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}}
and this wait "nextWakeupMillis" never stop.
Can you provide help on which is the right configuration and the problem is ?
****************** EDIT : 01/03/2022 ******************
Now I am able to promote my instance using :
mongod --port 27017 --dbpath /var/lib/mongodb/ --replSet rs0
and then opening a new shell and send
rs.initiate()
But why the service won't start with this new configuration ?
● 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'.
Moreover if I restart the server I have to repeat these actions. Is there any way to fix the configuration and start mongod service ?
--replSet rs0
(i.e.replication.replSetName
) in your config file/etc/mongod.conf
. See Configuration File Settings and Command-Line Options Mapping