暂无图片
暂无图片
1
暂无图片
暂无图片
暂无图片

MongoDB搭建完成replica set之后,备库读无法读

原创 伟鹏 2023-12-13
569

背景:

mongodb:7.0.4
架构:primary+replicat set+arbiter
问题:备库不能读

1、主库查询相关数据

wp [direct: primary] test> db.col.find({"likes":{$gt:50},$or:[{"by":"菜鸟教程"},{"title":"MongoDB"}]}).pretty() [ { _id: ObjectId('65795597419293c242906af7'), title: 'MongoDB', description: 'MongoDB 是一个nosql数据库', by: '菜鸟教程', url: 'http://www.runoob.com', tags: [ 'mongodb', 'database', 'NoSQL' ], likes: 100 }, { _id: ObjectId('6579566a419293c242906af8'), title: 'MongoDB', description: 'MongoDB 是一个Nosql数据库', by: '菜鸟教程', url: 'http://www.runoob.com', tags: [ 'mongodb', 'database', 'Nosql' ], likes: 100 }, { _id: ObjectId('65795819419293c24290b91c'), titile: 'MongoDB', description: 'MongoDB是一个Nosql数据库', by: '菜鸟教程', url: 'http://www.runoob.com', tags: [ 'mongodb', 'database', 'Nosql' ], likes: 100 } ]

2、replica set查看

swp [direct: secondary] test> db.col.find().pretty() MongoServerError: not primary and secondaryOk=false - consider using db.getMongo().setReadPref() or readPreference in the connection string

更换查询方式,可以查出来

swp [direct: secondary] test> db.col.find({}).readPref("secondary") [ { _id: ObjectId('65795597419293c242906af7'), title: 'MongoDB', description: 'MongoDB 是一个nosql数据库', by: '菜鸟教程', url: 'http://www.runoob.com', tags: [ 'mongodb', 'database', 'NoSQL' ], likes: 100 }, { _id: ObjectId('6579566a419293c242906af8'), title: 'MongoDB', description: 'MongoDB 是一个Nosql数据库', by: '菜鸟教程', url: 'http://www.runoob.com', tags: [ 'mongodb', 'database', 'Nosql' ], likes: 100 }, { _id: ObjectId('65795819419293c24290b91c'), titile: 'MongoDB', description: 'MongoDB是一个Nosql数据库', by: '菜鸟教程', url: 'http://www.runoob.com', tags: [ 'mongodb', 'database', 'Nosql' ], likes: 100 } ]

mongodb 的primary+replica set 默认情况下是不支持从库读的

3、查看help命令

swp [direct: secondary] test> rs.help() Replica Set Class: initiate Initiates the replica set. config Returns a document that contains the current replica set configuration. conf Calls replSetConfig reconfig Reconfigures an existing replica set, overwriting the existing replica set configuration. reconfigForPSASet Reconfigures an existing replica set, overwriting the existing replica set configuration, if the reconfiguration is a transition from a Primary-Arbiter to a Primary-Secondary-Arbiter set. status Calls replSetGetStatus isMaster Calls isMaster hello Calls hello printSecondaryReplicationInfo Calls db.printSecondaryReplicationInfo printSlaveReplicationInfo DEPRECATED. Use rs.printSecondaryReplicationInfo printReplicationInfo Calls db.printReplicationInfo add Adds replica set member to replica set. addArb Calls rs.add with arbiterOnly=true remove Removes a replica set member. freeze Prevents the current member from seeking election as primary for a period of time. Uses the replSetFreeze command stepDown Causes the current primary to become a secondary which forces an election. If no stepDownSecs is provided, uses 60 seconds. Uses the replSetStepDown command syncFrom Sets the member that this replica set member will sync from, overriding the default sync target selection logic. secondaryOk This method is deprecated. Use db.getMongo().setReadPref() instead

4、更改配置

swp [direct: secondary] test> rs.secondaryOk() DeprecationWarning: .setSecondaryOk() is deprecated. Use .setReadPref("primaryPreferred") instead Setting read preference from "primary" to "primaryPreferred"

5、查看结果

可以正常查询了

swp [direct: secondary] test> db.col.find() [ { _id: ObjectId('65795597419293c242906af7'), title: 'MongoDB', description: 'MongoDB 是一个nosql数据库', by: '菜鸟教程', url: 'http://www.runoob.com', tags: [ 'mongodb', 'database', 'NoSQL' ], likes: 100 }, { _id: ObjectId('6579566a419293c242906af8'), title: 'MongoDB', description: 'MongoDB 是一个Nosql数据库', by: '菜鸟教程', url: 'http://www.runoob.com', tags: [ 'mongodb', 'database', 'Nosql' ], likes: 100 }, { _id: ObjectId('65795819419293c24290b91c'), titile: 'MongoDB', description: 'MongoDB是一个Nosql数据库', by: '菜鸟教程', url: 'http://www.runoob.com', tags: [ 'mongodb', 'database', 'Nosql' ], likes: 100 } ]
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论