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

3

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

Guardianship answered 19/10, 2010 at 11:53 Comment(8)
Try 127.0.0.1 instead of localhostGamelan
Where about? accessing the app? in the php.ini file?Guardianship
$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.Designate
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!Guardianship
ca you submit your comment as an answer so I can check it :DGuardianship
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.Titular
Using 127.0.0.1 instead of localhost worked for me.Blight
Related/dupe: Warning: mysql_connect(): [2002] No such file or directory (trying to connect via unix:///tmp/mysql.sock) inBickford
T
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.

Titular answered 19/10, 2010 at 18:15 Comment(2)
This works in CakePHP up to version 1.3. For version 2.0 and above, see Kent Widman's answer below.Garvin
Makes sense considering the answer is almost 2 years old at this point.Titular
V
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
Vann answered 19/1, 2012 at 13:12 Comment(1)
Works for CakePHP 1.3 also, and for Xampp with sudo ln -s /Applications/XAMPP/xamppfiles/var/mysql/mysql.sock /var/mysql/mysql.sockLutenist
K
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
        );
    }
Kiarakibble answered 2/11, 2011 at 17:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.