PHP:プロになるためのPHPプログラミング入門 サンプル環境構築 5

公開:2026.05.22(金) 14:01

プロになるためのPHPプログラミング入門 サンプル環境構築

前回記事「PHP:プロになるためのPHPプログラミング入門 サンプル環境構築 4」の続き。

以下書籍に付属するサンプルの実行環境を構築する。
プロになるためのPHPプログラミング入門」(ISBN 978-4-7741-4972-1)

環境は以下記事のものを使用
PHP:プロになるためのPHPプログラミング入門 サンプル環境構築

サーバのバージョン: 8.0.46 - MySQL Community Server - GPL

A.3 MySQLの設定 / A.3.2 郵便番号データテーブル(zipcodes)の作成 (P.321)

1. ppadminユーザーでMySQLに接続
MySQLコンテナのコンソールで ppdbデータベース ppadminユーザーでMySQLに接続する。
mysql -u ppadmin -p ppdb

実行結果:
bash-5.1# mysql -u ppadmin -p ppdb
Enter password:
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 288
Server version: 8.0.46 MySQL Community Server - GPL

Copyright (c) 2000, 2026, Oracle and/or its affiliates.

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.

mysql>

2. zipcodesテーブルの作成
MySQLに ppdbデータベース、ppadminユーザーでに接続し サンプル prophp.sql の 41~48行目 の zipcodes テーブル作成SQLを実行する。
CREATE TABLE zipcodes (
jiscode  VARCHAR(8),
zipcode  VARCHAR(8),
pref     VARCHAR(128),
city     VARCHAR(128),
town     VARCHAR(128),
townkana VARCHAR(128)
);

実行結果:
mysql> CREATE TABLE zipcodes (
town     VARCHAR(128),
townkana VARCHAR(128)
);    -> jiscode  VARCHAR(8),
    -> zipcode  VARCHAR(8),
    -> pref     VARCHAR(128),
    -> city     VARCHAR(128),
    -> town     VARCHAR(128),
    -> townkana VARCHAR(128)
    -> );
Query OK, 0 rows affected (0.05 sec)

3. 郵便番号データのロード
3-1. KEN_ALL.ZIPのダウンロード
郵便番号データを郵便局のホームページからダウンロードするが URL が書籍記載のものから変更になっている。
正しいURL:https://www.post.japanpost.jp/service/search/zipcode/download/

上記から「住所の郵便番号(CSV形式)」→ 「読み仮名データの促音・拗音を小書きで表記しないもの」または「読み仮名データの促音・拗音を小書きで表記するもの」のリンクより "全国一括" をクリック

ここから KEN_ALL.ZIP をダウンロードし解凍、

このファイルをテキストエディタで開き UTF-8(BOMなし) 改行コード LF で保存しなおす。

サクラエディタの場合は KEN_ALL.CSV を開いたあと「名前を付けて保存」より
・文字コードセットを "UTF-8" (BOMのチェックは外す)
・改行コード LF(UNIX)
を指定し[保存]

3-2. コンテナへのコピー
編集した KEN_ALL.CSV を MySQLコンテナにコピーする。
KEN_ALL.CSV は Windowsの D:\temp\KEN_ALL.CSV として保存し以下のコマンドでコンテナへコピーする。
xxxxxxxx@yyyy:~/docker/php$ docker cp /mnt/d/temp/KEN_ALL.CSV d094e1650051:~
Successfully copied 18.9MB (transferred 18.9MB) to d094e1650051:/

3.3. CSVのロード
MySQLコンテナで ppdbデータベース ppadminユーザーで接続しCSVをロードする。

実行結果:エラー
bash-5.1# mysql -u ppadmin -p ppdb
Enter password:
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 288
Server version: 8.0.46 MySQL Community Server - GPL

Copyright (c) 2000, 2026, Oracle and/or its affiliates.

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.

mysql> LOAD DATA LOCAL INFILE "/KEN_ALL.CSV" INTO TABLE zipcodes
    -> FIELDS TERMINATED BY ','
    -> ENCLOSED BY '"'
    -> LINES TERMINATED BY '\n'
    -> (jiscode, @dmy, zipcode, @dmy, @dmy, townkana, pref, city, town,
;    -> @dmy, @dmy, @dmy, @dmy, @dmy, @dmy);
ERROR 3948 (42000): Loading local data is disabled; this must be enabled on both the client and server sides

原因:セキュリティ上の理由から、ローカルファイルの読み込み機能(LOCAL_INFILE)が無効化されているため

◆ 対応:ローカルの読み込み機能を有効化する
3.3.1. サーバ側の設定確認
現在のlocal_infile設定確認:→ "OFF" なので無効になっている
mysql> show variables like 'local_infile'
    -> ;
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| local_infile  | OFF   |
+---------------+-------+
1 row in set (0.05 sec)

3.3.2. 設定を有効にする
MySQLにrootでログインする
bash-5.1# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 289
Server version: 8.0.46 MySQL Community Server - GPL

Copyright (c) 2000, 2026, Oracle and/or its affiliates.

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.

以下コマンドを実行し local_infile を有効にする。
mysql> SET GLOBAL local_infile = 1;
Query OK, 0 rows affected (0.00 sec)

設定変更されたことを確認
mysql> show variables like 'local_infile'
    -> ;
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| local_infile  | ON    |
+---------------+-------+
1 row in set (0.03 sec)

3.3.3. 郵便番号データのロード
MySQLにppadminユーザでログインする。その際 "--local-infile=1" オプションを付けること。

実行結果:
bash-5.1# mysql --local-infile=1 -u ppadmin -p ppdb
Enter password:
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 291
Server version: 8.0.46 MySQL Community Server - GPL

Copyright (c) 2000, 2026, Oracle and/or its affiliates.

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.

prophp.sql の 50~55行目のSQLを実行する。
CSVのパスは /home/ppuser/KEN_ALL.CSV となっているが、今回は ROOTにコピーしたので /KEN_ALL.CSV 修正。

実行結果:OK
mysql> LOAD DATA LOCAL INFILE "/KEN_ALL.CSV" INTO TABLE zipcodes
    -> FIELDS TERMINATED BY ','
    -> ENCLOSED BY '"'
    -> LINES TERMINATED BY '\n'
    -> (jiscode, @dmy, zipcode, @dmy, @dmy, townkana, pref, city, town,
    -> @dmy, @dmy, @dmy, @dmy, @dmy, @dmy);
Query OK, 124822 rows affected (1.19 sec)
Records: 124822  Deleted: 0  Skipped: 0  Warnings: 0


0 件のコメント:

その他の記事