【问题标题】:Docker mongo - MongoError: command insert requires authenticationDocker mongo - MongoError:命令插入需要身份验证
【发布时间】:2020-08-15 08:36:42
【问题描述】:

我第一次尝试将 mongodb 用于在 docker 上运行的 node.js 应用程序

这是我的 docker-compose 文件

version: '3'

services:
    nodejs:
        container_name: sandbox_app
        build:
            context: .
            dockerfile: Dockerfile
        ports:
            - '3000:3000'
        volumes:
            - '.:/usr/src/app'
            - '/usr/src/app/node_modules'
        networks:
            - sandboxnet
        links:
            - mongo
    mongo:
        image: mongo
        container_name: sandbox_db
        # restart: always
        ports:
            - '27017:27017'
        environment:
            MONGO_INITDB_ROOT_USERNAME: root
            MONGO_INITDB_ROOT_PASSWORD: example
        networks:
            - sandboxnet
    mongo-express:
        image: mongo-express
        # restart: always
        ports:
            - 8081:8081
        environment:
            ME_CONFIG_MONGODB_ADMINUSERNAME: root
            ME_CONFIG_MONGODB_ADMINPASSWORD: example
        depends_on:
            - mongo
        networks:
            - sandboxnet

volumes:
    node_modules:
    web-root:
        driver: local
networks:
    sandboxnet:
        driver: 'bridge'

这是我的 app.js 文件

const mongodb = require('mongodb');
const MongoClient = mongodb.MongoClient;

const connectionUrl = 'mongodb://mongo:27017';
const databaseName = 'sandbox_db';

MongoClient.connect(
    connectionUrl,
    { useNewUrlParser: true },
    (error, client) => {
        if (error) {
            return console.log('Unable to connect to the database :(');
        }

        console.log('Connected succesfully to mongo :)');
        const db = client.db(databaseName);

        db.collection('users').insertOne({
            name: 'John Doe'
        });
    }
);

当我运行 docker-compose up 时,我从终端上的 mongo 收到以下错误,并且看不到新数据库创建的用户表或添加的记录。我该如何解决这个问题?

sandbox_app | Connected succesfully to mongo :)  // So skips the first console.log as there are no connection errors
sandbox_app | (node:80) UnhandledPromiseRejectionWarning: MongoError: command insert requires authentication

另外,如果我 docker-compose exec mongo bash 我明白了

docker-compose exec mongo bash
root@ashdja87a89s:/# mongo
MongoDB shell version v4.2.6
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("14aaa69b-8a7a-462b-ac5a-ff4ae0305d8b") }
MongoDB server version: 4.2.6
> show dbs
> 

【问题讨论】:

  • 从那个 mongo shell,尝试运行 db.runCommand("connectionStatus") 看看它是否为你隐式验证。
  • "authenticatedUsers" : [ ], "authenticatedUserRoles" : [ ] 都是空的。
  • 你需要在你的 mongo uri 连接字符串中包含 usernamepassword,看看这里 docs.mongodb.com/manual/reference/connection-string
  • 你指的是当我在MongoClient.connect() 中建立联系时?我需要包含哪些用户,我在 docker-compose 文件中使用的MONGO_INITDB_ROOT_USERNAME

标签: node.js mongodb docker


【解决方案1】:

使用用户名/密码拨打mongo

root@06e8e28840bb:/# mongo -u root -p example

你来了

> db.runCommand("connectionStatus")
{
"authInfo" : {
    "authenticatedUsers" : [
        {
            "user" : "root",
            "db" : "admin"
        }
    ],
    "authenticatedUserRoles" : [
        {
            "role" : "root",
            "db" : "admin"
        }
    ]
},
"ok" : 1

}

用户名/密码在docker-compose.yml中定义

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

【讨论】:

    猜你喜欢
    • 2019-03-25
    • 2019-12-17
    • 1970-01-01
    • 2021-04-10
    • 2020-11-16
    • 2022-01-10
    • 2019-07-20
    • 2021-11-22
    • 1970-01-01
    相关资源
    最近更新 更多