CakePHP: No such file or directory (trying to connect via unix:///var/mysql/mysql.sock)

14,449

Solution 1

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.

Solution 2

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

Solution 3

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
        );
    }
Share:
14,449

Related videos on Youtube

iamjonesy
Author by

iamjonesy

Lead dev at Appointedd. Based in Edinburgh, Scotland. A few of my sites: http://findr.fm http://defaqto.io http://masquerade.eu01.aws.af.cm

Updated on January 20, 2020

Comments

  • iamjonesy
    iamjonesy about 4 years

    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

    • Pekka
      Pekka over 13 years
      Try 127.0.0.1 instead of localhost
    • Marc B
      Marc B over 13 years
      $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.
  • Brad Koch
    Brad Koch almost 12 years
    This works in CakePHP up to version 1.3. For version 2.0 and above, see Kent Widman's answer below.
  • Abba Bryant
    Abba Bryant almost 12 years
    Makes sense considering the answer is almost 2 years old at this point.
  • damusnet
    damusnet over 10 years
    Works for CakePHP 1.3 also, and for Xampp with sudo ln -s /Applications/XAMPP/xamppfiles/var/mysql/mysql.sock /var/mysql/mysql.sock

Related