You can load data into tables only as a user who has the INSERT privilege on those tables. If you do not have the INSERT privilege, follow the instructions provided in GRANT to grant the INSERT privilege to the user that you use to connect to your cluster. The syntax is
GRANT INSERT ON TABLE <table_name> IN DATABASE <database_name> TO { ROLE <role_name> | USER <user_identity>}.
How it works
Tip Flink CDC is used in the synchronization from MySQL to Flink. This topic uses Flink CDC whose version is less than 3.0, so SMT is used to synchronize table schemas. However, if Flink CDC 3.0 is used, it is not necessary to use SMT to synchronize table schemas to the database. Flink CDC 3.0 can even synchronize the schemas of the entire MySQL database, the sharded databases and tables, and also supports schema changes synchronization. For detailed usage, see Streaming ELT from MySQL to StarRocks. The following figure illustrates the entire synchronization process.
- Synchronize database & table schema. The SMT reads the schema of the MySQL database & table to be synchronized and generates SQL files for creating a destination database & table in the database. This operation is based on the MySQL and the database information in SMTās configuration file.
-
Synchronize data.
a. The Flink SQL client executes the data loading statement
INSERT INTO SELECTto submit one or more Flink jobs to the Flink cluster. b. The Flink cluster runs the Flink jobs to obtain data. The Flink CDC connector first reads full historical data from the source database, then seamlessly switches to incremental reading, and sends the data to flink-connector-starrocks. c. flink-connector-starrocks accumulates data in mini-batches, and synchronizes each batch of data to the database. Info Only data manipulation language (DML) operations in MySQL can be synchronized to StarRocks. Data definition language (DDL) operations cannot be synchronized.
Scenarios
Real-time synchronization from MySQL has a broad range of use cases where data is constantly changed. Take a real-world use case āreal-time ranking of commodity salesā as an example. Flink calculates the real-time ranking of commodity sales based on the original order table in MySQL and synchronizes the ranking to the databaseā Primary Key table in real time. Users can connect a visualization tool to the database to view the ranking in real time to gain on-demand operational insights.Preparations
Download and install synchronization tools
To synchronize data from MySQL, you need to install the following tools: SMT, Flink, Flink CDC connector, and flink-connector-starrocks.-
Download and install Flink, and start the Flink cluster. You can also perform this step by following the instructions in Flink official documentation.
a. Install Java 8 or Java 11 in your operating system before you run Flink. You can run the following command to check the installed Java version.
b. Download the Flink installation package and decompress it. We recommend that you use Flink 1.14 or later. The minimum allowed version is Flink 1.11. This topic uses Flink 1.14.5.c. Start the Flink cluster.
-
Download Flink CDC connector. This topic uses MySQL as the data source and therefore,
flink-sql-connector-mysql-cdc-x.x.x.jaris downloaded. The connector version must match the Flink version. This topic uses Flink 1.14.5 and you can downloadflink-sql-connector-mysql-cdc-2.2.0.jar. -
Download flink-connector-starrocks. The version must match the Flink version.
The flink-connector-starrocks package
x.x.x_flink-y.yy _ z.zz.jarcontains three version numbers:x.x.xis the version number of flink-connector-starrocks.y.yyis the supported Flink version.z.zzis the Scala version supported by Flink. If the Flink version is 1.14.x or earlier, you must download a package that has the Scala version.
1.2.3_flink-14_2.11.jar. -
Move the JAR packages of Flink CDC connector (
flink-sql-connector-mysql-cdc-2.2.0.jar) and flink-connector-starrocks (1.2.3_flink-1.14_2.11.jar) to thelibdirectory of Flink.Note If a Flink cluster is already running in your system, you must stop the Flink cluster and restart it to load and validate the JAR packages.
-
Download and decompress the SMT package and place it in the
flink-1.14.5directory. The database provides SMT packages for Linux x86 and macos ARM64. You can choose one based on your operating system and CPU.
Enable MySQL binary log
To synchronize data from MySQL in real time, the system needs to read data from MySQL binary log (binlog), parse the data, and then synchronize the data to the database. Make sure that MySQL binary log is enabled.-
Edit the MySQL configuration file
my.cnf(default path:/etc/my.cnf) to enable MySQL binary log. -
Run one of the following commands to restart MySQL for the modified configuration file to take effect.
-
Connect to MySQL and check whether MySQL binary log is enabled.
Connect to the database
The SMT configuration file sets these addresses inflink.starrocks.jdbc-url and flink.starrocks.load-url.
Set the database connection options to addresses of the FE:
jdbc-url:jdbc:mysql://<fe_host>:<fe_query_port>. The query port defaults to9030.load-url:<fe_host>:<fe_http_port>. The HTTP port defaults to8030.
<fe_host>, use an address that stays the same when an FE restarts or is replaced, such as a DNS name or a load balancer in front of the FEs, not the IP address of an FE. If the address stops leading to an FE, the Flink job can no longer load data.
The Flink TaskManagers must be able to reach these FE ports, and also the HTTP port (default 8040) of every BE or CN, because the FE redirects each load request to a BE or CN.
Synchronize database & table schema
-
Edit the SMT configuration file.
Go to the SMT
confdirectory and edit the configuration fileconfig_prod.conf, such as MySQL connection information, the matching rules of the database & table to be synchronized, and configuration information of flink-connector-starrocks.-
[db]: information used to access the source database.type: type of the source database. In this topic, the source database ismysql.host: IP address of the MySQL server.port: port number of the MySQL database, defaults to3306user: username for accessing the MySQL databasepassword: password of the username
-
[table-rule]: database & table matching rules and the corresponding flink-connector-starrocks configuration.Database,table: the names of the database & table in MySQL. Regular expressions are supported.flink.starrocks.*: configuration information of flink-connector-starrocks. For more configurations and information, see flink-connector-starrocks.
If you need to use different flink-connector-starrocks configurations for different tables. For example, if some tables are frequently updated and you need to accelerate data loading, see Use different flink-connector-starrocks configurations for different tables. If you need to load multiple tables obtained from MySQL sharding into the same StarRocks table, see Synchronize multiple tables after MySQL sharding to one table in StarRocks.
-
[other]: other informationbe_num: The number of BEs in your StarRocks cluster (This parameter will be used for setting a reasonable number of tablets in subsequent StarRocks table creation).use_decimal_v3: Whether to enable Decimal V3. After Decimal V3 is enabled, MySQL decimal data will be converted into Decimal V3 data when data is synchronized to StarRocks.output_dir: The path to save the SQL files to be generated. The SQL files will be used to create a database & table in StarRocks and submit a Flink job to the Flink cluster. The default path is./resultand we recommend that you retain the default settings.
-
-
Run the SMT to read the database & table schema in MySQL and generate SQL files in the
./resultdirectory based on the configuration file. Thestarrocks-create.all.sqlfile is used to create a database & table in the database and theflink-create.all.sqlfile is used to submit a Flink job to the Flink cluster. -
Run the following command to connect to the database and execute the
starrocks-create.all.sqlfile to create a database and table in the database. We recommend that you use the default table creation statement in the SQL file to create a table of the Primary Key table.Note You can also modify the table creation statement based on your business needs and create a table that does not use the Primary Key table. However, the DELETE operation in the source MySQL database cannot be synchronized to the non- Primary Key table. Exercise caution when you create such a table.
If the data needs to be processed by Flink before it is written to the destination StarRocks table, the table schema will be different between the source and destination tables. In this case, you must modify the table creation statement. In this example, the destination table requires only theproduct_idandproduct_namecolumns and real-time ranking of commodity sales. You can use the following table creation statement.NOTICE Since v2.5.7, StarRocks can automatically set the number of buckets (BUCKETS) when you create a table or add a partition. You no longer need to manually set the number of buckets. For detailed information, see set the number of buckets.
Synchronize data
Run the Flink cluster and submit a Flink job to continuously synchronize full and incremental data from MySQL to the database.-
Go to the Flink directory and run the following command to run the
flink-create.all.sqlfile on your Flink SQL client.This SQL file defines dynamic tablessource tableandsink table, query statementINSERT INTO SELECT, and specifies the connector, source database, and destination database. After this file is executed, a Flink job is submitted to the Flink cluster to start data synchronization.Note
Process data during synchronization: If you need to process data during synchronization, such as performing GROUP BY or JOIN on the data, you can modify the- Make sure that the Flink cluster has been started. You can start the Flink cluster by running
flink/bin/start-cluster.sh. - If your Flink version is earlier than 1.13, you may not be able to directly run the SQL file
flink-create.all.sql. You need to execute SQL statements one by one in this file in the command line interface (CLI) of the SQL client. You also need to escape the\character.
flink-create.all.sqlfile. The following example calculates real-time ranking of commodity sales by executing COUNT (*) and GROUP BY.If you only need to synchronize only a portion of the data, such as data whose payment time is later than December 21, 2021, you can use theWHEREclause inINSERT INTO SELECTto set a filter condition, such asWHERE pay_dt > '2021-12-21'. Data that does not meet this condition will not be synchronized to StarRocks. If the following result is returned, the Flink job has been submitted for full and incremental synchronization. - Make sure that the Flink cluster has been started. You can start the Flink cluster by running
-
You can use the Flink WebUI or run the
bin/flink list -runningcommand on your Flink SQL client to view Flink jobs that are running in the Flink cluster and the job IDs.-
Flink WebUI

