macos CakePHP:无法访问 MySQL 数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1635474/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 06:30:18  来源:igfitidea点击:

CakePHP: Can't access MySQL database

phpmysqlmacoscakephposx-snow-leopard

提问by Jared

I'm new to CakePHP and am just running through the configuration process, but am stumped why Cake can't access my MySQL database. The Cake info page says my tmp directory is writable, the FileEngine is being used for caching (don't know what this means), and my database configuration file is present, but that CakePHP cannot connect to the database.

我是 CakePHP 的新手,刚刚完成配置过程,但我很困惑为什么 Cake 无法访问我的 MySQL 数据库。Cake info 页面说我的 tmp 目录是可写的,FileEngine 正在用于缓存(不知道这是什么意思),并且我的数据库配置文件存在,但是 CakePHP 无法连接到数据库。

Here are my setup details:

这是我的设置详细信息:

  • PHP 5.3 (pre-installed on Snow Leopard)
  • MySQL 5.1.40 64-bit
  • CakePHP 1.2.4.8284
  • PHP 5.3(雪豹预装)
  • MySQL 5.1.40 64 位
  • 蛋糕PHP 1.2.4.8284

Here are the steps I went through:

以下是我经历的步骤:

  • Created a MySQL schema called cake_blog
  • Created a MySQL user called cake_blog_user
  • Granted cake_blog_user the appropriate permissions on cake_blog@localhost and cake_blog@%
  • Copied the database.php.default file to database.php and edited the database connection details as appropriate
  • 创建了一个名为 cake_blog 的 MySQL 模式
  • 创建了一个名为 cake_blog_user 的 MySQL 用户
  • 授予 cake_blog_user 对 cake_blog@localhost 和 cake_blog@% 的适当权限
  • 将database.php.default 文件复制到database.php 并根据需要编辑数据库连接详细信息

Here is the relevant configuration data from database.php:

以下是database.php中的相关配置数据:

    var $default = array(
        'driver' => 'mysql',
        'persistent' => false,
        'host' => 'localhost',
        'login' => 'cake_blog_user',
        'password' => 'cake_blog_password',
        'database' => 'cake_blog',
        'prefix' => '',
    );

Am I missing something here? I should also mention that if I insert an echo mysql_error();into the /cake/libs/view/pages/home.ctp file right before it tests the database connection, the error displayed is "No such file or directory." I have no idea what file or directory it's talking about.

我在这里错过了什么吗?我还应该提到,如果我echo mysql_error();在测试数据库连接之前将一个插入到 /cake/libs/view/pages/home.ctp 文件中,则显示的错误是“没有这样的文件或目录”。我不知道它在谈论什么文件或目录。

Thanks!

谢谢!

采纳答案by nduplessis

If it is the socket, just edit /etc/php.ini to reflect the following

如果是socket,直接编辑/etc/php.ini反映如下

pdo_mysql.default_socket=/tmp/mysql.sock

and

mysql.default_socket = /tmp/mysql.sock

回答by Eduardo Romero

What usually bites me in it's that MySQL thinks of 'localhost' as 'connect thru the unix socket' and '127.0.0.1' 'connect thru TCP port'. With things like XAMPP (at least on mac) the unix socket file isn't there. Just use 127.0.0.1instead.

通常让我感到困扰的是 MySQL 认为“本地主机”是“通过 unix 套接字连接”和“127.0.0.1”是“通过 TCP 端口连接”。对于像 XAMPP(至少在 mac 上)这样的东西,unix 套接字文件不存在。只需使用 127.0.0.1 即可

var $default = array(
    'driver' => 'mysql',
    'persistent' => false,
    'host' => '127.0.0.1',
    'login' => 'cake_blog_user',
    'password' => 'cake_blog_password',
    'database' => 'cake_blog',
    'prefix' => '',
);

Should work all the time.

应该一直工作。

回答by Abba Bryant

I believe you can also do the following

相信你也可以做到以下几点

<?php
    public $default = array(
        'driver' => 'mysql',
        'persistent' => false,
        'host' => 'localhost',
        'login' => 'cake_blog_user',
        'password' => 'cake_blog_password',
        'database' => 'cake_blog',
        'prefix' => '',
        'port' => '/tmp/mysql.sock',            
    )
?>

doing this might mean you need to edit the database.php file when you go live on the production server.

这样做可能意味着您需要在生产服务器上上线时编辑 database.php 文件。

回答by Jared

Thanks everyone for pointing me in the right direction. The mysql.sock file has been moved to /tmp/mysql.sockinstead of its default location at /var/mysql/mysql.sock. Editing the php.ini file to reflect this has fixed the problem.

感谢大家为我指出正确的方向。mysql.sock 文件已移至/tmp/mysql.sock而不是其默认位置/var/mysql/mysql.sock。编辑 php.ini 文件以反映此问题已解决。

回答by lilbro1062000

check your phpinfo and use the socket listed. that worked for me.

检查您的 phpinfo 并使用列出的套接字。这对我有用。

回答by Stefan Bubonja

On Ubuntu, if you installed both 7.0 and 5.6 versions of PHP this wont work.

在 Ubuntu 上,如果您同时安装了 7.0 和 5.6 版本的 PHP,这将不起作用。

Youll need to switch if you have both versions:

如果您有两个版本,则需要切换:

Look first if is 7.0 version: the command is php -v.

先看如果是7.0版本:命令是php -v.

Next do

下一步做

sudo a2dismod php7.0
sudo a2enmod php5.6
sudo service apache2 restart