Connect to MongoDB via SRV Record
Bizfly Cloud Database is a service that provides database solutions on a cloud computing platform, helping users and businesses implement an effective data management solution, ensuring data integrity recovery in disaster scenarios. It also monitors information related to users’ database management systems, automatically adjusting data storage capacity according to the resource usage needs of the database management system.
Bizfly Cloud Database is a free service provided by Bizfly Cloud during the Beta and trial period. The information related to usage costs is for reference only.
In MongoDB, an SRV record is used to identify endpoints for a replica set or a MongoDB cluster. The SRV record contains information about the domain, port, and other parameters related to the MongoDB connection configuration. The SRV record allows MongoDB applications to automatically and flexibly discover and connect to nodes in the replica set or MongoDB cluster. Currently, Bizfly Cloud’s Cloud Database service provides the feature to connect to MongoDB via SRV record, making it easier for customers to build database models suitable for their systems.
Connection Guide
Access the database instance interface of MongoDB.
Go to the Connection Information section and retrieve the connection information from the SRV Connection line.
Testing Guide
You can further test the patroni component through the failover feature with the following code:
from pymongo import MongoClient
# Connect to MongoDB using SRV record
client = MongoClient("mongodb+srv://USERNAME:PASSWORD@srv-BFC_Mongo-1b8dfff.dbaas.bfcplatform.vn/?ssl=false")
# Access a specific database
db = client['your_database']
# Access a specific collection
collection = db['your_collection']
# Perform operations on the collection
data = {'name': 'John', 'age': 30}
collection.insert_one(data)
# Query documents in the collection
result = collection.find({'name': 'John'})
for doc in result:
print(doc)
The returned result will be:
{'_id': ObjectId('659619431769bd383d94980e'), 'name': 'John', 'age': 30}
After shutting down the primary node, the secondary node is promoted to become the primary node, and the connection remains uninterrupted.
The returned result will be:
{'_id': ObjectId('659619431769bd383d94980e'), 'name': 'John', 'age': 30}
{'_id': ObjectId('6596606405b3da45c1c3f21c'), 'name': 'John', 'age': 30}