3
votes

I am currently reading "Beginning CakePHP:From Novice to Professional" by David Golding. At one point I have to use the CLI-command "cake bake", I get the welcome-screen but when I try to bake e.g. a Controller I get the following error messages:

Warning: mysql_connect(): Can't connect to local MySQL server through socket '/var/mysql/mysql.sock' (2) in /Applications/MAMP/htdocs/blog/cake/libs/model/datasources/dbo/dbo_mysql.php on line 117

Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in /Applications/MAMP/htdocs/blog/cake/libs/model/datasources/dbo/dbo_mysql.php on line 122

Warning: mysql_get_server_info(): supplied argument is not a valid MySQL-Link resource in /Applications/MAMP/htdocs/blog/cake/libs/model/datasources/dbo/dbo_mysql.php on line 130

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /Applications/MAMP/htdocs/blog/cake/libs/model/datasources/dbo/dbo_mysql.php on line 154
Error: Your database does not have any tables.

I suspect that the error-messages has to do with php trying to access the wrong mysql-socket, namely the default osx mysql-socket - instead of the one that MAMP uses. Hence I change my database configurations to connect to the UNIX mysql-socket (:/Applications/MAMP/tmp/mysql/mysql.sock):

class DATABASE_CONFIG {

    var $default = array(
        'driver' => 'mysql',
        'connect' => 'mysql_connect',
        'persistent' => false,
        'host' =>':/Applications/MAMP/tmp/mysql/mysql.sock', // UNIX MySQL-socket
        'login' => 'my_user',
        'password' => 'my_pass',
        'database' => 'blog',
        'prefix' => '',
    );

}

But I get the same error-messages with the new socket:

Warning: mysql_connect(): Can't connect to local MySQL server through socket '/Applications/MAMP/tmp/mysql/mysql.sock:3306' (2) in /Applications/MAMP/htdocs/blog/cake/libs/model/datasources/dbo/dbo_mysql.php on line 117

Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in /Applications/MAMP/htdocs/blog/cake/libs/model/datasources/dbo/dbo_mysql.php on line 122

Warning: mysql_get_server_info(): supplied argument is not a valid MySQL-Link resource in /Applications/MAMP/htdocs/blog/cake/libs/model/datasources/dbo/dbo_mysql.php on line 130

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /Applications/MAMP/htdocs/blog/cake/libs/model/datasources/dbo/dbo_mysql.php on line 154
Error: Your database does not have any tables.

Also, even though I use the UNIX-socket that MAMP show on it's welcome-screen, CakePHP loses the database-connection, when using this socket instead of localhost.

Any ideas on how I can get bake to work?

-- Edit 1 --

Thank you guys for helping me out! :)

I have a problem figuring out where in my.cnf to edit to get MySQL to listen to TCP/IP request. The only paragraph I can find where TCP/IP is mentioned is the following:

# Don't listen on a TCP/IP port at all. This can be a security enhancement,
# if all processes that need to connect to mysqld run on the same host.
# All interaction with mysqld must be made via Unix sockets or named pipes.
# Note that using this option without enabling named pipes on Windows
# (via the "enable-named-pipe" option) will render mysqld useless!
# 
#skip-networking

That allows me to turn off TCP/IP completely, which is the opposite of my intention. I don't know how to go about what you suggest, if you could be more elaborate it would be great. I am a total n00b on these matters :S

Reg. connecting to a local socket: I removed the leading colon in the host-parameter, same result.

11

11 Answers

13
votes

From the error, it looks like it's trying to connect to an actual IP address and not a UNIX socket, look:

 '/Applications/MAMP/tmp/mysql/mysql.sock:3306'

It's appending a port to the socket, which is wrong.

So, I'd first try to configure MySQL to listen to TCP/IP requests (edit the proper section in my.cnf) and try providing 127.0.0.1 instead of the socket.

In case you won't scroll down:

To fix it at CakePHP level, change host on database.php to 'localhost' and add a port directive with its value set to the socket name '/Applications/MAMP/tmp/mysql/mysql.sock'

18
votes

I find the solution to this problem : Add a socket config in the cakephp app/config/database.php file

class DATABASE_CONFIG {

var $default = array(
    'driver' => 'mysql',
    'persistent' => false,
    'host' => 'localhost',
    'port' => '/Applications/MAMP/tmp/mysql/mysql.sock', // here is the key !
    'login' => 'you',
    'password' => 'yourpass',
    'database' => 'yourdb',
    'prefix' => '',

);
6
votes

For anyone running into this problem when using CakePHP 2.0: for me the above database configuration files didn't do the trick. Found the 'unix_socket' property though, that worked for me:

<?php
class DATABASE_CONFIG {

    public $default = array(
        'datasource' => 'Database/Mysql',
        'driver' => 'mysql',
        'persistent' => false,
        'host' => 'localhost',
        'unix_socket' => '/tmp/mysql.sock',
        'login' => 'xxx',
        'password' => 'xxx',
        'database' => 'xxx',
        'encoding' => 'UTF8',
        'prefix' => ''
    );

}
1
votes

I had the same problem, when using MAMP and the Cake CLI. I'm running CakePHP 1.1xxx and MAMP 1.7.

The problem is, that the MySQL socket can't be found :D

Open Terminal and enter the following:

my-macbook:~ chris$ php -i | grep mysql.default_socket
mysql.default_socket => no value => no value
my-macbook:~ chris$ php -i -c /Applications/MAMP/conf/php5 | grep mysql.default_socket
mysql.default_socket => /Applications/MAMP/tmp/mysql/mysql.sock => /Applications/MAMP/tmp/mysql/mysql.sock

The catch is that without explicitly giving the php binary the path to its (read MAMP's) config file, the mysql.default_socket is not set.

Using that I did not need to change my database configuration whatsoever.

1
votes
sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock /tmp/mysql.sock

I had this exact same problem with mamp, and fixed it with the above command. I think you have to run this command every time you restart your computer. Maybe there is a better way to do it, but I use this with clix.app, so it is usually pretty fast. Also, change your host to localhost.

1
votes

For me, I forgot to set the port on the host, since I was not using the default MySQL port in MAMP.

i.e. If your MySQL port is 8889, set host to localhost:8889.

0
votes

Yeah had a similar issue, I think pointing to the the socket:portno as Vinko suggested might work - however if you use an ip address / localhost you'll be fine.

0
votes
    class DATABASE_CONFIG
{
    public $default  =   array(
        'driver'        =>  'mysql',
        'persistent'    =>  false,
        'host'          =>  'localhost',
        'login'         =>  'account',
        'password'      =>  'password',
        'database'      =>  'database',
        'prefix'        =>  '',
        'port'          =>  '/var/mysql/mysql.sock'
    );
}
0
votes

This worked for me:

class DATABASE_CONFIG
{
        public $default  =       array(
                'driver'                =>      'mysql',
                'persistent'    =>      false,
                'host'                  =>      'localhost',
                'login'                 =>      'account',
                'password'              =>      'password',
                'database'              =>      'database',
                'prefix'                =>      '',
                'port'                  =>      '/Applications/MAMP/tmp/mysql/mysql.sock'
        );
}
0
votes

Try configuring your firewall... That was the case for me!

0
votes

You can also make a symbolic link, bake is looking for mysql.sock in /tmp

like this:

ln -s /Applications/MAMP/tmp/mysql/mysql.sock /tmp/mysql.sock

Cheers, Ebbot.