Cakephp console (bake) on Mac OS with Mamp : database connection error
I am trying to do the Simple Acl controlled Application tutorial in the cakephp cookbook. The idea is :
Making Databases
CREATE TABLE users (
id INT(11) NOT NULL AUTO_INCREME开发者_运维技巧NT PRIMARY KEY, username VARCHAR(255) NOT NULL UNIQUE,
password CHAR(40) NOT NULL,
group_id INT(11) NOT NULL,
created DATETIME,
modified DATETIME
);
CREATE TABLE groups (
id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
created DATETIME,
modified DATETIME
);
CREATE TABLE posts (
id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
user_id INT(11) NOT NULL,
title VARCHAR(255) NOT NULL,
body TEXT,
created DATETIME,
modified DATETIME
);
CREATE TABLE widgets (
id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
part_no VARCHAR(12),
quantity INT(11)
);
Then run the cake bake all command, this is when i have the problem :
Welcome to CakePHP v1.2.4.8284 Console
---------------------------------------------------------------
App : app
Path: /Applications/MAMP/htdocs/luis/app
---------------------------------------------------------------
---------------------------------------------------------------
Bake All
---------------------------------------------------------------
Warning: mysql_connect(): Can't connect to local MySQL server through socket '/var/mysql/mysql.sock' (2) in /Applications/MAMP/htdocs/luis/cake/libs/model/datasources/dbo/dbo_mysql.php on line 374
Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in /Applications/MAMP/htdocs/luis/cake/libs/model/datasources/dbo/dbo_mysql.php on line 379
Warning: mysql_get_server_info(): supplied argument is not a valid MySQL-Link resource in /Applications/MAMP/htdocs/luis/cake/libs/model/datasources/dbo/dbo_mysql.php on line 387
Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /Applications/MAMP/htdocs/luis/cake/libs/model/datasources/dbo/dbo_mysql.php on line 411
Error: Your database does not have any tables.
My database config is like this :
var $default = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'localhost',
'port' => 8889,
'login' => 'root',
'password' => 'root',
'database' => 'cake',
);
So i am using the last CakePHP Version (1.2.5), the last Mamp version (1.7.2) running PHP v5... Somebody knows what is the problem ?
This is the 2.x version of the database config for localhost on MAMP
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'USERNAME',
'password' => 'PASSWORD',
'database' => 'DATABASE',
'encoding' => 'utf8',
'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock'
);
You can also get around using the localhost ip address by doing
var $default = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'localhost',
'port' => 8889,
'login' => 'root',
'password' => 'root',
'database' => 'cake',
'port' => '/Applications/MAMP/tmp/mysql/mysql.sock /tmp/mysql.sock',
);
This is because when using a named localhost MYSQL will try and connect through a local socket. By specififying where the socket is via 'port' you change the path it looks for the socket file in. By using 127.0.0.1 or by specifying an IP of any sort you tell MYSQL to connect via http, which does not use the socket at all.
Ok i found a way to do it, it might help people who have the same problem : i set a the host to 127.0.0.1 so cake can connect to it. Thanks for help.
cake can't find the socket.
try making a symbolic link from where your socket is, to where Cake thinks it should be:
ln -s /Applications/MAMP/tmp/mysql/mysql.sock /tmp/mysql.sock
I had this problem when I moved to CakePHP. In my current MAMP 1.9.x version I simply make sure I use 'host' => '127.0.0.1' and 'port' => 8889 (unless you change the default mysql port).
Works perfectly for me.
I'm helping a friend to setup this on Windows.
Inside a EasyPHP stuff all work fine (connection with Mysql). So Cakephp use PDO to connect to database.
When try use BAKE to generate the models raise: "Error: Database connection "Mysql" is missing, or could not be created."
PHP on command line try find php.ini inside de Windows folder so they can't find and get the default configuration without PDO and Mysql extensions.
Copy php.ini inside de WINDOWS folder and bake stuffs will work fine!
精彩评论