美文网首页Docker容器Awesome Docker
docker安装mongodb数据库测试环境

docker安装mongodb数据库测试环境

作者: sleepforests | 来源:发表于2019-07-23 15:59 被阅读2次

    1、下载mongo的image

    ~ » docker pull mongo:4      
    
    4: Pulling from library/mongo
    35b42117c431: Pull complete
    ad9c569a8d98: Pull complete
    293b44f45162: Pull complete
    0c175077525d: Pull complete
    4e73525b52ba: Pull complete
    a22695a3f5e9: Pull complete
    c5175bcf2977: Pull complete
    3e320da07793: Pull complete
    01c6db6b2b5a: Pull complete
    3bd6e9d03e78: Pull complete
    e03dcf51513f: Pull complete
    c1956a9e136a: Pull complete
    4c35cf22b1d5: Pull complete
    Digest: sha256:71600e081274550f00647655db8b85e3103f763507c55a0636dcab1efc126630
    

    2、启动mongo

    docker run -p 27017:27017 -v    /data/docker/mongodb:/data/db   -v /data/docker/mongodb:/data/configdb   -d mongo:4  --auth 
    

    这里数据存放在 /data/docker/mongodb 文件夹,这里注意mac下需要sharing下文件夹。
    --auth表示开启权限相关

    3、验证

    本地没有mongo的客户端,使用容器里面的客户端链接执行
    第一次使用host报错

    /data/docker/mongodb » docker exec -it 25f796b9b97c mongo --host 172.17.28.85     
    MongoDB shell version v4.0.10
    connecting to: mongodb://172.17.28.85:27017/?gssapiServiceName=mongodb
    2019-07-23T07:44:40.439+0000 E QUERY    [js] Error: couldn't connect to server 172.17.28.85:27017, connection attempt failed: SocketException: Error connecting to 172.17.28.85:27017 :: caused by :: No route to host :
    connect@src/mongo/shell/mongo.js:344:17
    @(connect):2:6
    exception: connect failed
    

    第二次使用localhost成功

    /data/docker/mongodb » docker exec -it 25f796b9b97c mongo --host localhost     
    MongoDB shell version v4.0.10
    connecting to: mongodb://localhost:27017/?gssapiServiceName=mongodb
    Implicit session: session { "id" : UUID("01ae73d0-7a39-4303-8ca8-00f1fcfbb9d0") }
    MongoDB server version: 4.0.10
    Server has startup warnings:
    2019-07-23T07:38:42.440+0000 I CONTROL  [initandlisten]
    2019-07-23T07:38:42.441+0000 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
    2019-07-23T07:38:42.441+0000 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
    2019-07-23T07:38:42.441+0000 I CONTROL  [initandlisten]
    ---
    Enable MongoDB's free cloud-based monitoring service, which will then receive and display
    metrics about your deployment (disk utilization, CPU, operation statistics, etc).
    
    The monitoring data will be available on a MongoDB website with a unique URL accessible to you
    and anyone you share the URL with. MongoDB may use this information to make product
    improvements and to suggest MongoDB products and deployment options to you.
    
    To enable free monitoring, run the following command: db.enableFreeMonitoring()
    To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
    ---
    

    使用本机IP不通的问题可能是和防火墙有关系,网上说使用localhost可行,我试了试确实可以。

    https://forums.docker.com/t/no-route-to-host-network-request-from-container-to-host-ip-port-published-from-other-container/39063/10

    4、权限处理

    刚才加了--auth后需要添加mongo的账号,使用下面命令登录即可:

    docker exec -it 21aeff91b61e6ebfe2b347e0a7549510abcd4eeb8b095d51dfbb1360951864a5 mongo admin
    

    然后使用mongo的命令create user,授权等;具体命令查看mongo文档。

    不加--auth,测试环境裸奔一下也无妨。

    相关文章

      网友评论

        本文标题:docker安装mongodb数据库测试环境

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