Docker-compose Node.js Mongodb失败

时间:2019-03-01 10:59:24

标签: node.js mongodb docker docker-compose

我正在使用NodeJS + MongoDB在本地Mac OS上测试docker-compose,但是NodeJSMongoDB无法成功连接。


如果我没有通过以下代码为--auth设置MongoDB,一切正常。

代码如下:

猫鼬连接

mongodb://mongodb:27017/myprojectdatabase

docker-compose.yml

version: "3"
services:
  web:
    build: .
    restart: always
    ports:
    - "8080:8080"
    depends_on:
    - mongodb
    volumes:
     - .:/mycode
  mongodb:
    image: mongo:latest
    ports:
      - "27017:27017"

然后我想像下面那样为MongoDB启动--auth,我遇到了错误。

docker-compose.yml

version: "3"
services:
  web:
    build: .
    restart: always
    ports:
    - "8080:8080"
    depends_on:
    - mongodb
    volumes:
     - .:/mycode
    # environment:
    #  - NODE_ENV=production
  mongodb:
    image: mongo:latest
    command: [--auth]
    environment:
      MONGO_INITDB_ROOT_USERNAME: my_admin
      MONGO_INITDB_ROOT_PASSWORD: my2019
      MONGO_INITDB_DATABASE: myprojectdatabase
    ports:
      - "27017:27017"
    volumes:
      - ./mydata:/data/db

然后我跑

docker-compose down -v && docker-compose up --build 

我得到了输出:

mongodb_1  | 2019-03-01T10:54:09.847+0000 I CONTROL  [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
mongodb_1  | 2019-03-01T10:54:09.869+0000 I CONTROL  [initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/data/db 64-bit host=9554854909b1
mongodb_1  | 2019-03-01T10:54:09.869+0000 I CONTROL  [initandlisten] db version v4.0.4
mongodb_1  | 2019-03-01T10:54:09.869+0000 I CONTROL  [initandlisten] git version: f288a3bdf201007f3693c58e140056adf8b04839
mongodb_1  | 2019-03-01T10:54:09.869+0000 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.2g  1 Mar 2016
mongodb_1  | 2019-03-01T10:54:09.869+0000 I CONTROL  [initandlisten] allocator: tcmalloc
mongodb_1  | 2019-03-01T10:54:09.869+0000 I CONTROL  [initandlisten] modules: none
mongodb_1  | 2019-03-01T10:54:09.869+0000 I CONTROL  [initandlisten] build environment:
mongodb_1  | 2019-03-01T10:54:09.869+0000 I CONTROL  [initandlisten]     distmod: ubuntu1604
mongodb_1  | 2019-03-01T10:54:09.869+0000 I CONTROL  [initandlisten]     distarch: x86_64
mongodb_1  | 2019-03-01T10:54:09.869+0000 I CONTROL  [initandlisten]     target_arch: x86_64
mongodb_1  | 2019-03-01T10:54:09.869+0000 I CONTROL  [initandlisten] options: { net: { bindIpAll: true }, security: { authorization: "enabled" } }
mongodb_1  | 2019-03-01T10:54:09.873+0000 W STORAGE  [initandlisten] Detected unclean shutdown - /data/db/mongod.lock is not empty.
mongodb_1  | 2019-03-01T10:54:09.876+0000 I STORAGE  [initandlisten] Detected data files in /data/db created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
mongodb_1  | 2019-03-01T10:54:09.878+0000 W STORAGE  [initandlisten] Recovering data from the last clean checkpoint.
mongodb_1  | 2019-03-01T10:54:09.879+0000 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=487M,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),    
web_1      | connection error: { MongoNetworkError: failed to connect to server [mongodb:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 192.168.160.2:27017]
    web_1      |     at Pool.<anonymous> (/usr/src/app/node_modules/mongodb-core/lib/topologies/server.js:505:11)
    web_1      |     at emitOne (events.js:116:13)

有时候,我可以看到日志包含用户创建的信息,有时却没有。

 2019-03-01T10:38:50.323+0000 I STORAGE  [conn2] createCollection: admin.system.users with generated UUID: 6b3b88f9-e77c-4094-a1c7-153816202a9e
mongodb_1  | Successfully added user: {
mongodb_1  |    "user" : "my_admin",
mongodb_1  |    "roles" : [
mongodb_1  |        {
mongodb_1  |            "role" : "root",
mongodb_1  |            "db" : "admin"
mongodb_1  |        }
mongodb_1  |    ]
mongodb_1  | }
mongodb_1  | 2019-03-01T10:38:50.340+0000 E -        [main] Error saving history file: FileOpenFailed: Unable to open() file /home/mongodb/.dbshell: Unknown error

我是docker方面的新手。我想主要问题是web无法与mongodb建立连接。在这个问题上花费太长时间。

有帮助吗?谢谢!

1 个答案:

答案 0 :(得分:1)

确保您不使用Web容器的本地主机。将容器视为单独的机器:一个容器中的localhost不与另一个容器共享。这就是为什么在连接字符串中有mongodb:27017而不是localhost:27017的原因,因为默认docker网络中的mongodb是带有mongo的容器的DNS名称。在第一种情况(成功)中使用此连接字符串,请确保在第二种情况中使用有效的DNS名称。

还要确保在连接字符串中也包含您的数据库凭据(用户名:密码)。