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?

docker compose でMySQL

Last updated at Posted at 2024-10-19

docker-compose でMySQLを立ち上げ、ポート番号を変更してみる

インストール(例)

dnf -y remove podman runc
curl https://download.docker.com/linux/centos/docker-ce.repo -o /etc/yum.repos.d/docker-ce.repo
sed -i -e "s/enabled=1/enabled=0/g" /etc/yum.repos.d/docker-ce.repo
dnf --enablerepo=docker-ce-stable -y install docker-ce
systemctl enable --now docker


wget -O /usr/local/bin/docker-compose https://github.com/docker/compose/releases/download/v2.29.7/docker-compose-linux-x86_64

chmod +x /usr/local/bin/docker-compose

ディレクトリ構成(例)

├ db
│   ├ conf.d (/etc/mysql/conf.d)
│   │  └ my.cnf
│   └ mysql_data (/var/lib/mysql)
└ docker-compose.yml

docker-compose.yml の内容

	services:
	  mysql:
	    image: mysql:8.0.18
	    ports:
	      - "13306:3306"
	    environment:
	      MYSQL_ROOT_PASSWORD: PassWord1234
	      MYSQL_DATABASE: mysqldb
	      TZ: "Asia/Tokyo"
	    restart: always
	    volumes:
	      - ./db/mysql_data:/var/lib/mysql
	      - ./db/conf.d:/etc/mysql/conf.d

image: 8.0.18のバージョンを指定
ports: 13306は外側のポート番号。ここにクライアント側から接続する
MYSQL_ROOT_PASSWORD: rootのパスワード
MYSQL_DATABASE: データベース名
TZ: タイムゾーンの指定
volumes: 右側のディレクトリが、実体のどこになるかという風に記載する
 ./db/mysql_data:/var/lib/mysql :MySQLのデータが入る場所
 ./db/conf.d:/etc/mysql/conf.d :コンフィグファイルの置き場所

起動

docker-compose up -d mysql

接続例

mysql -u root -p -h 127.0.0.1 --port 13306

注:-h localhostだとうまくつながりません。(ネットワーク経由にならない模様)

停止

docker-compose down
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?