1

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 ?

4
  • I am shutting down the instance before trying the command
    – Mickael
    Jan 1, 2022 at 15:07
  • You need to put --replSet rs0 (i.e. replication.replSetName) in your config file /etc/mongod.conf. See Configuration File Settings and Command-Line Options Mapping Jan 3, 2022 at 14:25
  • If you are using systemd (systemctl) to run mongod, you must create pid-file (processManagement.pidFilePath -at mongod.conf). If there is no pid-file, systemd will kill the process about 5 seconds after it's start. And EVERY instance must have it's own pid file (like own dbPath, logFile, ...) nothing can be "shared" between instances.
    – JJussi
    Jan 4, 2022 at 11:29
  • adding #pid file processManagement: pidFilePath: /var/run/mongodb.pid replication: replSetName: rs0 do not change nothing
    – Mickael
    Jan 4, 2022 at 14:04

5 Answers 5

3

For beginners it is often confusing when the MongoDB documentation does not clearly emphases Configuration File Settings and Command-Line Options.

When you run MongoDB as service, then you must edit your config file /etc/mongod.conf like this

# 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
2

If you use localhost 127.0.0.1 address and want to have replica set, then all (different) replicas (mongoD processes) must exist at different ports AND have different data directories (storage.dbPath), because they are at the same machine. So, rs0 uses f.ex. 27007, rs1 uses f.ex. 27017, rs2 uses f.ex. 27027 and then directories are f.ex. /var/lib/mongodb/rs0, /var/lib/mongodb/rs1...

However, if you build your replica set with multiple machines, then

  1. you cannot use localhost address, you need to bind to the network card IP
  2. port can be 27017 at every node
4
  • I do not want to use the same machine, I want to build a cluster with 3 machines, each one hosting replicated datas.
    – Mickael
    Jan 1, 2022 at 15:01
  • I have set up an internal network between my VMs, they are all able to communicate on it. ens34: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 10.0.0.8 netmask 255.0.0.0 broadcast 10.255.255.255
    – Mickael
    Jan 1, 2022 at 15:17
  • And you still have a problem to get replica set running?
    – JJussi
    Jan 2, 2022 at 16:10
  • @JJusi yes, see my edit
    – Mickael
    Jan 3, 2022 at 10:49
1

It appears there is already a mongod running.

Look at your error messages

"ctx":"initandlisten","msg":"Error setting up listener","attr":{"error":{"code":9001,"codeName":"SocketException","errmsg":"Address already in use"}}}

then you got

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"}}

then

"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"}}}

Steps 1,2 in the Documentation says this

  1. Shut down the standalone mongod instance.
  2. Restart the instance. Use the --replSet option to specify the name of the new replica set.

Verify that mongod is running with this

ps -ef | grep -v grep | grep mongod

So log into mongo

mongo --port 27017

and run this to shut it down

use admin
db.shutdownServer()

Then, you should be able to start it with

mongod --port 27017 --dbpath /var/lib/mongodb/ --replSet rs0

since port 27017 should be free to use

UPDATE 2022-01-01 11:54 EST

I just spun up a vagrant instance and tried it

mongod --port 27017 --dbpath /var/lib/mongodb --logpath /var/log/mongodb.log --fork --replSet rs0

Here is Vagrant

C:\MongoDBClasses\M312 Diagnostics and Debugging>cd m312-vagrant-env

C:\MongoDBClasses\M312 Diagnostics and Debugging\m312-vagrant-env>vagrant up
Bringing machine 'm312' up with 'virtualbox' provider...
==> m312: Clearing any previously set forwarded ports...
==> m312: Clearing any previously set network interfaces...
==> m312: Preparing network interfaces based on configuration...
    m312: Adapter 1: nat
    m312: Adapter 2: hostonly
==> m312: Forwarding ports...
    m312: 30000 (guest) => 30000 (host) (adapter 1)
    m312: 30001 (guest) => 30001 (host) (adapter 1)
    m312: 30002 (guest) => 30002 (host) (adapter 1)
    m312: 22 (guest) => 2222 (host) (adapter 1)
