8

I have an issue here with baking.
I've read the previous answers to similar questions, but the solutions seem to not apply here.

I can't bake because the error I receive is: Database connection “Mysql” is missing, or could not be created

If I run which php the PHP it's reading is the correct path within MAMP.

If I check the PDO modules:

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

My application (or what I've completed on it so far) has no trouble connecting to the database.

All answers around the web point to PDO not being enabled, or the incorrect path for PHP, but neither of those apply in my case.

4

7 回答 7

7

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

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

于 2014-01-17T17:50:26.460 回答
6

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

您可能想使用纯 PHP 检查数据库连接,请参阅.

于 2013-07-30T19:08:08.347 回答
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()
于 2013-10-09T22:26:57.500 回答
2

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

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

须藤 mkdir /var/mysql

那么这个

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

然后,当我再次运行我的 cake bake 命令时,一切正常。

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

于 2013-11-26T13:41:05.377 回答
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”丢失或无法创建。 ”的情况下执行。

于 2014-02-03T18:01:43.907 回答
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

没有更多的成功。

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

高温高压

米歇尔

于 2013-10-09T19:44:00.770 回答
0

更改登录 => '设置数据库时创建的用户'。只需确保 db 配置反映创建数据库时使用的完全相同的参数,就可以了。

于 2016-06-18T09:04:12.647 回答