0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

CakePHPとMySQL(MariaDB)を連携させる方法

Posted at

対象の読者

これからCakePHPを始める、かつMySQL(またはMaria DB)でアプリを動かしたい方向けです。

接続自体大したことないんですが、なんせ同じような名前の設定ファイルがあっていろいろいじくりまわして
なかなか接続できなかった、あの日の自分にも向けた記事となっています。

検証動作環境

検証したOS
Windows11

CakePHPのバージョン
4.5.7

設定の対象ファイルは「app_local.php」です。

CakaPHPとMySQL(MariaDB)の接続情報を書き込むファイルはapp_local.phpです。

修正する箇所は、Datasourcesの配列の中の①username、②password、③database、の3か所を修正するだけです。

app_local.php
<?php
/*
 * Local configuration file to provide any overrides to your app.php configuration.
 * Copy and save this file as app_local.php and make changes as required.
 * Note: It is not recommended to commit files with credentials such as app_local.php
 * into source code version control.
 */
return [
    /*
     * Debug Level:
     *
     * Production Mode:
     * false: No error messages, errors, or warnings shown.
     *
     * Development Mode:
     * true: Errors and warnings shown.
     */
    'debug' => filter_var(env('DEBUG', true), FILTER_VALIDATE_BOOLEAN),

    /*
     * Security and encryption configuration
     *
     * - salt - A random string used in security hashing methods.
     *   The salt value is also used as the encryption key.
     *   You should treat it as extremely sensitive data.
     */
    'Security' => [
        'salt' => env('SECURITY_SALT', '4f88ac895c22d67f44a93334c5e02c4473a5e6b2c9498880848d16309a341233'),
    ],

    /*
     * Connection information used by the ORM to connect
     * to your application's datastores.
     *
     * See app.php for more configuration options.
     */
    'Datasources' => [
        'default' => [
            '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' => 'userName',
            'password' => 'password',

            'database' => 'databaseName',
            /*
             * If not using the default 'public' schema with the PostgreSQL driver
             * set it here.
             */
            //'schema' => 'myapp',

            /*
             * You can use a DSN string to set the entire configuration
             */
            'url' => env('DATABASE_URL', null),
        ],

        /*
         * The test connection is used during the test suite.
         */
        'test' => [
            'host' => 'localhost',
            //'port' => 'non_standard_port_number',
            'username' => 'userName',
            'password' => 'password',
            'database' => 'databaseName',
            //'schema' => 'myapp',
            'url' => env('DATABASE_TEST_URL', null),//'sqlite://127.0.0.1/tmp/tests.sqlite'
        ],
    ],

    /*
     * Email configuration.
     *
     * Host and credential configuration in case you are using SmtpTransport
     *
     * See app.php for more configuration options.
     */
    'EmailTransport' => [
        'default' => [
            'host' => 'localhost',
            'port' => 25,
            'username' => null,
            'password' => null,
            'client' => null,
            'url' => env('EMAIL_TRANSPORT_DEFAULT_URL', null),
        ],
    ],
];

下記のファイルは、設定の対象外です!
①app.phpや②app_local.example.phpのファイルは設定の対象外です!

以上となります。

0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?