Esta página ainda não está disponível no idioma selecionado. Estamos trabalhando para adicionar mais opções de idiomas. Agradecemos sua compreensão.

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 Cluster Instance Using Mongo Shell (Private Network)

Updated on 2025-02-13 GMT+08:00

Mongo shell is the default client for the MongoDB database server. You can use Mongo Shell to connect to DB instances, and query, update, and manage data in databases. DDS is compatible with MongoDB. Mongo Shell is a part of the MongoDB client. To use Mongo Shell, download and install the MongoDB client first, and then use the Mongo shell to connect to the DB instance.

By default, a DDS instance provides a private IP address. If your applications are deployed on an ECS and are in the same region and VPC as DDS instances, you can connect to DDS instances using a private IP address to achieve a fast transmission rate and high security.

This section describes how to use Mongo Shell to connect to a cluster instance over a private network.

You can connect to an instance using an SSL connection or an unencrypted connection. The SSL connection is encrypted and more secure. To improve data transmission security, connect to instances using SSL.

Prerequisites

  1. For details about how to create and log in to an ECS, see Purchasing an ECS and Logging In to an ECS.
  2. You have installed the MongoDB client on the ECS. To ensure successful authentication, install the MongoDB client of the same version as the target instance.

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

  3. The ECS can communicate with the DDS instance. For details, see Configuring Security Group Rules.

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 Enabling and Disabling SSL.

  1. Log in to the management console.
  2. Click in the upper left corner and select a region and a project.
  3. Click in the upper left corner of the page and choose Databases > Document Database Service.
  4. On the Instances page, click the instance name.
  5. In the navigation pane on the left, choose Connections.
  6. In the Basic Information area, click next to the SSL field.
  7. 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 Windows 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 is the ECS OS user.
      • REMOTE_ADDRESS is the ECS address.
      • REMOTE_DIR is the directory of the ECS to which the root certificate is uploaded.
    • In Windows, upload the root certificate using a remote connection tool.

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

    Method 1: Using the private HA connection address (recommended)

    DDS provides a private HA connection address that consists of IP addresses and ports of all dds mongos nodes in a cluster instance. You can use this address to connect to the cluster instance to improve availability of the cluster instance.

    Example command:

    ./mongo <Private HA connection address> --ssl --sslCAFile <FILE_PATH> --sslAllowInvalidHostnames

    Parameter description:

    • Private HA Connection Address: On the Instances page, click the instance name. The Basic Information page is displayed. Choose Connections. Click the Private Connection tab and obtain the connection address of the current instance from the Private HA Connection Address field.
      Figure 1 Obtaining the private HA connection address

      The format of the private HA connection address is as follows. The database username rwuser and authentication database admin cannot be changed.

      mongodb://rwuser:<password>@192.168.xx.xx:8635,192.168.xx.xx:8635/test?authSource=admin

      The following table lists the required parameters in the private HA address.

      Table 1 Parameter information

      Parameter

      Description

      rwuser

      Database username

      <password>

      Password for the database username. Replace it with the actual password.

      If the password contains at signs (@), exclamation marks (!), dollar signs ($), or percent signs (%), replace them with hexadecimal URL codes (ASCII) %40, %21, %24, and %25 respectively.

      For example, if the password is ****@%***!$, the corresponding URL code is ****%40%25***%21%24.

      192.168.***.***:8635,192.168.***.***:8635

      IP addresses and ports of the dds mongos nodes of the cluster instance to be connected.

      test

      The name of the test database. You can set this parameter based on your service requirements.

      authSource=admin

      The authentication database of user rwuser must be admin. authSource=admin is fixed in the command.

    • FILE_PATH is the path for storing the root certificate.
    • --sslAllowInvalidHostnames: To ensure that the internal communication of the cluster does not occupy resources such as the user IP address and bandwidth, the cluster certificate is generated using the internal management IP address. --sslAllowInvalidHostnames is needed for the SSL connection through a private network.

    Command example:

    ./mongo mongodb://rwuser:<password>@192.168.xx.xx:8635,192.168.xx.xx:8635/test?authSource=admin --ssl --sslCAFile /tmp/ca.crt --sslAllowInvalidHostnames

    Method 2: Using the private HA connection address (user-defined database and account)

    Example command:

    ./mongo <Private HA connection address> --ssl --sslCAFile <FILE_PATH> --sslAllowInvalidHostnames

    Parameter description:

    • Private HA Connection Address: On the Instances page, click the instance name. The Basic Information page is displayed. Choose Connections. Click the Private Connection tab and obtain the connection address of the current instance from the Private HA Connection Address field.
      Figure 2 Obtaining the private HA connection address

      The format of the obtained private HA connection address is as follows:

      mongodb://rwuser:<password>@192.168.xx.xx:8635,192.168.xx.xx:8635/test?authSource=admin

      The following table lists the required parameters in the private HA address.

      Table 2 Parameter information

      Parameter

      Description

      rwuser

      Database username. The default value is rwuser. You can change the value to the username based on your service requirements.

      <password>

      Password for the database username. Replace it with the actual password.

      If the password contains at signs (@), exclamation marks (!), dollar signs ($), or percent signs (%), replace them with hexadecimal URL codes (ASCII) %40, %21, %24, and %25 respectively.

      For example, if the password is ****@%***!$, the corresponding URL code is ****%40%25***%21%24.

      192.168.***.***:8635,192.168.***.***:8635

      IP addresses and ports of the dds mongos nodes of the cluster instance to be connected.

      test

      The name of the test database. You can set this parameter based on your service requirements.

      authSource=admin

      The authentication database of user rwuser is admin.

      NOTE:

      If you use a user-defined database for authentication, change the authentication database in the HA connection address to the name of the user-defined database. In addition, replace rwuser with the username created in the user-defined database.

    • FILE_PATH is the path for storing the root certificate.
    • --sslAllowInvalidHostnames: To ensure that the internal communication of the cluster does not occupy resources such as the user IP address and bandwidth, the cluster certificate is generated using the internal management IP address. --sslAllowInvalidHostnames is needed for the SSL connection through a private network.

    For example, if you create a user-defined database Database and user test1 in the database, the connection command is as follows:

    ./mongo mongodb://test1:<password>@192.168.xx.xx:8635,192.168.xx.xx:8635/Database?authSource=Database --ssl --sslCAFile /tmp/ca.crt --sslAllowInvalidHostnames

    Method 3: Using a private IP address

    Example command:

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

    Parameter description:

    • DB_HOST is the IP address of the dds mongos node of the cluster instance to be connected.

      Click the instance name. On the Basic Information page, choose Connections > Private Connection, obtain the private IP address of the dds mongos node on the dds mongos tab in the Node Information area.

      Figure 3 Obtaining the private IP address

    • DB_PORT is the port of the instance to be connected. The default port is 8635.

      Click the instance name. On the Basic Information page, choose Connections. On the Private Connection tab, obtain the database port information in the Database Port field in the Basic Information area.

      Figure 4 Obtaining the port
    • DB_USER is the database user. The default value is rwuser.
    • FILE_PATH is the path for storing the root certificate.
    • --sslAllowInvalidHostnames: To ensure that the internal communication of the cluster does not occupy resources such as the user IP address and bandwidth, the cluster certificate is generated using the internal management IP address. --sslAllowInvalidHostnames is needed for the SSL connection through a private network.

    Enter the database account password when prompted:

    Enter password:

    Command example:

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

  9. Check the connection result. If the following information is displayed, the connection is successful.

    mongos>

