0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Docker gorm MySQLの接続

Last updated at Posted at 2022-10-23

DB接続について

問題

docker compose up と同時にsplファイルを読んで作られたMySQLサーバーに gorm 上で作ったアカウントでアクセスしようとしてもアカウントは作られるが、接続ができなかった。

database.go
    func init() {
        USER := os.Getenv("MYSQL_USER")
        PASS := os.Getenv("MYSQL_PASSWORD")
        PROTOCOL := tcp("xxxxx")
        DBNAME := os.Getenv("MYSQL_DATABASE_DS")
        fmt.Println(PASS)
        dialector := mysql.Open(dsn)

        var err error

        if db, err = gorm.Open(dialector); err != nil {
            connect(dialector, 100)
        }
        fmt.Println("db connected!!")
    }

対処法

USERをrootに、PASSをdocker-compose.ymlの MYSQL_ROOT_PASSWORDと同じに設定し直したら問題なく接続できた。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?