LoginSignup
2
0

More than 3 years have passed since last update.

EC2上のCakephp4でRDSを使う

Posted at

前提

EC2(Amazon Linux 2 AMI)にCakePHP4をインストールしてあることを前提としています。
今回、PHPのversionは7.4とします。
基本的に前回の投稿の続きです。

RDSの立ち上げ

RDSの立ち上げは公式サイトにもありますのでここでは省略します。
初心者の方はこちらがおすすめです。

MySQLとPHP拡張モジュールのインストール

MySQLとそれに繋ぐためのPHP拡張モジュールをインストールします。

$ sudo yum install -y mysql
$ sudo yum install -y mysql57-server php74-mysqlnd

以下のコマンドで先ほど立ち上げたRDSに接続できると思います。

$ mysql -h db-resume.crcdiqvbtctf.ap-northeast-1.rds.amazonaws.com -P 3306 -u admin -p
Enter password: (パスワードを入力)
Server version: 8.0.20 Source distribution

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

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

MySQL [(none)]> 

Cakephp接続設定

git管理下にあるconfig/app.phpにデータベースのパスワードやらをのせるのはまずいと思いますので、
接続情報は app.php ではなく、デフォルトでgitignoreされている app_local.php に書いておくのが無難だと思います。

<?php
return [
    // 他の設定
    'Datasources' => [
        'default' => [
            'className' => 'Cake\Database\Connection',
            'driver' => 'Cake\Database\Driver\Mysql',
            'persistent' => false,
            'host' => 'testdb.abcdefghijkl.ap-northeast-1.rds.amazonaws.com',
            'username' => 'cakephp',
            'password' => 'secret',
            'database' => 'testdb',
            'encoding' => 'utf8mb4',
            'timezone' => 'Asia/Tokyo',
            'cacheMetadata' => true,
        ],
    ],
    // 他の設定
];

port番号を設定しなくても自動で探してくれるらしいです。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0