php CakePHP 数据库连接“Mysql”丢失,或无法创建

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19280245/
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-08-25 19:10:55  来源:igfitidea点击:

CakePHP Database connection "Mysql" is missing, or could not be created

phpmysqlmacoscakephpmamp

提问by Ben Caine

There have been several other posts about this, but none of the answers seemed to work for me.

还有其他几篇关于此的帖子,但似乎没有一个答案对我有用。

When I navigate to the CakePHP page on my local machine, there is one error:

当我导航到本地机器上的 CakePHP 页面时,出现一个错误:

Cake is NOT able to connect to the database. Database connection "Mysql" is missing, or could not be created.

Cake 无法连接到数据库。数据库连接“Mysql”丢失,或无法创建。

When I run this helpful codein my home.ctp, I get the following response:

当我在 home.ctp 中运行这个有用的代码时,我得到以下响应:

Error!: SQLSTATE[42000] [1049] Unknown database 'test'

错误!:SQLSTATE[42000] [1049] 未知数据库 'test'

However, my Users/Ben/Sites/myapp/app/Config/database.phplooks like this (I set MAMP to look for the document root in Users/Ben/Sites):

但是,我Users/Ben/Sites/myapp/app/Config/database.php看起来像这样(我将 MAMP 设置为在 Users/Ben/Sites 中查找文档根目录):

<?php
class DATABASE_CONFIG {

    public $default = array(
        'datasource' => 'Database/Mysql',
        'persistent' => false,
        'host' => 'localhost',
        'login' => 'Ben',
        'password' => 'mypass',
        'database' => 'CV',
    );
}

I have created a mysql user called Benwith password mypassand created a database called CVunder that. Moreover, I can't find mention of a testdatabase anywhere. Help?

我创建了一个Ben用密码调用的 mysql 用户,mypass并在该用户CV下创建了一个数据库。此外,我test在任何地方都找不到提到数据库的地方。帮助?

采纳答案by Ben Caine

What did it for me in the end was that I had created a table in my database, but there was no data in it.

最后对我造成的影响是我在我的数据库中创建了一个表,但是里面没有数据。

In order for CakePHP to recognize the MySql connection, there has to be a table with data in it.

为了让 CakePHP 识别 MySql 连接,必须有一个包含数据的表。

回答by Domingo C.

Try adding the socket:

尝试添加套接字:

'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',

回答by enagra

An alternative to unix_socket(especially for OS X people) is to replace localhostwith 127.0.0.1

替代unix_socket(特别是对于 OS X 人)是替换localhost127.0.0.1

Would be as Follows :

如下:

public $default = array(
                'datasource' => 'Database/Mysql',
                'persistent' => false,
                'host' => '127.0.0.1',
                'login' => 'user',
                'password' => 'password',
                'database' => 'database-name',
                'prefix' => '',
                'encoding' => 'utf8',
        );

回答by King Jk

Edit php.iniand add:

编辑php.ini并添加:

extension=php_pdo_mysql.dll 

Then restart your web server

然后重启你的网络服务器

回答by DecoMartins

On Mac, using MAMP as a development platform, for cake the correct solution is using Domingo Casarrubio solution.

在 Mac 上,使用 MAMP 作为开发平台,对于蛋糕,正确的解决方案是使用 Domingo Casarrubio 解决方案。

Add the unix_socketparameter to your database configurations.

将该unix_socket参数添加到您的数据库配置中。

'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',

回答by Scott

This error can also be caused if your connecting database user doesn't have the proper privileges. I believe you only need a minimum of INSERT, SELECT, UPDATE, and DELETE.

如果您的连接数据库用户没有适当的权限,也可能导致此错误。我相信您只需要最少的 INSERT、SELECT、UPDATE 和 DELETE。

Always check username/password andthe user privileges first since CakePHP will most likely give a vague database connection error for either.

总是首先检查用户名/密码用户权限,因为 CakePHP 很可能会给出一个模糊的数据库连接错误。

回答by Brajinder Singh

I noticed that you've had asked this an year ago, and most probably would've solved this by now. However, for those facing the same issues when attempting to install CakePHP on XAMPP, all you have to do is change the 'login' to 'root', i.e. the default login of XAMPP, and leave the 'password' as '', i.e. blank. The complete code in your database.php file should look like this:

我注意到你在一年前问过这个问题,现在很可能已经解决了这个问题。然而,对于那些在 XAMPP 上尝试安装 CakePHP 时遇到同样问题的人,您所要做的就是将“登录”更改为“root”,即 XAMPP 的默认登录名,并将“密码”保留为“”,即空白的。database.php 文件中的完整代码应如下所示:

public $default = array(
    'datasource' => 'Database/Mysql',
    'persistent' => false,
    'host' => 'localhost',
    'login' => 'root',
    'password' => '',
    'database' => 'ckblog',//replace with your own database name
    'prefix' => '',
    //'encoding' => 'utf8',
);

That's it.

就是这样。

回答by paulleephp

I have had this problem since upgrading to OSX Yosemite and inserting following line did the trick for me:

自从升级到 OSX Yosemite 并插入以下行后,我就遇到了这个问题:

 'unix_socket' => '/tmp/mysql.sock'

回答by Mike Rose

It can be that mysql PDO support is missing.

可能是缺少 mysql PDO 支持。

as root (or using sudo):

以 root 身份(或使用 sudo):

apt-get install php5-mysql

回答by user3834255

I had the same problem and found out eventually that it was caused by CakePhp not accepting that I used a user with a password, even if that user was created in PHPMyAdmin. I had to use the user 'root' with no password.

我遇到了同样的问题,最终发现它是由 CakePhp 不接受我使用带密码的用户引起的,即使该用户是在 PHPMyAdmin 中创建的。我不得不使用没有密码的用户“root”。

I found this out after making the following change to the file /lib/Cake/Error/exceptions.php.

在对文件/lib/Cake/Error/exceptions.php进行以下更改后,我发现了这一点

The original line:

原行:

protected $_messageTemplate = 'Database connection "%s" is missing, or could not be created.';

is changed into this instead:

改为:

protected $_messageTemplate = "Database connection \"%s\" is missing, or could not be created:\n    %s";

This will give you the reason for the problem so that you may change the cause properly.

这将为您提供问题的原因,以便您可以正确更改原因。

回答by Quy Le

Because, cake bake use unix socket for connecting to database
so that you need add unix_socket for connection string.
You have to confirm location that store mysql.sock in WAS
Example: in my case i'm using xampp on MACOS 10.11
(edit file Config/database.php)

因为,cake bake 使用unix socket 来连接数据库,
所以你需要为连接字符串添加unix_socket。
您必须确认在 WAS 中存储 mysql.sock 的位置
示例:在我的情况下,我在 MACOS 10.11 上使用 xampp
(编辑文件 Config/database.php)

public $default = array(
  ‘datasource' => ‘Database/Mysql',
  ‘persistent' => false,
  ‘host' => ‘localhost',
  ‘login' => ‘root',
  ‘password' => ‘root',
  ‘database' => ‘cakephp',
  ‘encoding' => ‘utf8',
  ‘unix_socket' => ‘/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock'
);

Finally, It's work for me!

最后,这对我有用!