简体   繁体   English

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

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

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. Cake 无法连接到数据库。 Database connection "Mysql" is missing, or could not be created.数据库连接“Mysql”丢失或无法创建。

When I run this helpful code in 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.php looks 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 Ben with password mypass and created a database called CV under that.我创建了一个名为Ben的 mysql 用户,密码为mypass并在其下创建了一个名为CV的数据库。 Moreover, I can't find mention of a test database anywhere.此外,我在任何地方都找不到test数据库的提及。 Help?帮助?

尝试添加套接字:

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

An alternative to unix_socket (especially for OS X people) is to replace localhost with 127.0.0.1 unix_socket的替代方案(特别是对于 OS X 用户)是将localhost替换为127.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',
        );

Edit php.ini and add:编辑php.ini并添加:

extension=php_pdo_mysql.dll 

Then restart your web server然后重启你的网络服务器

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_socket parameter to your database configurations.unix_socket参数添加到您的数据库配置中。

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

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 and the user privileges first since CakePHP will most likely give a vague database connection error for either.总是首先检查用户名/密码用户权限,因为 CakePHP 很可能会给出一个模糊的数据库连接错误。

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', ie the default login of XAMPP, and leave the 'password' as '', ie blank.然而,对于那些在 XAMPP 上尝试安装 CakePHP 时遇到同样问题的人,您所要做的就是将“登录”更改为“root”,即 XAMPP 的默认登录名,并将“密码”保留为“”,即空白的。 The complete code in your database.php file should look like this: 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.就是这样。

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.我遇到了同样的问题,最终发现它是由 CakePhp 不接受我使用带密码的用户引起的,即使该用户是在 PHPMyAdmin 中创建的。 I had to use the user 'root' with no password.我不得不使用没有密码的用户“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 (note the change from single to double quotes):改为这样(注意从单引号到双引号的变化):

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.这将为您提供问题的原因,以便您可以正确更改原因。

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

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

It can be that mysql PDO support is missing.可能是缺少 mysql PDO 支持。

as root (or using sudo):以 root 身份(或使用 sudo):

apt-get install php5-mysql

Just to help Ubuntu users out: I had the same error in my ubuntu 13.10 machine with the newest xampp downlaoded directly from apachefriends.只是为了帮助Ubuntu用户:我在 ubuntu 13.10 机器上遇到了同样的错误,最新的 xampp 直接从 apachefriends 下载。 Tried most of the stuff in every post I could find about this error, but not the mac-specific stuff.尝试了我能找到的关于此错误的每个帖子中的大部分内容,但没有尝试特定于 mac 的内容。 In the end, the fix happened to be the same as the elected answer here:最后,修复恰好与此处选择的答案相同:

Find the socket that mysqld creates for programs to connect to:找到 mysqld 为要连接的程序创建的套接字:

user@host /opt$ find . -name mysql.sock
/opt/lampp/var/mysql/mysql.sock

add it to your cakePHP database configuration file (cakePHP)/app/Config/database.php将它添加到您的 cakePHP 数据库配置文件 (cakePHP)/app/Config/database.php

'unix_socket' => '/opt/lampp/var/mysql/mysql.sock'

To me, this finally resulted in my cake commands being able to be executed without the " Error: Database connection "Mysql" is missing, or could not be created. ".对我来说,这最终导致我的蛋糕命令能够在没有“错误:数据库连接“Mysql”丢失或无法创建的情况下执行。 “。

