Could not connect to localhost:27017

Problem

I see an error about "could not connect to localhost:27017"

Solution

Port 27017 is the port that MongoDB runs on. If you see this error with MongoDB, that almost always means that MongoDB isn't running. You can test that by running:

 mongo

If that command succeeds, you'll be connected to the running MongoDB server, which means the problem is something else. If that command fails, then we've verified that the MongoDB server is down.

This error usually occurs when you shut down your computer without running vagrant halt to stop the VM safely. MongoDB has a lock file, which it uses to make sure that only one instance of MongoDB can run at a time. When the computer is shut down without running vagrant halt, the MongoDB process is immediately killed, and it doesn't have time to remove that lock file. As a result, when the VM tries to start up MongoDB again, it sees that lock file still sitting around, and assume that some other instance of MongoDB is already running, so it quits immediately. To resolve this issue, you should enter the VM, delete the lock file, and start up MongoDB again:

 vagrant sshsudo rm /edx/var/mongo/mongodb/mongod.locksudo service mongod start

Once you do that, try running mongo again, and it should succeed. If it does, try loading up Open edX again, and see if that works.

• Page: