42

I'm trying to run Mongo from the Command-Line: What's wrong? (I've IIS on localhost:80). And Apache on port 8080. Are there any issues with this?

C:\MONGO\Project1\mongo\bin>mongo --port 27017
MongoDB shell version: 2.0.3
connecting to: 127.0.0.1:27017/test
Sat Mar 10 16:16:45 Error: couldn't connect to server 127.0.0.1:27017 shell/mong
o.js:86
exception: connect failed
5
  • 1
    Check if there is something using that port. in cmd: netstat -ano | findstr 27017 Mar 10, 2012 at 15:30
  • 1
    Hey I came across the same problem and solved it by removing .lock file from \data\db folder and everything worked fine from there ! :) Apr 23, 2013 at 10:24
  • @Muhammad Riyaz how did you remove the lock file on windows 8 im trying \data\db\mongod.lock and it's just asking how i want to open the file.
    – Phil Famme
    Dec 15, 2013 at 8:20
  • @phil i just deleted the .lock file :) to be safe just cut and paste this file somewhere else and check if mongodb works. If it does (i m sure it will) you can dekete this .lock file Dec 15, 2013 at 11:48
  • A lot of the answers are plain confusing or obsolete. Go to the MSDN article - code.msdn.microsoft.com/Mongo-Database-setup-on-6963f46f
    – Sayan
    Feb 9, 2018 at 17:54

11 Answers 11

56

I found that when I got this error it wasn't because I didn't have my default db path set up. It was because I was trying to run mongo.exe before running mongod.exe.

3
  • 3
    this is indeed great advice.
    – Woot4Moo
    Apr 22, 2013 at 0:39
  • 1
    This should be the actual answer! That change in one letter is all that is needed. When searching for the exe, cmd auto completes to mongo.exe when tab is pressed. :D Apr 3, 2014 at 5:56
  • 2
    Fixed either - why official docs not telling about this with bold text =) Apr 30, 2015 at 20:17
32

Did you create the default db path?
It defaults to "/data/db directory (or c:\data\db on Windows)"
Source: http://www.mongodb.org/display/DOCS/Starting+and+Stopping+Mongo

4
  • 1
    C:\data\db - there is the path.. I thought it would create a path in the subfolder of the project, actually that I find a much nicer solution.
    – marko
    Mar 10, 2012 at 17:11
  • 1
    You can use a custom path, here's mine : mongod --dbpath /Mongo/db. This starts mongo in c:\mongo and the db is in c:\mongo\db
    – JHN
    Mar 10, 2012 at 19:38
  • What about when creating a web application. What about security and paths?
    – marko
    Mar 11, 2012 at 9:59
  • I installed mongo in e: so I needed to create e:/data/db
    – Neil
    Mar 31, 2015 at 14:27
26
  1. As Admin, create directory:

    mkdir c:\mongo\data\db
    
  2. As Admin, install service:

    .\mongod.exe --install --logpath c:\mongo\logs --logappend --bind_ip 127.0.0.1 --dbpath c:\mongo\data\db --directoryperdb
    
  3. Start MongoDB:

    net start MongoDB
    
  4. Start Mongo Shell:

    c:\mongo\bin\mongo.exe
    
1
  • Very useful. Thanks.For the people trying this solution, notice that the "As Admin" is very important ;) Jul 18, 2017 at 10:06
9

Follow

  1. Create default db folder.

    c:\data\db

    and also log folder

    c:\data\log\mongo.log

    or use following commands in command-prompt

    mkdir c:\data\log    
    mkdir c:\data\db
    
  2. Create config file in bin folder of mongo (or you may in save your desired destination).

    Add following in text file named "mongod" and save it as
    mongod.cfg
    dbpath=c:\data\db
    logpath=c:\data\log\mongo.log

    or use following commands in command-prompt

    echo dbpath=c:\data\db>> "mongod.cfg"
    echo logpath=c:\data\log\mongo.log>> "mongod.cfg"
    
  3. Now open command-prompt (administrator) and run the following command to start mongo server

    mongod
    
  4. Open another command-prompt (don't close 1st prompt) and run client command:

    mongo
    

Hope this will help or you have done this already.

2
  • +1 For point 4. that allows you to monitor what is actually happening if you complete 1., 2. and 3, with mongo as a service Oct 31, 2019 at 15:08
  • Should we type mongod in command prompt every time we want to start mongo server?
    – bdemirka
    Jan 10, 2021 at 14:02
5

The error occurs when trying to run mongo.exe WITHOUT having executed mongod.exe. The following batch script solved the problem:

@echo off
cd C:\mongodb\bin\
start mongod.exe
start mongo.exe
exit
0
4

If you are getting these type of errors when running mongod from command line or running mongodb server, enter image description here
then follow these steps,

  1. Create db and log directories in C: drive
    C:/data/db and C:data/log
  2. Create an empty log file in log dir named mongo.log
  3. Run mongod from command line to run the mongodb server or create a batch file on desktop which can run the mongod.exe file from your mongodb installation direction. That way you just have to click the batch file from your desktop and mongodb will start.
  4. If you have 32-bit system, try using --journal with mongod command.
2
  • This answer helped for me quick. "try using --journal with mongod command." May 31, 2017 at 20:27
  • I had previous version of mongodb 3.0 and after installation version 3.6 I had the same problem. Resolved by removing everything inside c:/data/log and run mongod
    – Oleg M
    Jan 9, 2018 at 19:47
2

Create default db folder.

c:\data\db

and also log folder

c:\data\log\mongo.log

or use following commands in command-prompt

mkdir c:\data\log    
mkdir c:\data\db
1

you can use below command,

mongod --dbpath=D:\home\mongodata

where D:\home\mongodata is the data storage path

1
  1. Go to C:\Program Files\MongoDB\Server\3.4\bin using cmd and write mongod.
  2. Open another cmd by right click and run as admin point to your monogodb installed directory as mentioned above and then just like write this mongo.exe
  3. After that, write db.test.save({Field:'Hello mongodb'}) this command will insert a field having name Field and value is Hello mongodb.
  4. After, check the record db.test.find() and press enter you will find the record that you have recently entered.
1
  • I.e. you have to run mongod to start server and mongo to run client that will use a server.
    – FelikZ
    Nov 15, 2017 at 13:30
1

Steps to start a certain local MongoDB instance and to connect to in from NodeJS app:

  1. Create mongod.cfg for a new database using the path C:\Program Files\MongoDB\Server\4.0\mongod.cfg with the content

    systemLog:
      destination: file
      path: C:\Program Files\MongoDB\Server\4.0\log\mongod.log
    storage:
      dbPath: C:\Program Files\MongoDB\Server\4.0\data\db
    
  2. Install mongoDB database by running

    mongod.exe --config "C:\Program Files\MongoDB\Server\4.0\mongod.cfg" --install

  3. Run a particular mongoDB database

    mongod.exe --config "C:\Program Files\MongoDB\Server\4.0\mongod.cfg"

  4. Run mongoDB service

    mongo 127.0.0.1:27017/db
    

    and !see mongoDB actual connection string to coonect to the service from NodeJS app

    MongoDB shell version v4.0.9
    connecting to: mongodb://127.0.0.1:27017/db?gssapiServiceName=mongodb
    Implicit session: session { "id" : UUID("c7ed5ab4-c64e-4bb8-aad0-ab4736406c03") }
    MongoDB server version: 4.0.9
    Server has startup warnings:
    ...
    
0

For this error, if you are using windows 7 or windows server 2008 R2, the problem could be that you have to install a microsoft hotfix.

Refer to this link: https://support.microsoft.com/en-us/kb/2731284

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.