问题
I have a 3 member replica set. Read preference is set as "Secondary Preferred" How to check application is reading from Secondary Node in MongodB? Please suggest.
回答1:
Firstly you can configure profiling. For that you need to start your mongodb servers with option --profile 2
and configure log file. It'll log all queries.
After that you can read log for each instance db. Simple example:
db.your_collection.profile.find({ns: "name_your_db.your_collection"})
Secondly you can use mongotop
. You need to start it for each mongodb server.
For example:
mongotop -h server_ip:server_port seconds
mongotop -h 127.0.0.1:27017 5
It'll print every specified period of time log, where you can read how much time for read or write is taken for each collection.
回答2:
Other means of determining whether queries are sent to secondaries:
- Enable command logging in the driver, which should tell you which server each command was sent to.
- Inspect server logs for connections being made or not made, then set minimum connection pool size to 0 so that a connection is only made when needed for a query, then run some queries.
来源:https://stackoverflow.com/questions/34267909/how-to-check-application-is-reading-from-secondary-node-in-mongodb