Common Methods for Connecting to a DDS DB Instance
This section describes how to connect to a DDS DB instance using the following three measures:
- Mongo shell
- Python Mongo client
- Java Mongo client
Mongo Shell
- Prerequisites
- To connect an ECS to a database, the ECS must be able to communicate with the DDS DB instance that runs the database. You can run the following command to connect to the IP address and port of the instance server to test the network connectivity.
If the message It looks like you are trying to access MongoDB over HTTP on the native driver port is displayed, the network connectivity is normal.
- On the MongoDB official website, you have downloaded the client installation package of version 3.2 or 3.4 that is consistent with the DB instance engine version. Decompress the package, obtain the mongo file, and upload it to the ECS.
- If SSL is enabled, you need to download the root certificate and upload it to the ECS.
- To connect an ECS to a database, the ECS must be able to communicate with the DDS DB instance that runs the database. You can run the following command to connect to the IP address and port of the instance server to test the network connectivity.
- Connection commands
- Precautions
- If SSL is enabled, the connection command must contain --ssl and --sslCAFile.
- The --authenticationDatabase value must be set to admin. If you log in to the database as user rwuser, switch to the admin database for authentication.
For details, see section Connecting to a DB Instance in Document Database Service Getting Started.
Python Mongo Client
- Prerequisites
- To connect an ECS to a database, the ECS must be able to communicate with the DDS DB instance that runs the database. You can run the following command to connect to the IP address and port of the instance server to test the network connectivity.
If the message It looks like you are trying to access MongoDB over HTTP on the native driver port is displayed, the network connectivity is normal.
- Install Python and third-party installation package pymongo on the ECS. Pymongo 2.8 is recommended.
- If SSL is enabled, you need to download the root certificate and upload it to the ECS.
- To connect an ECS to a database, the ECS must be able to communicate with the DDS DB instance that runs the database. You can run the following command to connect to the IP address and port of the instance server to test the network connectivity.
- Input the connection code.
- Enabling SSL
import ssl from pymongo import MongoClient conn_urls="mongodb://rwuser:rwuserpassword@ip:port/{mydb}?authSource=admin" connection = MongoClient(conn_urls,connectTimeoutMS=5000,ssl=True, ssl_cert_reqs=ssl.CERT_REQUIRED,ssl_match_hostname=False,ssl_ca_certs=${path to certificate authority file}) dbs = connection.database_names() print "connect database success! database names is %s" % dbs - Disabling SSL
import ssl from pymongo import MongoClient conn_urls="mongodb://rwuser:rwuserpassword@ip:port/{mydb}?authSource=admin" connection = MongoClient(conn_urls,connectTimeoutMS=5000) dbs = connection.database_names() print "connect database success! database names is %s" % dbs
- Enabling SSL
- Precautions
The authentication database in the URL must be admin. That means setting authSource to admin.
Java Mongo Client
- Prerequisites
- To connect an ECS to a database, the ECS must be able to communicate with the DDS DB instance that runs the database. You can run the following command to connect to the IP address and port of the instance server to test the network connectivity.
If the message It looks like you are trying to access MongoDB over HTTP on the native driver port is displayed, the network connectivity is normal.
- Download the MongoDB JAR package compatible with the DB instance version by referring to the MongoDB Compatibility table.
- The JDK is installed on the ECS.
- If SSL is enabled, you need to download the root certificate and upload it to the ECS.
- To connect an ECS to a database, the ECS must be able to communicate with the DDS DB instance that runs the database. You can run the following command to connect to the IP address and port of the instance server to test the network connectivity.
- Input the connection code.
Use keytool to generate a truststore.
keytool -import -file /var/chroot/mongodb/CA/ca.crt -keystore /home/Mike/jdk1.8.0_112/jre/lib/security/mongostore -storetype pkcs12 -storepass test123
- /var/chroot/mongodb/CA/ca.crt indicates the root certificate path.
- /home/Mike/jdk1.8.0_112/jre/lib/security/mongostore indicates the path of the generated truststore.
- test123 indicates the password of the truststore.
- Enabling SSL
import java.util.ArrayList; import java.util.List; import org.bson.Document; import com.mongodb.MongoClient; import com.mongodb.MongoCredential; import com.mongodb.ServerAddress; import com.mongodb.client.MongoDatabase; import com.mongodb.client.MongoCollection; import com.mongodb.MongoClientURI; import com.mongodb.MongoClientOptions; public class MongoDBJDBC { public static void main(String[] args){ try { System.setProperty("javax.net.ssl.trustStore", "/home/Mike/jdk1.8.0_112/jre/lib/security/mongostore"); System.setProperty("javax.net.ssl.trustStorePassword", "test123"); ServerAddress serverAddress = new ServerAddress("ip", port); List addrs = new ArrayList(); addrs.add(serverAddress); MongoCredential credential = MongoCredential.createScramSha1Credential("rwuser", "admin", "!rwuserPassword".toCharArray()); List credentials = new ArrayList(); credentials.add(credential); MongoClientOptions opts= MongoClientOptions.builder() .sslEnabled(true) .sslInvalidHostNameAllowed(true) .build(); MongoClient mongoClient = new MongoClient(addrs,credentials,opts); MongoDatabase mongoDatabase = mongoClient.getDatabase("testdb"); MongoCollection collection = mongoDatabase.getCollection("testCollection"); Document document = new Document("title", "MongoDB"). append("description", "database"). append("likes", 100). append("by", "Fly"); List documents = new ArrayList(); documents.add(document); collection.insertMany(documents); System.out.println("Connect to database successfully"); } catch (Exception e) { System.err.println( e.getClass().getName() + ": " + e.getMessage() ); } } }Sample codes:
javac -cp .:mongo-java-driver-3.2.0.jar MongoDBJDBC.java
java -cp .:mongo-java-driver-3.2.0.jar MongoDBJDBC
- Disabling SSL
import java.util.ArrayList; import java.util.List; import org.bson.Document; import com.mongodb.MongoClient; import com.mongodb.MongoCredential; import com.mongodb.ServerAddress; import com.mongodb.client.MongoDatabase; import com.mongodb.client.MongoCollection; import com.mongodb.MongoClientURI; import com.mongodb.MongoClientOptions; public class MongoDBJDBC { public static void main(String[] args){ try { ServerAddress serverAddress = new ServerAddress("ip", port); List addrs = new ArrayList(); addrs.add(serverAddress); MongoCredential credential = MongoCredential.createScramSha1Credential("rwuser", "admin", "!rwuserPassword".toCharArray()); List credentials = new ArrayList(); credentials.add(credential); MongoClient mongoClient = new MongoClient(addrs,credentials); MongoDatabase mongoDatabase = mongoClient.getDatabase("testdb"); MongoCollection collection = mongoDatabase.getCollection("testCollection"); Document document = new Document("title", "MongoDB"). append("description", "database"). append("likes", 100). append("by", "Fly"); List documents = new ArrayList(); documents.add(document); collection.insertMany(documents); System.out.println("Connect to database successfully"); } catch (Exception e) { System.err.println( e.getClass().getName() + ": " + e.getMessage() ); } } }
- Precautions
- In SSL mode, you need to manually generate the truststore file.
- The authentication database must be admin, and then switch to the service database.
Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.