==> m312: Running 'pre-boot' VM customizations...
==> m312: Booting VM...
==> m312: Waiting for machine to boot. This may take a few minutes...
    m312: SSH address: 127.0.0.1:2222
    m312: SSH username: vagrant
    m312: SSH auth method: private key
==> m312: Machine booted and ready!
[m312] GuestAdditions 6.0.14 running --- OK.
==> m312: Checking for guest additions in VM...
==> m312: Setting hostname...
==> m312: Configuring and enabling network interfaces...
==> m312: Mounting shared folders...
    m312: /shared => C:/MongoDBClasses/M312 Diagnostics and Debugging/m312-vagrant-env/shared
    m312: /vagrant => C:/MongoDBClasses/M312 Diagnostics and Debugging/m312-vagrant-env
    m312: /dataset => C:/MongoDBClasses/M312 Diagnostics and Debugging/m312-vagrant-env/dataset
==> m312: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> m312: flag to force provisioning. Provisioners marked to run always will still run.

C:\MongoDBClasses\M312 Diagnostics and Debugging\m312-vagrant-env>vagrant ssh
Welcome to Ubuntu 14.04.6 LTS (GNU/Linux 3.13.0-170-generic x86_64)

 * Documentation:  https://help.ubuntu.com/

  System information as of Sat Jan  1 16:31:19 UTC 2022



Last login: Thu Dec 23 02:34:39 2021 from 10.0.2.2
Welcome to Ubuntu 14.04.6 LTS (GNU/Linux 3.13.0-170-generic x86_64)

 * Documentation:  https://help.ubuntu.com/

  System information as of Sat Jan  1 16:31:19 UTC 2022



Last login: Thu Dec 23 02:34:39 2021 from 10.0.2.2
root@m312:~#

Here is how I deployed mongod

root@m312:~# mkdir /var/lib/mongodb
root@m312:~# mongod --port 27017 --dbpath /var/lib/mongodb --logpath /var/log/mongodb.log --fork --replSet rs0
about to fork child process, waiting until server is ready for connections.
forked process: 2348
child process started successfully, parent exiting
root@m312:~#
root@m312:~# mongo
MongoDB shell version v3.4.2
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.2
Server has startup warnings:
2022-01-01T16:44:19.551+0000 I STORAGE  [initandlisten]
2022-01-01T16:44:19.551+0000 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2022-01-01T16:44:19.551+0000 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
2022-01-01T16:44:19.822+0000 I CONTROL  [initandlisten]
2022-01-01T16:44:19.822+0000 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2022-01-01T16:44:19.822+0000 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2022-01-01T16:44:19.822+0000 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2022-01-01T16:44:19.822+0000 I CONTROL  [initandlisten]
2022-01-01T16:44:19.822+0000 I CONTROL  [initandlisten]
2022-01-01T16:44:19.822+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2022-01-01T16:44:19.822+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2022-01-01T16:44:19.822+0000 I CONTROL  [initandlisten]
2022-01-01T16:44:19.822+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2022-01-01T16:44:19.822+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2022-01-01T16:44:19.822+0000 I CONTROL  [initandlisten]
MongoDB Enterprise > rs.initiate()
{
        "info2" : "no configuration specified. Using a default configuration for the set",
        "me" : "m312:27017",
        "ok" : 1
}
MongoDB Enterprise rs0:SECONDARY>
MongoDB Enterprise rs0:PRIMARY>

I suspect that the mongod.conf file is interfering with the command you are using from the documentation.

SUGGESTION : Rename the conf file

mv mongod.conf mongod.conf.bak

Then, deploy the mongod using my command or the one in the MongoDB Documentation

2
  • @@RolandoMySQLDBA renaming the conf file do not change anything, but using the parameter --fork worked. rs0:PRIMARY> show databases; admin 0.000GB config 0.000GB local 0.000GB new_york 0.004GB After that the mongo service stay inactive or fail if i triy to start it. Normal ? root@UMONGO1:/media/msc/MONGO/mongodb-database-tools-ubuntu2004-x86_64-100.5.1/bin# systemctl status mongod ● mongod.service - MongoDB Database Server Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled) Active: inactive (dead) since Sat 2022-01-01 19:50:26
    – Mickael
    Jan 1, 2022 at 18:57
  • 2
    Cannot use non-local read concern until replica set is finished initializing means rs.initiate() has not yet been successfully run.
    – Joe
    Jan 2, 2022 at 6:00
