Help us understand the problem. What is going on with this article?

【備忘】Docker Container の MySQL Server にアクセスする方法メモ

Dockerコンテナ上に立てたMySQL Server にアクセスする際に意外と手間取ったのでメモ。

前提

OS: macOS 10.14.6
docker desktop: v2.3.0.3
Dockerイメージ: https://hub.docker.com/r/mysql/mysql-server

手順

コンテナ起動してアクセス。ポートマッピングを忘れない!

$ docker run --name=mysql-develop \
             -p 3306:3306 \
             -d \
             mysql/mysql-server
$ docker exec -it mysql-develop bash

設定ファイル(/etc/my.cnf)に以下を追記。
docker run で起動している場合、ホストのIPは127.10.0.1です。

my.cnf
bind-address = 127.10.0.1

ルートユーザでコンテナ内からサーバーにアクセス。
パスワードはコンテナ作成時に自動でせいせされるので、ログから確認する。

$ docker logs mysql-develop 2>&1 | grep GENERATED
[Entrypoint] GENERATED ROOT PASSWORD: XXXXXXXX
$ docker exec -it mysql-develop mysql -u root -p
Enter password: # XXXXXXXX

リモートアクセス用のユーザー作成、権限付与。

CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'mypass';
CREATE USER 'myuser'@'%' IDENTIFIED BY 'mypass';
GRANT ALL ON *.* TO 'myuser'@'localhost';
GRANT ALL ON *.* TO 'myuser'@'%';
flush privileges;

コンテナから抜けて、ホストのクライアントからアクセス。

$ mysql -u remote_user -h '127.0.0.1' -p
Enter password: # mypass入力

参考

https://stackoverflow.com/questions/16287559/mysql-adding-user-for-remote-access

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした