MongoDB connection to specific DB not working

default discord avatar
harleyqueens
10 months ago
37

MONGODB_URI=mongodb://root:password@mongodb_container:27017/evcs


payload aint working


is it supposed to be database, then port?


I just renamed my DB


in mongodb


trying to switch payload over, getting destroyed



Original connection I had was



MONGODB_URI=mongodb://root:password@mongodb_container:27017/



btw i am using mongodb_container instead of localhost



due to container stuff

  • discord user avatar
    denolfe
    Payload Team
    10 months ago

    is mongo running via docker-compose or something? What network mode?

  • default discord avatar
    harleyqueens
    10 months ago

    Mongo is running via docker-compose

  • discord user avatar
    denolfe
    Payload Team
    10 months ago

    I would expect it to still be localhost if you're running locally and just hitting a DB running in docker

  • default discord avatar
    harleyqueens
    10 months ago

    Yes, but I am trying to connect from another container



    furthermore, if I do



    MONGODB_URI=mongodb://root:password@localhost



    it works



    but



    MONGODB_URI=mongodb://root:password@mongodb_container/evcs it does not work

  • discord user avatar
    denolfe
    Payload Team
    10 months ago

    I'm not very familiar with communicating container to container, and it seems outside the realm of Payload. Might need to look elsewhere to troubleshoot. Here is the docker-compose that can be used for development - that may be some help, not sure.



    version: '3'
    
    services:
    
      payload:
        image: node:18-alpine
        ports:
          - "3000:3000"
        volumes:
          - .:/home/node/app
          - node_modules:/home/node/app/node_modules
        working_dir: /home/node/app/
        command: sh -c "yarn install && yarn dev"
        depends_on:
          - mongo
        environment:
          MONGODB_URI: mongodb://mongo:27017/payload
          PORT: 3000
          NODE_ENV: development
          PAYLOAD_SECRET: TESTING
    
      mongo:
        image: mongo:latest
        ports:
          - "27017:27017"
        command:
          - --storageEngine=wiredTiger
        volumes:
          - data:/data/db
        logging:
          driver: none
    
    volumes:
      data:
      node_modules:
  • default discord avatar
    harleyqueens
    10 months ago

    Thats fine Elliot



    how do I then connect to



    MONGODB_URI=mongodb://root:password@localhost/evcs



    ?



    Because I cannot connect to mongodb container even in localhsot



    from my host machine



    MONGODB_URI=mongodb://root:password@localhost



    Works



    but adding /evcs doesnt

  • discord user avatar
    denolfe
    Payload Team
    10 months ago

    That seems like a valid URI to me. Maybe a permissions thing? What is the error?

  • default discord avatar
    harleyqueens
    10 months ago

    Will paste it soon



    [03:12:09] ERROR (payload): Error: cannot connect to MongoDB. Details: Authentication failed.

  • discord user avatar
    denolfe
    Payload Team
    10 months ago

    Yeah, seems like your user creds don't have permission to create a new DB possibly

  • default discord avatar
    harleyqueens
    10 months ago

    Hello Elliot



    MongoServerError: not authorized on test to execute command { aggregate: "users", pipeline: [ { $match: {} }, { $group: { _id: 1, n: { $sum: 1 } } } ], cursor: {}, lsid: { id: UUID("49c9917e-62a5-4adf-a529-29d73271d386") }, $db: "test" }



    So I have done things correctly



    1. Created a new database called EVCS



    2. Gave the correct user to EVCS via mongosh shell



    3. Connected successfully with payload



    4. But payload is still wired to target the "test" database

  • discord user avatar
    denolfe
    Payload Team
    10 months ago

    This is getting into MongoDB administration territory, but a quick search of that error turns up some solutions.

    https://dba.stackexchange.com/questions/285345/not-authorized-on-test-to-execute-command


    Custom auth db of some sort possibly

  • default discord avatar
    harleyqueens
    10 months ago

    Thx it works now!



    I thought it was a payload error



    lmao

  • discord user avatar
    denolfe
    Payload Team
    10 months ago

    Glad we were able to figure it out

  • default discord avatar
    Brian WIlliams
    3 months ago

    I had a similar issue with using a local podman container. For me, I was getting the same error, but when trying to initialize payload for the first time with

    yarn dev

    . The solution for me was to add

    ?authSource=admin

    to the end of my MONGODB_URL in .env after the payload database name.

Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.