我目前正在阅读David Golding的"Beginning CakePHP:从新手到专业".有一次我必须使用CLI命令"蛋糕烘焙",我得到欢迎屏幕,但当我尝试烘烤例如控制器时,我收到以下错误消息:
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.
我怀疑错误消息与php试图访问错误的mysql-socket,即默认的osx mysql-socket - 而不是MAMP使用的错误消息有关.因此,我更改了我的数据库配置以连接到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' => '', ); }
但我得到与新套接字相同的错误消息:
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.
此外,即使我使用MAMP在欢迎屏幕上显示的UNIX套接字,当使用此套接字而不是localhost时,CakePHP会丢失数据库连接.
关于如何烘焙工作的任何想法?
- 编辑1 -
谢谢大家帮帮我!:)
我有一个问题,想知道在my.cnf中编辑哪些地方让MySQL听取TCP/IP请求.我能找到的唯一提到TCP/IP的段落如下:
# 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
这允许我完全关闭TCP/IP,这与我的意图相反.我不知道如何处理你的建议,如果你能更详细一点就会很棒.在这些问题上我总共n00b:S
注册.连接到本地套接字:我删除了host-parameter中的前导冒号,结果相同.
我找到了解决这个问题的方法:在cakephp app/config/database.php文件中添加套接字配置
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' => '', );
从错误中,它看起来像是尝试连接到实际的IP地址而不是UNIX套接字,看看:
'/Applications/MAMP/tmp/mysql/mysql.sock:3306'
它将一个端口附加到套接字,这是错误的.
所以,我首先尝试配置MySQL来监听TCP/IP请求(编辑my.cnf中的正确部分)并尝试提供127.0.0.1而不是套接字.
如果您不向下滚动:
要在CakePHP级别修复它,将database.php上的主机更改为'localhost'并添加一个port指令,其值设置为套接字名称'/Applications/MAMP/tmp/mysql/mysql.sock'
对于使用CakePHP 2.0时遇到此问题的任何人:对我来说,上面的数据库配置文件并不能解决问题.找到了'unix_socket'属性,这对我有用:
'Database/Mysql', 'driver' => 'mysql', 'persistent' => false, 'host' => 'localhost', 'unix_socket' => '/tmp/mysql.sock', 'login' => 'xxx', 'password' => 'xxx', 'database' => 'xxx', 'encoding' => 'UTF8', 'prefix' => '' ); }