0
0

More than 1 year has passed since last update.

RustのMySQLでURLが正しいのに接続できない

Posted at

はじめに

久しぶりに昔作成したリポジトリをクローンしてきて.envを設定したところ、なぜかDBに接続できなかったのでまとめます

問題

DBに接続したところ以下のエラーがでました

failures:

---- presentation::controller::user_controller::test::crud_scenario stdout ----
=============================
database_url: mysql://root:password@127.0.0.1:3306/sample_test
thread 'main' panicked at 'assertion failed: result.is_ok()', src/infrastructure/user_repository.rs:32:13
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

環境変数は以下のように設定しています。

.env
DATABASE_URL=mysql://root:password@127.0.0.1:3306/sample
TEST_DATABASE_URL=mysql://root:password@127.0.0.1:3306/sample_test

これは正しいURLでした。
また読み込みは以下のようにしています

util.rs
    let database_url = if cfg!(test) {
        env::var("TEST_DATABASE_URL").expect("TEST_DATABASE_URL must be set")
    } else {
        env::var("DATABASE_URL").expect("DATABASE_URL must be set")
    };

いままで問題なく読み込めていたのになぜか接続ができません

解決方法

.envを以下のようにしたところ接続できました

.env
DATABASE_URL="mysql://root:password@127.0.0.1:3306/sample"
TEST_DATABASE_URL="mysql://root:password@localhost:3306/sample_test"

URLを""で囲む必要があるようです

おわりに

こういう地味に引っかかりそうなところは注意が必要です

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0