Rust, Software Development

Rust – Connect to MySQL

Photo by Mike from Pexels

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.

You Might Also Like


  1. 1

    Thank you for sharing.

    Under Rust Codes in your post you make reference to Postgres, is not supposed to be Mysql?

  2. 2
  3. 3

    I’m new to Rust and wanted to see how it worked with MySQL so thanks for your tutorial. I have one problem, it fails when compiling on line 119 of – error: expected an item keyword (let function_table_access = dbghelp.SymFunctionTableAccess64();).
    Have I missed something obvious setting up Rust environment? My database is a remote database hosted on my website, if that makes any difference?

  4. 4

    Are you using crates with the same versions as in the post? Perhaps SysFunctionTableAccess64 is missing? Do you have the codes in public github repo?

  5. 5

    Thanks for the reply – my mistake had put ‘MySQL = “*”‘ in dependencies, changed to MySQL = “16.1.0” and now working perfectly. Thanks again for sharing original code.

Comments are closed.