Because, cake bake use unix socket for connecting to database因为, cake bake 使用 unix socket 来连接数据库
so that you need add unix_socket for connection string.所以你需要为连接字符串添加 unix_socket 。
You have to confirm location that store mysql.sock in WAS您必须确认在 WAS 中存储 mysql.sock 的位置
Example: in my case i'm using xampp on MACOS 10.11示例:就我而言,我在 MACOS 10.11 上使用 xampp
(edit file Config/database.php) (编辑文件 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!最后,这对我有用!

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 连接,必须有一个包含数据的表。

您可能需要在 php 文件中创建表...打开 phpMyAdmin 并检查以确保它们的数据库 CV 存在。

It's your model.是你的模型。 Open that up and there must be the following line打开它,必须有以下行

public $useDbConfig = 'local';

This overwrites global config & set it back to local这会覆盖全局配置并将其设置回本地

I tried splicing the code from Example 2 of http://php.net/manual/en/pdo.connections.php into /app/View/Pages/home.ctp.我尝试将http://php.net/manual/en/pdo.connections.php示例 2 中的代码拼接到 /app/View/Pages/home.ctp 中。 I had to fix the arguments the PDO constructor and change the name of the table in the query.我必须修复 PDO 构造函数的参数并更改查询中表的名称。 The example 2 code returned the error "Error!: could not find driver".示例 2 代码返回错误“错误!:找不到驱动程序”。 Based on King Jk's answer I was attempting to modify the php.ini when I started to wonder where a php_pdo_mysql.so might live.根据 King Jk 的回答,当我开始怀疑 php_pdo_mysql.so 可能存在于何处时,我试图修改 php.ini。 http://php.net/pdo_mysql showed how it was compiled as part of PHP via the --with-pdo-mysql option to configure. http://php.net/pdo_mysql展示了它是如何通过 --with-pdo-mysql 选项配置为 PHP 的一部分编译的。 Recompiling fixed my problem.重新编译解决了我的问题。 Note I'm working on a Ubuntu 12.10 system with PHP 5.5.9 and Apache Webserver 2.4.6注意我正在使用 PHP 5.5.9 和 Apache Webserver 2.4.6 的 Ubuntu 12.10 系统

In my case it was because the database didn't exist.就我而言,这是因为数据库不存在。 I expected running ./app/Console/cake schema create would create it but it did not.我希望运行./app/Console/cake schema create会创建它,但它没有。 Creating it with create database <database name> in mysql did the trick (although I had already assigned privileges).在 mysql 中使用create database <database name>创建它可以解决问题(尽管我已经分配了权限)。

I've been struggling with this the whole weekend and finally solved it.整个周末我都在为此苦苦挣扎,终于解决了。 Turns out that the php.ini is pointing to a non-existing "extensions dir".结果是 php.ini 指向一个不存在的“扩展目录”。 Create a phpinfo() file and look at the value of this field:创建一个 phpinfo() 文件并查看该字段的值:扩展目录

I noticed that in the mamp php installed folder there is a no-debug-non-zts-20131226 folder, which is different from the value shown in the phpinfo().我注意到在mamp php installed文件夹中有一个no-debug-non-zts-20131226文件夹,与phpinfo()中显示的值不同。 What I did was to clone this folder and changed the name to the value of the phpinfo().我所做的是克隆这个文件夹并将名称更改为 phpinfo() 的值。 Probably you could modify the php.ini file but I didn't want to.也许你可以修改 php.ini 文件,但我不想。

I don't know if you solved your problem, but I'm posting this because my problem was different and google took me here, so I hope to help future googlers having a similiar issue.我不知道你是否解决了你的问题,但我发布这个是因为我的问题不同,谷歌把我带到了这里,所以我希望能帮助未来遇到类似问题的谷歌员工。

Hope this helps.希望这可以帮助。

如果您使用的是 Godaddy(或任何其他与此相关的共享主机),他们可能会将传出连接限制为仅端口 80 和 443。

System configuration:系统配置:

  • Fedora 32软呢帽 32
  • php-fpm 7.4.13 php-fpm 7.4.13
  • mariadb 10.4.17玛丽亚数据库 10.4.17
  • CAKE 2.10.17蛋糕 2.10.17

Error message from CAKE:来自 CAKE 的错误消息:

Database connection "Mysql" is missing, or could not be created.数据库连接“Mysql”丢失或无法创建。

Enhanced error message using answer at https://stackoverflow.com/a/24722976/5025060使用https://stackoverflow.com/a/24722976/5025060 上的答案增强错误消息

Database connection "Mysql" is missing, or could not be created: Selected driver is not enabled数据库连接“Mysql”丢失或无法创建:未启用选定的驱动程序

My problem was no "connector" between PHP and SQL was installed.我的问题是没有安装 PHP 和 SQL 之间的“连接器”。 The solution was:解决方案是:

dnf install php-mysqlnd

This allowed PHP to connect to the database as specified in CAKE's database.php configuration file.这允许 PHP 连接到 CAKE 的 database.php 配置文件中指定的数据库。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 CakePHP烘焙错误:数据库连接“ Mysql”丢失,或无法创建 - CakePHP Bake Error: Database connection “Mysql” is missing, or could not be created CakePHP 2.0:数据库连接“Mysql”丢失或无法创建 - CakePHP 2.0 : Database connection “Mysql” is missing, or could not be created CakePHP Bake Shell错误:数据库连接“Mysql”丢失或无法创建 - CakePHP Bake Shell Error: Database connection “Mysql” is missing, or could not be created CakePHP数据库连接“ Mysql”丢失,或无法创建。 core.php和bootstrap.php - CakePHP Database connection “Mysql” is missing, or could not be created. core.php and bootstrap.php 数据库连接“ Sqlserver”丢失,或无法创建 - Database connection “Sqlserver” is missing, or could not be created CakePHP WAMP-数据库Mysql连接丢失 - CakePHP WAMP - Database Mysql connection missing 带有Cakephp内联的错误消息-缺少数据库连接 - An error message with cakephp inline - Missing Database Connection 需要帮助,以MAMP调试CakePHP与MySQL的连接,错误:数据库中缺少表(出于某种原因) - Need Help Debugging CakePHP Connection to MySQL with MAMP, error: missing table in database (for what seems like no reason) 数据库连接错误(2):无法连接到MySQL - Database connection error (2): Could not connect to MySQL 使用“ Mysql”的数据库连接丢失或无法连接 - A Database connection using “Mysql” was missing or unable to connect
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM