13

I have had a cakephp app running fine on my local machine (mac osx) for a while and then suddently I realise that I can't connect to mysql.sock.

I'm getting this error:

Warning (2): mysql_connect() [http://php.net/function.mysql-connect]: [2002] No such file or directory (trying to connect via unix:///var/mysql/mysql.sock) [CORE/cake/libs/model/datasources/dbo/dbo_mysql.php, line 540]

The line 540 of dbo_mysql.php reads:

$this->connection = mysql_connect($config['host'] . ':' . $config['port'], $config['login'], $config['password'], true);

I've checked, there is no fle //var/mysql/mysql.sock. It's actually in /tmp/mysql.sock

I tried changing my php.ini.default to match the above but it's already set to look in /tmp/ for local connections. Why, and where is the error coming from?

Has anyone come across a similar error?

Thanks,

Jonesy

iamjonesy
  • 23,004
  • 39
  • 130
  • 203
  • 7
    Try `127.0.0.1` instead of `localhost` – Pekka Oct 19 '10 at 11:58
  • Where about? accessing the app? in the php.ini file? – iamjonesy Oct 19 '10 at 12:06
  • 6
    `$config['host'] = '127.0.0.1'`. mysql defaults to use local unix domain sockets if you use `localhost`. Switching to the IP forces it to use TCP sockets instead. – Marc B Oct 19 '10 at 12:17
  • Hi that worked! It's a bit annoying isn't it! now when I upload the project to the webserver, i'll have to change that piece of code :( thanks! – iamjonesy Oct 19 '10 at 12:33
  • ca you submit your comment as an answer so I can check it :D – iamjonesy Oct 19 '10 at 14:36
  • The correct way to do this is to point the 'port' key in the db config or the 'socket' key in the db config (either works) to the mysql.sock file you want to use. -- A vetted, tested example is below. Please give the nod where recognition is due. – Abba Bryant Jun 29 '11 at 23:15
  • Using 127.0.0.1 instead of localhost worked for me. – Arunabh Das Jul 21 '12 at 22:51
  • Related/dupe: [Warning: mysql_connect\(\): \[2002\] No such file or directory \(trying to connect via unix:///tmp/mysql.sock\) in](http://stackoverflow.com/q/4219970) – blahdiblah Oct 28 '13 at 18:55

3 Answers3

12

Try passing an absolute path the the mysql.sock file in the APP/config/database.php

<?php
    class DATABASE_CONFIG {
        var $default = array(
            'driver' => 'mysql',
            'persistent' => false,
            'host' => 'localhost',
            'login' => 'dbUser',
            'password' => 'dbPassword',
            'database' => 'dbName',
            'prefix' => '',
            'port' => '/path/to/mysql.sock'
        );
    }

This is better than running via an ip for local connection as the socket connection is much, much faster.

Abba Bryant
  • 4,002
  • 20
  • 18
12

If you are having problem with CakePHP 2.0, try this:

sudo mkdir /var/mysql
sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock /var/mysql/mysql.sock
Milos Matic
  • 1,223
  • 11
  • 10
  • Works for CakePHP 1.3 also, and for Xampp with `sudo ln -s /Applications/XAMPP/xamppfiles/var/mysql/mysql.sock /var/mysql/mysql.sock` – damusnet Aug 05 '13 at 13:28
6

In phpcake 2.0 use 'unix_socket' instead of port

<?php
    class DATABASE_CONFIG {
        var $default = array(
            'datasource' => 'Database/Mysql',
            'persistent' => false,
            'host' => 'localhost',
            'login' => 'dbUser',
            'password' => 'dbPassword',
            'database' => 'dbName',
            'prefix' => '',
            'unix_socket' => '/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock', //Path for mac XAMPP
        );
    }
Kent Widman
  • 71
  • 1
  • 3