JDBC source and sink Connector for MySQL, version 6.7.0
JDBC Connector for MySQL
If you already have a publicly accessible deployment of MySQL, you can skip ahead and Configure and install a connector, using the Axual Self-Service UI.
Use the configuration instructions below.
If you don’t have one available, the following section instructs you to deploy a publicly available instance of MySQL.
Deploying a MySQL instance
We’ll deploy a MySQL instance using Google Cloud Services.
If you have a Google account, you can sign up for a free trial of Google cloud.
You can read more about MySQL on Google cloud here.
Let’s get started.
-
-
instance ID:
my-axual-mysql-connector-test
The instance ID is irrelevant for our example: It will only be displayed in your Google overview. -
Password: You can set the password for the
root
user of the database. We’ll use this password when configuring the Connector-Application later.
You can reset this password later. -
Database: Select "MySQL 8.0" (likely the default option).
-
Choose a configuration to start with:
Development
-
Region:
europe-west1
The region isn’t very irrelevant for our example, but usually you would select a region geographically closest to the Connect-Cluster. -
Zonal availability:
Single zone
-
Customize your instance: Click "Show configuration options"
-
Machine type: Click the dropdown menu and select
Lightweight
.1vCPU
is enough. -
Storage: go for the least amount of storage.
-
If available, Use HDDs.
-
If given the option, do not enable automatic storage increases.
-
-
Connections: Leave only "Public IP" selected.
-
Authorized networks. Click "Add network". Use any name and
0.0.0.0/0
as the CIDR notation (and click "Done").
This will open the database to the entire internet. That’s ok, we’ll delete it shortly anyway.
-
-
Authentication: Skip this config if it’s present
-
Data protection:
-
Enable backups. Under "Advanced options", select (single) Region (
europe-west1
) -
If available, enable point-in-time recovery
-
Disable deletion protection
-
-
Maintenance: Skip this config
-
Flags and parameters: Skip this config
-
Labels: Skip this config
-
-
Click "Create instance".
-
-
While the database server is getting deployed, let’s create a bucket.
-
Name your bucket:
mysql-init-axual-connect
(or something different, in case the name is already taken).
Skip labels.
Click "Continue".
The bucket name is irrelevant for our example: It will only be displayed in your Google overview. -
Choose where to store your data:
-
Region:
europe-west1
(you don’t need Multi or dual region. Single region is enough)
The region isn’t very relevant for our example, but usually you would select a region geographically closest to the Connect-Cluster.
-
-
Choose a default storage class for your data:
Standard
.
Click "Continue". -
Choose how to control access to objects:
Uniform
.
Click "Continue". -
Choose how to protect object data:
None
-
Data encryption: Skip this config
-
-
-
Click "Create" to create the bucket.
If you get a pop-up with "Public access will be prevented", click "Confirm". -
The bucket page will open. Click "Upload files"
-
Save the following text to a file on your system, and then click "Upload files" to upload it into the bucket:
USE mysql_database_name; CREATE TABLE HOTEL ( hotel_id INT NOT NULL AUTO_INCREMENT, hotel_name VARCHAR(100) NULL, PRIMARY KEY (hotel_id) ); INSERT INTO `HOTEL` VALUES (1, 'iris'), (2, 'altair'), (3, 'turbulence'); CREATE TABLE kafka_mysql_HOTEL ( hotel_id INT NOT NULL AUTO_INCREMENT, hotel_name VARCHAR(100) NULL, PRIMARY KEY (hotel_id) );
-
You can close the buckets page. Let’s go back to our SQL instance.
Select your instance to open it. Note down the public IP address.
We’ll use it when configuring the Connector-Application later. -
[Optional] Change the
root
user’s password:-
On the left-side menu, click "Users".
-
Click the dots next to the
root
user. -
Click "Change password".
-
Type a new password and click "OK".
-
-
On the left-side menu, click "Databases".
-
Click "Create Database".
Usemysql_database_name
as the name, since this is the name used in this example instance-setup (we referenced it in the SQL snippet above, and we’ll do it again in the connector configuration).-
Click "Create".
-
-
-
On the left-side menu, click "Overview". Click the "Import" button.
-
Source: click "Browse".
-
Double-click your bucket and select the SQL file we saved earlier.
-
Click "SELECT" at the bottom of the page.
-
-
File format:
SQL
-
Destination:
mysql_database_name
-
Click "Import"
-
Configuring a new source Connector
-
Follow the Creating streams documentation in order to create one stream and deploy it onto an environment.
The name of the stream will bemysql_HOTEL
.
The key/value types will beJSON/JSON
. -
Follow the Configure and install a connector documentation to set up a new Connector-Application.
Let’s call itmy.mysql.source
.
The plugin name isio.aiven.connect.jdbc.JdbcSourceConnector
.
If a plugin isn’t available, ask a platform operator to install plugins.For advanced configuration, see the official source connector documentation.
-
Provide the following minimal configuration in order to connect to the previously configured MySQL instance.
connection.url
jdbc:mysql://PASTE_THE_IP_ADDRESS:3306/mysql_database_name
You noted down the instance IP address after creating the MySQL instance.connection.user
root
connection.password
This is the database password. You noted it down while creating the MySQL instance.Example value:
1234abcdEFG
mode
incrementing
incrementing.column.name
The unique primary key of the database tablehotel_id
table.whitelist
HOTEL
This value is CASE SENSITIVEtopic.prefix
mysql_
value.converter
org.apache.kafka.connect.json.JsonConverter
value.converter.schemas.enable
This property-key is not prefilled in the configuration by default, so you’ll have to add it as a custom property.
Normally, this property is not necessary.
We enable it this time in order to include the JSON schema in every event (which yes, is wasteful, however): this will enable us to easily produce these events back to MySQL, when configuring the sink connector.true
-
You can now start the source Connector-Application.
-
You can now use the stream-browse functionality in the Axual Self-Service UI to check the events published by the source Connector to the
mysql_HOTEL
stream.
This is an example event:{ "key" : null, "value": { "schema": { "name": "HOTEL", "type": "struct", "optional": false, "fields": [ { "field": "hotel_id", "type": "int32", "optional": false }, { "field": "hotel_name", "type": "string", "optional": true } ] }, "payload": { "hotel_id": 1, "hotel_name": "iris" } } }
Notice how the "value" contains 2 fields:
-
"Payload" is the actual event. In this case, the first entry in the db table.
-
"Schema" is the database schema, which will be used when converting the json payload to the insert SQL statement later, when configuring the sink connector.
-
Configuring a new sink Connector
To keep things simple, we’ll consume the topic we just used for the source connector and publish everything into another MySQL table.
If you did not follow the previous example, then you will need to produce an event to the mysql_HOTEL
topic which includes the db-table schema, just as in the example above.
-
Follow the Configure and install a connector documentation to set up a new Connector-Application.
Let’s call itmy.mysql.sink
.
The plugin name isio.aiven.connect.jdbc.JdbcSinkConnector
.
If a plugin isn’t available, ask a platform operator to install plugins.For advanced configuration, see the official sink connector documentation.
-
Provide the following minimal configuration in order to connect to the previously configured MySQL instance.
connection.url
jdbc:mysql://PASTE_THE_IP_ADDRESS:3306/mysql_database_name
You noted down the instance IP address after creating the MySQL instance.connection.user
root
connection.password
This is the database password. You noted it down while creating the MySQL instance.Example value:
1234abcdEFG
insert.mode
upsert
pk.fields
hotel_id
pk.mode
record_value
Instruct the connector to extract the primary key from the kafka-event value.table.name.format
kafka_${topic}
Instruct the connector to put events into thekafka
mysql_HOTEL MySQL table._topics
mysql_HOTEL
This field is case-sensitive.value.converter
org.apache.kafka.connect.json.JsonConverter
value.converter.schemas.enable
true
-
You can now start the sink Connector-Application.
-
We’ll now list the contents of the
kafka_mysql_HOTEL
MySQL table using Google Cloud shell.
At the top-right of your screen, next to your picture, are a few buttons.
One of them is "Activate Cloud Shell". Click it.
A shell will open at the bottom of your screen.-
Copy-Paste the following code-block to connect to your SQL instance:
gcloud sql connect my-axual-mysql-connector-test --user=root
-
Click "Authorize" in case you get a pop-up.
You might also have to type 'y' in the console to enable the sql API for your console. -
Type your password when prompted.
-
Now paste the following command to list everything in the table:
USE mysql_database_name; SELECT * FROM kafka_mysql_HOTEL;
-
You can now type
exit
to close the SQL shell or just close the terminal window.
-
Cleanup
Once you are done, stop the Connector-Application and cleanup the unused axual resources.
In case you deployed your resources via Google Cloud,
don’t forget to delete your bucket
and your SQL instance once you are done.
License
MySQL source and sink-Connector is licensed under the Apache License, Version 2.0.
Source code
The source code for the Connect-Plugin can be found at github.com/aiven/jdbc-connector-for-apache-kafka.