MongoDB Up And Running

I’ve tried to get up-and-running with a small MongoDB web application. I need to connect to a fresh mongodb on every CI run. I cannot connect. What am I missing?

Here is my config.yml:

version: 2.1
orbs:
  node: circleci/node@3.0.0
jobs:
  build:
    # pre-built images: https://circleci.com/docs/2.0/circleci-images/
    docker:
      - image: circleci/node:10-browsers
      - image: circleci/mongo:3.6
        environment:
          - MONGODB_USERNAME: mongouser
          - MONGODB_PASSWORD: mongopass
    steps:
      - setup_remote_docker
      - run:
          name: Waiting for Mongo
          command: dockerize -wait tcp://localhost:27017 -timeout 1m
      - run:
          name: Install mongo client
          command: |
            sudo apt-get install -y mongodb
      - checkout
      - run:
          name: Connect to DB
          command: mongo --username mongouser --password mongopass --eval "printjson(db.apikeys.count());" dbname

And here is the error in the “Connect to DB” step:

#!/bin/bash -eo pipefail
mongo --username=mongouser --password=mongopass --eval "printjson(db.apikeys.count());" dbname

MongoDB shell version: 3.2.11
connecting to: dbname
2021-04-01T17:13:49.031+0000 E QUERY    [thread1] Error: Authentication failed. :
DB.prototype._authOrThrow@src/mongo/shell/db.js:1441:20
@(auth):6:1
@(auth):1:2

exception: login failed

Exited with code exit status 1

CircleCI received exit code 1

I’ve noticed there are a lot of questions here about getting up and running with a Mongo app, not a lot of good answers. I want to use Mongo 4.x but cannot figure out how to install the db tools alone. So I’ve tried 3.6 and now I can’t authenticate for some reason? What am I missing?

Do you absolutely need auth to run your app in CI? I was able to get your config to run a green job by omitting the auth. If you need the auth do let me know, but it might be easier to bypass this blocker by removing it if unnecessary.

Maybe you could also share a bit more about your issue with Mongo4.x. I’m not sure I totally understand the issue with installing the db tools and that version. Additional insights would be helpful.

1 Like

Thank you! You saved me.

I’ve had some luck in another environment installing the mongo 4.x tools, so I will try that again too.

1 Like

The problem with Mongo 4.x is that the client tools cannot be installed.

According to this document Install MongoDB Community Edition on Ubuntu — MongoDB Manual I should be able to install the CLI using this circleci config:

version: 2.1
orbs:
  node: circleci/node@3.0.0
jobs:
  build:
    # pre-built images: https://circleci.com/docs/2.0/circleci-images/
    docker:
      - image: circleci/node:10-browsers
      - image: circleci/mongo:4.4
        environment:
          MONGO_USERNAME: mongouser
          MONGO_PASSWORD: mongopass
    steps:
      - run:
          name: MongoDB Install
          command: |
            wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
            echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list
            sudo apt-get update
            sudo apt-get install -y mongodb-org-shell
      - setup_remote_docker
      - run:
          name: Waiting for Mongo
          command: dockerize -wait tcp://localhost:27017 -timeout 1m
      - checkout
      - run:
          name: Connect to DB
          command: mongo localhost --eval "printjson(db.apikeys.count());"

But you get an error because those client tools require libcurl4 and you cannot install it:

The following packages have unmet dependencies:
mongodb-org-shell : Depends: libcurl4 (>= 7.16.2) but it is not installable
E: Unable to correct problems, you have held broken packages.

I was able to get the 4.x shell tools installed into the circleci/node:10-browsers image using the instructions here. Can you try them and see if they work for your job?

circleci@7f15d1a7272c:/$ mongo --version
MongoDB shell version v4.0.23
git version: 07c6611b38d2aacbdb1846b688db70b3273170fb
OpenSSL version: OpenSSL 1.1.0l  10 Sep 2019
allocator: tcmalloc
modules: none
build environment:
    distmod: ubuntu1804
    distarch: x86_64
    target_arch: x86_64
1 Like

Thank you again! It appears the problem was in the orb circleci/node@3.0.0. I didn’t realize that line was completely superfluous.

I have also upped the node version from 10 to 12.

Here is the final config file, which works for a web app in Node that opens a connection to a Mongo 4.4 server with the Mongo 4.0 client installed:

version: 2.1
jobs:
  build:
    docker:
      - image: circleci/node:12-browsers
      - image: circleci/mongo:4.4
        environment:
          MONGO_USERNAME: mongouser
          MONGO_PASSWORD: mongopass
    steps:
      - run:
          name: MongoDB Install
          command: |
             sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4
             echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list
             sudo apt-get update
             sudo apt-get install -y mongodb-org-shell
      - setup_remote_docker
      - run:
          name: Waiting for Mongo
          command: dockerize -wait tcp://localhost:27017 -timeout 1m
      - checkout
      - run:
          name: Get versions
          command: |
              node -v
              npm -v
              mongo -v
              mongo localhost --eval "db.version()"

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.