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
Help Center/ Cloud Search Service/ Best Practices/ Synchronizing Data from RDS for MySQL to Elasticsearch Through Logstash

Synchronizing Data from RDS for MySQL to Elasticsearch Through Logstash

Updated on 2025-01-06 GMT+08:00

Scenarios

The logstash-input-jdbc plugin is installed by default in CSS's Logstash clusters. This plugin allows Logstash to fetch data from the relational database RDS for MySQL and push the data to Elasticsearch for deeper analysis. All you need to do is to configure the Logstash configuration file to define the JDBC input and Elasticsearch output. This solution can be used for the following purposes:
  • Real-time data update and synchronization: Synchronizes an RDS for MySQL database with Elasticsearch in real time to leverage the latter's powerful search and analytics capabilities.
  • Log analytics and search: Synchronizes log data from RDS for MySQL to Elasticsearch for quick search and analytics.
  • Application performance monitoring: Synchronizes application performance data stored in RDS for MySQL to Elasticsearch through Logstash for real-time performance monitoring and performance analysis.
  • Data backup and recovery: Uses Logstash to back up the data in RDS for MySQL to Elasticsearch, allowing for quick recovery in case of data loss or corruption.

Solution Architecture

Figure 1 Synchronizing RDS for MySQL with Elasticsearch

Figure 1 illustrates the process of synchronizing RDS for MySQL with Elasticsearch through Logstash.

The logstash-input-jdbc plugin enables both full data migrations and incremental data updates from RDS for MySQL to Elasticsearch.

Advantages

  • Flexible: Logstash offers versatile data collection, transformation, optimization, and output capabilities, meeting diverse data synchronization needs.
  • Real-time: Logstash supports near-real time data synchronization, accommodating the needs of most scenarios.
  • Easy-to-use: The Logstash configuration file can be configured to meet various synchronization needs. The configuration is straightforward and requires no coding.

Constraints

  • The same ID fields must be used in both Elasticsearch and RDS for MySQL.

    This is to establish a direct mapping between MySQL records and Elasticsearch indexes. For instance, when a record is updated in RDS for MySQL, the synchronization task overwrites the corresponding index in Elasticsearch with the same ID.

  • New or updated records in RDS for MySQL must include a field indicating the insertion or update time.

    Logstash records the latest timestamp for each record during each polling cycle. During the next poll, it only processes records with timestamps later than the previously recorded ones.

  • The RDS for MySQL database, Logstash cluster, and Elasticsearch cluster must be in the same time zone; otherwise, there may be time zone-related errors after the synchronization.

Prerequisites

  • An RDS for MySQL database is available. This topic uses an RDS for MySQL instance on Huawei Cloud as an example. For details, see Buying an RDS for MySQL DB Instance.
  • A Logstash cluster used for data synchronization has been created. For details, see Creating a Logstash Cluster. This topic uses a Logstash 7.10.0 cluster as an example.
  • An Elasticsearch cluster has been created. For details, see Creating an Elasticsearch Cluster. This topic uses an Elasticsearch 7.10.2 cluster as an example.

The RDS for MySQL DB, Logstash cluster, and Elasticsearch cluster are in the same VPC.

If an in-house built or third-party MySQL database is used, check whether the database driver is MariaDB.

