Deploy MongoDB as a service - ARA-Trans/iAM GitHub Wiki

Installing Mongodb as a service (in a replica set mode) on the server

  • Download the msi file from https://www.mongodb.com/try/download/community and download the (Current) version (not development or later)
  • Install mongodb, follow the instructions, but deselect Install as a service checkbox. We ll do it manually
  • Finish the installation. Do not install 'Mongo compass' unless you intend to use that as your Mongo management UI
  • Go to the path where MongoDb has been installed mostly under 'C:\Program Files\MongoDB\Server<version>'
  • In the above mentioned directory, create log folder and data folder
  • Create the data path (example: 'C:\data\db').
  • Open the mongod.cfg and change the dbPath setting to the path created in the last step. Also change the Path variable under systemLog to point to a location where the log file will be stored.
  • Open command prompt in admin mode and go to the path C:\Program Files\MongoDB\Server\4.0\bin
  • Now, run the following command - mongod --logpath="C:\Program Files\MongoDB\Server\4.0\log\mongod.log" --install --serviceUser mongodb --replSet r1
  • You can set up the config file and give it's path instead of passing logPath or dbPath. Example - mongod --install --config "configurationFileLocation\mongod.cfg" --serviceUser mongodb --servicePassword mypassword --replSet r1
  • After running the above command, you can check mongo service with sc qc mongodb utility
  • To stop the service use command net stop mongodb
  • To remove the service use command mongod --remove
  • The config file can look something like this -
# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# Where and how to store data.
storage:
  dbPath: C:\data\db
  journal:
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path:  C:\Program Files\MongoDB\Server\4.0\log\mongod.log

# network interfaces
net:
  port: 27017
  bindIp: 127.0.0.1


#processManagement:

#security:

#operationProfiling:

#replication:

#sharding:

## Enterprise-Only Options:

#auditLog:

#snmp:
  • This file is in yaml format. So, TABs for indentation will not work. Only spaces will work
  • In the above command, the user is mongodb, logpath is required and --replSet is to start the service as a replica set.
  • Now open task manager as an admin -> go to Services Tab and search for MongoDb.
  • Right click on MongoDb and select open services. A new window will open. Search for MongoDB and double click.
  • A pop-up will open. Click on Log On Tab. Select Local System account.
  • Now start the service.
  • The MongoDB as a service is now running.
  • You can check that by heading back to the command prompt. Under the path C:\Program Files\MongoDB\Server\4.0\bin. Run mongo
  • This will open up a mongo shell
  • Use command rs.initiate() to initiate the replica set
  • To add a user in MongoDb from shell:
    • First open the shell by using command mongo
    • Then use
db.createUser(
{
... user: "admin",
... pwd: "your password",
... roles: [
... {role: "read", db: "BridgeCare"},
... {role: "readWrite", db: "BridgeCare"}
... ]
... }
)

Connect to the mongodb on a remote network:

  • change bindIp: 0.0.0.0 - This means, the mongodb on the remote network is accepting connections from all of the IPs.
  • Instead of 0.0.0.0, we can specify which Ips it should listen to. The multiple ip addresses should be comma separated
  • Once bindIP is changed, restart the service net start mongodb
  • Now, from another machine, which is not on the same network where MongoDB exists, open command shell
  • and navigate to C:\Program Files\MongoDB\Server\4.0\bin>
  • Now, enter the command mongo -u <user_for_the_database> -p <password_to_access_database> 40.121.5.125:27017/<databaseName>

Restoring an existing database to a new server:

  • Download the data to a folder called dump in the Mongo bin directory
  • Use the command mongorestore with no parameters to restore all data in the bin\dump directory
  • After restoring, delete the networks and scenarios collections. These are pulled by the app from the SQL database.

Backing up the existing database(es) from a server:

  • Use the command mongodump with no parameters to create a backup of the data. If no dump directory exists, it will be created in the bin folder
⚠️ **GitHub.com Fallback** ⚠️