1、查找mongodb相关镜像

docker search mongo

找到相关的镜像进行拉取,如果不指定版本,默认下载最新的mongoDB。建议自己先查找需要那个版本后在进行拉取,因为mongoDB不同版本之间差距较大。

2、拉取镜像

这里拉取mongodb6.0

docker pull mongodb:6.0

拉取成功后,查看docker镜像

docker images

3、本地创建容器卷文件夹

首先在本地创建文件夹

D:mongodbconf
D:mongodbdata
D:mongodblogs

进入conf文件夹创建mongod.conf文件

# 数据库文件存储位置
dbpath = /data/db
# log文件存储位置
logpath = /data/log/mongod.log
#pid运行目录
#pidfilepath = /var/run/mongodb/mongodb.pid # 使用追加的方式写日志 logappend = true
#启用日志文件,默认启用
journal=true
#最大连接数
maxConns=2048 # 是否以守护进程方式运行 # fork = true # 全部ip可以访问 bind_ip = 0.0.0.0 # 端口号 port = 27017 # 是否启用认证 auth = true # 设置oplog的大小(MB) oplogSize=
1755

进入logs目录文件创建mongod.log文件

4、通过命令创建新容器

docker run -d --name mongodb -p 27017:27017 --privileged=true --restart=always 
-v D:/mongodb/data:/data/db
-v D:/mongodb/conf:/data/configdb
-v D:/mongodb/logs:/data/log/
-e MONGO_INITDB_ROOT_USERNAME=root
-e MONGO_INITDB_ROOT_PASSWORD=1234qwer mongo:6.0
-f /data/configdb/mongod.conf --auth

-d 后台运行

-p 映射端口

--privileged  以root 用户身份在容器内运行

--restart  自动重启容器

-v 容器卷

-e 设置环境

-f 指明配置文件路径 容器以容器内的配置文件启动

--auth mongo开启安全认证,需要账号密码才能访问

 

容器启动成功

5、查看mongo版本

进入容器实例

docker exec -it cfb571a9a7e4  /bin/bash

输入命令:

mongod -version

docker安装mongodb成功。

6、错误问题

上面我们使用docker安装的mongodb已经可以使用了,但是还有一个问题。如果重新启动容器,会发现容器启动不了。会报错:

