Connect to LindormTable from a Rust application using the MySQL compatibility protocol and the mysql crate.
Prerequisites
Before you begin, make sure you have:
The MySQL compatibility feature enabled on your Lindorm instance. See Enable the MySQL compatibility feature.
Your client IP address added to the instance whitelist. See Configure whitelists.
Rust installed on your machine. See Install Rust.
Connect to LindormTable
Step 1: Add the mysql dependency
In your Cargo.toml, add the mysql crate under [dependencies]:
[dependencies]
mysql = "*"Setting the version to * pulls the latest release.
Step 2: Configure connection parameters
Build the connection options using OptsBuilder:
let opts = OptsBuilder::new()
.ip_or_hostname(Some("ld-uf6k8yqb741t3****-proxy-sql-lindorm-public.lindorm.rds.aliyuncs.com"))
.user(Some("user"))
.pass(Some("test"))
.db_name(Some("default"))
.tcp_port(33060);| Parameter | Description |
|---|---|
ip_or_hostname | LindormTable endpoint for MySQL. See View endpoints. |
user | Username. To reset a forgotten password, see Manage users. |
pass | Password. |
db_name | Database name. Defaults to default. |
tcp_port | Fixed value: 33060. |
Choosing the right endpoint type
VPC (recommended for ECS-deployed apps): Use the LindormTable VPC endpoint for MySQL. VPC access offers higher security and lower network latency.
Internet (for local or external clients): Use the LindormTable Internet endpoint for MySQL. To enable Internet access, go to the Lindorm console, navigate to Database Connections > Wide Table Engine, and click Enable Public Endpoint.
Step 3: Establish a connection and run SQL
Create a connection pool and get a connection, then use LindormTable SQL to operate on your data:
let pool = Pool::new(opts).unwrap();
let mut conn = pool.get_conn().unwrap();
// Create a table
conn.query_drop(
r"create table if not exists user_test(id int, name varchar, age int, primary key(id))"
).expect("Failed to create table");Choosing the right execute method
| Method | Use when |
|---|---|
query_drop | DDL statements (CREATE, DROP) or DML where you don't need the result |
exec_drop | DML with parameter binding (upsert, update, delete) |
query | SELECT statements that return rows |
Step 4: Build and run
cargo build
cargo runSample code
The following example connects to LindormTable, creates a table, inserts two rows, and queries all rows.
use mysql::*;
use mysql::prelude::*;
fn main() {
// Configure the endpoint, credentials, database, and port
let opts = OptsBuilder::new()
.ip_or_hostname(Some("ld-xxxx-sql-lindorm.lindorm.rds.aliyuncs.com"))
.user(Some("root"))
.pass(Some("root"))
.db_name(Some("default"))
.tcp_port(33060);
let pool = Pool::new(opts).unwrap();
let mut conn = pool.get_conn().unwrap();
// Create a table
conn.query_drop(
r"create table if not exists user_test(id int, name varchar, age int, primary key(id))"
).expect("Failed to create table");
// Insert data using upsert
conn.exec_drop(
r"upsert into user_test(id,name,age) values(?,?,?)",
(1, "zhangsan", 17)
).expect("Failed to insert data 1");
conn.exec_drop(
r"upsert into user_test(id,name,age) values(?,?,?)",
(2, "lisi", 27)
).expect("Failed to insert data 2");
// Query all rows
let result = conn.query(r"select * from user_test").expect("Failed to select");
for row in result {
let (id, name, age): (i32, String, i32) = from_row(row);
println!("Id: {}, Name: {}, Age: {}", id, name, age);
}
}Expected output:
Finished dev [unoptimized + debuginfo] target(s) in 0.09s
Running `target/debug/hello_word`
Id: 1, Name: zhangsan, Age: 17
Id: 2, Name: lisi, Age: 27What's next
View endpoints — Get your VPC or Internet endpoint.
Manage users — Manage LindormTable usernames and passwords.
Configure whitelists — Control which IP addresses can connect to your instance.