tags:

views:

3526

answers:

12

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?

+6  A: 

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.

Then, if that works, I'll start digging into Cake and its configuration because it really should be able to connect to a local socket. Maybe (just a guess) removing the leading : in the host parameter.

Vinko Vrsalovic
A: 

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.

A: 

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.

timkl
+1  A: 

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.

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

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'
        );
}
A: 
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.

jimiyash
A: 

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

+4  A: 

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' => '',

);
This was the real answer. Thank you Julien.
Frederico
A: 

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.

Ebbot
A: 

Abba Bryant and the others who posted that...THANK YOU!!! THANK YOU SO MUCH! I've been the entire day trying to fix that problem unsuccessfully. Finally it works! :D

Mr. Potato
A: 

Thans all for your help. I was having the same problem.

'port' => '/Applications/MAMP/tmp/mysql/mysql.sock', // here is the key !

Thx!

alberto