docker部署mongodb

1:拉去momgodb镜像

docker部署mongodb_第1张图片

2:拉去成功后,通过docker-compose.yml配置文件启动mongodb,docker-compose.yml配置如下

version: '3.8'
services:

  mongodb-1:
    container_name: mongodb
    image: mongo	
    ports:
      - "27017:27017"
    volumes:
      - G:\temptemptemp\mongodb\data:/data/db
      - G:\temptemptemp\mongodb\config\mongod.conf:/etc/mongod.conf
    environment:
      - MONGO_INITDB_ROOT_USERNAME=root22
      - MONGO_INITDB_ROOT_PASSWORD=root33
      - MONGODB_DATABASE=mydb
      - MONGODB_DISABLE_JAVASCRIPT=true
      - MONGODB_AUTH=yes

3:进入容器mongodb操作(必须输入密码才能操作后续db.auth(“账号”,“密码”),数据库admin是默认的)

PS C:\Users\dell>
PS C:\Users\dell> docker ps
CONTAINER ID   IMAGE     COMMAND                   CREATED        STATUS        PORTS                      NAMES
707d09b9c8f5   mongo     "docker-entrypoint.s…"   28 hours ago   Up 28 hours   0.0.0.0:27017->27017/tcp   mongodb
PS C:\Users\dell> docker exec -it mongodb mongosh admin
Current Mongosh Log ID: 654d1859a8446ad93cccb0cf
Connecting to:          mongodb://127.0.0.1:27017/admin?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.0.1
Using MongoDB:          7.0.2
Using Mongosh:          2.0.1

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

admin> db.auth("root22","root33");
{ ok: 1 }
admin> use testdb;
switched to db testdb
testdb> db.createUser({ user:'root1',pwd:'root1',roles:[ { role:'readWrite', db: 'testdb'}]});
{ ok: 1 }
testdb> db.auth("root1","root1");
{ ok: 1 }
testdb> db.createCollection("testTable");
{ ok: 1 }
testdb>

4:注意事项都需要输入账号密码才能操作

你可能感兴趣的:(docker,mongodb,容器)