Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use MongoDB in Docker

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you how to use MongoDB in Docker. The content is concise and easy to understand. It will definitely make your eyes shine. I hope you can gain something from the detailed introduction of this article.

1. Search mirror $ docker search mongoNAME DESCRIPTION STARS OFFICIAL AUTOMATEDmongo MongoDB document databases provide high avai… 6073 [OK]mongo-express Web-based MongoDB admin interface, written w… 493 [OK]2. pull images from the nearest region

MongoDB provides official images. Download and install images as follows:

$ docker pull mongo3. View mirror $ docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEmongo latest f7adfc4dbcf5 29 hours ago 413MB4. Start Mirror $ docker run \-p :27017 -v :/data/db \-v :/data/configdb \--configsvr \-v :/data/backup--name docker_mongodb \-d mongo \-- auth

-p Specifies the port mapping of the container, mongodb default port is 27017

-v :/data/db is the mount directory of the container data. Here, the directory in the local computer is mounted to/data/db in the container as the storage directory of mongodb.

-v :/data/configdb -- configsvr When we need to modify the configuration file, we just need to create a mongodb.conf file on the host and map the folder where the file is located to the container's/data/configdb folder. At the same time, add the--configsvr parameter to the container's startup command.

-v :/data/backup is the mount directory of the container backup. Here, the directory in the local computer is mounted to/data/backup in the container as the backup directory of mongodb.

--name is the name of the container

-d Sets container to run as a daemon

--auth If you need verification, add--auth. If you don't need verification, remove it. The default mongodb is not to use user authentication

5. View progress $ docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES1eafdfe92c89 mongo "docker-entrypoint.s…" 5 seconds ago Up 4 seconds 0.0.0.0:27017->27017/tcp docker_mongodb6. 6.1 Enter mongo--Linux$ docker exec --it 1eafdfe92c89 mongo admin--Windows$ docker exec --it 1eafdfe92c89 mongo amdinthe input device is not a TTY. If you are using mintty, try fixing the command with 'winpty'>>> to $ winpty docker exec -it 1eafdfe92c89 mongo amdinMongoDB shell version v4.0.11connecting to: mongodb://127.0.0.1:27017/amdin? gssapiServiceName=mongodbImplicit session: session { "id" : UUID("90590eee-7f0f-4336-844b-0ebcd267902e") }MongoDB server version: 4.0.11Welcome to the MongoDB shell.For interactive help, type "help".For more comprehensive documentation, see http://docs.mongodb.org/Questions? Try the support group http://groups.google.com/group/mongodb-user>6.2 Create User> db.createUser({user:"root",pwd:"root",roles:[{role:'root',db:'admin'}]})Successfully added user: { "user" : "root", "roles" : [ { "role" : "root", "db" : "admin" } ]}> exitbye

Exit mongo

7 Data backup $ docker exec -it /bin/bash$ mongodump -h ip -d Database name-o Address of container storing backup data For example: root@123456:/# mongodump -h 127.0.0.1-d lyb -o /var/test/test.bat2019-07-31T05:15:24.585+0000 writing lyb.ali_access_token_log to 2019-07-31T05:15:24.585+0000 writing lyb.nls_log to 2019-07-31T05:15:24.586+0000 done dumping lyb.ali_access_token_log (17 documents)2019-07-31T05:15:24.586+0000 done dumping lyb.nls_log (1 document)root@123456:/# cd /var/test/root@123456:/var/test#lstest.bat The above content is how to use MongoDB in Docker. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserves, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report