CakePHP Bake Shell错误:数据库连接“Mysql”丢失或无法创建

时间:2013-07-30 16:49:41

标签: php cakephp pdo command-line-interface cakephp-bake

我在这里有烘焙问题 我已经阅读了类似问题的先前答案,但解决方案似乎不适用于此。

我无法烘焙,因为我收到的错误是:Database connection “Mysql” is missing, or could not be created

如果我运行which php PHP,它的读取是MAMP中的正确路径。

如果我检查PDO模块:

php -i | grep "PDO"
PDO
PDO support => enabled
PDO drivers => sqlite, pgsql, mysql
PDO Driver for MySQL => enabled
PDO Driver for PostgreSQL => enabled
PDO Driver for SQLite 3.x => enabled

我的应用程序(或我迄今为止已经完成的应用程序)连接数据库没有问题。

网络上的所有答案都指向PDO未启用,或者PHP的路径不正确,但这些都不适用于我的情况。

7 个答案:

答案 0 :(得分:6)

可能是错误的用户名/密码或拼写错误的数据库名称。我曾经有一个以空格开头的数据库(名称)。

您可能希望使用普通PHP see检查数据库连接。

答案 1 :(得分:5)

另一种解决方案(Mac和MAMP)是更新数据库连接HOST。我已指定 localhost 并收到此错误消息。当我将主机更新为 127.0.0.1 时,蛋糕能够建立连接。

cfkane描述的符号链接方法也应该解决这个问题。

答案 2 :(得分:3)

我终于找到了问题所在,至少对我而言: 在database.php中,我指定了:

'encoding' => 'utf-8'

,而:

'encoding' => 'utf8'

会好多了!

为了节省用户的时间,我建议异常处理程序可以显示来自PDO的完整错误消息。这是我的补丁:

