0
1

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でMysqlサーバの構築

Last updated at Posted at 2022-09-30

Dockerにて日本語化対応したMySQLサーバを構築します。
ポートホワードの設定を行い外部からは、ポートの33060で待ち受けします。

■完成イメージ

+----------+            +-----------+            +-----------+
|   App    |<---------->|   host    |<---------->| Docker    |
|          | port=33060 | (Ubuntu)  | port=3306  | Container |
|          |            |           |            |  (MySQL)  |
+----------+            +-----------+            +-----------+

■構築環境

・Ubuntu 18.04.6 LTS
・Docker20.10.7

1. 作業用ディレクトリ作成

$sudo mkdir ~/work/docker-mysql
$cd ~/work/docker-mysql

2. Docker HubよりMysqlイメージ取得

$ sudo dokker pull mysql

3. イメージの確認

$ sudo docker image ls
REPOSITORY   TAG       IMAGE ID       CREATED      SIZE
mysql        latest    4ddc4f9f1152   3 days ago   491MB

4. Mysql日本語化作業

4.1. Dockerfileを作成

    FROM mysql
    ADD my.cnf /etc/mysql/conf.d/my.cnf

4.2. my.cnfを作成

MySQLで扱う文字コードをUTF8に設定。

my.cnf
    [mysqld]
    character-set-server=utf8
    [mysql]
    default-character-set=utf8
    [mysqldump]
    default-character-set=utf8

5. イメージ名を”mysql-ja”としてビルド

$ sudo docker build -t nysql-ja .

6. イメージの確認

$ sudo docker image ls
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
mysql-ja     latest    b367bb02f70f   33 seconds ago   491MB
mysql        latest    4ddc4f9f1152   3 days ago       491MB

7. コンテナ名を”mysql-main”で作成し起動

$ sudo docker run -it --name mysql-main MYSQL_ROOT_PASSWORD=[パスワード] -p 33060:3306 -d mysql-ja

8. 起動済みコンテナの確認

$ sudo docker ps
ONTAINER ID   IMAGE      COMMAND                  CREATED         STATUS         PORTS                                                    NAMES
2dd21af0940e mysql-ja "docker-entrypoint.s…"   2 minutes ago
Up 2 minutes   33060/tcp, 0.0.0.0:33060->3306/tcp, :::33060->3306/tcp mysql-main

9. ホストOSから、33060ポートが待ち受け中か確認

$ sudo netstat -ant
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 127.0.0.1:2947          0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:33060           0.0.0.0:*               LISTEN

10. MySQLコンテナにbashログイン

$ sudo docker exec -it コンテナID bash

11. MySQLへログイン

bash-4.4# mysql -u root -p[パスワード]
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?