Compute
Elastic Cloud Server
Huawei Cloud Flexus
Bare Metal Server
Auto Scaling
Image Management Service
Dedicated Host
FunctionGraph
Cloud Phone Host
Huawei Cloud EulerOS
Networking
Virtual Private Cloud
Elastic IP
Elastic Load Balance
NAT Gateway
Direct Connect
Virtual Private Network
VPC Endpoint
Cloud Connect
Enterprise Router
Enterprise Switch
Global Accelerator
Management & Governance
Cloud Eye
Identity and Access Management
Cloud Trace Service
Resource Formation Service
Tag Management Service
Log Tank Service
Config
OneAccess
Resource Access Manager
Simple Message Notification
Application Performance Management
Application Operations Management
Organizations
Optimization Advisor
IAM Identity Center
Cloud Operations Center
Resource Governance Center
Migration
Server Migration Service
Object Storage Migration Service
Cloud Data Migration
Migration Center
Cloud Ecosystem
KooGallery
Partner Center
User Support
My Account
Billing Center
Cost Center
Resource Center
Enterprise Management
Service Tickets
HUAWEI CLOUD (International) FAQs
ICP Filing
Support Plans
My Credentials
Customer Operation Capabilities
Partner Support Plans
Professional Services
Analytics
MapReduce Service
Data Lake Insight
CloudTable Service
Cloud Search Service
Data Lake Visualization
Data Ingestion Service
GaussDB(DWS)
DataArts Studio
Data Lake Factory
DataArts Lake Formation
IoT
IoT Device Access
Others
Product Pricing Details
System Permissions
Console Quick Start
Common FAQs
Instructions for Associating with a HUAWEI CLOUD Partner
Message Center
Security & Compliance
Security Technologies and Applications
Web Application Firewall
Host Security Service
Cloud Firewall
SecMaster
Anti-DDoS Service
Data Encryption Workshop
Database Security Service
Cloud Bastion Host
Data Security Center
Cloud Certificate Manager
Edge Security
Situation Awareness
Managed Threat Detection
Blockchain
Blockchain Service
Web3 Node Engine Service
Media Services
Media Processing Center
Video On Demand
Live
SparkRTC
MetaStudio
Storage
Object Storage Service
Elastic Volume Service
Cloud Backup and Recovery
Storage Disaster Recovery Service
Scalable File Service Turbo
Scalable File Service
Volume Backup Service
Cloud Server Backup Service
Data Express Service
Dedicated Distributed Storage Service
Containers
Cloud Container Engine
SoftWare Repository for Container
Application Service Mesh
Ubiquitous Cloud Native Service
Cloud Container Instance
Databases
Relational Database Service
Document Database Service
Data Admin Service
Data Replication Service
GeminiDB
GaussDB
Distributed Database Middleware
Database and Application Migration UGO
TaurusDB
Middleware
Distributed Cache Service
API Gateway
Distributed Message Service for Kafka
Distributed Message Service for RabbitMQ
Distributed Message Service for RocketMQ
Cloud Service Engine
Multi-Site High Availability Service
EventGrid
Dedicated Cloud
Dedicated Computing Cluster
Business Applications
Workspace
ROMA Connect
Message & SMS
Domain Name Service
Edge Data Center Management
Meeting
AI
Face Recognition Service
Graph Engine Service
Content Moderation
Image Recognition
Optical Character Recognition
ModelArts
ImageSearch
Conversational Bot Service
Speech Interaction Service
Huawei HiLens
Video Intelligent Analysis Service
Developer Tools
SDK Developer Guide
API Request Signing Guide
Terraform
Koo Command Line Interface
Content Delivery & Edge Computing
Content Delivery Network
Intelligent EdgeFabric
CloudPond
Intelligent EdgeCloud
Solutions
SAP Cloud
High Performance Computing
Developer Services
ServiceStage
CodeArts
CodeArts PerfTest
CodeArts Req
CodeArts Pipeline
CodeArts Build
CodeArts Deploy
CodeArts Artifact
CodeArts TestPlan
CodeArts Check
CodeArts Repo
Cloud Application Engine
MacroVerse aPaaS
KooMessage
KooPhone
KooDrive

Connecting to a Replica Set Instance Using the MongoDB Client

Updated on 2024-08-03 GMT+08:00

If a DB instance cannot be accessed over a private network, you can bind an EIP to the instance.

This section describes how to use the MongoDB client to connect to a replica set instance over a public network.

A Linux ECS is used as an example.

The MongoDB client can connect to an instance with an unencrypted connection or an encrypted connection (SSL). To improve data transmission security, you are advised to connect to instances using the SSL connection.

Prerequisites

  1. You have bound an EIP to the instance and configured security group rules. In this way, you can access the instance from the ECS. For details, see Binding and Unbinding an EIP and Configuring Security Group Rules.
  2. Install the MongoDB client.
    1. You have created and logged in to an ECS. For details, see Purchasing an ECS and Logging In to an ECS.
    2. You have installed the MongoDB client on the ECS.

      For details about how to install the MongoDB client, see How Can I Install a MongoDB Client?

SSL Connection

NOTICE:

If you connect to an instance over the SSL connection, enable SSL first. Otherwise, an error is reported. For details about how to enable SSL, see Configuring an SSL Connection.

  1. Log in to the management console.
  2. In the service list, choose Databases > GeminiDB Mongo API.
  3. On the Instance Management page, click the instance.
  4. In the navigation pane on the left, choose Connections.
  5. In the Basic Information area, click next to the SSL field to download the root certificate.
  6. Upload the root certificate to the ECS to be connected to the instance.

    The following describes how to upload the certificate to a Linux and a Window ECS

    • In Linux, run the following command:
      scp<IDENTITY_FILE><REMOTE_USER>@<REMOTE_ADDRESS>:<REMOTE_DIR>
      NOTE:
      • IDENTITY_FILE is the directory where the root certificate resides. The file access permission is 600.
      • REMOTE_USER indicates the OS user.
      • REMOTE_ADDRESS indicates the host address.
      • REMOTE_DIR indicates the directory to which the root certificate is uploaded.
    • In Windows, upload the root certificate using a remote connection tool.

  7. Connect to the DB instance in the directory where the MongoDB client is located.

    Example command:

    ./mongo --host <DB_HOST> --port <DB_PORT> -u <DB_USER> -p --authenticationDatabaseadmin --ssl --sslCAFile<FILE_PATH> --sslAllowInvalidHostnames

    Enter the password of the database account if the following information is displayed:

    Enter password:
    Table 1 Parameter description

    Parameter

    Description

    <DB_HOST>

    EIP bound to the instance node to be connected.

    Primary node: You can read and write data.

    Secondary node: You can only read data.

    On the Instance Management page, click the instance to go to the Basic Information page. On the Connections page, obtain the EIP of the corresponding node.

    Figure 1 Obtaining the EIP of a Node

    Alternatively, click the instance name and obtain the EIP of the corresponding node in the Node Information area.

    Figure 2 Viewing EIPs

    <DB_PORT>

    Database port

    <DB_USER>

    Username of the instance administrator. The default value is rwuser.

    <FILE_PATH>

    Path for storing the root certificate.

    Example:

    ./mongo --host 192.168.1.6 --port 8635 -u rwuser -p --authenticationDatabase admin --ssl --sslCAFile /tmp/ca.crt --sslAllowInvalidHostnames

  8. Check the results. If the following information is displayed, the connection is successful.

    • The primary node of the replica set is connected.
      replica:PRIMARY>
    • The standby node of the replica set is connected.
      replica:SECONDARY>

Non-SSL Connection

NOTICE:

If you connect to an instance over an unencrypted connection, disable SSL first. Otherwise, an error is reported. For details about how to disable SSL, see Configuring an SSL Connection.

  1. Connect the ECS.
  2. Connect to the instance.

    Example command:

    ./mongo --host <DB_HOST> --port <DB_PORT> -u <DB_USER> -p --authenticationDatabase admin

    Table 2 Parameter description

    Parameter

    Description

    <DB_HOST>

    EIP of the primary or standby node of the instance to be connected.

    • Primary node: You can read and write data.
    • Secondary node: You can only read data.

    On the Instance Management page, click the instance to go to the Basic Information page. Obtain the EIP of the corresponding node from the node information list.

    Figure 3 Obtaining an EIP

    Alternatively, click the target instance to go to the Basic Information page. In the navigation pane on the left, choose Connections. On the Connections page, obtain the EIP of the node.

    Figure 4 Obtaining the EIP of a node

    <DB_PORT>

    Database port

    <DB_USER>

    Username of the instance administrator. The default value is rwuser.

    Enter the password of the database account if the following information is displayed:

    Enter password:

    Example:

    ./mongo --host 192.168.1.6 --port 8635 -u rwuser -p --authenticationDatabase admin

  3. Check the results. If the following information is displayed, the connection is successful.

    • The primary node of the replica set is connected.
      replica:PRIMARY>
    • The standby node of the replica set is connected.
      replica:SECONDARY>

We use cookies to improve our site and your experience. By continuing to browse our site you accept our cookie policy. Find out more

Feedback

Feedback

Feedback

0/500

Selected Content

Submit selected content with the feedback