1. [Rust] diesel支持mysql方法

如果使用rocket框架,那么就不需要往下看了。因為rocket框架已經內置了ORM

配置diesel遇到了N個坑!在這里記錄一下!我的電腦是win10 64位。diesel是數據庫持久層。

開始操作

  • 訪問 https://downloads.mysql.com/archives/c-c/ 下載 mysql-connector-c-6.1.11-winx64安裝
  • 找到mysql-connector-c安裝路徑, 我的路徑如下 C:\Program Files\MySQL\MySQL Connector C 6.1\lib\vs14
  • 以管理員權限啟動CMD, 執行如下命令
setx MYSQLCLIENT_LIB_DIR "C:\Program Files\MySQL\MySQL Connector C 6.1\lib\vs14"
  • 配置config文件,文件路徑為 C:\Users\用戶名\.cargo\config,如果不存在那就創建config文件,內容如下。
#配置使用中科大源
[http]
check-revoke = false

[source.crates-io]
replace-with = 'ustc'

[source.ustc]
registry = "https://mirrors.ustc.edu.cn/crates.io-index"

#配置mysql編譯
[build]
rustflags = ["-L", "C:\\Program Files\\MySQL\\MySQL Connector C 6.1\\lib\\vs14"]

在上面的配置文件中有[build]配置內容,這一步非常重要,如果在這里不進行配置的話,即使是diesel安裝成功了,但是項目啟動時無法正常啟動,原因是在編譯、測試時還是要這個環境變量,因為其中的依賴diesel = { version = "1.4.5", features = ["mysql"] }需要它

  • 安裝diesel
cargo install diesel_cli --no-default-features --features mysql
  • 安裝完成后執行diesel,結果如下。
C:\WINDOWS\system32>diesel
diesel 1.4.1

USAGE:
    diesel [FLAGS] [OPTIONS] <SUBCOMMAND>

FLAGS:
        --locked-schema    Require that the schema file is up to date
    -h, --help             Prints help information
    -V, --version          Prints version information

OPTIONS:
        --config-file <CONFIG_FILE>      The location of the configuration file to use. Falls back to the
                                         `DIESEL_CONFIG_FILE` environment variable if unspecified. Defaults to
                                         `diesel.toml` in your project root. See diesel.rs/guides/configuring-diesel-cli
                                         for documentation on this file.
        --database-url <DATABASE_URL>    Specifies the database URL to connect to. Falls back to the DATABASE_URL
                                         environment variable if unspecified.

SUBCOMMANDS:
    bash-completion    DEPRECATED: Generate bash completion script for the diesel command.
    completions        Generate shell completion scripts for the diesel command.
    database           A group of commands for setting up and resetting your database.
    help               Prints this message or the help of the given subcommand(s)
    migration          A group of commands for generating, running, and reverting migrations.
    print-schema       Print table definitions for database schema.
    setup              Creates the migrations directory, creates the database specified in your DATABASE_URL, and
                       runs existing migrations.

You can also run `diesel SUBCOMMAND -h` to get more information about that subcommand.
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。