--- a/lib/Cake/Error/exceptions.php
+++ b/lib/Cake/Error/exceptions.php
@@ -378,7 +378,7 @@ class MissingDatabaseException extends CakeException {
  */
 class MissingConnectionException extends CakeException {

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

        public function __construct($message, $code = 500) {
                if (is_array($message)) {

结果是:

[default] > 
Error: Database connection "Mysql" is missing, or could not be created:
    SQLSTATE[42000] [1115] Unknown character set: 'utf'
#0 /usr/local/share/cakePHP/cakephp/lib/Cake/Model/Datasource/DboSource.php(262): Mysql->connect()

答案 3 :(得分:2)

我在MacOS上使用MAMP时遇到了这个问题。

在命令提示符下,我必须运行这个sudo

  

sudo mkdir / var / mysql

然后这个

  

sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock/var/mysql/mysql.sock

然后,当我再次运行蛋糕烘烤命令时,一切正常。

在此处找到:http://www.mojowill.com/developer/quick-tip-cakephp-baking-and-mamp/

答案 4 :(得分:0)

我面临同样的问题,以相同的方式检查PDO支持,结果相同。

以下脚本(我写的)可能有助于确保我们的设置正常:

<?php
include("../new-project/app/Config/database.php");
$config= new DATABASE_CONFIG();

$name = 'default';

$settings=$config->{$name};
$dsn = 'mysql:dbname='.$settings['database'].';host='.$settings['host'];
$user = $settings['login'];
$password = $settings['password'];

try {
    $dbh = new PDO($dsn, $user, $password);
    echo "Connection succeeded with dsn: ". $dsn . "\n";
    $sql = 'SELECT id, title FROM posts';
    echo "Here is the contents of the table `posts:";
    foreach ($dbh->query($sql) as $row) {
        print $row['id'] . "\t" . $row['title'] . "\n";
    }
} catch (PDOException $e) {
    echo 'PDO error: ' . $e->getMessage();
}

?>

它运作良好:

mich@dennet:~/www/learn/cakePHP/tools$ php test-pdo.php
Connection succeeded with dsn: mysql:dbname=test;host=localhost
Here is the contents of the table `posts:1  The title
3   Title strikes back
4   Once again is back

仅供参考,这是版本细节:

mich@dennet:~/www/learn/cakePHP/tools$ php --version
PHP 5.4.9-4ubuntu2.3 (cli) (built: Sep  4 2013 19:32:25)
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2012 Zend Technologies
    with Xdebug v2.3.0dev, Copyright (c) 2002-2012, by Derick Rethans

路径会出现问题吗?这是我的烘焙互动:

mich@dennet:~/www/learn/cakePHP/new-project$ /usr/local/bin/cake -app  /home/mich/www/learn/cakePHP/new-project/app bake

Welcome to CakePHP v2.4.1 Console
---------------------------------------------------------------
App : app
Path: /home/mich/www/learn/cakePHP/new-project/app/
...
What would you like to Bake? (D/M/V/C/P/F/T/Q)
> m
---------------------------------------------------------------
Bake Model
Path: /home/mich/www/learn/cakePHP/new-project/app/Model/
---------------------------------------------------------------
Use Database Config: (default/forbaking)
[default] >
Error: Database connection "Mysql" is missing, or could not be created.
#0 /usr/local/share/cakePHP/cakephp/lib/Cake/Model/Datasource/DboSource.php(262): Mysql->connect()
#1 /usr/local/share/cakePHP/cakephp/lib/Cake/Model/ConnectionManager.php(107): DboSource->__construct(Array)
#2 /usr/local/share/cakePHP/cakephp/lib/Cake/Console/Command/Task/ModelTask.php(927): ConnectionManager::getDataSource('default')
#3 /usr/local/share/cakePHP/cakephp/lib/Cake/Console/Command/Task/ModelTask.php(864): ModelTask->getAllTables(NULL)
#4 /usr/local/share/cakePHP/cakephp/lib/Cake/Console/Command/Task/ModelTask.php(953): ModelTask->listAll(NULL)
#5 /usr/local/share/cakePHP/cakephp/lib/Cake/Console/Command/Task/ModelTask.php(205): ModelTask->getName()
#6 /usr/local/share/cakePHP/cakephp/lib/Cake/Console/Command/Task/ModelTask.php(93): ModelTask->_interactive()
#7 /usr/local/share/cakePHP/cakephp/lib/Cake/Console/Command/BakeShell.php(111): ModelTask->execute()
#8 /usr/local/share/cakePHP/cakephp/lib/Cake/Console/Shell.php(435): BakeShell->main()
#9 /usr/local/share/cakePHP/cakephp/lib/Cake/Console/ShellDispatcher.php(210): Shell->runCommand(NULL, Array)
#10 /usr/local/share/cakePHP/cakephp/lib/Cake/Console/ShellDispatcher.php(68): ShellDispatcher->dispatch()
#11 /usr/local/share/cakePHP/cakephp/app/Console/cake.php(37): ShellDispatcher::run(Array)
#12 {main}

我也尝试过:

mich@dennet:/usr/local/share/cakePHP/cakephp$ app/Console/cake bake

没有更多的成功。

现在我怀疑数据源本身......

HTH

米歇尔

答案 5 :(得分:0)

只是为了帮助 Ubuntu 用户: 我在我的ubuntu 13.10机器上遇到了同样的错误,最新的xampp是直接从apachefriends下载的。 总结:

找到mysqld为要连接的程序创建的套接字:

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

将它添加到cakePHP数据库配置文件(cakePHP)/app/Config/database.php

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

对我来说,这最终导致我的蛋糕命令能够在没有“错误:数据库连接”的情况下执行.Mysql“缺失,或者无法创建。”。

答案 6 :(得分:0)

更改登录=&gt; &#39;设置数据库时创建的用户&#39;。只需确保db配置反映出在创建数据库时使用的完全相同的参数,并且al应该很好。