2020-12-26に投稿

Dockerの学習 #4 (Wordpressを使おう その1:Mysqlセットアップ)

読了目安:10分

Qrunchからお引越しした記事です : created_at: 2019-05-19 15:15:39 +0900

今回から実際に手を動かして行こうと思う。
とりあえず小さな目標として、ローカル環境でWordpressの利用ができるところまでをやってみる。

参考URL

https://qiita.com/ymstshinichiro/items/01f6a4c299fdb4b832a2
MYSQLへのログイン。

MySqlのセットアップ

Wordpress自体あまり詳しくないが、どうやらMySQLが必要みたいとの情報をどこかでみた。
MySQLのセットアップをやっていく。

まずイメージファイルの有無を確認。

$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED
SIZE
mysql               5.7                 2f52e94d8acb        3 weeks ago
373MB
mysql               latest              d72169616e20        3 weeks ago
443MB
wordpress           latest              837092bc87de        5 weeks ago
421MB
php                 latest              750fb3e3d6ef        6 weeks ago
367MB
httpd               latest              d4a07e6ce470        6 weeks ago
132MB
hello-world         latest              fce289e99eb9        4 months ago
1.84kB
php                 7.0-apache          aa67a9c9814f        4 months ago
368MB
docker/whalesay     latest              6b362a9f73eb        3 years ago
247MB

以前落としていたのがあるっぽい。
なければそのまま次でRUNを実行すれば自動でイメージも取得してくるはず。

ではrunを実行。コンテナに「--name」オプションで名前をつけて実行。

$ docker run --name mysql_con01 mysql
error: database is uninitialized and password option is not specified
  You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and
 MYSQL_RANDOM_ROOT_PASSWORD

「MYSQL_ROOT_PASSWORD」、「MYSQL_ALLOW_EMPTY_PASSWORD 」、「MYSQL_RANDOM_ROOT_PASSWORD」
のどれかを指定しろとのこと。環境変数を「-e」で指定。

$ docker run --name mysql_con01 -e MYSQL_ROOT_PASSWORD=password mysql
F:\Docker Toolbox\docker.exe: Error response from daemon: Conflict. The containe
r name "/mysql_con01" is already in use by container "a72e4d4a02636be1f3c73722e8
9b0a1d2ab66a55abffb31b3c18a87de5586b4f". You have to remove (or rename) that con
tainer to be able to reuse that name.
See 'F:\Docker Toolbox\docker.exe run --help'.

・・・どうやらERRORが発生していてもコンテナ自体は作成されているようで、
同一名称が存在する旨のエラーが表示された。削除していく。

$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED
STATUS              PORTS               NAMES

psコマンドで見てもコンテナは出てこないな・・・。
あ、オプションは無しだと実行中のコンテナしか出ないからか。

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED
     STATUS                     PORTS               NAMES
a72e4d4a0263        mysql               "docker-entrypoint.s"   3 minutes ago
    Exited (1) 3 minutes ago                       mysql_con01
8adaae58b85c        docker/whalesay     "cowsay Hello World!"    2 weeks ago
     Exited (0) 2 weeks ago                         heuristic_torvalds
e2d544b0e813        docker/whalesay     "Hello-World!"           2 weeks ago
     Created                                        hardcore_pasteur
64cc887485f4        docker/whalesay     "Hello World!"           2 weeks ago
     Created                                        gifted_robinson
223951c34db3        hello-world         "/hello"                 2 weeks ago
     Exited (0) 2 weeks ago                         laughing_antonelli

やはりあるみたいね。消します。

$ docker rm mysql_con01
mysql_con01

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                 CREATED
    STATUS                   PORTS               NAMES
8adaae58b85c        docker/whalesay     "cowsay Hello World!"   2 weeks ago
    Exited (0) 2 weeks ago                       heuristic_torvalds
e2d544b0e813        docker/whalesay     "Hello-World!"          2 weeks ago
    Created                                      hardcore_pasteur
64cc887485f4        docker/whalesay     "Hello World!"          2 weeks ago
    Created                                      gifted_robinson
223951c34db3        hello-world         "/hello"                2 weeks ago
    Exited (0) 2 weeks ago                       laughing_antonelli

無事消えました。気を取り直して再実行。

$ docker run --name mysql_con01 -e MYSQL_ROOT_PASSWORD=password mysql
Initializing database
2019-05-19T05:39:10.121840Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic
 links using --skip-symbolic-links (or equivalent) is the default. Consider not
using this option as it' is deprecated and will be removed in a future release.
2019-05-19T05:39:10.122801Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (my
sqld 8.0.16) initializing of server in progress as process 27
2019-05-19T05:39:59.746085Z 5 [Warning] [MY-010453] [Server] root@localhost is c
reated with an empty password ! Please consider switching off the --initialize-i
nsecure option.
2019-05-19T05:40:25.020508Z 0 [System] [MY-013170] [Server] /usr/sbin/mysqld (my
sqld 8.0.16) initializing of server has completed
Database initialized
MySQL init process in progress...
MySQL init process in progress...
MySQL init process in progress...
2019-05-19T05:40:29.693459Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic
 links using --skip-symbolic-links (or equivalent) is the default. Consider not
using this option as it' is deprecated and will be removed in a future release.
2019-05-19T05:40:29.693540Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (my
sqld 8.0.16) starting as process 78
2019-05-19T05:40:32.312165Z 0 [Warning] [MY-010068] [Server] CA certificate ca.p
em is self signed.
2019-05-19T05:40:32.389363Z 0 [Warning] [MY-011810] [Server] Insecure configurat
ion for --pid-file: Location '/var/run/mysqld' in the path is accessible to all
OS users. Consider choosing a different directory.
2019-05-19T05:40:32.432437Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: re
ady for connections. Version: '8.0.16'  socket: '/var/run/mysqld/mysqld.sock'  p
ort: 0  MySQL Community Server - GPL.
2019-05-19T05:40:32.713605Z 0 [System] [MY-011323] [Server] X Plugin ready for c
onnections. Socket: '/var/run/mysqld/mysqlx.sock'
Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping
 it.
Warning: Unable to load '/usr/share/zoneinfo/leap-seconds.list' as time zone. Sk
ipping it.
Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it
.
Warning: Unable to load '/usr/share/zoneinfo/zone1970.tab' as time zone. Skippin
g it.

2019-05-19T05:40:49.327025Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Sh
utdown complete (mysqld 8.0.16)  MySQL Community Server - GPL.

MySQL init process done. Ready for start up.

2019-05-19T05:40:49.617494Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic
 links using --skip-symbolic-links (or equivalent) is the default. Consider not
using this option as it' is deprecated and will be removed in a future release.
2019-05-19T05:40:49.617603Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (my
sqld 8.0.16) starting as process 1
2019-05-19T05:40:52.265763Z 0 [Warning] [MY-010068] [Server] CA certificate ca.p
em is self signed.
2019-05-19T05:40:53.604994Z 0 [Warning] [MY-011810] [Server] Insecure configurat
ion for --pid-file: Location '/var/run/mysqld' in the path is accessible to all
OS users. Consider choosing a different directory.
2019-05-19T05:40:53.648528Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: re
ady for connections. Version: '8.0.16'  socket: '/var/run/mysqld/mysqld.sock'  p
ort: 3306  MySQL Community Server - GPL.
2019-05-19T05:40:53.921685Z 0 [System] [MY-011323] [Server] X Plugin ready for c
onnections. Socket: '/var/run/mysqld/mysqlx.sock' bind-address: '::' port: 33060

ここで出力がStop。どうやら、この状態で待機し続けるらしい。
Ctrl + Cで一度抜ける。

MySQLへの接続を別プロセスで試行する。というわけでEXECコマンドを使用。

$  docker exec -it mysql_con01 mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.16 MySQL Community Server - GPL

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

無事接続できた!せっかくなので適当にSQL発行をして動作確認。

mysql> select Host,User from mysql.user;
+-----------+------------------+
| Host      | User             |
+-----------+------------------+
| %         | root             |
| localhost | mysql.infoschema |
| localhost | mysql.session    |
| localhost | mysql.sys        |
| localhost | root             |
+-----------+------------------+
5 rows in set (0.00 sec)

mysql> exit
Bye

問題なさそう。

まとめ

長々と記載したが、以下コマンドでコンテナ作っただけである。
docker run --name [コンテナ名] -e MYSQL_ROOT_PASSWORD=[パスワード] mysql

ツイッターでシェア
みんなに共有、忘れないようにメモ

view_list Docker自己学習
第1回 Dockerの学習 #1 (イメージファイルとPULLコマンド)
第2回 Dockerの学習 #2 (コンテナの起動・操作コマンド RUN、EXEC、ATTACH、STOP・KILL・START)
第3回 Dockerの学習 #3 (PS、IMAGES、COMMIT、PUSH、RM、RMI、(BUILD))
第4回 Dockerの学習 #4 (Wordpressを使おう その1:Mysqlセットアップ)
第5回 Dockerの学習 #5 (Wordpressを使おう その2:Wordpressセットアップ)

ANA

日々勉強中です ( ..)φ

Crieitは誰でも投稿できるサービスです。 是非記事の投稿をお願いします。どんな軽い内容でも投稿できます。

また、「こんな記事が読みたいけど見つからない!」という方は是非記事投稿リクエストボードへ!

有料記事を販売できるようになりました!

こじんまりと作業ログやメモ、進捗を書き残しておきたい方はボード機能をご利用ください。
ボードとは?

コメント