无法连接到mongodb容器主机 [英] cannot connect to mongodb container host

查看:59
本文介绍了无法连接到mongodb容器主机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

im试图连接到mongodb容器,但是它不起作用,无法识别主机.

im trying to connect to a mongodb container, but it does not work, does not recognize the host.

这是我的带有服务的docker-compose.yml文件:

this is my docker-compose.yml file with the service:

  version: '3'
  services:

  mongodb:
    build:
      context: .
      dockerfile: docker/mongodb/Dockerfile
    restart: always
    environment:
      DATABASE: database
      USERNAME: user
      PASSWORD: pass
    ports:
      - '5100:27017'
    volumes:
      - ./data/db:/data/db

这是我的mongodb的dockerfile:

This is my dockerfile for mongodb:

FROM mvertes/alpine-mongo:4.0.5-0

ENV MONGO_INITDB_DATABASE: ${DATABASE}
ENV MONGO_INITDB_ROOT_USERNAME: ${USERNAME}
ENV MONGO_INITDB_ROOT_PASSWORD: ${PASSWORD}

COPY ./docker/mongodb/init.js /docker-entrypoint-initdb.d

CMD ["mongod"]

EXPOSE 27017

我正在尝试从spring建立连接,但是找不到服务,这在我的属性中具有

I am trying to establish the connection from spring, but can not find the service, this I have in my properties:

spring.data.mongodb.host=mongodb
spring.data.mongodb.port=5100
spring.data.mongodb.username=user
spring.data.mongodb.password=pass
spring.data.mongodb.database=database

而且我正在尝试与robo3T建立连接,问题是相同的:

and also i am trying to establish the connection with robo3T, and the problem is the same:

Address: mongodb://mongodb
port:5100

春天的错误说:

com.mongodb.MongoSocketException: mongodb: unknown name or service

在robo3T中说:

Cannot connect to the MongoDB at mongodb:5100.

Error:
Network is unreachable.

我在做什么错了?

我的容器日志:

