docker 安装mongodb 并配置

1. 下载

[root@host-10-23-110-128 mysql]# docker pull mongo:latest
latest: Pulling from library/mongo
7b1a6ab2e44d: Pull complete
90eb44ebc60b: Pull complete
5085b59f2efb: Pull complete
c7499923d022: Pull complete
019496b6c44a: Pull complete
c0df4f407f69: Pull complete
351daa315b6c: Pull complete
a233e6240acc: Pull complete
a3f57d6be64f: Pull complete
dd1b5b345323: Pull complete
Digest: sha256:5be752bc5f2ac4182252d0f15d74df080923aba39700905cb26d9f70f39e9702
Status: Downloaded newer image for mongo:latest
docker.io/library/mongo:latest
[root@host-10-23-110-128 mysql]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
redis        latest    7614ae9453d1   11 months ago   113MB
mysql        latest    3218b38490ce   11 months ago   516MB
mongo        latest    dfda7a2cf273   11 months ago   693MB
[root@host-10-23-110-128 mysql]#
2. 配置

[root@host-10-23-110-128 mysql]# docker run --name mongo -p 27017:27017 -v /home/docker/data/mongo/data/:/data/db -v /home/docker/data/mongo/backup/:/data/backup -d mongo --auth
391a31a9bbe0b791c6d9ea2ae53fd431c8e38e27cbff5f528dbec67278367832
[root@host-10-23-110-128 mysql]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                               NAMES
391a31a9bbe0   mongo     "docker-entrypoint.s…"   4 seconds ago    Up 4 seconds    0.0.0.0:27017->27017/tcp            mongo
129f0779069d   mysql     "docker-entrypoint.s…"   24 minutes ago   Up 24 minutes   0.0.0.0:3306->3306/tcp, 33060/tcp   mysql
a7aafa90cd99   redis     "docker-entrypoint.s…"   2 hours ago      Up 5 minutes    0.0.0.0:6379->6379/tcp              redis
[root@host-10-23-110-128 mysql]# [root@host-10-23-110-128 mysql]# docker exec -it mongo mongo admin
MongoDB shell version v5.0.5
connecting to: mongodb://127.0.0.1:27017/admin?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("46447b1d-2681-4aae-ab77-84202a2c0d1f") }
MongoDB server version: 5.0.5
================
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: 'hett', pwd: 'hett', roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] });
Successfully added user: {
    "user" : "hett",
    "roles" : [
        {
            "role" : "userAdminAnyDatabase",
            "db" : "admin"
        }
    ]
}
> use admin
switched to db admin
> db.auth("hett","hett")
1
> use test01
switched to db test01
> db.createUser({user:"hett",pwd:"hett",roles:["readWrite"]});
4. 如果出现无法配置用户的错误

通过以上配置无法开启admin的用户配置
OCI runtime exec failed: exec failed: unable to start container process: exec: "mongo": executable file not found in $PATH: unknown

需要创建一个默认的网络

root@reqq:/home/docker/mongodb# docker network create default_network
e3726f02c1d387e4f03007d122ffd0243c163a74be0c6cb442c91fefbb78ac46
然后运行一下版本的

docker run -d --network default_network --name mongo  -p 27017:27017 -e MONGO_INITDB_ROOT_USERNAME=admin -e MONGO_INITDB_ROOT_PASSWORD=hett mongo
通过次命令进入一个db去配置
docker run -it --rm --network default_network mongo mongosh --host mongo -u admin -p hett --authenticationDatabase admin testdb

root@reqq:/home/docker/mongodb# docker run -it --rm --network default_network mongo mongosh --host mongo -u admin -p hett --authenticationDatabase admin testdb
Current Mongosh Log ID:    639a703d3cf65d5e746555ea
Connecting to:        mongodb://<credentials>@mongo:27017/testdb?directConnection=true&authSource=admin&appName=mongosh+1.6.1
Using MongoDB:        6.0.3
Using Mongosh:        1.6.1

For mongosh info see: https://docs.mongodb.com/mongodb-shell/


To help improve our products, anonymous usage data is collected and sent to MongoDB periodically (https://www.mongodb.com/legal/privacy-policy).
You can opt-out by running the disableTelemetry() command.

------
   The server generated these startup warnings when booting
   2022-12-15T00:54:06.733+00:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
   2022-12-15T00:54:07.401+00:00: vm.max_map_count is too low
------

------
   Enable MongoDB's free cloud-based monitoring service, which will then receive and display
   metrics about your deployment (disk utilization, CPU, operation statistics, etc).
   
   The monitoring data will be available on a MongoDB website with a unique URL accessible to you
   and anyone you share the URL with. MongoDB may use this information to make product
   improvements and to suggest MongoDB products and deployment options to you.
   
   To enable free monitoring, run the following command: db.enableFreeMonitoring()
   To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
------

testdb> show dbs
admin   100.00 KiB
config   12.00 KiB
local    40.00 KiB
testdb> use uav
switched to db uav
uav> use admin\
MongoshInvalidInputError: [COMMON-10001] Invalid database name: admin\
uav> use admin
switched to db admin
admin> db.createUser({ user: 'hett', pwd: 'hett', roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] });
{ ok: 1 }
admin> use uav
switched to db uav
uav> db.createUser({user:"hett",pwd:"hett",roles:["readWrite"]});
{ ok: 1 }
uav> show collections

uav> db.uav.insert({"name:":"this is an test"})
DeprecationWarning: Collection.insert() is deprecated. Use insertOne, insertMany, or bulkWrite.
{
  acknowledged: true,
  insertedIds: { '0': ObjectId("639a70a28ec6e6cd6faef640") }
}
uav> show collections
uav
uav> exit
然后通过robot 3.t 连接

 

posted @ 2022-11-24 16:38  李悠然  阅读(352)  评论(0编辑  收藏  举报