Get the App
SLTechnology News&Howtos  ›  Database  › 

The solution that mongodb can not directly obtain the information of primary in secondary

Shulou Source: shulou.com Published: 2022-06-01 12:55:24 10月05日 Update

If you access mongo through shell, query it in secondary. The following error occurs:

[root@mongodb-secondary2 ~] # mongoMongoDB shell version: 3.0.9connecting to: testabc:SECONDARY > show dbs2016-02-20T02:13:18.527+0800 E QUERY Error: listDatabases failed: {"note": "from execCommand", "ok": 0, "errmsg": "not master"}

There are two ways to implement slave query:

The first method: db.getMongo (). SetSlaveOk ()

The second method: rs.slaveOk ()

But one drawback of this approach is that the next time you enter the instance through mongo, the query will still report an error, which can be done in the following ways

In order to avoid how many mongodb are vi .mongorc.js without setting weights, add one line: rs.slaveOk ();, this is to avoid the original primary, to prevent another secondary from switching to primary after primary failure, and to change the original primary repair to secondary without permission to execute rs.slaveOk (). It is best to add x permissions to the .mongorc.js file:

Vi monogorc.jsrs.slaveOk (): wq

[root@mongodb-secondary1] # chmod uplix.mongorc.js

In this way, you can query it every time you enter it through the mongo command.

[root@mongodb-secondary1 ~] # mongoMongoDB shell version: 3.0.9connecting to: testabc:PRIMARY > use mydbswitched to db mydbabc:PRIMARY > show tablessystem.indexestestc

The above method can solve the problem that secondary logs in again without prompting "not master".

Tags: Method query time original way permission for this reason in this case one line content again can be passed command instance that is case fault file best weight Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft MySQL vpn Shulou Technology Linux