[CakePHP3]2つのデータベースを設定し同時に使用する

CakePHP3のデータベース設定についての備忘録です。(2019年12月8日時点の情報です)

Webサーバーを動かして、CakePHP3経由でワードプレスなど他のデータベースを稼働させる場合のデータベース設定方法です。Google検索で方法を探し参考にさせていただいた結果は下記の通りです。

【CakePHPのバージョン】
CakePHP 3.7.5

【設定ファイルの場所】
CakePHPフォルダ/config/app.php

【1つ目のデータベース】
下記スクリプトの’username’ => ‘db_user1’の部分が1つ目のデータベースの箇所です。

【2つ目のデータベース】
下記スクリプトの’wp_db1’ の部分が2つ目のデータベースの箇所です。筆者はここでWordPress用のDBを設定しました。

/**
* Connection information used by the ORM to connect
* to your application's datastores.
*
* ### Notes
* - Drivers include Mysql Postgres Sqlite Sqlserver
* See vendor\cakephp\cakephp\src\Database\Driver for complete list
* - Do not use periods in database name - it may lead to error.
* See https://github.com/cakephp/cakephp/issues/6471 for details.
* - 'encoding' is recommended to be set to full UTF-8 4-Byte support.
* E.g set it to 'utf8mb4' in MariaDB and MySQL and 'utf8' for any
* other RDBMS.
*/
'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' => 'db_user1',
'password' => 'your_password',
'database' => 'db_name1',
/*
* You do not need to set this flag to use full utf-8 encoding (internal default since CakePHP 3.6).
*/
//'encoding' => 'utf8mb4',
'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),
],
'wp_db1' => [ /*Remote Database 1*/
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => 'localhost', /*YOUR_REMOTE_SERVER_IP*/
'port' => '3306',
'username' => 'db-user2',
'password' => 'your-password',
'database' => 'db_name2',
'encoding' => 'utf8mb4',
'timezone' => 'UTC',
'flags' => [],
'cacheMetadata' => false,
'log' => false,
'quoteIdentifiers' => false,
'url' => env('DATABASE_URL', null),
],

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” 

【和訳】
「CakePHPは選択されたドライバーに基づいたデフォルトのdbポートを使用します。MAMP上のMySQLの場合ポート8889を使用しますので、MAMPユーザーは下記のライン「’port’ => ‘non_standard_port_number’」をアンコメントし、ポートの設定をしてください。
※MAMPでCakePHPを試したことはありません。

* During development, if using MySQL < 5.6, uncommenting the
* following line could boost the speed at which schema metadata is
* fetched from the database.
—-途中省略—-
//’init’ => [‘SET GLOBAL innodb_stats_on_metadata = 0’],

【和訳】
開発中、MySQL 5.6未満を使用する場合、下記ラインをアンコメントすることで、スキーマメタデータがデータベースから取得されるスピードを速めることができるでしょう。

//’init’ => [‘SET GLOBAL innodb_stats_on_metadata = 0’],

関連記事

  1. [node.js]Lambda Node.jsランタイムのアップグレー…

  2. [NATIVE INSTURUMENTS]macOS 11(Big S…

  3. [CakePHP]CakePHP3インストール:PHP7.1

  4. [Google Search Console]ページにリダイレクトがあ…

  5. [Googleランキング]検索順位についての検証作業

  6. [AWS]Elastic Beanstalk、使ってみました

  7. [CakePHP]bakeでファイルを生成

  8. [Amazon Polly]SSMLタグで発音記号を使用する

最近の記事

PAGE TOP