2019-03-16T16:22:00.714+0000 I CONTROL  [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
2019-03-16T16:22:00.750+0000 I CONTROL  [initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/data/db 64-bit host=1220eaf96d91
2019-03-16T16:22:00.750+0000 I CONTROL  [initandlisten] db version v4.0.5
2019-03-16T16:22:00.750+0000 I CONTROL  [initandlisten] git version: 3739429dd92b92d1b0ab120911a23d50bf03c412
2019-03-16T16:22:00.750+0000 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.1.1a  20 Nov 2018
2019-03-16T16:22:00.750+0000 I CONTROL  [initandlisten] allocator: system
2019-03-16T16:22:00.750+0000 I CONTROL  [initandlisten] modules: none
2019-03-16T16:22:00.750+0000 I CONTROL  [initandlisten] build environment:
2019-03-16T16:22:00.750+0000 I CONTROL  [initandlisten]     distarch: x86_64
2019-03-16T16:22:00.750+0000 I CONTROL  [initandlisten]     target_arch: x86_64
2019-03-16T16:22:00.750+0000 I CONTROL  [initandlisten] options: {}
2019-03-16T16:22:00.760+0000 I STORAGE  [initandlisten] 
2019-03-16T16:22:00.760+0000 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2019-03-16T16:22:00.760+0000 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
2019-03-16T16:22:00.768+0000 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=1432M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),statistics_log=(wait=0),verbose=(recovery_progress),
2019-03-16T16:22:01.523+0000 I STORAGE  [initandlisten] WiredTiger message [1552753321:523789][1:0x7f9d46a64ca8], txn-recover: Set global recovery timestamp: 0
2019-03-16T16:22:01.562+0000 I RECOVERY [initandlisten] WiredTiger recoveryTimestamp. Ts: Timestamp(0, 0)
2019-03-16T16:22:01.623+0000 I CONTROL  [initandlisten] 
2019-03-16T16:22:01.623+0000 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-03-16T16:22:01.623+0000 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2019-03-16T16:22:01.623+0000 I CONTROL  [initandlisten] 
2019-03-16T16:22:01.623+0000 I CONTROL  [initandlisten] ** WARNING: This server is bound to localhost.
2019-03-16T16:22:01.623+0000 I CONTROL  [initandlisten] **          Remote systems will be unable to connect to this server. 
2019-03-16T16:22:01.623+0000 I CONTROL  [initandlisten] **          Start the server with --bind_ip <address> to specify which IP 
2019-03-16T16:22:01.623+0000 I CONTROL  [initandlisten] **          addresses it should serve responses from, or with --bind_ip_all to
2019-03-16T16:22:01.623+0000 I CONTROL  [initandlisten] **          bind to all interfaces. If this behavior is desired, start the
2019-03-16T16:22:01.623+0000 I CONTROL  [initandlisten] **          server with --bind_ip 127.0.0.1 to disable this warning.
2019-03-16T16:22:01.623+0000 I CONTROL  [initandlisten] 
2019-03-16T16:22:01.638+0000 I STORAGE  [initandlisten] createCollection: admin.system.version with provided UUID: 74fe0f10-e6a8-439c-9548-a2cdd18df3c7
2019-03-16T16:22:01.688+0000 I COMMAND  [initandlisten] setting featureCompatibilityVersion to 4.0
2019-03-16T16:22:01.700+0000 I STORAGE  [initandlisten] createCollection: local.startup_log with generated UUID: 0d6bdbd7-4b15-4ad1-8ded-fa28b89ae871
2019-03-16T16:22:01.762+0000 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
2019-03-16T16:22:01.765+0000 I NETWORK  [initandlisten] waiting for connections on port 27017
2019-03-16T16:22:01.825+0000 I STORAGE  [LogicalSessionCacheRefresh] createCollection: config.system.sessions with generated UUID: b5462e15-4724-4b1f-8537-57d9ebb1a2e8
2019-03-16T16:22:01.902+0000 I INDEX    [LogicalSessionCacheRefresh] build index on: config.system.sessions properties: { v: 2, key: { lastUse: 1 }, name: "lsidTTLIndex", ns: "config.system.sessions", expireAfterSeconds: 1800 }
2019-03-16T16:22:01.902+0000 I INDEX    [LogicalSessionCacheRefresh]     building index using bulk method; build may temporarily use up to 500 megabytes of RAM
2019-03-16T16:22:01.905+0000 I INDEX    [LogicalSessionCacheRefresh] build index done.  scanned 0 total records. 0 secs

推荐答案

docker-compose.yml 的这一部分意味着您要公开在端口 27017 上工作的容器>在端口 5100 上公开,因此任何人都可以通过localhost或公共ip访问它,但是作为主机的 mongodb 一词只能在docker网络内部的容器之间识别,因此如果您在同一网络中有另一个容器,它可以作为主机名和端口 27017

This part of docker-compose.yml means you want to expose the container which works on port 27017 to the public on port 5100 so anyone can access it either localhost or public ip, but the word mongodb as a host will be only recognizable between containers inside a docker network so if you have another container within the same network it can reach mongodb on mongodb as a hostname and port 27017

ports:
  - '5100:27017'

基于mongodb日志,您遇到以下问题:

Based on the mongodb logs you have the following problem:

** WARNING: This server is bound to localhost. Remote systems will be unable to connect to this server. Start the server with --bind_ip <address> to specify which IP 

因此,您需要以-bind_ip 0.0.0.0 开始 mongodb ,以便可以从容器外部访问它.将您的 CMD 更改为以下内容:

So you need to start mongodb with --bind_ip 0.0.0.0 so you can reach it from outside the container. Change your CMD to the following:

CMD [ "mongod", "--bind_ip", "0.0.0.0" ]

或者从您的Dockerfile中删除 CMD ,它将回退到基础映像的原始 CMD ,即

Or remove the CMD from your Dockerfile and it will fallback to the original CMD of the base image which is the same as above.

从MongoDB 3.6开始,默认情况下mongod绑定到localhost.请参见默认绑定到本地主机.

这篇关于无法连接到mongodb容器主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