1、查找mongodb相关镜像
找到相关的镜像进行拉取,如果不指定版本,默认下载最新的mongoDB。建议自己先查找需要那个版本后在进行拉取,因为mongoDB不同版本之间差距较大。
2、拉取镜像
这里拉取mongodb6.0拉取成功后,查看docker镜像
3、本地创建容器卷文件夹
首先在本地创建文件夹- D:\mongodb\conf
- D:\mongodb\data
- D:\mongodb\logs
复制代码 进入conf文件夹创建mongod.conf文件- # 数据库文件存储位置
- dbpath = /data/db
- # log文件存储位置
- logpath = /data/log/mongod.log<br>#pid运行目录<br>#pidfilepath = /var/run/mongodb/mongodb.pid
- # 使用追加的方式写日志
- logappend = true<br>#启用日志文件,默认启用<br>journal=true
复制代码- #最大连接数<br>maxConns=2048
- # 是否以守护进程方式运行
- # fork = true
- # 全部ip可以访问
- bind_ip = 0.0.0.0
- # 端口号
- port = 27017
- # 是否启用认证
- auth = true
- # 设置oplog的大小(MB)
- oplogSize=1755
复制代码- [/code]进入logs目录文件创建mongod.log文件
- 4、通过命令创建新容器
- [code]docker run -d --name mongodb -p 27017:27017 --privileged=true --restart=always <br>-v D:/mongodb/data:/data/db <br>-v D:/mongodb/conf:/data/configdb <br>-v D:/mongodb/logs:/data/log/ <br>-e MONGO_INITDB_ROOT_USERNAME=root <br>-e MONGO_INITDB_ROOT_PASSWORD=1234qwer mongo:6.0 <br>-f /data/configdb/mongod.conf --auth
复制代码 -d 后台运行
-p 映射端口
--privileged 以root 用户身份在容器内运行
--restart 自动重启容器
-v 容器卷
-e 设置环境
-f 指明配置文件路径 容器以容器内的配置文件启动
--auth mongo开启安全认证,需要账号密码才能访问
容器启动成功
5、查看mongo版本
进入容器实例- docker exec -it cfb571a9a7e4 /bin/bash
复制代码 输入命令:
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":"\n\n***aborting after fassert() failure\n\n"}
复制代码 主要看这条日志:- ":"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文件拥有相应权限
增加容器卷:把之前的容器删除,重新启动一个新容器。由于之前使用了容器卷,所以启动新容器数据也不会消失。完整命令:- docker run -d --name mongodb -p 27017:27017 --privileged=true <br>-v D:/mongodb/data:/data/db <br>-v D:/mongodb/conf:/data/configdb <br>-v D:/mongodb/logs:/data/log/ <br>-v D:/mongodb/tmp:/tmp <br>-e MONGO_INITDB_ROOT_USERNAME=root <br>-e MONGO_INITDB_ROOT_PASSWORD=1234qwer <br>mongo:6.0 <br>-f /data/configdb/mongod.conf --auth
复制代码 启动容器。然后停止容器再次重新启动。发现没有报错问题解决。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |