佐藤洋介のログ

だいたいすぐに忘れるからここに書き残す

Mac OS X El CapitanにMAMPとCakePHP3.2をインストールしてデータベースに接続するまでを行う

 1. 環境

 

 

2. MAMPをインストールする

Mac標準のPHP, MySQLは必要なプラグイン等の準備が面倒な為、MAMPを使用する。

Freeとついている方をダウンロードする。

www.mamp.info

 

インストール後、アプリケーションを実行しサーバーの起動ボタンを押す。

この際、設定からWebサーバのドキュメントルートの場所を確認しておく。

/Applications/MAMP/htdocs

ここにCakePHP3.2をインストールします。

 

インストール後は、ブラウザから確認ができます。

localhost:8888/MAMP

デフォルトだとポートがApache:8888, Nginx:7888, MySQL:8889になっています。

変更する際には設定からポートにて行う。今回はこのまま進めます。

 

3. CakePHP3.2をインストールする

先ほどのドキュメントルートまで移動する。

$ cd /Applications/MAMP/htdocs

 

Composerを使用してCakePHP3.2をインストールする。

$ composer create-project --prefer-dist cakephp/app test_cake

Composerのインストールについては以前の記事にて。

satouyousuke.hatenablog.com

 

ブラウザで確認する。

localhost:8888/test_cake

f:id:satouyousuke:20160808155712p:plain

上記ページが表示されていればOKです。

何の支障もなく表示されると思います。私はポート番号のつけ忘れで表示されなくて焦ったりしましたが。

 

4. CakePHPからMySQLへの接続を行う

CakePHPのインストール直後にWebページを確認するとデータベースの部分に赤バツマークが表示されている。

Database

CakePHP is NOT able to connect to the database.

Connection to database could not be established: SQLSTATE[HY000] [1045] Access denied for user 'my_app'@'localhost' (using password: YES)

f:id:satouyousuke:20160808163359p:plain

 

まず、MAMPでユーザーとデータベースを作成する。

ブラウザからlocalhost:8888/MAMPのページを表示する。

 

画面上の以下のリンクからphpMyAdminページを表示する。

MySQL
MySQLデータベースは phpMyAdmin を使って管理できます。

 

英語表示の場合にはURLアドレスのパラメーター、language=Englishをlanguage=Japaneseにすると日本語になります。

localhost:8888/MAMP/index.php?page=phpmyadmin&language=Japanese

 

phpMyAdminのユーザータブからCakePHPが使用するユーザーを作成します。

f:id:satouyousuke:20160808161659p:plain

ユーザーを追加する、を選択します。

 

f:id:satouyousuke:20160808162040p:plain

User name : test_cake

Host : localhost

パスワード : test_pass

Re-type : test_pass

以上を入力して、「同名のデータベースを作成して全ての特権を与える。」にチェックを入れます。

※Hostではコンボボックスからローカルを選択すると自動的にlocalhostと右側に表示されます。

※パスワードは適当です。ちゃんとしたやつをつけてください。

※Re-typeは入力確認です。

※パスワードを生成する、は行いません。

※特権は適当です。適宜変更してください。

 

実行ボタンを選択してユーザーとデータベースを作成する。

 

今度はCakePHP側のデータベース接続設定を変更します。

/Applications/MAMP/test_cake/config/app.phpをエディタで開きます。

以下、データベース接続設定部分の赤字部分にphpMyAdminで作成したユーザーとデータベースの名前とパスを入力します。

 

'Datasources' => [
'default' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => 'localhost',
/**
* CakePHP will use the default DB port based on the driver selected
* MySQL on MAMP uses port 8889, MAMP users will want to uncomment
* the following line and set the port accordingly
*/
//'port' => 'non_standard_port_number',
'username' => 'test_cake',
'password' => 'test_pass',
'database' => 'test_cake',
'encoding' => 'utf8',
'timezone' => 'UTC',
'flags' => [],
'cacheMetadata' => true,
'log' => false,

/**
* Set identifier quoting to true if you are using reserved words or
* special characters in your table or column names. Enabling this
* setting will result in queries built using the Query Builder having
* identifiers quoted when creating SQL. It should be noted that this
* decreases performance because each query needs to be traversed and
* manipulated before being executed.
*/
'quoteIdentifiers' => false,

/**
* During development, if using MySQL < 5.6, uncommenting the
* following line could boost the speed at which schema metadata is
* fetched from the database. It can also be set directly with the
* mysql configuration directive 'innodb_stats_on_metadata = 0'
* which is the recommended value in production environments
*/
//'init' => ['SET GLOBAL innodb_stats_on_metadata = 0'],

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

 

これで設定は終了です。

ブラウザでCakePHPのページを確認してみます。

localhost:8888/test_cake/

 

データベース部分の赤バツマークが消えていればOKです。

Database

CakePHP is able to connect to the database.

 

f:id:satouyousuke:20160808163247p:plain

 

 

 

以下のサイト様を参考にさせていただきました。ありがとうございます。

qiita.com

 

CakePHP公式のCakePHP3.xクイックスタートガイド

クイックスタートガイド