Error: couldn't connect to server

OS: Mac OSX 10.14.6
After following the course’s instructions failthfully, I like many, get the following:

Screen Shot 2019-12-21 at 6.19.22 PM

When I try

mongo --nodb

I get:

Screen Shot 2019-12-21 at 6.23.32 PM

I do have a .mongorc.js file and my permissions are:

Screen Shot 2019-12-21 at 6.24.57 PM

I also tried opening a new terminal and tried

`mongod --repair`

Screen Shot 2019-12-21 at 6.34.36 PM
Screen Shot 2019-12-21 at 6.35.36 PM

and that results in no change. I’ve checked other posts and stack overflow and I’m stuck. Please Help. Thanks!

in a new terminal I enter:

mongo --nodb --norc

and I the Mongo shell opens, but doesn’t show any db’s. What do I do about the mongorc.js file, delete it or keep it? What can I do to get it to connect with just mongo? What are the things I need to fix? I don’t know if this result is correct and I should have or not have dbs.

Screen Shot 2019-12-21 at 6.56.32 PM


I deleted the .mongorc.js file:

sudo rm .mongorc.js

and now when I run

`mongo --nodb`

IScreen Shot 2019-12-21 at 8.00.01 PM

I think I am on the right track, is there anything else that seems incorrect or am I set?

As per your first snapshot .mongorc.js is not having execute permissions
I think giving exec permissions would have solved
–norc is used to prevent .mongorc.js from being loaded
If you have removed the file is it not same as above?

Are you able to get show dbs working now?
You will need it to list databases for your work

Yes, I am able to run,

`show dbs`

What is/are the commands to enable permissons for the mongorc.js if I restore it?

You’re trying out a lot of admin routines without fully understanding the repercussions… slow down a bit. :slight_smile:

First of all, I would suggest that you restore the .mongorc.js file. Then show us the location of this file, i.e. the full path.

Secondly, you don’t need to use --nodb or --norc. They have specific uses. For example, you cannot show dbs if you pass the –nodb option hence the error you’re getting in one of your screenshots.

Thirdly, for this course, use the connection strings that you’re given in the course lectures/labs, i.e. from your last screenshot you look like you’re connected and ready to go.

The location of the .mongorc.js file is

MacHD>Users>kevinturney>.mongorc.js and I restored it to this location.

The contents are:

 EDITOR="subl -w"

 function line() {

function showDbs() {
   var dbs = db.getMongo().getDBs();
    var size = {};
    dbs.databases.forEach(function (x) { size[] = x.sizeOnDisk; });
    var names = (z) { return; }).sort();
    names.forEach(function (n) {
        if (size[n] > 1) {
            print(n + "\t" + size[n] / 1024 / 1024 / 1024 + "GB");
        } else {
            print(n + "\t(empty)");

function welcome() {
  print("Host: " + db.serverStatus().host );

prompt = function() {
  return db + "> ";


After restoring the file did you try show dbs from a fresh session
Can you please show output of ls -lart on that hidden file

-rw------- 1 kevinturney staff 755 Dec 24 20:58 .mongorc.js

The output when running just “mongo” is the same as before,

and unlike before when I run “mongo --nodb”, I don’t get the prompt.

And the result of mongo --nodb?

You should be getting same error as in your first snapshot
Try to chmod 700 .mongorc.js and see

chomod 700 .mongorc.js

The above command did nothing, i just had my command line prompt
“kevinturney ~ $” appear on the next line.

Typo in your command

chomod vs chmod

no typo in execution, just in the reply.

Please check permissions
ls -lart

Screen Shot 2019-12-24 at 9.53.00 PM

Note, I can connect to both my sandbox and the classes shared cluster. My goal, is that with this fresh install of 4.2, is that everything works. I am also working on a project with mongo outside of this class and there are no databases set up yet.

The file permissions are fine. It’s actually telling you the exact line where the problem originates :arrow_up: and if you remember what I mentioned about --nodb :arrow_down:

You need something like this if (typeof db == "undefined") in your welcome() function to circumvent this show dbs error.

And like I suggested earlier…

… fully comprehend how mongo and mongod work before delving into customising the mongo shell.


Hi @Revelstoke,

Post #1

Just curious to know if you made any changes other than what was suggested in the video lecture ?

I would also suggest you to not make any further changes in these files without proper consultation. Let me sync with the relevant team and I will get back to you shortly.

Shubham Ranjan
Curriculum Support Engineer

The .mongorc.js file is what’s used to configure the mongo shell on a user’s machine so it won’t harm to make changes.

To resolve the issue, it’s either @Revelstoke deletes all the code from the file, doesn’t use --nodb, or refactors the code as I hinted in my last post. To be more verbose:

function welcome() {
  if (typeof db !== 'undefined') {
    print("Host: " + db.serverStatus().host );

Hi @007_jb,

I appreciate your contribution to the community.

The post #15 was in response to :

Apparently the user added some code for customising the mongo shell which introduced this error because the code wasn’t written with the edge cases in mind.

Since the user complained that the error was introduced by following the course instruction. Clearly we do not ask to modify the .mongorc.js file in our course lecture. So, I just wanted to sync with the relevant team to ensure that we are not shipping this code with the binaries.

Hope it helps!

Shubham Ranjan
Curriculum Services Engineer