MongoDB 是一个免费的开源跨平台面向文档的 NoSQL 数据库程序。
1、查看可用的 MongoDB 版本
访问 MongoDB 镜像库地址: https://hub.docker.com/_/mongo?tab=tags&page=1。
可以通过 Sort by 查看其他版本的 MongoDB,默认是最新版本 mongo:latest。
你也可以在下拉列表中找到其他你想要的版本:
此外,我们还可以用 docker search mongo 命令来查看可用版本:
[root@topcheer ~]# docker search mongo INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED docker.io docker.io/mongo MongoDB document databases provide high av... 6370 [OK] docker.io docker.io/mongo-express Web-based MongoDB admin interface, written... 558 [OK] docker.io docker.io/tutum/mongodb MongoDB Docker image – listens in port 270... 228 [OK] docker.io docker.io/bitnami/mongodb Bitnami MongoDB Docker Image 104 [OK] docker.io docker.io/mongoclient/mongoclient Official docker image for Mongoclient, fea... 78 [OK] docker.io docker.io/mongooseim/mongooseim Small docker image for MongooseIM - robust... 18 docker.io docker.io/frodenas/mongodb A Docker Image for MongoDB 17 [OK] docker.io docker.io/cvallance/mongo-k8s-sidecar Kubernetes side car to setup and maintain ... 14 [OK] docker.io docker.io/centos/mongodb-32-centos7 MongoDB NoSQL database server 8 docker.io docker.io/arm64v8/mongo MongoDB document databases provide high av... 6 docker.io docker.io/centos/mongodb-26-centos7 MongoDB NoSQL database server 5 docker.io docker.io/circleci/mongo CircleCI images for MongoDB 5 [OK] docker.io docker.io/istepanov/mongodump Docker image with mongodump running as a c... 5 [OK] docker.io docker.io/centos/mongodb-36-centos7 MongoDB NoSQL database server 4 docker.io docker.io/eses/mongodb_exporter mongodb exporter for prometheus 4 [OK] docker.io docker.io/webhippie/mongodb Docker images for MongoDB 4 [OK] docker.io docker.io/centos/mongodb-34-centos7 MongoDB NoSQL database server 2 docker.io docker.io/neowaylabs/mongodb-mms-agent This Docker image with MongoDB Monitoring ... 2 [OK] docker.io docker.io/ansibleplaybookbundle/mongodb-apb An APB to deploy MongoDB. 1 [OK] docker.io docker.io/ekesken/mongo docker image for mongo that is configurabl... 1 [OK] docker.io docker.io/openshift/mongodb-24-centos7 DEPRECATED: A Centos7 based MongoDB v2.4 i... 1 docker.io docker.io/andreasleicher/mongo-azure-backup a docker container to backup a mongodb usi... 0 [OK] docker.io docker.io/ccitest/mongo CircleCI test images for Mongo 0 [OK] docker.io docker.io/fuww/mongo-connector mongo-connector + alpine + docker 0 [OK] docker.io docker.io/targetprocess/mongodb_exporter MongoDB exporter for prometheus 0 [OK] [root@topcheer ~]#
2、取最新版的 MongoDB 镜像
这里我们拉取官方的最新版本的镜像:
[root@topcheer ~]# docker pull mongo:latest Trying to pull repository docker.io/library/mongo ... latest: Pulling from docker.io/library/mongo 7ddbc47eeb70: Pull complete c1bbdc448b72: Pull complete 8c3b70e39044: Pull complete 45d437916d57: Pull complete e119fb0e0a55: Pull complete 91f0b9bae1ea: Pull complete 53e7c2967f11: Pull complete 69a945568374: Pull complete 93333bc225a7: Pull complete b9c10bd6c9bd: Pull complete 7f4e3538e99c: Pull complete 1164b51d180a: Pull complete a715a7d71f27: Pull complete Digest: sha256:1a9478d8188d6be31dd2e8de076d402edf20446e54933aad7ff49f5b457d486c Status: Downloaded newer image for docker.io/mongo:latest 您在 /var/spool/mail/root 中有新邮件 [root@topcheer ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE docker.io/mongo latest 965553e202a4 3 weeks ago 363 MB docker.io/rabbitmq management 4b23cfb64730 5 weeks ago 180 MB docker.io/sonatype/nexus3 latest 8eb898be2a53 6 weeks ago 611 MB docker.io/redis latest 01a52b3b5cd1 8 weeks ago 98.2 MB docker.io/nginx latest f949e7d76d63 2 months ago 126 MB wgr/centos latest fb7e3d506043 2 months ago 202 MB mytomcat9 latest 6c243064a028 2 months ago 749 MB myip 1.2 00a0a1f80e36 2 months ago 271 MB myip latest 420c99c3b707 2 months ago 271 MB mycentosfile 1.1 f022cd7b9017 2 months ago 395 MB topcher/tomcat 1.0.1 3d8737216a1e 2 months ago 508 MB docker.io/tomcat latest 8973f493aa0a 2 months ago 508 MB docker.io/mysql latest b8fd9553f1f0 2 months ago 445 MB docker.io/centos latest 67fa590cfc1c 3 months ago 202 MB docker.io/portainer/portainer latest 2b4ddf654e1c 4 months ago 77.7 MB docker.io/hello-world latest fce289e99eb9 10 months ago 1.84 kB docker.elastic.co/logstash/logstash 6.4.3 69c8a126e24a 12 months ago 674 MB docker.elastic.co/kibana/kibana 6.4.3 69271c2e80d6 12 months ago 724 MB docker.elastic.co/elasticsearch/elasticsearch 6.4.3 01e5bee1e059 12 months ago 795 MB
3 运行容器
安装完成后,我们可以使用以下命令来运行 mongo 容器:
[root@topcheer ~]# docker run -itd --name mongo -p 27017:27017 mongo --auth WARNING: IPv4 forwarding is disabled. Networking will not work. 800f53908b52ed76e31a191bd1885a7eec3b07c46554293a15712dabaecfd45c [root@topcheer ~]# docker ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 800f53908b52 mongo "docker-entrypoint..." 11 seconds ago Up 9 seconds 0.0.0.0:27017->27017/tcp mongo [root@topcheer ~]# docker ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 800f53908b52 mongo "docker-entrypoint..." 13 seconds ago Up 11 seconds 0.0.0.0:27017->27017/tcp mongo [root@topcheer ~]# docker ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 800f53908b52 mongo "docker-entrypoint..." 14 seconds ago Up 12 seconds 0.0.0.0:27017->27017/tcp mongo [root@topcheer ~]# docker ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 800f53908b52 mongo "docker-entrypoint..." 16 seconds ago Up 14 seconds 0.0.0.0:27017->27017/tcp mongo
参数说明:
- -p 27017:27017 :映射容器服务的 27017 端口到宿主机的 27017 端口。外部可以直接通过 宿主机ip:3306 访问到 MySQL 的服务。
- --auth:需要密码才能访问容器服务。
4、安装成功
# 创建一个名为 admin,密码为 123456 的用户。
# 尝试使用上面创建的用户信息进行连接。
root@topcheer ~]# docker exec -it mongo mongo admin MongoDB shell version v4.2.1 connecting to: mongodb://127.0.0.1:27017/admin?compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("032dda34-c626-401e-8003-94e6680cbbe0") } MongoDB server version: 4.2.1 Welcome 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 > db.createUser({ user:'admin',pwd:'123456',roles:[ { role:'userAdminAnyDatabase', db: 'admin'}]}); Successfully added user: { "user" : "admin", "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ] } > db.auth('admin', '123456') 1 > exit bye [root@topcheer ~]#