Procedure

  1. Test connectivity between the Logstash cluster and data sources.

    1. Log in to the CSS management console.
    2. In the navigation pane on the left, choose Clusters > Logstash. The cluster list is displayed.
    3. In the displayed cluster list, locate the row that contains the target cluster, and click Configuration Center in the Operation column. The Configuration Center page is displayed.

      Alternatively, in the cluster list, click the cluster name to go to the cluster information page. In the navigation pane on the left, choose Configuration Center.

    4. On the Configuration Center page, click Test Connectivity.
    5. In the Test Connectivity dialog box, enter the IP address and port number of the data source and destination, and click Test.

      You can test a maximum of 10 IP addresses at a time. You can click Add to add more IP addresses and click Test at the bottom to test connectivity to multiple IP addresses at a time.

      Figure 2 Test Connectivity

      If Available is displayed, the network is connected. If the network is disconnected, configure routes for the Logstash cluster to connect the clusters. For details, see Configuring Routes for a Logstash Cluster.

  2. Create a Logstash configuration file for data synchronization.

    1. On the Configuration Center page of the Logstash cluster, click Create in the upper right corner. On the Create Configuration File page, edit the configuration file.
      Table 1 Creating a Logstash configuration file

      Parameter

      Description

      Name

      User-defined configuration file name.

      It can contain only letters, digits, hyphens (-), and underscores (_), and must start with a letter. The minimum length is 4 characters.

      Configuration File Content

      Configure the configuration file by referring to the code example below.

      NOTE:

      The size of each configuration file cannot exceed 100 KB.

      Hidden Content

      For items that you enter in this box, the corresponding strings will be replaced with *** in the configurations.

      This configuration is not required in our example here.

      input {
        jdbc{
          # Configure the JDBC driver.
          jdbc_driver_library => "/rds/datastore/logstash/v7.10.0/package/logstash-7.10.0/extend/jars/mariadb-java-client-2.7.0.jar"
          jdbc_driver_class => "org.mariadb.jdbc.Driver"
          jdbc_connection_string => "jdbc:mariadb://xxx.xxx.xxx.xxx:port/cms?useUnicode=true&characterEncoding=utf8mb4&autoReconnect=true&allowMultiQueries=true"
          jdbc_user => "root"
          jdbc_password => "xx"
          # Retain the default values.
          jdbc_paging_enabled => "true"
          jdbc_page_size => "50000"
          # SQL statement for data migration.
          statement => "select a.user_code AS doctor_id,a.record_status from cluster "
          # Scheduled task, which repeats itself every 5 minutes. This interval can be customized.
          schedule => "*/5 * * * *"
        }
      }
      filter {
      
      }
      output {
        elasticsearch {
          hosts => ["xxx.xxx.xxx.xxx:port","xxx.xxx.xxx.xxx:port","xxx.xxx.xxx.xxx:port"]
          # Set the index name.
          index => "rds_doctor_index"
          user => "admin"
          password => "xx"
          # Document ID in the index. Keep it consistent with a primary key of the target table in RDS for MySQL.
          document_id => "%{primary_id}"
          # Configure a certificate only if the destination Elasticsearch cluster uses HTTPS.
          ssl => true
          ssl_certificate_verification => false
          cacert => "/rds/datastore/logstash/v7.10.0/package/logstash-7.10.0/extend/cert/CloudSearchService.cer"
          # Retain the default values.
          manage_template => false
          ilm_enabled => false
        }
      }
      Table 2 Configuration items

      Configuration Item

      Mandatory

      Description

      input

      jdbc_driver_library

      Yes

      Path of the JDBC driver library.

      • If the database driver is MariaDB, set the value to /rds/datastore/logstash/v7.10.0/package/logstash-7.10.0/extend/jars/mariadb-java-client-2.7.0.jar.
      • If the database driver is a SQL JDBC driver compatible with the RDS database version in use, contact technical support to set the value.

      For how to set JDBC driver parameters, see Jdbc input plugin.

      jdbc_driver_class

      Yes

      Class path of the driver library.

      • If the database driver is MariaDB, set the value to org.mariadb.jdbc.Driver.
      • If the database driver is a SQL JDBC driver compatible with the RDS database version in use, set the value to com.mysql.jdbc.Driver.

      jdbc_connection_string

      Yes

      Address for accessing MySQL JDBC.

      • If the database driver is MariaDB, set the value to jdbc:mariadb://xxx.xxx.xxx.xxx:port/cms?useUnicode=true&characterEncoding=utf8mb4&autoReconnect=true&allowMultiQueries=true.
      • If the database driver is a SQL JDBC driver compatible with the RDS database version in use, set the value to jdbc:mysql://xxx.xxx.xxx.xxx:port/cms.

      Replace xxx.xxx.xxx.xxx:port with the database address plus port number.

      jdbc_user

      Yes

      Username for accessing MySQL JDBC.

      jdbc_password

      Yes

      Password for accessing MySQL JDBC.

      statement

      Yes

      SQL statement for data migration.

      schedule

      Yes

      Scheduled task. The data synchronization interval is customizable.

      output

      hosts

      Yes

      Address for accessing the Elasticsearch cluster.

      index

      Yes

      Index to which data is loaded.

      user

      No

      Username for accessing the Elasticsearch cluster, which is required only for security-mode clusters.

      password

      No

      Password for accessing the Elasticsearch cluster, which is required only for security-mode clusters.

      document_id

      Yes

      Document ID in the index. Keep it consistent with a primary key used in the target table in RDS for MySQL.

      ssl

      No

      Whether to enable HTTPS communication.

      If HTTPS access is enabled for the Elasticsearch cluster, set this parameter to true. Otherwise, do not set this parameter.

      ssl_certificate_verification

      No

      Whether to verify the server-end Elasticsearch certificate. Set this parameter only when ssl is set to true.

      • true: Verify the certificate.
      • false: Ignore the certificate.

      cacert

      No

      HTTPS access certificate. Retain the default for a CSS cluster.

    2. Click Next to configure Logstash pipeline parameters. In this example, retain the default settings.
    3. After the configuration is complete, click Create.

      On the Configuration Center page, you can check the created configuration file. If its status changes to Available, it has been successfully created.

  3. Start the Logstash configuration file.

    1. In the configuration file list, select the configuration file you want to start, and click Start in the upper left corner.
    2. In the Start Logstash dialog box, select Keepalive if necessary.
    3. Click OK to start the configuration file and hence the Logstash migration task.

      You can check the started configuration file in the pipeline list.

  4. Verify that the database is now synchronized with the Elasticsearch cluster.

    1. On the CSS management console, choose Clusters > Elasticsearch.
    2. In the Elasticsearch cluster list, locate the destination cluster, and click Access Kibana in the Operation column to log in to the Kibana console.
    3. In the navigation tree on the left, choose Dev Tools.
    4. Run the following command to query index data:
      GET rds_doctor_index/_count 
      {  
        "query": {"match_all": {}}
      }

      If the value of count is not 0 in the command output, data synchronization is successful.

