mrvautin / openKB

Open Source Nodejs Markdown based knowledge base (FAQ) app
https://openkb.markmoffat.com
MIT License
654 stars 200 forks source link

mongodb credentials #272

Closed WebMatrixware closed 5 years ago

WebMatrixware commented 5 years ago

How do you pass credentials and db name for mongodb? There is no clarification and this is something you will always have to do for an external DB.

WebMatrixware commented 5 years ago

Checked on password protected connection string and found details on mongodb site. [https://docs.mongodb.com/manual/reference/connection-string/]

Still throws error even though I can connect to the db while up with mongodb compass.

uhs-docs-openkb_v0.4.1 |
uhs-docs-openkb_v0.4.1 | > openkb@1.0.23 start /var/openKB
uhs-docs-openkb_v0.4.1 | > node app.js
uhs-docs-openkb_v0.4.1 |
mongodb_1  | 2019-06-20T11:48:03.276+0000 I NETWORK  [listener] connection accepted from 172.18.0.1:55808 #1 (1 connection now open)
mongodb_1  | 2019-06-20T11:48:03.284+0000 I ACCESS   [conn1] Successfully authenticated as principal root on admin from client 172.18.0.1:55808
uhs-docs-openkb_v0.4.1 | Error connecting to MongoDB: MongoError: failed to connect to server [127.0.0.1:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017]
uhs-docs-openkb_v0.4.1 | clean
uhs-docs-openkb_v0.4.1 | TypeError: Cannot read property 'close' of undefined
uhs-docs-openkb_v0.4.1 |     at exitHandler (/var/openKB/app.js:364:19)
uhs-docs-openkb_v0.4.1 |     at emitOne (events.js:96:13)
uhs-docs-openkb_v0.4.1 |     at process.emit (events.js:188:7)
uhs-docs-openkb_v0.4.1 |     at process.exit (internal/process.js:146:15)
uhs-docs-openkb_v0.4.1 |     at MongoClient.connect (/var/openKB/app.js:336:21)
uhs-docs-openkb_v0.4.1 |     at connectCallback (/var/openKB/node_modules/mongodb/lib/mongo_client.js:527:5)
uhs-docs-openkb_v0.4.1 |     at /var/openKB/node_modules/mongodb/lib/mongo_client.js:418:11
uhs-docs-openkb_v0.4.1 |     at _combinedTickCallback (internal/process/next_tick.js:67:7)
uhs-docs-openkb_v0.4.1 |     at process._tickCallback (internal/process/next_tick.js:98:9)
uhs-docs-openkb_v0.4.1 exited with code 0
Wachiwi commented 5 years ago

As far as I understand your MongoDB instance is not on the same host as your openKB instance? If so your connection string for MongoDB still includes 127.0.0.1 aka localhost.

 ECONNREFUSED 127.0.0.1:27017
WebMatrixware commented 5 years ago

I am running two containers through docker desktop, openKB in one and mongodb in the other. I can connect with mongodb compass utility to 127.0.0.1:27017 as noted in the last post.

EDIT: as I typed that I realized what you meant @Wachiwi (I think what you meant at least), it is localhost for my desktop, but not for the openkb container. Let me try a change to the string here and update.

Wachiwi commented 5 years ago

@WebMatrixware Any news on this? Could you solve your issue?

WebMatrixware commented 5 years ago

I did. It turns out I had two issues that were obscuring each other.

1) I was forgetting that though I access both the mongodb instance and the openKB site from my localhost, that is being passed through and they are not localhost to each other. As a result I needed to use a named network in my docker-compose file and point the config path for the mongodb in openKB at the name of that host, rather than at localhost as I was thinking.

2) The syntax suggested in the documents page does not provide any credentials in the string which is never the case on any real mongo instance you will be connecting to, so I was chasing around a little to make sure I was adjusting the connection string correctly without breaking where I was pointing it. This turned out to not be hard, but I was not sure I had it right at one point due to problem #1. A more real-world example in the docs would go a long way on this one.

Anyway, my config line now looks like

        "database": {
          "type": "mongodb",
          "connection_string": "mongodb://root:amcdba@mongo:27017"
        },

And my compose file is

version: "3.7"

networks:
  internal:

volumes:
  mongodb-store:
    name: mongodb-store

services:
  openkb:
    build:
      context: ./openKB
    image: docs-openkb:v0.4.4
    container_name: docs-openkb_v0.4.4

    ports:
      - "4444:4444"
    networks:
      - internal
    depends_on:
      - mongodb
    stdin_open: true
    tty: true

  mongodb:
    image: mongo
    container_name: mongo
    ports:
      - "27017:27017"
    networks:
      - internal
    volumes:
      - mongodb-store:/data
    environment:
      MONGO_INITDB_ROOT_USERNAME: someuser
      MONGO_INITDB_ROOT_PASSWORD: somepassword

And finally the Dockerfile I am using (I know he suggests using the straight alpine image, but why not let the folks at node set that part up for you rather than reinventing the wheel...)

FROM node:alpine

WORKDIR /var/openKB

COPY openKB/locales/ /var/openKB/locales/
COPY openKB/public/ /var/openKB/public/
COPY openKB/routes/ /var/openKB/routes/
COPY openKB/views/ /var/openKB/views/
COPY openKB/config/ /var/openKB/config/
COPY openKB/app.js /var/openKB/
COPY openKB/package.json /var/openKB/

RUN npm install

VOLUME /var/openKB/data

EXPOSE 4444
ENTRYPOINT ["npm", "start"]