Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-04-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

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".

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report