【 PHP 】PHP8に入門してみた 22日目 環境構築を進める ( DBを構築する )

PHP8技術者認定初級試験 が始まるようなので 試験に向けて (できるだけ)勉強しようと思います! 使用する書籍は独習PHP 第4版(山田 祥寛)|翔泳社の本 (shoeisha.co.jp) となります。

環境構築を進める

初期データを投入する

コンテナを起動する

準備はできたので動作確認をしてみます。

以前か出ていた-p、docker-composeでは「プロジェクト名」です。

今回 VS Code Remote – Containers用PHP開発環境を構築する (zenn.dev) の記事を参考にした方法(+独自の方法)だとphp8_study_devcontainerというプロジェクト名が

付与されるようです。

また通常はup -dとするのですが、確認するだけなのでupのみとしています。

 

PS D:\dockers\files\php8_study\.devcontainer> docker-compose -p php8_study_devcontainer up mysql

実行したところエラーでした。

Creating php8_study_devcontainer_mysql_1 ... done
Attaching to php8_study_devcontainer_mysql_1
mysql_1 | 2022-09-03 20:39:33+09:00 [Note] [Entrypoint]: Entrypoint script for MariaDB Server 1:10.8.3+maria~jammy started.
mysql_1 | 2022-09-03 20:39:33+09:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
mysql_1 | 2022-09-03 20:39:33+09:00 [Note] [Entrypoint]: Entrypoint script for MariaDB Server 1:10.8.3+maria~jammy started.
mysql_1 | 2022-09-03 20:39:33+09:00 [Note] [Entrypoint]: Initializing database files
mysql_1 |
mysql_1 |
・・・
mysql_1 | 2022-09-03 20:39:42+09:00 [Note] [Entrypoint]: Securing system users (equivalent to running mysql_secure_installation)
mysql_1 | 2022-09-03 20:39:42+09:00 [Note] [Entrypoint]: Creating database docker
mysql_1 | 2022-09-03 20:39:42+09:00 [Note] [Entrypoint]: Creating user docker
mysql_1 | 2022-09-03 20:39:42+09:00 [Note] [Entrypoint]: Giving user docker access to schema docker
mysql_1 |
mysql_1 | 2022-09-03 20:39:42+09:00 [Note] [Entrypoint]: /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/01_firststep.sql
mysql_1 | ERROR 1064 (42000) at line 2: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'CHARCTER SET utf8' at line 1
php8_study_devcontainer_mysql_1 exited with code 1

01_firststep.sqlの2行目で構文を誤っているようですね🤔

・・・スペルミスのようです。

.devcontainer\docker\mysql\init_data\01_firststep.sqlを修正します。

[誤]CREATE DATABASE selfphp CHARCTER SET utf8;

[正]CREATE DATABASE selfphp CHARACTER SET utf8;

D:\dockers\files\php8_study\.devcontainer\mysqlフォルダを削除してから再度実行!

今度はうまく実行できたようです!

PS D:\dockers\files\php8_study\.devcontainer> docker-compose -p php8_study_devcontainer up mysql
Starting php8_study_devcontainer_mysql_1 ... done
Attaching to php8_study_devcontainer_mysql_1
mysql_1 | 2022-09-03 20:46:50+09:00 [Note] [Entrypoint]: Entrypoint script for MariaDB Server 1:10.8.3+maria~jammy started.
mysql_1 | 2022-09-03 20:46:50+09:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
mysql_1 | 2022-09-03 20:46:50+09:00 [Note] [Entrypoint]: Entrypoint script for MariaDB Server 1:10.8.3+maria~jammy started.
mysql_1 | 2022-09-03 20:46:50+09:00 [Note] [Entrypoint]: Initializing database files
mysql_1 |
mysql_1 |
mysql_1 | PLEASE REMEMBER TO SET A PASSWORD FOR THE MariaDB root USER !
mysql_1 | To do so, start the server, then issue the following command:
・・・
mysql_1 | 2022-09-03 20:47:01 0 [Note] mysqld (server 10.8.3-MariaDB-1:10.8.3+maria~jammy) starting as process 1 ...
mysql_1 | 2022-09-03 20:47:01 0 [Warning] Setting lower_case_table_names=2 because file system for /var/lib/mysql/ is case insensitive
mysql_1 | 2022-09-03 20:47:01 0 [Note] InnoDB: Compressed tables use zlib 1.2.11
mysql_1 | 2022-09-03 20:47:01 0 [Note] InnoDB: Number of transaction pools: 1
mysql_1 | 2022-09-03 20:47:01 0 [Note] InnoDB: Using crc32 + pclmulqdq instructions
mysql_1 | 2022-09-03 20:47:01 0 [Note] mysqld: O_TMPFILE is not supported on /tmp (disabling future attempts)
mysql_1 | 2022-09-03 20:47:01 0 [Note] InnoDB: Using liburing
mysql_1 | 2022-09-03 20:47:01 0 [Note] InnoDB: Initializing buffer pool, total size = 128.000MiB, chunk size = 2.000MiB
mysql_1 | 2022-09-03 20:47:01 0 [Note] InnoDB: Completed initialization of buffer pool
mysql_1 | 2022-09-03 20:47:01 0 [Note] InnoDB: 128 rollback segments are active.
mysql_1 | 2022-09-03 20:47:01 0 [Note] InnoDB: Setting file './ibtmp1' size to 12.000MiB. Physically writing the file full; Please wait ...
mysql_1 | 2022-09-03 20:47:01 0 [Note] InnoDB: File './ibtmp1' size is now 12.000MiB.
mysql_1 | 2022-09-03 20:47:01 0 [Note] InnoDB: log sequence number 56376; transaction id 37
mysql_1 | 2022-09-03 20:47:01 0 [Note] Plugin 'FEEDBACK' is disabled.
mysql_1 | 2022-09-03 20:47:01 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool
mysql_1 | 2022-09-03 20:47:01 0 [Warning] You need to use --log-bin to make --expire-logs-days or --binlog-expire-logs-seconds work.
mysql_1 | 2022-09-03 20:47:01 0 [Note] InnoDB: Buffer pool(s) load completed at 220903 20:47:01
mysql_1 | 2022-09-03 20:47:01 0 [Note] Server socket created on IP: '0.0.0.0'.
mysql_1 | 2022-09-03 20:47:01 0 [Note] Server socket created on IP: '::'.
mysql_1 | 2022-09-03 20:47:01 0 [Note] mysqld: ready for connections.
mysql_1 | Version: '10.8.3-MariaDB-1:10.8.3+maria~jammy' socket: '/run/mysqld/mysqld.sock' port: 3306 mariadb.org binary distribution

 

動作することが分かったので、Ctrl + Cで終了させてup -dで起動します。

次にコンテナの中に入って確認です。

docker-compose -p php8_study_devcontainer exec mysql /bin/bash
root@86c690a69d6d:/# mysql -u root -p
Enter password: # パスワードは"docker"です。
user selfphp
MariaDB [selfphp]> select host,user from mysql.user;
+-----------+-------------+
| Host      | User        |
+-----------+-------------+
| %         | docker      |
| %         | root        |
| localhost | mariadb.sys |
| localhost | root        |
| localhost | selfuser    |
+-----------+-------------+
5 rows in set (0.026 sec)

MariaDB [selfphp]> show tables;
+-------------------+
| Tables_in_selfphp |
+-------------------+
| book              |
| photo             |
| usr               |
+-------------------+
3 rows in set (0.004 sec)

MariaDB [selfphp]>\q
root@86c690a69d6d:/# exit

メモ

ばっちり!

PHPPHP

Posted by raika@blog