All Products
Search
Document Center

Lindorm:Use Rust to develop applications

Last Updated:Feb 29, 2024

This topic describes how to use Rust to connect to and use LindormTable.

Prerequisites

Procedure

  1. Install Rust. For more information, see Install Rust.

  2. Add the following dependencies to the Cargo.toml file:

    [dependencies]
    mysql="*"

    If you set mysql to *, the latest version of MySQL is used by default.

  3. Configure connection parameters.

    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);

    Parameters

    Parameter

    Description

    ip_or_hostname

    The LindormTable endpoint for MySQL. For more information about how to obtain the endpoint, see View endpoints.

    Important
    • If your application is deployed on an ECS instance, we recommend that you use a VPC to connect to the Lindorm instance to ensure higher security and lower network latency.

    • If your application is deployed on a local server and needs to connect to the Lindorm instance over the Internet, you can perform the following steps to enable the Internet endpoint for the instance in the Lindorm console: In the left-side navigation pane, select Database Connections > Wide Table Engine. On the Wide Table Engine tab, click Enable Public Endpoint.

    • If you use a VPC to access the Lindorm instance, specify the LindormTable VPC endpoint for MySQL in the value of ip_or_hostname. If you use the Internet to access the Lindorm instance, specify the LindormTable Internet endpoint for MySQL in the value of ip_or_hostname.

    user

    If you forget your password, you can change the password in the cluster management system of LindormTable. For more information, see Manage users.

    pass

    db_name

    The name of the database to which you want to connect. By default, your client is connected to a database named default.

    tcp_port

    The port used to access LindormTable by using MySQL. The value of this parameter is fixed to 33060.

  4. Establish a connection and use LindormTable SQL to perform operations in LindormTable. The following code block provides an example on how to use LindormTable SQL to create a table:

    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");
  5. Run the following command to build the Rust project:

    cargo build
  6. Run the following command to run the Rust project:

    cargo run

Sample code

The following code provides a complete example on how to connect to and use LindormTable by using Rust:

use mysql::*;
use mysql::prelude::*;

fn main() {
    // Specify the LindormTable endpoint for MySQL, username and password used to connect to LindormTable, the database that you want to connect, and the port used to connect to LindormTable.
    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 to the table.
    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 data in the table.
    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);
    }
}

The following result is returned:

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