Synchronizing Data from RDS for MySQL to Elasticsearch Through Logstash
Scenarios
- 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 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.
- Yes: Proceed to data synchronization configuration.
- No: Upload a SQL JDBC driver that is compatible with the RDS database version in use to the Logstash cluster. For details, see FAQ: What Do I Do If the MySQL Driver Is Incompatible?.
Procedure
- Test connectivity between the Logstash cluster and data sources.
- Go to the Configuration Center page.
- Log in to the CSS management consoleCSS management console.
- In the navigation pane on the left, choose Clusters > Logstash.
- In the cluster list, click the name of the target cluster. The cluster information page is displayed.
- Click the Configuration Center tab.
- On the Configuration Center page, click Test Connectivity.
- 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 ConnectivityIf 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.
- Go to the Configuration Center page.
- Create a Logstash configuration file for data synchronization.
- 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/certs" # 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.
- Click Next to configure Logstash pipeline parameters. In this example, retain the default settings.
- 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.
- 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.
- Start the Logstash configuration file.
- In the configuration file list, select the configuration file you want to start, and click Start in the upper left corner.
- In the Start Logstash dialog box, select Keepalive if necessary.
- Click OK to start the configuration file and hence the Logstash migration task.
You can check the started configuration file in the pipeline list.
- Verify that the database is now synchronized with the Elasticsearch cluster.
- On the CSS management console, choose Clusters > Elasticsearch.
- In the Elasticsearch cluster list, locate the destination cluster, and click Access Kibana in the Operation column to log in to the Kibana console.
- In the navigation tree on the left, choose Dev Tools.
- 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:
- Stop the Logstash configuration file.
- 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/
- Contact technical support to upload the .jar file to the Logstash cluster used for data synchronization.
- 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"
- Restart the configuration file.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot