CakePHP: Can't access MySQL database

倖福魔咒の 提交于 2019-12-05 03:33:47

If it is the socket, just edit /etc/php.ini to reflect the following

pdo_mysql.default_socket=/tmp/mysql.sock

and

mysql.default_socket = /tmp/mysql.sock

What usually bites me in it's that MySQL thinks of 'localhost' as 'connect thru the unix socket' and '127.0.0.1' 'connect thru TCP port'. With things like XAMPP (at least on mac) the unix socket file isn't there. Just use 127.0.0.1 instead.

var $default = array(
    'driver' => 'mysql',
    'persistent' => false,
    'host' => '127.0.0.1',
    'login' => 'cake_blog_user',
    'password' => 'cake_blog_password',
    'database' => 'cake_blog',
    'prefix' => '',
);

Should work all the time.

I believe you can also do the following

<?php
    public $default = array(
        'driver' => 'mysql',
        'persistent' => false,
        'host' => 'localhost',
        'login' => 'cake_blog_user',
        'password' => 'cake_blog_password',
        'database' => 'cake_blog',
        'prefix' => '',
        'port' => '/tmp/mysql.sock',            
    )
?>

doing this might mean you need to edit the database.php file when you go live on the production server.

Thanks everyone for pointing me in the right direction. The mysql.sock file has been moved to /tmp/mysql.sock instead of its default location at /var/mysql/mysql.sock. Editing the php.ini file to reflect this has fixed the problem.

check your phpinfo and use the socket listed. that worked for me.

Stefan Bubonja

On Ubuntu, if you installed both 7.0 and 5.6 versions of PHP this wont work.

Youll need to switch if you have both versions:

Look first if is 7.0 version: the command is php -v.

Next do

sudo a2dismod php7.0
sudo a2enmod php5.6
sudo service apache2 restart
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!