Unencrypted 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 Enabling and Disabling SSL.

  1. Connect to the ECS.
  2. Connect to the instance in the directory where the MongoDB client is located.

    Method 1: Private HA connection address (recommended)

    Example command:

    ./mongo "<Private HA Connection Address>"

    Private HA Connection Address: On the Instances page, click the instance name. The Basic Information page is displayed. Choose Connections. Click the Private Connection tab and obtain the connection address of the current instance from the Private HA Connection Address field.

    Figure 5 Obtaining the private HA connection address

    The format of the private HA connection address is as follows. The database username rwuser and authentication database admin cannot be changed.

    mongodb://rwuser:<password>@192.168.xx.xx:8635,192.168.xx.xx:8635/test?authSource=admin

    The following table lists the required parameters in the private HA address.

    Table 3 Parameter information

    Parameter

    Description

    rwuser

    Database username

    <password>

    Password for the database username. Replace it with the actual password.

    If the password contains at signs (@), exclamation marks (!), dollar signs ($), or percent signs (%), replace them with hexadecimal URL codes (ASCII) %40, %21, %24, and %25 respectively.

    For example, if the password is ****@%***!$, the corresponding URL code is ****%40%25***%21%24.

    192.168.***.***:8635,192.168.***.***:8635

    IP addresses and ports of the dds mongos nodes of the cluster instance to be connected.

    test

    The name of the test database. You can set this parameter based on your service requirements.

    authSource=admin

    The authentication database of user rwuser must be admin. authSource=admin is fixed in the command.

    Command example:

    ./mongo mongodb://rwuser:<password>@192.168.xx.xx:8635,192.168.xx.xx:8635/test?authSource=admin

    Method 2: Private HA connection (user-defined database and account)

    Example command:

    ./mongo "<Private HA Connection Address>"

    Private HA Connection Address: On the Instances page, click the instance name. The Basic Information page is displayed. Choose Connections. Click the Private Connection tab and obtain the connection address of the current instance from the Private HA Connection Address field.

    Figure 6 Obtaining the private HA connection address

    The format of the obtained private HA connection address is as follows:

    mongodb://rwuser:<password>@192.168.xx.xx:8635,192.168.xx.xx:8635/test?authSource=admin

    The following table lists the required parameters in the private HA address.

    Table 4 Parameter information

    Parameter

    Description

    rwuser

    Database username. The default value is rwuser. You can change the value to the username based on your service requirements.

    <password>

    Password for the database username. Replace it with the actual password.

    If the password contains at signs (@), exclamation marks (!), dollar signs ($), or percent signs (%), replace them with hexadecimal URL codes (ASCII) %40, %21, %24, and %25 respectively.

    For example, if the password is ****@%***!$, the corresponding URL code is ****%40%25***%21%24.

    192.168.***.***:8635,192.168.***.***:8635

    IP addresses and ports of the dds mongos nodes of the cluster instance to be connected.

    test

    The name of the test database. You can set this parameter based on your service requirements.

    authSource=admin

    The authentication database of user rwuser is admin.

    NOTE:

    If you use a user-defined database for authentication, change the authentication database in the HA connection address to the name of the user-defined database. In addition, replace rwuser with the username created in the user-defined database.

    For example, if you create a user-defined database Database and user test1 in the database, the connection command is as follows:

    ./mongo mongodb://test1:<password>@192.168.xx.xx:8635,192.168.xx.xx:8635/Database?authSource=Database

    Method 3: Using a private IP address

    Example command:

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

    Parameter description:

    • DB_HOST is the IP address of the dds mongos node of the cluster instance to be connected.

      Click the instance name. On the Basic Information page, choose Connections > Private Connection, obtain the private IP address of the dds mongos node on the dds mongos tab in the Node Information area.

      Figure 7 Obtaining the private IP address

    • DB_PORT is the port of the instance to be connected. The default port is 8635.

      Click the instance name. On the Basic Information page, choose Connections. On the Private Connection tab, obtain the database port information in the Database Port field in the Basic Information area.

      Figure 8 Obtaining the port
    • DB_USER is the database user. The default value is rwuser.
    Enter the database password when prompted:
    Enter password:

    Command example:

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

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

    mongos>

Usamos cookies para aprimorar nosso site e sua experiência. Ao continuar a navegar em nosso site, você aceita nossa política de cookies. Saiba mais

Feedback

Feedback

Feedback

0/500

Selected Content

Submit selected content with the feedback