8

I'm new to CakePHP and am just running through the configuration process, but am stumped why Cake can't access my MySQL database. The Cake info page says my tmp directory is writable, the FileEngine is being used for caching (don't know what this means), and my database configuration file is present, but that CakePHP cannot connect to the database.

Here are my setup details:

  • PHP 5.3 (pre-installed on Snow Leopard)
  • MySQL 5.1.40 64-bit
  • CakePHP 1.2.4.8284

Here are the steps I went through:

  • Created a MySQL schema called cake_blog
  • Created a MySQL user called cake_blog_user
  • Granted cake_blog_user the appropriate permissions on cake_blog@localhost and cake_blog@%
  • Copied the database.php.default file to database.php and edited the database connection details as appropriate

Here is the relevant configuration data from database.php:

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

Am I missing something here? I should also mention that if I insert an echo mysql_error(); into the /cake/libs/view/pages/home.ctp file right before it tests the database connection, the error displayed is "No such file or directory." I have no idea what file or directory it's talking about.

Thanks!

6
  • What does your database setup look like, i.e. what parameters would you usually use to connect to it? Post the details of the database.php file. Commented Oct 28, 2009 at 6:25
  • 2
    I'll go out on a limb and guess that mysql.sock is not in the standard location anymore Commented Oct 28, 2009 at 6:28
  • Not exactly a solution to this specific issue, but I've had great success doing cake development on a Mac using MAMP (self-contained LAMP-like stack). Perhaps take a look at that. Commented Oct 28, 2009 at 6:52
  • Thanks nduplessis! In Snow Leopard mysql.sock has been moved to /tmp/mysql.sock instead of the default location /var/mysql/mysql.sock. Commented Oct 28, 2009 at 6:59
  • 1
    Oh that's right yes. I added a symlink instead of updating my php.ini Commented Oct 28, 2009 at 7:02

5 Answers 5

9

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
Sign up to request clarification or add additional context in comments.

Comments

9

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.

Comments

6

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.

Comments

1

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.

1 Comment

As nduplessis notes above, you can also solve this problem by creating a symlink like this: sudo ln -s /tmp/mysql.sock /var/mysql/mysql.sock (make sure you sudo mkdir /var/mysql/ first!) This way you don't have to worry about editing the php.ini file.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.