centos8 docker 创建mongo 无法进入admin 设置密码
[root@ ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mongo latest d34d21a9eb5b 3 days ago 693MB
mysql 5.7 daff57b7d2d1 11 days ago 430MB
[root@ ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f7908efbac3c mongo "docker-entrypoint.s…" 18 minutes ago Up 18 minutes 0.0.0.0:27017->27017/tcp, :::27017->27017/tcp mongodb
[root@ ~]# docker exec -it mongodb mongo admin
OCI runtime exec failed: exec failed: unable to start container process: exec: "mongo": executable file not found in $PATH: unknown
[root@ ~]#
明明都有为啥进不去
这能进去容器但无法进入admin
[root@ ~]# docker exec -it mongodb mongo admin
OCI runtime exec failed: exec failed: unable to start container process: exec: "mongo": executable file not found in $PATH: unknown
[root@ ~]# docker exec -it mongodb /bin/bash
root@f7908efbac3c:/# use admin
bash: use: command not found
root@f7908efbac3c:/# mongo
bash: mongo: command not found
root@f7908efbac3c:/#
我都是按网上正常创建容器
docker run --name mongodb -p 27017:27017 -d mongo --auth
求解答
评论区有位老哥提供了解答,我做后续补全
创建网络
docker network create some-network
环境变量设设置
docker run -d --network some-network --name mongo \
-e MONGO_INITDB_ROOT_USERNAME=admin\
-e MONGO_INITDB_ROOT_PASSWORD=pwd\
mongo
创建登录数据库 --这一步的登录跟进入后创建用户的区分,我没搞明白,知道的老哥,麻烦指导一下
docker run -it --rm --network some-network mongo \
mongosh --host mongo \
-u admin\
-p pwd\
--authenticationDatabase admin \
some-db
创建用户
db.createUser({ user:
'root',pwd:'pwd',roles:[ { role:'userAdminAnyDatabase', db: 'admin'},"readWriteAnyDatabase"]});
补充 查看创建用户
show users
[
{
_id: 'admin.admin',
userId: UUID(""),
user: 'admin',
db: 'admin',
roles: [ { role: 'root', db: 'admin' } ],
mechanisms: [ 'SCRAM-SHA-1', 'SCRAM-SHA-256' ]
},
{
_id: 'admin.root',
userId: UUID(""),
user: 'root',
db: 'admin',
roles: [
{ role: 'readWriteAnyDatabase', db: 'admin' },
{ role: 'userAdminAnyDatabase', db: 'admin' }
],
mechanisms: [ 'SCRAM-SHA-1', 'SCRAM-SHA-256' ]
}
]
创建后又进入了一个新坑,用客户端工具无法连接到数据库,不知道我忽略的啥