Rust – Connect to MySQL

Last Updated on

This post is about connecting to MySQL from Rust.


Database Table

We have this table structure for our codes and testing.

Sample data:

Update Cargo.toml

Under the [dependencies] section, add the mysql = "16.1.0" :

Rust Codes

Before we create the codes to connect to MySQL and retrieve data from it, we need to reference to MySQL crate as follows.

Then, we need to create a struct that resembles and represents the persons table.

The main function

In the main function, we first get a connection:

Note that we are passing both the database user name and password.

With a database connection, we can now retrieve records from a table.


When we run our application, we get the following output.

Tested with Rust 1.37.0.

Karl San Gabriel

Karl San Gabriel

Professional Software Developer