连接到MongoDB Atlas:com.mongodb.MongoCommandException:命令失败,错误8000

时间:2018-01-04 14:42:00

标签: java mongodb mongodb-atlas

所以我试图连接MongoDB Atlas服务,它托管我的数据库(我过去没有遇到任何问题),但我一直收到这个错误,我无法理解为什么。我找不到任何有同样问题的人,所以我很困惑。

Exception in thread "main" com.mongodb.MongoCommandException: Command failed with error 8000: 'not authorized on admin to execute command { insert: "adminCol", ordered: true, documents: [[{_id ObjectIdHex("5a4e3b6dd04f1c047975fdd5")} {id 512} {name peter jones} {hello hi}]] }' on server *******-shard-00-02-xygnn.mongodb.net:27017. The full response is { "ok" : 0, "errmsg" : "not authorized on admin to execute command { insert: \"adminCol\", ordered: true, documents: [[{_id ObjectIdHex(\"5a4e3b6dd04f1c047975fdd5\")} {id 512} {name peter jones} {hello hi}]] }", "code" : 8000, "codeName" : "AtlasError" }
at com.mongodb.connection.ProtocolHelper.getCommandFailureException(ProtocolHelper.java:164)
at com.mongodb.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:295)
at com.mongodb.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:255)
at com.mongodb.connection.UsageTrackingInternalConnection.sendAndReceive(UsageTrackingInternalConnection.java:98)
at com.mongodb.connection.DefaultConnectionPool$PooledConnection.sendAndReceive(DefaultConnectionPool.java:441)
at com.mongodb.connection.CommandProtocolImpl.execute(CommandProtocolImpl.java:76)
at com.mongodb.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:189)
at com.mongodb.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:263)
at com.mongodb.connection.DefaultServerConnection.command(DefaultServerConnection.java:126)
at com.mongodb.operation.MixedBulkWriteOperation.executeCommand(MixedBulkWriteOperation.java:373)
at com.mongodb.operation.MixedBulkWriteOperation.executeBulkWriteBatch(MixedBulkWriteOperation.java:255)
at com.mongodb.operation.MixedBulkWriteOperation.access$700(MixedBulkWriteOperation.java:66)
at com.mongodb.operation.MixedBulkWriteOperation$1.call(MixedBulkWriteOperation.java:199)
at com.mongodb.operation.MixedBulkWriteOperation$1.call(MixedBulkWriteOperation.java:190)
at com.mongodb.operation.OperationHelper.withReleasableConnection(OperationHelper.java:432)
at com.mongodb.operation.MixedBulkWriteOperation.execute(MixedBulkWriteOperation.java:190)
at com.mongodb.operation.MixedBulkWriteOperation.execute(MixedBulkWriteOperation.java:66)
at com.mongodb.Mongo$3.execute(Mongo.java:833)
at com.mongodb.MongoCollectionImpl.executeSingleWriteRequest(MongoCollectionImpl.java:1025)
at com.mongodb.MongoCollectionImpl.executeInsertOne(MongoCollectionImpl.java:513)
at com.mongodb.MongoCollectionImpl.insertOne(MongoCollectionImpl.java:493)
at com.mongodb.MongoCollectionImpl.insertOne(MongoCollectionImpl.java:487)
at AdminDB.addAdmin(AdminDB.java:51)
at AdminDB.main(AdminDB.java:76)

这是我正在使用的代码。

public class AdminDB {
    private MongoDatabase adminDB;
    private MongoCollection<Document> adminCollection;
    private String adminCol = "adminCol";

    public AdminDB() {
        MongoClientURI uri = new MongoClientURI(
                "mongodb://***********-shard-00-00-xygnn.mongodb.net:27017,*******-shard-00-01-xygnn.mongodb.net:27017,******-shard-00-02-xygnn.mongodb.net:27017/test?ssl=true&replicaSet=******-shard-0&authSource=admin");

        MongoClient mongoClient = new MongoClient(uri);
        adminDB = mongoClient.getDatabase("admin");
        adminCollection = adminDB.getCollection(adminCol);
    }

    public void addAdmin(JsonObject json) {
        adminCollection.insertOne(Document.parse(json.toString()));
    }

    public static void main(String[] args) {
        AdminDB adminDB = new AdminDB();
        JsonObject json = new JsonObject();
        json.addProperty("id", 512);
        json.addProperty("name", "peter jones");
        json.addProperty("hello", "hi");

        adminDB.addAdmin(json);
    }
}

1 个答案:

答案 0 :(得分:1)

为了澄清,MongoDB Atlas M0(免费层),M2或M5共享入门集群不支持对admin数据库中的任何集合进行读/写操作。另请参阅Command Limitations in Free Tier Clusters

更改数据库名称以将文档插入:

    database = mongoClient.getDatabase("anotherDatabase");