Skip to content
This repository has been archived by the owner on Apr 24, 2022. It is now read-only.

How To Setup Local MongoDB

DerekRuiz edited this page Nov 16, 2021 · 3 revisions

How To Setup Local MongoDB

Download

Navigate to Bin Folder

  • Open File Explorer
  • Navigate to the folder that the MongoDB was downloaded:

for example "C:\Program Files\MongoDB\Server\5.0\bin"

Open Command Prompt

  • Open a command prompt (open in administration mode if MongoDB is in "C:\Program Files")
  • Change directory to the folder that the MongoDB was downloaded:

for example cd "C:\Program Files\MongoDB\Server\5.0\bin"

Stopping the default DB Instance

  • Open task manager
  • Go to the processes tab
  • Find the processes called "MongoDB Database Server"
  • Click on the process and End Task

Start Local DB Instance

  • In the command prompt enter the following command
    mongod.exe --replSet rs0 --dbpath="C:\Program Files\MongoDB\Server\5.0\data"
    (the --dbpath flag is optional, this is where the database files will be saved)
  • Keep this command prompt running in the background

Create Replica Set

  • Using File Explorer, open the application called: "mongo.exe"
  • A new command window should open
  • type the following command:
    rs.initiate()
  • A message should output stating "ok": 1, and the command should start with "rs0: Primary>"
> rs.initiate()
{
    "info2" : "no configuration specified. Using a default configuration for the set",
    "me" : "tutorialkart:27017",
    "ok" : 1
}
rs0: Primary > |

Now MongoDB is running locally using replica sets.

Future Use

  • To use replica sets in the future, you only need to run the command:
    mongod.exe --replSet rs0 --dbpath="C:\Program Files\MongoDB\Server\5.0\data"
    (the --dbpath flag is optional, this is where the database files will be saved)

Tips

  • Create a .bat file in the bin folder and enter the line:
    mongod.exe --replSet rs0 --dbpath="C:\Program Files\MongoDB\Server\5.0\data"
  • Save the .bat file.
  • To run a MongoDB instance, just run that .bat file