SPICY SPACE BLOG

愛犬家WEBエンジニアの日常。

FuelPHPのDB接続設定

FuelPHP勉強中です。
DBに接続できずハマってしまったので覚え書き。

PDOでの接続を試みました。設定ファイル(path_to_project/fuel/app/config/db.php)は以下。

return array(
    'default' => array(
        'type'        => 'pdo',
        'connection'  => array(
            'dsn'        => 'mysql:host=localhost;dbname=fuel_practice;charset=utf8;',
            'username'   => 'username',
            'password'   => 'password',
            'persistent' => false,
            'compress'       => false,
        ),
        'identifier'   => '`',
        'table_prefix' => '',
        'charset'      => 'utf8',
        'enable_cache' => true,
        'profiling'    => false,
    ),
);

するとこんなエラーが。

Fuel\Core\Database_Exception [ 2002 ]: SQLSTATE[HY000] [2002] No such file or directory

解決策は、mysql.sock(ソケットファイル)の場所をdsnに指定してあげればおk。
参考サイト:FuelPHPをはじめてみたら思った以上につまりました

- 'dsn'        => 'mysql:host=localhost;dbname=fuel_practice;charset=utf8;',
+ 'dsn'        => 'mysql:host=localhost;dbname=fuel_practice;charset=utf8;unix_socket=/var/lib/mysql/mysql.sock',

ちなみにソケットファイルの場所は下記コマンドで確認できます。

mysql_config --socket

すると今度はこんなエラー

mysqlnd cannot connect to MySQL 4.1+ using the old insecure authentication. Please use an administration tool to reset your password with the command SET PASSWORD = PASSWORD('your_existing_password'). This will store a new, and more secure, hash value in mysql.user. If this user is used in other scripts executed by PHP 5.2 or earlier you might need to remove the old-passwords flag from your my.cnf file

パスワードの保存形式が古いから、PHP5.3からはアクセスできませんよとのこと。
下記のブログ記事を参考に、my.cnf設定を変更&パスワードを再設定。
参考サイト:PHP 5.3.xからMySQLに接続できない

やっとこさこれでつながりました。。。
まだmysqliで接続できてないんですが、PDOでつながったからとりあえずはいいか。

Heroku: Up and Running

Heroku: Up and Running