Help Center/ Cloud Search Service/ Best Practices/ Logstash Data Processing and Ingestion/ Synchronizing Data from RDS for MySQL to Elasticsearch Through Logstash
Updated on 2026-04-29 GMT+08:00

Synchronizing Data from RDS for MySQL to Elasticsearch Through Logstash

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. Go to the Configuration Center page.
      1. Log in to the CSS management console.
      2. In the navigation pane on the left, choose Clusters > Logstash.
      3. In the cluster list, click the name of the target cluster. The cluster information page is displayed.
      4. Click the Configuration Center tab.
    2. On the Configuration Center page, click Test Connectivity.
    3. In the Test Connectivity dialog box, enter the IP addresses and port numbers 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 network. 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

      Configure the strings you want to hide.

      This configuration is not required in our example here.

      Description

      Add a description for the configuration file for easy identification.

      input {
          jdbc {
              # Path of the JDBC driver package
              jdbc_driver_library => "/opt/logstash/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:xxx/database_name"
              jdbc_user => "xxx"
              jdbc_password => "xxx"
              # Retain the default values.
              jdbc_paging_enabled => "true"
              jdbc_page_size => "50000"
              # SQL query statement, used to determine the scope of data synchronization
              statement => "select * from table_name"
              # Scheduled task, which repeats itself every 5 minutes. This interval can be customized.
              schedule => "*/5 * * * *"
          }
      }
      
      filter {
          # Removes some metadata fields automatically added by Logstash
          mutate {
              remove_field => ["@timestamp", "@version"]
          }
      }
      
      output {
          elasticsearch {
              # Destination cluster node addresses. No need to include the protocol.
              hosts => ["xxx.xxx.xxx.xxx:9200", "xxx.xxx.xxx.xxx:9200"]
              # Name of the index that events are written into.
              index => "rds_doctor_index"
              # Document ID in the index. Keep it consistent with a primary key of the target table in RDS for MySQL.
              document_id => "%{primary_id}"
              # Retain the default values.
              manage_template => false              # Do not manage index templates
              ilm_enabled => false                  # Disable ILM policies (not supported in CSS)
              # Mandatory fields for a security-mode cluster. (Delete them for a cluster with the security mode disabled.)
              # user => "xxx"           # Username for accessing the cluster.
              # password => "xxx"       # Password corresponding to the username.
              # If SSL is enabled for the destination cluster, additionally configure the following information:
              # ssl => true
              # cacert => "/opt/logstash/extend/certs"      # Path of the CA certificate used to verify the destination cluster.
              # ssl_certificate_verification => false        # Whether to enable SSL certificate verification for the destination cluster.
          }
      }
      Table 2 Database migration configuration items

      Configuration Item

      Mandatory

      Description

      input

      jdbc_driver_library

      Yes

      Path of the JDBC driver package.

      MariaDB and MySQL JDBC drivers are pre-installed for Logstash clusters in CSS.

      Value format: <Default certificate path>jars/<Driver name> (for example, /opt/logstash/extend/jars/mariadb-java-client-2.7.0.jar)

      • For details about how to obtain the default certificate path, see Viewing Default Certificates.
      • Driver name:
        • MariaDB driver: mariadb-java-client-2.7.0.jar and mariadb-java-client-2.4.0.jar
        • MySQL driver (supported only by Logstash clusters whose image version is not earlier than x.x.x_26.1.0_xxx): mysql-connector-j-9.4.0.jar, mysql-connector-java-8.0.16.jar, and mysql-connector-java-8.0.30.jar

      jdbc_driver_class

      Yes

      Driver class name.

      • For MariaDB, set this field to org.mariadb.jdbc.Driver.
      • For MySQL, set it to com.mysql.cj.jdbc.Driver.

      jdbc_connection_string

      Yes

      Database connection string.

      • If the MariaDB driver is used, the value format is jdbc:mariadb://<Database access address>:<Listening port>/<Database name>.
      • If the MySQL driver is used, the value format is jdbc:mysql://<Database access address>:<Listening port>/<Database name>.

      jdbc_user

      Yes

      Database username

      jdbc_password

      Yes

      Password of the user

      jdbc_paging_enabled

      No

      Whether to enable database pagination.

      The value can be:

      • true: Enable pagination. The system will modify SQL queries to add pagination.
      • false (default): Disable pagination. All matches are retrieved at once, which may lead to out of memory (OOM) errors.

      jdbc_page_size

      No

      Number of records retrieved per page. This parameter determines the number of records retrieved in a single database request.

      statement

      Yes

      SQL query statement, used to determine the scope of data synchronization.

      schedule

      No

      Scheduled task. The data synchronization interval is customizable.

      output

      hosts

      Yes

      Destination cluster node addresses. You can configure multiple IP addresses.

      Value format: ["<Node IP address 1>:<Port number>", "<Node IP address 2>:<Port number>"]

      index

      Yes

      Name of the index to which events are written.

      • Single index: Enter the index name, for example, my_index.
      • Multiple indexes: Use dynamic naming (based on event fields) or configure multiple conditional output blocks to route events to different indexes.

      document_id

      No

      ID of document to which events are written. Keep it consistent with a primary key used in the target table in RDS for MySQL.

      manage_template

      No

      Whether to manage index templates.

      The value can be:

      • true: Automatically manage and push the index template to the destination.
      • false (default): Do not manage index templates.

      ilm_enabled

      No

      Whether to enable the ILM policy.

      Since CSS does not support ILM, this value is fixed to false.

      user

      No

      Username for accessing the destination cluster.

      Mandatory for a security-mode cluster.

      password

      No

      Password for accessing the destination cluster.

      Mandatory for a security-mode cluster.

      ssl

      No

      Whether SSL is enabled for the destination cluster.

      The value can be:

      • true: Uses HTTPS to transmit data.
      • false: Uses HTTP to transmit data.

      ssl_certificate_verification

      No

      Whether SSL certificate verification is enabled for the destination cluster.

      The value can be:

      • true (default): Uses an SSL certificate to verify the destination cluster.
      • false: Ignores SSL certificate verification.

      cacert

      No

      Path of the CA certificate used to verify the destination cluster.

      Value format: <certificate path><certificate name>, for example, /opt/logstash/extend/certs.

      • If the destination is a CSS Elasticsearch or OpenSearch cluster, the certificate name and certificate path of the default CA certificate will be obtained. For details, see Viewing Default Certificates.
      • If the destination is a self-managed or third-party Elasticsearch or OpenSearch cluster, upload the destination cluster's security certificate to Logstash and obtain the certificate name and certificate path. For details, see Uploading a Custom Certificate.
    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 execute the configuration file and thereby start 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?

If the Logstash pipeline does not function properly after the Logstash configuration file is started in the Logstash cluster, 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.