Updated on 2022-09-20 GMT+08:00

Java

If you are connecting to an instance using Java, an SSL certificate is optional, but downloading an SSL certificate and encrypting the connection will improve the security of your instance. SSL is disabled by default for newly created instances, but you can enable SSL by referring to Enabling or Disabling SSL. SSL encrypts connections to databases but it increases the connection response time and CPU usage. For this reason, enabling SSL is not recommended.

Prerequisites

Familiarize yourself with:

  • Computer basics
  • Java code

Using an SSL Certificate

Download the SSL certificate and verify the certificate before connecting to databases.

In the DB Information area on the Basic Information page, click in the SSL field to download the root certificate or certificate bundle.

If you connect to a cluster instance using Java, the format of code is as follows:
mongodb://<username>:<password>@<instance_ip>:<instance_port>/<database_name>?authSource=admin&ssl=true
Table 1 Parameter description

Parameter

Description

<username>

Current username.

<password>

Password for the current username

<instance_ip>

If you attempt to access the instance from an ECS, set instance_ip to the private IP address displayed on the Basic Information page of the instance to which you intend to connect.

If you intend to access the instance through an EIP, set instance_ip to the EIP that has been bound to the instance.

<instance_port>

Database port displayed on the Basic Information page. Default value: 8635

<database_name>

Name of the database to be connected.

authSource

Authentication user database. The value is admin.

ssl

Connection mode. true indicates that the SSL connection mode is used.

For details about the Java code, see the following example:

import com.mongodb.ConnectionString;
import com.mongodb.reactivestreams.client.MongoClients;
import com.mongodb.reactivestreams.client.MongoClient;
import com.mongodb.reactivestreams.client.MongoDatabase;
import com.mongodb.MongoClientSettings;
public class MyConnTest { 
    final public static void main(String[] args) { 
	try {
        // set ssl 
	ConnectionString connString = new ConnectionString("mongodb://rwuser:<password>@192.*.*.*:8635,192.*.*.*:8635/test?authSource=admin&ssl=true");
	MongoClientSettings settings = MongoClientSettings.builder()
		.applyConnectionString(connString)
		.retryWrites(true)
		.build();
	MongoClient mongoClient = MongoClients.create(settings);
	MongoDatabase database = mongoClient.getDatabase("test");
     System.out.println("Connect to database successfully");  
	} catch (Exception e) { 
            e.printStackTrace(); 
            System.out.println("Test failed"); 
        } 
}
} 

Connection Without the SSL Certificate

You do not need to download the SSL certificate because certificate verification on the server is not required.

If you connect to a cluster instance using Java, the format of code is as follows:
mongodb://<username>:<password>@<instance_ip>:<instance_port>/<database_name>?authSource=admin
Table 2 Parameter description

Parameter

Description

<username>

Current username.

<password>

Password for the current username

<instance_ip>

If you attempt to access the instance from an ECS, set instance_ip to the private IP address displayed on the Basic Information page of the instance to which you intend to connect.

If you intend to access the instance through an EIP, set instance_ip to the EIP that has been bound to the instance.

<instance_port>

Database port displayed on the Basic Information page. Default value: 8635

<database_name>

Name of the database to be connected.

authSource

Authentication user database. The value is admin.

For details about the Java code, see the following example:

import com.mongodb.ConnectionString;
import com.mongodb.reactivestreams.client.MongoClients;
import com.mongodb.reactivestreams.client.MongoClient;
import com.mongodb.reactivestreams.client.MongoDatabase;
import com.mongodb.MongoClientSettings;
public class MyConnTest { 
    final public static void main(String[] args) { 
	try {
        // no ssl 
	ConnectionString connString = new ConnectionString("mongodb://rwuser:<password>@192.*.*.*:8635,192.*.*.*:8635/test?authSource=admin");
	MongoClientSettings settings = MongoClientSettings.builder()
		.applyConnectionString(connString)
		.retryWrites(true)
		.build();
	MongoClient mongoClient = MongoClients.create(settings);
	MongoDatabase database = mongoClient.getDatabase("test");
     System.out.println("Connect to database successfully");  
	} catch (Exception e) { 
            e.printStackTrace(); 
            System.out.println("Test failed"); 
        } 
}
}