Docker 单节点安装 Mogodb

魏晨
2023-12-01

1 搜索 Mogodb

PS C:\Users\Administrator> docker search mongodb
NAME                                                                        DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mongo                                                                       MongoDB document databases provide high avai…   8634      [OK]
mongo-express                                                               Web-based MongoDB admin interface, written w…   1148      [OK]
bitnami/mongodb                                                             Bitnami MongoDB Docker Image                    173                  [OK]
percona/percona-server-mongodb                                              Percona Server for MongoDB docker images        35
circleci/mongo                                                              CircleCI images for MongoDB                     10                   [OK]
centos/mongodb-36-centos7                                                   MongoDB NoSQL database server                   8
bitnami/mongodb-sharded                                                                                                     7
bitnami/mongodb-exporter                                                                                                    6
centos/mongodb-26-centos7                                                   MongoDB NoSQL database server                   5
centos/mongodb-34-centos7                                                   MongoDB NoSQL database server                   3
mongodbsap/mongodbdocker                                                                                                    2
ansibleplaybookbundle/mongodb-apb                                           An APB to deploy MongoDB.                       2                    [OK]
rancher/mongodb-conf                                                                                                        2
ibmcom/mongodb-ppc64le                                                                                                      1
ibmcom/mongodb                                                                                                              1
percona/mongodb_exporter                                                    A Prometheus exporter for MongoDB including …   1
rancher/mongodb-config                                                                                                      0
mongodb/mongodb-atlas-kubernetes-operator-prerelease                        This is a MongoDB Atlas Operator image built…   0
mongodbinternal/test-app                                                                                                    0
mongodbinternal/mongodb-atlas-kubernetes-bundles-prerelease                                                                 0
mongodbinternal/mongodb-atlas-kubernetes-bundles-prerelease-index-catalog                                                   0
mongodb/mongodb-atlas-kubernetes-operator                                                                                   0
ibmcom/mongodb-amd64                                                                                                        0
ibmcom/mongodb-s390x                                                                                                        0
ibmcom/mongodb-exporter-ppc64le                                                                                             0
PS C:\Users\Administrator>

2 下载 Mogodb

PS C:\Users\Administrator> docker pull mongo
Using default tag: latest
latest: Pulling from library/mongo
4d32b49e2995: Pull complete
26a89ffa9c8e: Pull complete
c6a26a1adeb9: Pull complete
0f6c4ca429ae: Pull complete
87cd51bf7ebc: Pull complete
68750eb424ec: Pull complete
008900bad1d7: Pull complete
e33eed19868f: Pull complete
e7bc3cbfdaeb: Pull complete
358eefa21051: Pull complete
Digest: sha256:ad947856db716ddd0b9cc525e341c77208ed8dafcb4a6ad23f9b3addd7a4f71c
Status: Downloaded newer image for mongo:latest
docker.io/library/mongo:latest
PS C:\Users\Administrator>

3 查看 Mogodb

PS C:\Users\Administrator> docker images
REPOSITORY           TAG       IMAGE ID       CREATED        SIZE
mongo                latest    798d1656acba   8 days ago     698MB
elasticsearch        7.17.1    515ab4fba870   4 weeks ago    618MB
kibana               7.17.1    8fbc840d074f   4 weeks ago    895MB
mysql                latest    9da615fced53   5 months ago   514MB
redis                latest    7faaec683238   5 months ago   113MB
nacos/nacos-server   latest    bdf60dc2ada3   7 months ago   1.05GB
PS C:\Users\Administrator>

4 部署 Mongodb

docker run --name mongodb -v D:\Environment\Mongodb\data:/data/db -p 27017:27017 -d mongo:latest --auth
命令参数作用
-p27017:27017指定映射容器服务的 27017 端口到宿主机的 27017 端口
-v-v D:\Environment\Mongodb\data:/data/db指定外部挂载
-dmongo:latest指定创建容器的镜像和版本
–namemongodb指定容器的名称
–auth-指定需要密码才能访问容器服务。
PS C:\Users\Administrator> docker run --name mongodb -v D:\Environment\Mongodb\data:/data/db -p 27017:27017 -d mongo:latest --auth
1c842a01d24aacf8d8ddc266056e8f134b62a8cb2e601e51e688be6e2701ab3b
PS C:\Users\Administrator>

5 运行 Mongodb

PS C:\Users\Administrator> docker start mongodb
mongodb
PS C:\Users\Administrator> docker ps
CONTAINER ID   IMAGE                       COMMAND                  CREATED         STATUS          PORTS                      NAMES
1c842a01d24a   mongo:latest                "docker-entrypoint.s…"   8 minutes ago   Up 13 seconds   0.0.0.0:27017->27017/tcp   mongodb
7af5ffd056f1   nacos/nacos-server:latest   "bin/docker-startup.…"   5 months ago    Up 17 minutes   0.0.0.0:10000->8848/tcp    nacos
PS C:\Users\Administrator>

6 设置 Mongodb

6.1 为MongoDB添加管理员用户

角色字段
数据库用户角色read、readWrite;
数据库管理角色dbAdmin、dbOwner、userAdmin;
集群管理角色clusterAdmin、clusterManager、clusterMonitor、hostManager;
备份恢复角色backup、restore;
所有数据库角色readAnyDatabase、readWriteAnyDatabase、userAdminAnyDatabase、dbAdminAnyDatabase
超级用户角色root // 这里还有几个角色间接或直接提供了系统超级用户的访问(dbOwner 、userAdmin、userAdminAnyDatabase)
内部角色__system
角色说明
root只在admin数据库中可用。超级账号,超级权限
Read允许用户读取指定数据库
readWrite允许用户读写指定数据库
dbAdmin允许用户在指定数据库中执行管理函数,如索引创建、删除,查看统计或访问system.profile
userAdmin允许用户向system.users集合写入,可以找指定数据库里创建、删除和管理用户
clusterAdmin只在admin数据库中可用,赋予用户所有分片和复制集相关函数的管理权限。
readAnyDatabase只在admin数据库中可用,赋予用户所有数据库的读权限
readWriteAnyDatabase只在admin数据库中可用,赋予用户所有数据库的读写权限
userAdminAnyDatabase只在admin数据库中可用,赋予用户所有数据库的userAdmin权限
dbAdminAnyDatabase只在admin数据库中可用,赋予用户所有数据库的dbAdmin权限。
docker exec -it 1c842a01d24a mongo admin
db.createUser({ user: 'admin', pwd: '123456', roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] });
db.auth("admin","123456");
PS C:\Users\Administrator> docker exec -it 1c842a01d24a mongo admin
MongoDB shell version v5.0.6
connecting to: mongodb://127.0.0.1:27017/admin?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("5d641621-3433-4965-b29f-7e6055672a26") }
MongoDB server version: 5.0.6
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
an upcoming release.
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
================
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
        https://docs.mongodb.com/
Questions? Try the MongoDB Developer Community Forums
        https://community.mongodb.com
> 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
>

Docker 副本集安装 Mogodb

 类似资料: