45

I have Docker installed and am running a MongoDB container for my local development on my Mac. The problem is that I can't connect to said DB easily from CLI. I have Robo 3T installed, but I would prefer to use the CLI client instead.

Is there a known way to install JUST the mongo shell (command mongo) and not the full DB distribution on OS X?

2

4 Answers 4

57

You can use HomeBrew, see the official MongoDB HomeBrew page.

Just add the tap:

brew tap mongodb/brew

And then after adding the tap from above, you can then install the Mongo shell with:

brew install mongodb-community-shell
1
  • 8
    brew install mongosh. The "mongo" shell has been superseded by "mongosh" (c) Commented Dec 6, 2021 at 11:59
11

You can download the latest Mongo Shell for MacOS from the following URL:

https://downloads.mongodb.org/osx/mongodb-shell-osx-ssl-x86_64-3.6.2.tgz

For the sake of completion, the Mongo Shell for Windows is available at https://downloads.mongodb.org/win32/mongodb-win32-x86_64-2008plus-ssl-3.6.2-signed.msi

Furthermore, for other Linux distributions, I suggest you create a MongoDB Atlas cluster (for instance, a free M0 cluster) at www.mongodb.com/atlas where you'll find the Mongo Shell downloads for all OSes in the Connect dialog of your cluster.

I hope this helps.

5
  • O.P operating systems environment is Mac-os-x. How he will install 'mongo shell' in that environment. And how will be set the default port 27017 there. Commented Jan 29, 2018 at 11:42
  • @MdHaidarAliKhan The MacOS tarball (tgz) link in this answer is the minimal download asked for in the question: a ~10MB download which only includes the mongo 3.6.2 shell binary and the accompanying license notices. There really isn't an install step aside from unpacking the tgz file (double-click to open) and running bin/mongo to connect to an existing MongoDB deployment with appropriate connection parameters. An alternative would be to download the full MacOS distribution (~90MB) and only run the bin/mongo command. That would leave other tools like mongodump available if needed.
    – Stennie
    Commented Jan 31, 2018 at 9:58
  • Thanks Rahael. Very useful answer. Just what I was looking for. Commented Jul 10, 2018 at 11:13
  • @Md Haidar Ali Khan no need to set the port, 27017 is the default. You only need to set it if the server is different.
    – mckenzm
    Commented Mar 15, 2019 at 5:13
  • newer version: fastdl.mongodb.org/osx/mongodb-shell-macos-x86_64-4.2.2.tgz
    – Amos
    Commented Jan 26, 2020 at 15:34
1

I understand you have mongo running on Docker, so as a workaround you can use the same running container to connect:

docker exec -ti $container_name bash
bash-4.2$ mongo -u $MONGODB_USER -p $MONGODB_PASSWORD $MONGODB_DATABASE
MongoDB shell version v3.6.3
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.3

Ideally you could connect with just one command but I don't know why is failing when calling from outside:

# this fail but you can use with other databases
docker exec -ti $container_name mongo -u $MONGODB_USER -p $MONGODB_PASSWORD $MONGODB_DATABASE

The idea is simple, mongo is installed on the container so you tell docker to run from inside the container. If you need to load files from outside you always can mount a volume.

1
  • this should be the accepted answer. why install tools when it is already there inside container Commented Aug 11, 2021 at 6:10
0

I don't think so in macOS whitout installation of MongoDB ,you can able to connect mongo shell. To install MongoDB Community Edition on macOS system Install MongoDB Community Edition on macOS You may download MongoDB Community Edition through either the MongoDB Download Center or the popular macOS package manager Homebrew.

As The mongo Shell is an interactive JavaScript interface to MongoDB.

Note: Ensure that MongoDB is running before attempting to start the mongo shell.

For your further ref Here and Here

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.