Secure your MongoDB Deployment¶
Author: MongoDB Documentation Team
This guide describes how to enforce authentication on your local MongoDB deployment.
Time required: 10 minutes
What You’ll Need¶
- A local installation of MongoDB. See Install MongoDB
- The
mongo
shell. See the mongo shell section of Connect to MongoDB
Check Your Environment¶
Ensure that your MongoDB instance is running.
- Windows
- macOS
- Linux
To make sure that your MongoDB instance is running on Windows, run the following command from the Windows command prompt:
If a mongod.exe
instance is running, you will
see something like:
To make sure your MongoDB instance is running on mac, run the following command from your terminal:
If a mongod
instance is running, you will see
something like:
To make sure your MongoDB instance is running on linux, run the following command from your terminal:
If a mongod
instance is running, you will see
something like:
Procedure¶
Locate the mongo
shell.¶
The mongo
shell is packaged with the MongoDB Server
Community and Enterprise distributions, and is also available for users
of Atlas as a client-only download.
MongoDB binaries are located in a directory that starts with
mongodb-
. Within a bin
directory, you should see a file named
mongo
, which is the shell executable.
If you do not have mongo
shell installed, follow the
install directions for your environment.
- Windows
- macOS
- Linux
Download the latest stable version for your environment.
After downloading, click on the downloaded .msi
file. The
Windows Installer will guide you through the installation.
Download the latest stable version for your environment.
Double click the tgz
file to untar the file.
Download the latest stable version for your environment.
Extract the tar file and locate the mongo
executable under
the bin
directory of your install root.
Connect to your MongoDB instance¶
Select the operating system platform on which you are running the MongoDB client you have selected.
Note
If you are running your mongod
instance with the default host
(localhost)
and port
(27017), you can leave those parameters out when running
mongo
shell.
- Windows
- macOS
- Linux
Switch to the admin
database.¶
Verify that you have successfully added your user.¶
Run show users
to see if your user was created:
You should see output similar to the following:
Restart your MongoDB instance with access control.¶
- Windows
- macOS
- Linux
To restart MongoDB, run mongod.exe
with the
--auth
option.
This starts the main MongoDB database process. The waiting for
connections
message in the console output indicates that the
mongod.exe
process is running successfully.
To restart MongoDB with access control, run the mongod
process from your terminal with the --auth
option. The mongod
process is located in a bin
folder in the MongoDB installation directory.
If you used the default data directory for your MongoDB deployment,
(i.e., /data/db
), you can leave off the
--dbpath
option.
If your mongod
instance has successfully started, you
will see logging output in your terminal that includes
[initandlisten] waiting for connections
.
Note
The following instructions assume that you installed MongoDB
from a tar.gz
archive rather than using a package
manager. If you used the package manager for your Linux
distribution to install MongoDB, edit your
configuration file to include
the security.authorization
setting before starting
your mongod
service as usual. Refer to the
configuration file
documentation for more information.
To restart MongoDB with access control, run the mongod
process from your terminal with the --auth
option. The mongod
process is located in a bin
folder in the MongoDB installation directory.
If you used the default data directory for your MongoDB deployment,
(i.e., /data/db
), you can leave off the
--dbpath
option.
If your mongod
instance has successfully started, you
will see logging output in your terminal that includes
[initandlisten] waiting for connections
.
Summary¶
If you have successfully completed this guide you have enabled basic authentication on your local MongoDB instance.
What’s Next¶
The next guide walks you through connecting to your new MongoDB instance.