-
bin/flink list -running
Note If the job is abnormal, you can perform troubleshooting by using Flink WebUI or by viewing the log file in the
/logdirectory of Flink 1.14.5. -
Flink WebUI
FAQ
Use different flink-connector-starrocks configurations for different tables
If some tables in the data source are frequently updated and you want to accelerate the loading speed of flink-connector-starrocks, you must set a separate flink-connector-starrocks configuration for each table in the SMT configuration fileconfig_prod.conf.
Synchronize multiple tables after MySQL sharding to one table in the database
After sharding is performed, data in one MySQL table may be split into multiple tables or even distributed to multiple databases. All the tables have the same schema. In this case, you can set[table-rule] to synchronize these tables to one table. For example, MySQL has two databases edu_db_1 and edu_db_2, each of which has two tables course_1 and course_2, and the schema of all tables is the same. You can use the following [table-rule] configuration to synchronize all the tables to one table.
Note The name of the table defaults tocourse__auto_shard. If you need to use a different name, you can modify it in the SQL filesstarrocks-create.all.sqlandflink-create.all.sql
Import data in JSON format
Data in the preceding example is imported in CSV format. If you are unable to choose a suitable delimiter, you need to replace the following parameters offlink.starrocks.* in [table-rule].
Note This method slightly slows down the loading speed.
Execute multiple INSERT INTO statements as one Flink job
You can use the STATEMENT SET syntax in theflink-create.all.sql file to execute multiple INSERT INTO statements as one Flink job, which prevents multiple statements from taking up too many Flink job resources and improves the efficiency of executing multiple queries.
Note Flink supports the STATEMENT SET syntax from 1.13 onwards.
-
Open the
result/flink-create.all.sqlfile. -
Modify the SQL statements in the file. Move all the INSERT INTO statements to the end of the file. Place
EXECUTE STATEMENT SET BEGINbefore the first INSERT INTO statement and placeEND;after the last INSERT INTO statement.
Note The positions of CREATE DATABASE and CREATE TABLE remain unchanged.