English | 中文
| mysql column type | binlog column type(raw) | binlog column type(parsed from binlog column meta) | rust type |
|---|---|---|---|
| BIT | MYSQL_TYPE_BIT = 16 | ColumnType::Bit | ColumnValue::Bit(u64) |
| TINYINT [UNSIGNED] | MYSQL_TYPE_TINY = 1 | ColumnType::Tiny | ColumnValue::Tiny(i8) |
| SMALLINT [UNSIGNED] | MYSQL_TYPE_SHORT = 2 | ColumnType::Short | ColumnValue::Short(i16) |
| MEDIUMINT [UNSIGNED] | MYSQL_TYPE_INT24 = 9 | ColumnType::Int24 | ColumnValue::Long(i32) |
| INT [UNSIGNED] | MYSQL_TYPE_LONG = 3 | ColumnType::Long | ColumnValue::Long(i32) |
| BIGINT [UNSIGNED] | MYSQL_TYPE_LONGLONG = 8 | ColumnType::LongLong | ColumnValue::LongLong(i64) |
| FLOAT | MYSQL_TYPE_FLOAT = 4 | ColumnType::Float | ColumnValue::Float(f32) |
| DOUBLE | MYSQL_TYPE_DOUBLE = 5 | ColumnType::Double | ColumnValue::Double(f64) |
| DECIMAL | MYSQL_TYPE_NEWDECIMAL = 246 | ColumnType::NewDecimal | ColumnValue::Decimal(String) |
| DATE | MYSQL_TYPE_DATE = 10 | ColumnType::Date | ColumnValue::Date(String) |
| TIME | MYSQL_TYPE_TIME2 = 19 | ColumnType::Time2 | ColumnValue::Time(String) |
| TIMESTAMP | MYSQL_TYPE_TIMESTAMP2 = 17 | ColumnType::TimeStamp2 | ColumnValue::Timestamp(i64) |
| DATETIME | MYSQL_TYPE_DATETIME2 = 18 | ColumnType::DateTime2 | ColumnValue::DateTime(String) |
| YEAR | MYSQL_TYPE_YEAR = 13 | ColumnType::Year | ColumnValue::Year(u16) |
| CHAR | MYSQL_TYPE_STRING = 254 | ColumnType::String | ColumnValue::String(Vec) |
| VARCHAR | MYSQL_TYPE_VARCHAR = 15 | ColumnType::VarChar | ColumnValue::String(Vec) |
| BINARY | MYSQL_TYPE_STRING = 254 | ColumnType::String | ColumnValue::String(Vec) |
| VARBINARY | MYSQL_TYPE_VARCHAR = 15 | ColumnType::VarChar | ColumnValue::String(Vec) |
| ENUM | MYSQL_TYPE_STRING = 254 | ColumnType::Enum | ColumnValue::Enum(u32) |
| SET | MYSQL_TYPE_STRING = 254 | ColumnType::Set | ColumnValue::Set(u64) |
| TINYTEXT TEXT MEDIUMTEXT LONGTEXT TINYBLOB BLOB MEDIUMBLOB LONGBLOB | MYSQL_TYPE_BLOB = 252 | ColumnType::Blob | ColumnValue::Blob(Vec) |
| GEOMETRY | MYSQL_TYPE_GEOMETRY = 255 | ColumnType::Geometry | ColumnValue::Blob(Vec) |
| JSON | MYSQL_TYPE_JSON = 245 | ColumnType::Json | ColumnValue::Json(Vec) |
docker run -d --name mysql57 \
--platform linux/x86_64 \
-it --restart=always \
-p 3307:3306 \
-e MYSQL_ROOT_PASSWORD="123456" \
mysql:5.7.40 \
--lower_case_table_names=1 \
--character-set-server=utf8 \
--collation-server=utf8_general_ci \
--datadir=/var/lib/mysql \
--user=mysql \
--server_id=1 \
--log_bin=/var/lib/mysql/mysql-bin.log \
--max_binlog_size=100M \
--gtid_mode=ON \
--enforce_gtid_consistency=ON \
--binlog_format=ROW
docker run -d --name mysql80 \
--platform linux/x86_64 \
-it --restart=always \
-p 3308:3306 -e MYSQL_ROOT_PASSWORD="123456" \
mysql:8.0.31 \
--lower_case_table_names=1 \
--character-set-server=utf8 \
--collation-server=utf8_general_ci \
--datadir=/var/lib/mysql \
--user=mysql \
--server_id=1 \
--log_bin=/var/lib/mysql/mysql-bin.log \
--max_binlog_size=100M \
--gtid_mode=ON \
--enforce_gtid_consistency=ON \
--binlog_format=ROW \
--binlog-transaction-compression \
--binlog_rows_query_log_events=ON \
--default_authentication_plugin=mysql_native_password \
--default_time_zone="+08:00"
db_url=mysql://root:123456@127.0.0.1:3307
server_id=200
default_db="db_test"
default_tb="tb_test"
binlog_parse_millis=100
cargo test --package mysql-binlog-connector-rust --test integration_test
ssl-mode=required to db_url. To use plain TCP, set ssl-mode=disabled or omit it.openssl-tls: better compatibility with older MySQL 5.7 TLS stacksrustls: pure Rust TLS backendcargo test --package mysql-binlog-connector-rust --test integration_test --features openssl-tls
or
cargo test --package mysql-binlog-connector-rust --test integration_test --features rustls
db_url with TLS enabled:db_url=mysql://root:123456@127.0.0.1:3307?ssl-mode=required
db_url with TLS disabled:db_url=mysql://root:123456@127.0.0.1:3307?ssl-mode=disabled
fn main() {
block_on(dump_and_parse())
}
async fn dump_and_parse() {
let env_path = env::current_dir().unwrap().join("example/src/.env");
dotenv::from_path(env_path).unwrap();
let url = env::var("db_url").unwrap();
let server_id: u64 = env::var("server_id").unwrap().parse().unwrap();
let binlog_filename = env::var("binlog_filename").unwrap();
let binlog_position: u32 = env::var("binlog_position").unwrap().parse().unwrap();
let gtid_set = env::var("gtid_set").unwrap();
let start_position = if !gtid_set.is_empty() {
StartPosition::Gtid(gtid_set)
} else if !binlog_filename.is_empty() {
StartPosition::BinlogPosition(binlog_filename, binlog_position)
} else {
StartPosition::Latest
};
let mut stream = BinlogClient::new(url.as_str(), server_id, start_position)
/// Heartbeat interval in seconds
/// Server will send a heartbeat event if no binlog events are received within this interval
/// If heartbeat_interval_secs=0, server won't send heartbeat events
/// default is 0 means not enabled
.with_master_heartbeat(Duration::from_secs(5))
/// Network operation timeout in seconds
/// Maximum wait time for operations like connection establishment and data reading
/// default is 60 secs
.with_read_timeout(Duration::from_secs(60))
/// TCP keepalive idle time and keepalive interval time
/// default is (0 secs, 0 secs) means keepalive not enabled
.with_keepalive(Duration::from_secs(60), Duration::from_secs(10))
.connect()
.await
.unwrap();
let mut stream = client.connect().await.unwrap();
loop {
let (header, data) = stream.read().await.unwrap();
println!("header: {:?}", header);
println!("data: {:?}", data);
println!();
}
}
cargo run -p mysql-binlog-connector-rust-example
openssl-tls by default. If you want to switch to rustls, update example/Cargo.toml and set:mysql-binlog-connector-rust = { path = "../", features = ["rustls"] }
flush logs;
SET autocommit=0;
CREATE DATABASE test_db;
USE test_db;
CREATE TABLE test_tb(id INT, value INT);
INSERT INTO test_tb VALUES(1,1),(2,2),(3,3),(4,4);
UPDATE test_tb SET value=3 WHERE id in(1,2);
DELETE FROM test_tb WHERE id in (1,2);
TRUNCATE TABLE test_tb;
DROP TABLE test_tb;
commit;
```sql mysql> show binary logs; +------------------+-----------+ | Log_name | File_size | +------------------+-----------+ | mysql-bin.000050 | 1255 |
mysql> show binlog events in 'mysql-bin.000050';
+------------------+------+----------------+-----------+-------------+------------------------------------------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+------------------+------+----------------+-----------+-------------+------------------------------------------------------------------------+
| mysql-bin.000050 | 4 | Format_desc | 1 | 123 | Server ver: 5.7.40-log, Binlog ver: 4 |
| mysql-bin.000050 | 123 | Previous_gtids | 1 | 194 | 50dc6874-13d3-11ee-a17a-0242ac110002:1-176027 |
| mysql-bin.000050 | 194 | Gtid | 1 | 259 | SET @@SESSION.GTID_NEXT= '50dc6874-13d3-11ee-a17a-0242ac110002:176028' |
| mysql-bin.000050 | 259 | Query | 1 | 378 | use test_db; CREATE TABLE test_tb(id INT, value INT) |
| mysql-bin.000050 | 378 | Gtid | 1 | 443 | SET @@SESSION.GTID_NEXT= '50dc6874-13d3-11ee-a17a-0242ac110002:176029' |
| mysql-bin.000050 | 443 | Query | 1 | 518 | BEGIN |
| mysql-bin.000050 | 518 | Table_map | 1 | 572 | table_id: 12832 (test_db.test_tb) |
| mysql-bin.000050 | 572 | Write_rows | 1 | 643 | table_id: 12832 flags: STMT_END_F |
| mysql-bin.000050 | 643 | Table_map | 1 | 697 | table_id: 12832 (test_db.test_tb) |
| mysql-bin.000050 | 697 | Update_rows | 1 | 769 | table_id: 12832 flags: STMT_END_F |
| mysql-bin.000050 | 769 | Table_map | 1 | 823 | table_id: 12832 (test_db.test_tb) |
| mysql-bin.000050 | 823 | Delete_rows | 1 | 876 | table_id: 12832 flags: STMT_END_F |
| mysql-bin.0000
$ claude mcp add mysql-binlog-connector-rust \
-- python -m otcore.mcp_server <graph>