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

CakePHP3 MySQL設定(CakePHP3チュートリアルでのデータベース作成と設定)

はじめに

CakePHP3チュートリアル実施時にMySQL関連で色々引っかかったので、備忘録的な形でまとめます。

データベース作成

MySQL起動

mysql -uroot -p

MYSQL内にデータベース作成

mysql> CREATE DATABASE cake_cms;
Query OK, 1 row affected (0.11 sec)

MySQL内のデータベース一覧表示 

mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| cake_cms           |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.41 sec)

MySQL内のデータベースの削除

mysql> DROP DATABASE cake_cms;
Query OK, 0 rows affected (0.27 sec)

存在しているデータベースへの接続

mysql> USE cake_cms;
Database changed

テーブル作成

mysql> CREATE TABLE users (
    ->     id INT AUTO_INCREMENT PRIMARY KEY,
    ->     email VARCHAR(255) NOT NULL,
    ->     password VARCHAR(255) NOT NULL,
    ->     created DATETIME,
    ->     modified DATETIME
    -> );
Query OK, 0 rows affected (0.06 sec)

データベース内のテーブル一覧表示

mysql> SHOW TABLES;
+--------------------+
| Tables_in_cake_cms |
+--------------------+
| users              |
+--------------------+
1 row in set (0.05 sec)

SELECT:テーブルからデータ取得

mysql> SELECT * FROM users;
Empty set (0.08 sec)

INSERT:テーブルにデータ追加

mysql> INSERT INTO users (email, password, created, modified)
    -> VALUES
    -> ('cakephp@example.com', 'sekret', NOW(), NOW());
Query OK, 1 row affected (0.24 sec)

mysql> SELECT * FROM users;
+----+---------------------+----------+---------------------+---------------------+
| id | email               | password | created             | modified            |
+----+---------------------+----------+---------------------+---------------------+
|  1 | cakephp@example.com | sekret   | 2019-04-11 21:55:06 | 2019-04-11 21:55:06 |
+----+---------------------+----------+---------------------+---------------------+
1 row in set (0.02 sec)

ここまでの操作が分かってればcakePHP3チュートリアルのデータベースの作成はできると思います。

データベース設定

ユーザーの認証方式変更

mysql> SELECT user, host, plugin FROM mysql.user;
+------------------+-----------+-----------------------+
| user             | host      | plugin                |
+------------------+-----------+-----------------------+
| mysql.infoschema | localhost | caching_sha2_password |
| mysql.session    | localhost | caching_sha2_password |
| mysql.sys        | localhost | caching_sha2_password |
| root             | localhost | caching_sha2_password |
+------------------+-----------+-----------------------+
5 rows in set (0.01 sec)
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'Password';
Query OK, 0 rows affected (0.09 sec)

mysql> SELECT user, host, plugin FROM mysql.user;
+------------------+-----------+-----------------------+
| user             | host      | plugin                |
+------------------+-----------+-----------------------+
| mysql.infoschema | localhost | caching_sha2_password |
| mysql.session    | localhost | caching_sha2_password |
| mysql.sys        | localhost | caching_sha2_password |
| root             | localhost | mysql_native_password |
+------------------+-----------+-----------------------+
5 rows in set (0.03 sec)

rootユーザーの認証方式がcaching_sha2_passwordからmysql_native_passwordに変更されました。

デフォルトの認証方式の変更

vi /usr/local/etc/my.cnf

default_authentication_plugin= mysql_native_passwordを追加。

my.cnf
# Default Homebrew MySQL server config
[mysqld]
# Only allow connections from localhost
bind-address = 127.0.0.1
default_authentication_plugin= mysql_native_password

app.phpのusername,password,databaseは先ほど認証方式の変更を行なったユーザーのものをそれぞれ設定してください。

app.php
    'Datasources' => [
        'default' => [
            'className' => 'Cake\Database\Connection',
            'driver' => 'Cake\Database\Driver\Mysql',
            'persistent' => false,
            'host' => 'localhost',
            //'port' => 'non_standard_port_number',
            'username' => 'root',
            'password' => 'secret',
            'database' => 'cake_cms',
            'encoding' => 'utf8mb4',
            'timezone' => 'UTC',
            'flags' => [],
            'cacheMetadata' => true,
            'log' => false,
            'quoteIdentifiers' => false,
            //'init' => ['SET GLOBAL innodb_stats_on_metadata = 0'],

            'url' => env('DATABASE_URL', null),
        ],

これで起動し直せば、Databaseの項目部分がCakePHP is able to connect to the database.になり、無事設定は完了になります。

最後に

平日の仕事終わりにやってましたが設定だけで2日かかりましたね・・・
Railsチュートリアルや環境構築など色々調べながらやる方が多いと思うので備忘録としてQiitaにまとめながら作業を進めるのも後で振り返りもできてオススメなのでQiitaに何書こうか悩む人はぜひやってみてはいかがでしょうか?

参考

【MySQL, SQL】データベースを扱う基本SQL一覧

CakePHP is NOT able to connect to the database. を解決する

phpからMySQL 8.0へPDOで接続時「SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client」

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
ユーザーは見つかりませんでした