{"t":{"$date":"2023-06-29T09:25:48.932+00:00"},"s":"I",  "c":"CONTROL",  "id":20698,   "ctx":"-","msg":"***** SERVER RESTARTED *****"}
{"t":{"$date":"2023-06-29T09:25:48.933+00:00"},"s":"I",  "c":"NETWORK",  "id":4915701, "ctx":"-","msg":"Initialized wire specification","attr":{"spec":{"incomingExternalClient":{"minWireVersion":0,"maxWireVersion":17},"incomingInternalClient":{"minWireVersion":0,"maxWireVersion":17},"outgoing":{"minWireVersion":6,"maxWireVersion":17},"isInternalClient":true}}}
{"t":{"$date":"2023-06-29T09:25:48.935+00:00"},"s":"I",  "c":"CONTROL",  "id":23285,   "ctx":"-","msg":"Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'"}
{"t":{"$date":"2023-06-29T09:25:48.935+00:00"},"s":"I",  "c":"NETWORK",  "id":4648601, "ctx":"main","msg":"Implicit TCP FastOpen unavailable. If TCP FastOpen is required, set tcpFastOpenServer, tcpFastOpenClient, and tcpFastOpenQueueSize."}
{"t":{"$date":"2023-06-29T09:25:48.937+00:00"},"s":"I",  "c":"REPL",     "id":5123008, "ctx":"main","msg":"Successfully registered PrimaryOnlyService","attr":{"service":"TenantMigrationDonorService","namespace":"config.tenantMigrationDonors"}}
{"t":{"$date":"2023-06-29T09:25:48.937+00:00"},"s":"I",  "c":"REPL",     "id":5123008, "ctx":"main","msg":"Successfully registered PrimaryOnlyService","attr":{"service":"TenantMigrationRecipientService","namespace":"config.tenantMigrationRecipients"}}
{"t":{"$date":"2023-06-29T09:25:48.937+00:00"},"s":"I",  "c":"REPL",     "id":5123008, "ctx":"main","msg":"Successfully registered PrimaryOnlyService","attr":{"service":"ShardSplitDonorService","namespace":"config.tenantSplitDonors"}}
{"t":{"$date":"2023-06-29T09:25:48.937+00:00"},"s":"I",  "c":"CONTROL",  "id":5945603, "ctx":"main","msg":"Multi threading initialized"}
{"t":{"$date":"2023-06-29T09:25:48.938+00:00"},"s":"I",  "c":"CONTROL",  "id":4615611, "ctx":"initandlisten","msg":"MongoDB starting","attr":{"pid":1,"port":27017,"dbPath":"/data/db","architecture":"64-bit","host":"cfb571a9a7e4"}}
{"t":{"$date":"2023-06-29T09:25:48.939+00:00"},"s":"I",  "c":"CONTROL",  "id":23403,   "ctx":"initandlisten","msg":"Build Info","attr":{"buildInfo":{"version":"6.0.6","gitVersion":"26b4851a412cc8b9b4a18cdb6cd0f9f642e06aa7","openSSLVersion":"OpenSSL 3.0.2 15 Mar 2022","modules":[],"allocator":"tcmalloc","environment":{"distmod":"ubuntu2204","distarch":"x86_64","target_arch":"x86_64"}}}}
{"t":{"$date":"2023-06-29T09:25:48.939+00:00"},"s":"I",  "c":"CONTROL",  "id":51765,   "ctx":"initandlisten","msg":"Operating System","attr":{"os":{"name":"Ubuntu","version":"22.04"}}}
{"t":{"$date":"2023-06-29T09:25:48.939+00:00"},"s":"I",  "c":"CONTROL",  "id":21951,   "ctx":"initandlisten","msg":"Options set by command line","attr":{"options":{"config":"/data/configdb/mongod.conf","net":{"bindIp":"*","port":27017},"replication":{"oplogSizeMB":2048},"security":{"authorization":"enabled"},"storage":{"dbPath":"/data/db"},"systemLog":{"destination":"file","logAppend":true,"path":"/data/log/mongod.log"}}}}
{"t":{"$date":"2023-06-29T09:25:48.940+00:00"},"s":"E",  "c":"NETWORK",  "id":23024,   "ctx":"initandlisten","msg":"Failed to unlink socket file","attr":{"path":"/tmp/mongodb-27017.sock","error":"Operation not permitted"}}
{"t":{"$date":"2023-06-29T09:25:48.941+00:00"},"s":"F",  "c":"ASSERT",   "id":23091,   "ctx":"initandlisten","msg":"Fatal assertion","attr":{"msgid":40486,"file":"src/mongo/transport/transport_layer_asio.cpp","line":1126}}
{"t":{"$date":"2023-06-29T09:25:48.941+00:00"},"s":"F",  "c":"ASSERT",   "id":23092,   "ctx":"initandlisten","msg":"nn***aborting after fassert() failurenn"}

主要看这条日志:

":"E",  "c":"NETWORK",  "id":23024,   "ctx":"initandlisten","msg":"Failed to unlink socket file","attr":{"path":"/tmp/mongodb-27017.sock","error":"Operation not permitted"}}

发现  /tmp/mongodb-27017.sock 没有操作权限,导致容器重新启动失败。

解决方法:

删除mongodb-27017.sock,重启mongo服务。

sudo rm /tmp/mongodb-27017.sock

因为使用的docker,容器没有运行进入不了容器,所以无法使用。

所以需要使用容器卷映射tmp 使mongodb-27017文件拥有相应权限

增加容器卷:

 -v D:/mongodb/tmp:/tmp

把之前的容器删除,重新启动一个新容器。由于之前使用了容器卷,所以启动新容器数据也不会消失。完整命令:

docker run -d --name mongodb -p 27017:27017 --privileged=true 
-v D:/mongodb/data:/data/db
-v D:/mongodb/conf:/data/configdb
-v D:/mongodb/logs:/data/log/
-v D:/mongodb/tmp:/tmp
-e MONGO_INITDB_ROOT_USERNAME=root
-e MONGO_INITDB_ROOT_PASSWORD=1234qwer
mongo:6.0
-f /data/configdb/mongod.conf --auth

启动容器。然后停止容器再次重新启动。发现没有报错问题解决。

 

内容来源于网络如有侵权请私信删除

文章来源: 博客园

原文链接: https://www.cnblogs.com/eternality/p/17513973.html

你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!