How to check application is reading from Secondary Node in MongodB? - mongodb

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.

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.

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.

Related

MongoDB connection abnormal increase

I use mongodb client to connect Database like here. Notice, i only use one connect to DB, but when i start my application and check Mongodb connection by command.
db.serverStatus().connections
Before start app at 8 connection, after it increase more 20 connection. I miss something, any one help me to explain ?

How to monitor mongo db replica status and reconfiguring repluica

We are using mongodb cluster with set of 3 members , and all consumers are able to read/write data.
Use mongo.conf file to create cluster && configure replica set, which are executed in ansible script as part of system restart.
Our system does heavy writes/read operation on mongo clusters ,and sometime replica get broken.
Then it is no longer feasible to restore replica without deleting data from one of the corrupted server.
Here are my queries:
what is better way to notify when replica is broken using script/some code?We can not use Mongo atlas
etc.
What is efficient way to restore data on corrupted member.
What is better way to notify when replica is broken using script/some code?
Use the replSetGetStatus database command.
You can run this command from any driver or the shell. Among other things, it returns the state (primary/secondary/startup/recovery/unreachable) and last applied operation time for each member.
What is efficient way to restore data on corrupted member.
Check out the docs for a few options: https://docs.mongodb.com/manual/tutorial/resync-replica-set-member/#resync-a-member-of-a-replica-set

Restore HAProxy stats

I use following command to restart HAProxy, when changing the configurration file:
/usr/local/sbin/haproxy -f /etc/haproxy.cfg -p /var/run/haproxy.pid -sf $(</var/run/haproxy.pid)
Sadly after the HAProxy is back all stats of the previous launch are away.
Is there a possibility in HAProxy to restore stats from a previous HAProxy start?
As of version 1.6, you can dump server states into a flat file right before performing the reload and let the new process know where the states are stored.
See example here:seamless_reload
The "show servers state" command is used to keep servers uptime and healthy status cross reload, but it doesn't give session data, or bytes in/out, etc. "show stat" command can dump these stats to a file that you can use to create a report later, although HAproxy doesn't have a feature to reload this information.
Can't be done unfortunately. HAProxy's stats are all in memory, so when restarting (even gracefully with -sf), those stats get lost.
Might you can export data to CSV file before doing reload/restart
"http://localhost:8080/haproxy?stats;csv"
or
curl -u <USER>:<MyPASSWORD> "http://localhost:8080/haproxy?stats;csv"
according to HAproxy 1.5 doc you can clear all stats using the unix socket.
clear counters all
Clear all statistics counters in each proxy (frontend & backend) and in each
server. This has the same effect as restarting. This command is restricted
and can only be issued on sockets configured for level "admin".

Feasible to install mongo-connector on separate server from mongo databases?

Does anyone know if its more feasible to install mongo-connector on its own server or to just have it running on one of the mongo database servers? I currently have it running on a secondary mongo server and want to move it off the secondary server but I am not sure if it really matters.
Mongo Connector is just another client application connecting to your MongoDB server. You can install it wherever you like, as long as it is able to connect to your MongoDB server and whatever external system you're trying to feed data to.
Note: If Mongo Connector is matching a comparatively small selection of data in the replication oplog to insert into your external system, it would make sense to keep this on the same secondary or node whose oplog is being monitored. Otherwise the Connector will be pulling the full oplog over the network, which may consume unnecessary bandwidth.

How to get a consistent MongoDB backup for a single node setup

I'm using MongoDB in a pretty simple setup and need a consistent backup strategy. I found out the hard way that wrapping a mongodump in a lock/unlock is a bad idea. Then I read that the --oplog option should be able to provide consistency without lock/unlock. However, when I tried that, it said that I could only use the --oplog option on a "full dump." I've poked around the docs and lots of articles but it still seems unclear on how to dump a mongo database from a single point in time.
For now I'm just going with a normal dump but I'm assuming that if there are writes during the dump it would make the backup not from a single point in time, correct?
mongodump -h $MONGO_HOST:$MONGO_PORT -d $MONGO_DATABASE -o ./${EXPORT_FILE} -u backup -p password --authenticationDatabase admin
In production environment, MongoDB is typically deployed as replica set(s) to ensure redundancy and high availability. There are a few options available for point in time backup if you are running a standalone mongod instance.
One option as you have mentioned is to do a mongodump with –oplog option. However, this option is only available if you are running a replica set. You can convert a standalone mongod instance to a single node replica set easily without adding any new replica set members. Please check the following document for details.
http://docs.mongodb.org/manual/tutorial/convert-standalone-to-replica-set/
This way, if there are writes while mongodump is running, they will be part of your backup. Please see Point in Time Operation Using Oplogs section from the following link.
http://docs.mongodb.org/manual/tutorial/backup-databases-with-binary-database-dumps/#point-in-time-operation-using-oplogs
Be aware that using mongodump and mongorestore to back up and restore MongodDB can be slow.
File system snapshot is another option. Information from the following link details two snapshot options for performing hot backup of MongoDB.
http://docs.mongodb.org/manual/tutorial/backup-databases-with-filesystem-snapshots/
You can also look into MongoDB backup service.
http://www.10gen.com/products/mongodb-backup-service
In addition, mongodump with oplog options does not work with single db/collection at this moment. There are plans to implement the feature. You can follow the ticket and vote for the feature under the More Actions button.
https://jira.mongodb.org/browse/SERVER-4273