Fix for Rust on Windows not able to find MySQL library

Posted on . Reading time: 1 min. Tags: mysql, rust, windows.

I was tinkering with the Diesel ORM on a Rust project, and when building it I faced the following error:

$ cargo run
Compiling tokio v1.34.0
Compiling tinyvec_macros v0.1.1
Compiling serde v1.0.193
Compiling tinyvec v1.6.0
Compiling unicode-bidi v0.3.13
Compiling pin-project v1.1.3
Compiling axum-core v0.3.4
Compiling mysqlclient-sys v0.2.5
Compiling byteorder v1.5.0
error: could not find native static library `mysqlclient`, perhaps an -L flag is missing?

error: could not compile `mysqlclient-sys` (lib) due to previous error
warning: build failed, waiting for other jobs to finish...

After looking online, I found this GitHub comment instructing that I needed to specify the location of mysqlclient.lib using environment variables. Some old comments on that discussion say to point the environment variable to the C connector, but it seems that MySQL moved mysqlclient.lib from the C connector to the Server between versions 6 and 7. So, in my case, I had to set the environment variable to $env:MYSQLCLIENT_LIB_DIR='C:\Program Files\MySQL\MySQL Server 8.0\lib'.

After that, success!