1

Finaly after hours of research/testing and thanks to severals usefull answers here, I made it to build the cluster and I am begining to understand more clearly how mongodb cluster work. Totaly forgot Ubuntu, whatever I tried the service will not start.

I have two members, MONGO2 et MONGO3 with mongo service running.

  • First step is to modify conf file of each machines :

MONGO2

# network interfaces
net:
  port: 27017
  bindIp: 10.0.0.5,127.0.0.1,0.0.0.0
  
  #replication:
replication:
  replSetName: rs0

MONGO3:

# network interfaces
net:
  port: 27017
  bindIp: 10.0.0.6,127.0.0.1,0.0.0.0
  
  #replication:
replication:
  replSetName: rs0
  • Restart services so changes will be effective.
  • Stop the service on the server who is going to be the primary (MONGO2).
  • Promote the server.

./mongod.exe --port 27017 --dbpath M:\db\data --replSet rs0 --bind_ip_all

When this error message appear:

{"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}}
  • Open a new PS console and connect to mongodb ./mongo.exe --port 27017

then rs.initiate()

  • Close everything and start mongo service.

  • Add your other server to the cluster rs.add("MONGO3:27017");

Result

rs0:PRIMARY> rs.conf()
{
        "_id" : "rs0",
        "version" : 3,
        "term" : 4,
        "members" : [
                {
                        "_id" : 0,
                        "host" : "MONGO2:27017",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "secondaryDelaySecs" : NumberLong(0),
                        "votes" : 1
                },
                {
                        "_id" : 1,
                        "host" : "MONGO3:27017",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "secondaryDelaySecs" : NumberLong(0),
                        "votes" : 1
                }
        ],
        "protocolVersion" : NumberLong(1),
        "writeConcernMajorityJournalDefault" : true,
        "settings" : {
                "chainingAllowed" : true,
                "heartbeatIntervalMillis" : 2000,
                "heartbeatTimeoutSecs" : 10,
                "electionTimeoutMillis" : 10000,
                "catchUpTimeoutMillis" : -1,
                "catchUpTakeoverDelayMillis" : 30000,
                "getLastErrorModes" : {

                },
                "getLastErrorDefaults" : {
                        "w" : 1,
                        "wtimeout" : 0
                },
                "replicaSetId" : ObjectId("61d5799d9249ea1cc550e79d")
        }
}
1
  • Usually private address like 10.0.0.5 may change at any time. And you added 0.0.0.0, so it is pointless anyway. Better use bindIpAll: true. Actually using bindIp: <list of IPs'> makes only sense on a machine with multiple network interfaces. Jan 12, 2023 at 12:27
1

After observing all the answers above I did my own reasearch and this worked for me to turn a standalone instance to 1-node replica set. First I edited my /etc/mongo.conf file with

replication:
  replSetName:rs0

Then changed the data directory /var/lib/mongodb and log directory /var/log/mongodb permissions to

sudo chmod -R go+w /var/lib/mongodb /var/log/mongodb
sudo chown -R $USER:mongodb /var/lib/mongodb /var/log/mongodb

so that I don't have to run mongod service with sudo.
Then removing the mongodb-27017.sock file from /tmp directory
Stopping the mongod service with systemctl stop mongod
To initiate the replication in mongod instance on my server I run

mongod --dbpath /var/lib/mongodb --logpath /var/log/mongodb --fork --replSet rs0

fork attribute doesn't seem to work without logpath attribute

Enter the mongosh shell and run

rs.initiate()

if it turns out to be all right then it will return 'ok':1
Then exit from the mongosh shell and terminate the mongod running instance. Now again start the mongod instance using systemctl start mongod connect the daemon with any client and you will see now 1-node replica set has been created in this instance.

1
  • Note, when you run mongod --dbpath /var/lib/mongodb --logpath /var/log/mongodb --fork --replSet rs0 then it does not read the config file /etc/mongo.conf. When you run systemctl start mongod, then the config file is used. In general I recommend not to to mix these methods. Jan 12, 2023 at 12:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.