美文网首页
docker-compose搭建mongodb

docker-compose搭建mongodb

作者: 销毁first | 来源:发表于2021-03-16 15:45 被阅读0次

一、docker-compose版

1.配置网络(复用已有网络)

1.1 创建docker虚拟网络

# 创建一个名为clickhouse的桥接(bridge)虚拟网络,网关172.26.0.1,网段为172.25.0.0
docker network create -d bridge --subnet=172.26.0.0/16 --gateway=172.26.0.1 clickhouse

2.启停命令

在docker-compose.yml路径下:

构建开启: docker-compose up -d

停止: docker-compose stop

开启:docker-compose start

2.编写docker-compose.yml

version: '3.7'
services:
  mongo-db:
    image: mongo:latest
    container_name: mongo-db
    #network_mode: "host"
    #restart: always
    ports:
      - 27017:27017
    environment:
      TZ: Asia/Shanghai
      MONGO_INITDB_ROOT_USERNAME: admin
      MONGO_INITDB_ROOT_PASSWORD: admin@123
    volumes:
      - /f/DockerData/mongodb:/data/db
    logging:
      driver: "json-file"
      options:
        max-size: "200k"
        max-file: "10"
    networks:
      clickhouse:
        ipv4_address: 172.26.0.101

  mongo-express:
    image: mongo-express:latest
    container_name: mongo-express
    #restart: always
    links:
      - mongo-db:mongodb
    depends_on:
      - mongo-db
    ports:
      - 27018:8081
    environment:
      ME_CONFIG_OPTIONS_EDITORTHEME: 3024-night
      ME_CONFIG_MONGODB_SERVER: mongodb
      ME_CONFIG_MONGODB_ADMINUSERNAME: admin
      ME_CONFIG_MONGODB_ADMINPASSWORD: admin@123
      ME_CONFIG_BASICAUTH_USERNAME: admin
      ME_CONFIG_BASICAUTH_PASSWORD: admin@123
    networks:
      clickhouse:
        ipv4_address: 172.26.0.102
    
networks:
 clickhouse:
  external: true

2.客户端

  1. mongo-express界面:http://localhost:27018/

二、docker版-无界面(不建议使用)

# 运行mongodb容器
docker run -itd --net=clickhouse -e TZ="Asia/Shanghai" --ip="172.26.0.101" -p 27017:27017 -e MONGO_INITDB_ROOT_USERNAME=admin -e MONGO_INITDB_ROOT_PASSWORD=admin@123 -v C:/DockerData/mongodb:/data/db --name mongo-db  mongo --auth

三、测试mongodb

参考连接:https://www.cnblogs.com/oloroso/p/9777141.html

1、拷贝中国县级行政边界文件到容器
docker cp border_0 mongo-db:/tmp/border_0
docker cp border_1 mongo-db:/tmp/border_1
2、登录容器
docker exec -it mongo-db sh
cd /tmp
3、导入数据到us数据库
mongoimport -h localhost --authenticationDatabase admin -u admin -p admin@123 -d us -c xzbj --file border_0
mongoimport -h localhost --authenticationDatabase admin -u admin -p admin@123 -d us -c xzbj --file border_1
4、连接接mongodb
mongo --authenticationDatabase admin -u admin -p admin@123

5、其他查看操作,也可以在web界面查看

相关文章

网友评论

      本文标题:docker-compose搭建mongodb

      本文链接:https://www.haomeiwen.com/subject/bgedcltx.html