mongo-express-docker icon indicating copy to clipboard operation
mongo-express-docker copied to clipboard

Could not connect to database using connectionString: mongodb://mongo:27017"

Open jrujano-zz opened this issue 3 years ago • 15 comments

Trying in different ways I have not been able to connect to my mongodb through mongo-express

Selección_136

Selección_137

jrujano-zz avatar Jul 06 '21 04:07 jrujano-zz

same result withing the following environment: Latest (as of now) mongo-express and mongo (4.4-bionic and latest) docker-images.

sample "docker-compose.yml":

services:

  mongo:
    image: mongo #:4.4-bionic
   
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: example

    # optionally make it available to the outside
    ports:
      - "27017:27017" 

  mongo-express:
    image: mongo-express
    ports:
      - 8081:8081
    environment:
      ME_CONFIG_MONGODB_ADMINUSERNAME: root
      ME_CONFIG_MONGODB_ADMINPASSWORD: example

thomas-hofmann-dci avatar Jul 06 '21 09:07 thomas-hofmann-dci

@thomas-hofmann-dci Try connecting using ME_CONFIG_MONGODB_URL

JafarAkhondali avatar Jul 29 '21 11:07 JafarAkhondali

Same error here. Tried a few config. Event the docker-compose.yml on the main page of mongo (https://hub.docker.com/_/mongo) leads to the same error.

The only way to get this working is to set the 0.54.0 tag version :

version: "3.7" services: mongodb: container_name: mongo-dev image: mongo environment: - MONGO_INITDB_ROOT_USERNAME=admin - MONGO_INITDB_DATABASE=auth - MONGO_INITDB_ROOT_PASSWORD=pass ports: - '27017:27017' mongo-express: container_name: mongo-express image: mongo-express**:0.54.0** depends_on: - mongodb environment: - ME_CONFIG_MONGODB_ADMINUSERNAME=admin - ME_CONFIG_MONGODB_ADMINPASSWORD=pass - ME_CONFIG_MONGODB_SERVER=mongo-dev - ME_CONFIG_BASICAUTH_USERNAME=admin - ME_CONFIG_BASICAUTH_PASSWORD=ihavealongpassword ports: - '8081:8081'

And i will not use ME_CONFIG_MONGODB_URL as it is not documented on the main page of the mongo-express dockerhub and therefore it should work without it.

mtwinux avatar Jul 30 '21 07:07 mtwinux

@mtwinux The page on dockerhub is outdated, as of version 1, you should use ME_CONFIG_MONGODB_URL, if for some reason you don't want to, you'll have to use version 0.X

JafarAkhondali avatar Jul 30 '21 10:07 JafarAkhondali

This configuration should work:

version: '3.1'

services:

  mongo:
    image: mongo
    restart: always
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: example

  mongo-express:
    image: mongo-express
    restart: always
    ports:
      - 8081:8081
    environment:
      ME_CONFIG_MONGODB_URL: "mongodb://root:example@mongo:27017/"
      ME_CONFIG_MONGODB_ADMINUSERNAME: root
      ME_CONFIG_MONGODB_ADMINPASSWORD: example

gBusato avatar Jul 31 '21 07:07 gBusato

Screenshot from 2021-10-18 09-10-25

Looking at this error message, it says "failed to connect to server [mongo:27017] on first connect". So I added the restart option to the mongo-express service and it worked for me.

mongo-express:
    image: mongo-express
    restart: always
    ...

I hope that this reply is helpful

ebukaodini avatar Oct 18 '21 08:10 ebukaodini

Solution for latest mongo-express (on Mac OS)

Digests of used images:

  • mongo-express sha256:2a25aafdf23296823b06bc9a0a2af2656971262041b8dbf11b40444804fdc104
  • mongo sha256:cf9f5df5419319390cc3b5d9abfc2d0d0b149b3e04a6c9936990129e3e29b579

In my case, I had to change the host name in the ME_CONFIG_MONGODB_URL variable to kubernetes.docker.internal: "mongodb://admin:[email protected]:27017". I fetched that from the /private/etc/hosts file on my Mac OS. (The .yaml file is below.)

Using restart for mongo-express is also mandatory.

Until finding the described solution, I tried restarting the Docker daemon and rebooting my machine; both actions did nothing.

Here's the working .yaml file:

version: '3'
services:
    mongodb:
        image: mongo
        ports:
            - 27017:27017
        environment:
            - MONGO_INITDB_ROOT_USERNAME=admin
            - MONGO_INITDB_ROOT_PASSWORD=password
    mongo-express:
        image: mongo-express
        restart: always
        ports:
            - 8080:8081
        environment:
            - ME_CONFIG_MONGO_DB_ADMINUSERNAME=admin
            - ME_CONFIG_MONGO_DB_ADMINPASSWORD=password
            - ME_CONFIG_MONGO_DB_SERVER=mongodb
            - ME_CONFIG_MONGODB_URL="mongodb://admin:[email protected]:27017"

m-ocean-it avatar Nov 28 '21 09:11 m-ocean-it

mongo-express**:0.54.0**

This solution works with me. As the end of March, 2022, mongo-express:latest version is [1.0.0-alpha.4]. mongo:latest version 5.0.6-focal

following docker-compose works with me. Many thanks to @mtwinux

How can I share code here? The format looks bad.

version: '3.1'

services:

mongo: image: mongo restart: always environment: MONGO_INITDB_ROOT_USERNAME: root MONGO_INITDB_ROOT_PASSWORD: example

mongo-express: image: mongo-express:0.54.0 restart: always ports: - 8081:8081 environment: ME_CONFIG_MONGODB_URL: "mongodb://root:example@mongo:27017/" ME_CONFIG_MONGODB_ADMINUSERNAME: root ME_CONFIG_MONGODB_ADMINPASSWORD: example

wbqtac avatar Mar 31 '22 00:03 wbqtac

How can I share code here? The format looks bad.

@wbqtac to format code in blocks wrap it in ` (backticks).

If you only want to include something inline, like this, the syntax is `<text goes here>`

For full code blocks use:

```<language goes here (e.g. java or cpp)>
<code goes here>
```

For example:

```kotlin
println("Hello World!")
```

will display,

println("Hello World!")

See GitHub's formatting guide for more details.

psoder avatar Jun 21 '22 22:06 psoder

As of today using an image of mongo-express version 0.54.0 Works for me.

The is my .yaml file

version: '3'
services: 
  mongodb:
    image:  mongo:4.4.17-focal
    container_name: mongodb
    ports:
      - 27017:27017
    environment:
      - MONGO_INITDB_ROOT_USERNAME=admin
      - MONGO_INITDB_ROOT_PASSWORD=password
  mongo-express:
    image: mongo-express:0.54.0
    restart: always
    container_name: mongo-express
    ports:
      - 8081:8081
    environment:
      - ME_CONFIG_MONGODB_ADMINUSERNAME=admin
      - ME_CONFIG_MONGODB_ADMINPASSWORD=password
      - ME_CONFIG_MONGODB_SERVER=mongodb

johurul000 avatar Oct 30 '22 20:10 johurul000

As of today using an image of mongo-express version 0.54.0 Works for me.

The is my .yaml file

version: '3'
services: 
  mongodb:
    image:  mongo:4.4.17-focal
    container_name: mongodb
    ports:
      - 27017:27017
    environment:
      - MONGO_INITDB_ROOT_USERNAME=admin
      - MONGO_INITDB_ROOT_PASSWORD=password
  mongo-express:
    image: mongo-express:0.54.0
    restart: always
    container_name: mongo-express
    ports:
      - 8081:8081
    environment:
      - ME_CONFIG_MONGODB_ADMINUSERNAME=admin
      - ME_CONFIG_MONGODB_ADMINPASSWORD=password
      - ME_CONFIG_MONGODB_SERVER=mongodb

Yeah thanks this works for me too, but still haven't able to figure it out why it is showing error with the latest packages

siddharthans2000 avatar Dec 21 '22 10:12 siddharthans2000

This is what worked for me

version: '3'
services:
  mongodb:
    image: mongo
    container_name: mongodb
    ports:
      - 27017:27017
    environment:
      - MONGO_INITDB_ROOT_USERNAME=admin
      - MONGO_INITDB_ROOT_PASSWORD=password

  mongo-express:
    image: mongo-express
    container_name: mongo-express
    ports:
      - 8081:8081
    depends_on:
      - mongodb
    restart: always
    environment:
      - ME_CONFIG_MONGODB_ADMINUSERNAME=admin
      - ME_CONFIG_MONGODB_ADMINPASSWORD=password
      - ME_CONFIG_MONGODB_SERVER=mongodb

Nischal2015 avatar Mar 11 '23 11:03 Nischal2015

I also have same issue with this currently trying using cli now I guess compose is the only option

TESLAOPUnix avatar Apr 29 '23 05:04 TESLAOPUnix

The documentation on Docker Hub is outdated: see https://github.com/mongo-express/mongo-express/issues/1414 and #106 After 1.0 the usage of ME_CONFIG_MONGODB_SERVER has been changed, normally you would use ME_CONFIG_MONGODB_URL, e.g. mongodb://admin:password@mongodb:27017

Make sure to read the README here on GitHub regarding the tag you are using.

maddes-b avatar Feb 21 '24 13:02 maddes-b

add this env param to mongo-express ME_CONFIG_BASICAUTH: false It worked for me! 🚀

ademaldemir avatar May 23 '24 15:05 ademaldemir