rust actix-webv2.0 diesel开发及相关问题解决

cargo.toml

[package]
name = "book-of-changes"
version = "0.1.0"
authors = ["litttley "]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
actix-rt="1.1.1"
actix-web="2.0.0"
env_logger="0.6"
dotenv = "0.15"

diesel = { version = "1.4.5", features = ["mysql", "r2d2","chrono"] }

数据库连接池创建

use diesel::mysql::MysqlConnection;
use diesel::r2d2::{ConnectionManager, Pool, PoolError, PooledConnection};

pub type MysqlPool = Pool>;
//pub type MySqlPooledConnection = PooledConnection>;

pub fn connect() -> MysqlPool {
    let connspec = dotenv::var("DATABASE_URL").expect("DATABASE_URL must be set");
    let manager = ConnectionManager::::new(connspec);
    Pool::builder().build(manager).expect("Error")
}



diesel mysql驱动安装方法参照

cargo install diesel_cli --no-default-features --features mysql
cd api_server
echo "DATABASE_URL=mysql://root@localhost:3306/test_db" > .env

问题 1

could not find native static library `mysqlclient`, perhaps an -L flag is missing?
解决办法

1.下载windows mysql connector

下载地址:https://downloads.mysql.com/archives/c-c/

2、添加windows环境变量

MYSQLCLIENT_LIB_DIR=D:\soft\mysql-connector-c-6.1.11\lib\vs14

然后再重新执行cargo install diesel_cli --no-default-features --features mysql
建议在git-bash 窗口中执行,cmd窗口有可能报错
在这里插入图片描述
在这里插入图片描述如果之前用过postgres的数据库,此时生成diesel.exe会替换为mysql版本的
此时cargo build 编译项目还是会报类似错误,可以用cargo clean 清理后再重新编译解决,如果还有问题就将 D:\soft\mysql-connector-c-6.1.11\lib\vs14 mysqlclient.lib 复制到 D:\soft\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.15.26726\lib\x64目录下

你可能感兴趣的:(rust)