FAQ: What Do I Do If the MySQL Driver Is Incompatible?

After the Logstash configuration file is started in the Logstash cluster, the Logstash pipeline is not working properly. Click Run Log. If the log contains error information similar to the following, the MySQL driver is incompatible.

[2024-05-21T11:31:00,196][ERROR][logstash.inputs.jdbc     ] Java::JavaSql::SQLSyntaxErrorException: (conn=-1409730930) You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '"T1" LIMIT 1' at line 1: SELECT count(*) AS "COUNT" FROM (select * from logstash_broker where updatetime+30000 > 0 order by updatetime) AS "T1" LIMIT 1

Solution:

  1. Stop the Logstash configuration file.
  2. Download a SQL JDBC driver compatible with the RDS database version in use, for example, mysql-connector-java-8.0.11.tar.gz. Extract mysql-connector-java-8.0.11.jar from it.

    Download address: https://downloads.mysql.com/archives/c-j/

  3. Contact technical support to upload the .jar file to the Logstash cluster used for data synchronization.
  4. Modify the Logstash configuration file.

    Change the values of jdbc_driver_class and jdbc_connection_string. Replace xxx.xxx.xxx.xxx:port with the database address plus port number.

    jdbc_driver_class => "com.mysql.jdbc.Driver"
    # Enter the MySQL JDBC URL.
    jdbc_connection_string => "jdbc:mysql://xxx.xxx.xxx.xxx:port/cms"
  5. Restart the configuration file.

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