How to start a mongo database with authentication using docker container ….

Anuradh
2 min readMay 19, 2019

--

prerequisites ..

  • Some knowledge in docker.
  • Some knowledge in mongoDB.

— — — — — — — — — — — — — @@@@@@@— — — — — — — — — — — —

Let’s Begin.

1. Create a mongodb docker container using mongo image

docker container run --name mydatabase --publish 27017:27017 -d mongo

2. Get access into running mongo container bash.

docker container exec -it mydatabase bash

(this will login you to the bash)

3. Then run mongo by typing following command. it will start the mongo shell.

mongo

4. Create you desired database (ex: mydbone) with following command.

use mydbone

5. Then create a user to grant privileges to your database.

db.createUser({ user: "username", pwd: "password", roles: [] })

this will create a user for your database and to see the users type show users on terminal. for more info : mongodb official

6. Exit from the mongo shell by typing exit command. now you are on the bash.

exit

7. Now enable authentication to created database by typing following command on the bash.

mongo --port 27017 -u username -p password --authenticationDatabase mydbone

now you should be able to connect to the mydbone database with the given username and password.

for a real world use with Nodejs, refer this article : mongodb with authentication using Nodejs

--

--