DEV Community

loading...

Install MongoDB with Docker (in Ubuntu 18.04)

jemaloqiu profile image jemaloQiu ・2 min read
Info: This post is one of the articles in context of this post: Huawei Cloud ECS server notes

Firstly, create the mapping repository for mongodb:

  mkdir ~/docker/mongo_8301
  mkdir ~/docker/mongo_8301/db
  mkdir ~/docker/mongo_8301/config
  mkdir ~/docker/mongo_8301/log
Enter fullscreen mode Exit fullscreen mode

Then create the launch file for starting the docker container:

  touch ~/launchers/docker_mongo_8301.sh
  vi ~/launchers/docker_mongo_8301.sh
Enter fullscreen mode Exit fullscreen mode

Fill the following code into the launch file:

docker rm -f mongo_docker_8301
docker run -d -p 8301:27017 \
--name mongo_docker_8301 \
-v /home/jemaloQ/docker/mongo_8301/config:/data/configdb \
-v /home/jemaloQ/docker/mongo_8301/db:/data/db \
-v /home/jemaloQ/docker/mongo_8301/log:/data/log \
mongo --auth
Enter fullscreen mode Exit fullscreen mode

Now launch docker using cmd line: sh ~/launchers/docker_mongo_8301.sh. Wait till the end of image pull. Then check if mongo_docker_8301 is running correctly by executing docker ps -a | grep mongo.

Enter the inside of the container and execute mongo admin to login the default 'admin' database

   docker exec -it mongo_docker_8301 mongo admin
Enter fullscreen mode Exit fullscreen mode

Create an user of role administrator:

  > db.createUser({user:'admin',pwd:'JemaloAdmin', roles:[ { role:'userAdminAnyDatabase', db: 'admin'} ] })
  > exit
Enter fullscreen mode Exit fullscreen mode

Later on, I can login 'admin' as user admin:

  docker exec -it mongo_docker_8301 mongo admin
  > db.auth('admin','JemaloAdmin')
Enter fullscreen mode Exit fullscreen mode

I can create other users by specifying their info and roles. Now I create user "jemaloQ" as "dbOwner" of database "knowledge":

  > db.createUser({user:'jemaloQ',pwd:'123456', roles:[ { role:'dbOwner', db: 'knowledge'} ] })
Enter fullscreen mode Exit fullscreen mode

Snapshot of my Putty console:
Alt Text

Now try to connect the newly launched MongoDB by Python:

from pymongo import MongoClient

client = MongoClient("mongodb://jemaloQ:123456@111.22.123.117:8301")

# print all the Databases names in Mongodb, returns empty list if no database has been created 
client.list_database_names()

# connect to the 'knowledge' database, if it does not exist, this line shall create it and then connect to it
db = client['knowledge']

# get the 'test' collection of the 'knowledge' database, if it does not exist, this line shall create it
col = db.get_collection('test')

# this item shall be uploaded to my database
item_dic = {'uid': 123, 'name': 'jemaloQ'}

# a query filter for checking whether an item with same id exists or not
query_filter = {"uid": item_dic["uid"]}

# now insert the item to 'test' collection of knowledge' database
col.update( query_filter,  {'$setOnInsert': item_dic},  upsert=True)

# now, we can see the 'knowledge' database since it is no longer empty
client.list_database_names() # => ['knowledge']

Enter fullscreen mode Exit fullscreen mode

Thus, I have now implemented my MongoDB service using docker on my server.

Discussion (3)

pic
Editor guide
Collapse
wclayferguson profile image
Clay Ferguson

You can also use "docker compose" like this:

github.com/Clay-Ferguson/quantizr/...

Collapse
jemaloqiu profile image
jemaloQiu Author • Edited

Thanks a lot. It's so helpful! I will try to implement my docker services in this way.

Collapse
hanco profile image
Hanco

Thanks for sharing :DD