Published on

Rust 静态编译libmysql.mdx

Authors
[dependencies]
diesel = { version = "2.2.0", features = ["mysql"] }
# build libmysqlclient as part of the build process
# uncomment this line if you run into setup issues
# mysqlclient-sys = { version = "0.4", features = ["bundled"] }
dotenvy = "0.15"

根据diesel官方文档,放开mysqlclient那行,就可静态把mysql编译进程序。但是在一些底版本的linux系统中,gcc条件不满足会编译失败。

https://github.com/BlackDex/rust-musl/ 使用rust-musl镜像进行编译,里面已经配置好需要的mysql,openssl等源码,这样就可以纯静态编译程式,Cargo.toml也不需要做任何修改。 执行如下命令:

docker run --rm -it -v "$(pwd)":/home/rust/src docker.io/blackdex/rust-musl:x86_64-musl-nightly cargo build --release