LoginSignup
1
0

More than 1 year has passed since last update.

SQLへの繋ぎ方(mySQL)

Posted at

やること

Dockerでdbサーバを立てて、そこにアプリケーション上からアクセスする

Dockerでdbサーバを立てる

今回はMySQLを使用(理由はなんとなく)

docker-compose.yml
version: '3'

services:
  db:
    image: mysql:5.7
    environment: 
    - MYSQL_DATABASE=sample_db
    - MYSQL_ROOT_PASSWORD=password
    command: >
      --character-set-server=utf8mb4
      --collation-server=utf8mb4_general_ci
    ports:
      - 3306:3306

と記述した。
その後、このdocker-compose.ymlがあるディレクトリで、

$ docker-compose up -d

を実行して、コンテナを起動する。
コンテナが起動できているかどうか確認するには、

$ docker ps
または
$ docker-compose ps

で確認でき、NAMESNAME(ディレクトリ名)-db-1とあれば、今回作成したコンテナが起動できているとわかる。

dbにアプリケーション上からアクセスする

今回はgolangのgormを使ってアクセスする。
今回のアプリケーションのディレクトリ構造は以下のようなものにした。

.
├── controllers
│   ├── controlles.go
│   └── init.go
├── docker-compose.yml
├── go.mod
├── go.sum
├── main.go
└── model
    └── type.go

(ディレクトリの分け方については勉強できていないので、勘です。)

まず、gormを使ってdbに接続します。

controllers/init.go
package controllers

import (
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

func DbInit() *gorm.DB {
	parseTime=true
	dsn := "root:password@tcp(localhost:3306)/sample_db?charset=utf8mb4&parseTime=true"
	db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		panic(err)
	}

	return db
}

また、gormは構造体でテーブルにカラムを生成してくれるので、構造体を用意する。

model/type.go
package model

import "gorm.io/gorm"

type User struct {
	gorm.Model

	Name     string
	Age      int
	IsActive bool
}

このUserという構造体をもとに、DBを作る。

main.go
package main

import (
	"github.com/39shin52/gormsample/controllers"
	"github.com/39shin52/gormsample/model"
)

func main() {
	db := controllers.DbInit()

	db.AutoMigrate(&model.User{})
}

AutoMigrateというものが、構造体をもとにテーブルを自動的に生成してくれる。便利。

この後

このあとは、よしなにInsert(Create)の機能やSELECT(Read)の機能を実装すれば、dbを自由に自由に弄ることが出来る!
ちなみに、本当にデータ追加されてる?とか気になった時は、コンテナ内を見に行く。

$ docker-compose exec -it CONTAINER ID /bin/bash
bash-4.2\# mysql -u root -p
> Enter password: (passwordと出てこないけど打ち込む)
> Welcome to the MySQL monitor...
> ...
mysql> show databases;
> +--------------------+
> | Database           |
> +--------------------+
> | information_schema |
> | mysql              |
> | performance_schema |
> | sample_db          |
> | sys                |
> +--------------------+ 
mysql> use sample_db;
> Database changed
mysql> show tables;
> +---------------------+
> | Tables_in_sample_db |
> +---------------------+
> | users               |
> +---------------------+
mysql> select * from users;
> +----+-------------------------+-------------------------+------------+--------+------+-----------+
> | id | created_at              | updated_at              | deleted_at | name   | age  | is_active |
> +----+-------------------------+-------------------------+------------+--------+------+-----------+
> |  1 | 2022-11-27 00:00:00.000 | 2022-11-27 00:00:00.000 | NULL       | Tanaka |   20 |         1 |
> |  2 | 2022-11-27 00:00:00.000 | 2022-11-27 00:00:00.000 | NULL       | Sato   |   22 |         0 |
> +----+-------------------------+-------------------------+------------+--------+------+-----------+

上に書いたのは、Insertしたあとなので、一番最初にmain.goを実行したときは、何も入ってないよみたいなこと言われます。

参考にさせていただいた記事

1
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
1
0