All Products
Search
Document Center

Lindorm:Use Rust to develop applications

Last Updated:Mar 28, 2026

Connect to LindormTable from a Rust application using the MySQL compatibility protocol and the mysql crate.

Prerequisites

Before you begin, make sure you have:

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);
ParameterDescription
ip_or_hostnameLindormTable endpoint for MySQL. See View endpoints.
userUsername. To reset a forgotten password, see Manage users.
passPassword.
db_nameDatabase name. Defaults to default.
tcp_portFixed 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

MethodUse when
query_dropDDL statements (CREATE, DROP) or DML where you don't need the result
exec_dropDML with parameter binding (upsert, update, delete)
querySELECT statements that return rows

Step 4: Build and run

cargo build
cargo run

Sample 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: 27

What's next