tencent cloud


Connection Sample

Last updated: 2019-08-12 11:33:00

    After initializing the instance, you can use either MongoDB shell or drivers in different languages to access the database to perform administrative operations. Using CVM, you can only access the database through the private network, instead of the public network.


    Client version

    A driver with version 3.2 or above is required to connect to the TencentDB for MongoDB service. Please use the latest version of the client driver, including shell kit, java jar package, php expansion and nodejs module, to achieve the best compatibility. For more information, see MongoDB Drivers.

    Use mongo shell to connect to MongoDB

    The mongo shell is an interactive JavaScript interface to MongoDB. You can use the mongo shell to query and update data as well as perform administrative operations. The mongo shell is a component of the MongoDB distributions. Once you have installed and have started MongoDB, connect the mongo shell to your running MongoDB instance. Download a MongoDB distribution here link.

    The example below describes how to start the mongo Shell and connect to MongoDB:

        cd <mongodb installation dir>
        ./bin/mongo -umongouser -plxh2081*

    In the above example, "-u" indicates the user name, "-p" indicates the password, “” is the IP, and 27017 is the default port of the MongoDB instance.

    Use connection String URI to connect to MongoDB

    The MongoDB service can be connected by passing parameters, and most drivers can also be connected by using URI. Connection to the MongoDB service using URI is officially recommended by MongoDB. You can specify URI to define the connections between most of the applications and MongoDB instances in the official MongoDB drivers. MongoDB recommends you use connection string URI.

    Below are examples of URIs:

    Example 1


    Example 2


    Example 3


    The parameters in URI are described as follows:

    Parameter Description Required
    mongodb:// A specific string indicating MognoDB protocol Yes
    username The user name used to log in to the MongoDB service Yes. See "Default users" on this page.
    password The user password used to log in to the MongoDB service Yes
    IP:27017 IP and port of the MongoDB service Yes
    /admin The database to be authenticated. TencentDB for MongoDB is always admin. Yes. See "Authentication database" on this page.
    authMechanism=MONGODB-CR Authentication method See "Authentication mechanism" on this page.
    authSource=admin The database for authentication. TencentDB for MongoDB is always admin. Yes. See "Authentication database" on this page.
    readPreference=secondaryPreferred You can decide whether the slave has higher read priority No. See "Master/Slave Read Priority" on this page.
    Only some of the parameters for the connection string URI are listed here. For more information, see MongoDB official reference documentation.

    Default users

    The default users vary by the version of TencentDB for MongoDB. For the latest instances, The built-in default users are "rwuser" and "mongouser". For older instances, the default user is "rwuser" (we will upgrade these old instances, and contact you before the upgrade). You can also use TencentDB for MongoDB Console to manage account and permission to meet your business requirements.

    Example of rwuser (MONGODB-CR authentication) URI

    MONGODB-CR authentication is used by rwuser only.


    Example of mongouser (SCRAM-SHA-1 authentication) URI

    SCRAM-SHA-1 authentication is used by mongouser and users created in the console.


    Authentication database

    TencentDB for MongoDB uses "admin" database as the authentication database for login authentication, so the port in the URI must be followed by "/admin" to specify the authentication database. After authentication, you can read and write the specific business database. URI example:


    You can also directly access the destination database by specifying a destination database for read and write operation and an additional authentication database parameter (authSource=admin). URI example:


    You must use one of the above methods to add admin as an authentication database into the URI.

    Authentication mechanism

    MongoDB supports multiple authentication methods. SCRAM-SHA-1 is recommended.
    TencentDB for MongoDB supports two authentication methods: "MONGODB-CR" and "SCRAM-SHA-1".
    As mentioned above, there are two default users built in TencentDB for MongoDB: "rwuser" and "mongouser", and you can also create additional users in the Console of TencentDB for MongoDB. These users are classified into two types based on the following authentication methods:

    User Name Authentication Mechanism Required in URI
    rwuser MONGODB-CR The parameter "authMechanism=MONGODB-CR" must be added.
    mongouser and users created in the console SCRAM-SHA-1 (recommended) No parameter needs to be added.

    Adjust priority for replica set member (Master/Slaves)

    TencentDB for MongoDB provides a load balancer IP to access the entire replica set. If you need slave databases to have higher read priority, add the "readPreference" parameter in the URI. Relevant values are described below:

    Value Description Default
    primary Reads master node only Yes
    primaryPreferred Reads master node first. If it is not available, read slave node.
    secondary Reads slave node only. If it is not available, an error will occur.
    secondaryPreferred Reads slave node first. If it is not available, read master node.

    To set up a priority to read slave node first, you can configure the URI as follows:


    Examples for Languages


    Shell Connection Example


    PHP Connection Example


    Node.js Connection Example
    mongoose Example


    Java Connection Example


    Python Connection Example

    Reconnection mechanism

    Reconnection Mechanism

    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support