简体   繁体   English

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

[英]CakePHP Bake Shell Error: Database connection “Mysql” is missing, or could not be created

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 我无法烘焙,因为我收到的错误是: 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. 如果我运行which php PHP它正在阅读是MAMP中的正确路径。

If I check the PDO modules: 如果我检查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

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. Web上的所有答案都指向PDO未启用,或者PHP的路径不正确,但这些都不适用于我的情况。

Maybe a wrong username / password or misspelled database name. 可能是错误的用户名/密码或拼写错误的数据库名称。 I once had a database (name) beginning with a space. 我曾经有一个以空格开头的数据库(名称)。

You may want to check the database connection using plain PHP, see . 您可能希望使用普通PHP检查数据库连接, 请参阅

Another solution (Mac and MAMP) is to update your database connection HOST. 另一个解决方案(Mac和MAMP)是更新数据库连接HOST。 I had specified localhost and received this error message. 我指定了localhost并收到此错误消息。 When I updated the host to 127.0.0.1 cake was able to make the connection. 当我更新主机到127.0.0.1蛋糕能够进行连接。

The symlink method described by cfkane should also address the issue. cfkane描述的符号链接方法也应该解决这个问题。

I finally found out what was the problem, at least for me: In database.php, I specified: 我终于找到了问题所在,至少对我而言:在database.php中,我指定:

'encoding' => 'utf-8'

whereas: 然而:

'encoding' => 'utf8'

would be much better ! 会好得多!

In order to save time to users, I suggest the exception handler could display the full error message from PDO. 为了节省用户的时间,我建议异常处理程序可以显示PDO的完整错误消息。 Here is my patch: 这是我的补丁:

--- 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)) {

The result is: 结果是:

[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()

I was having this issue while using MAMP on MacOS. 我在MacOS上使用MAMP时遇到了这个问题。

From the command prompt, I had to run this sudo 从命令提示符,我不得不运行这个sudo

sudo mkdir /var/mysql sudo mkdir / var / mysql

Then this one 然后这一个

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

Then when I ran my cake bake command again, everything worked. 然后当我再次运行我的蛋糕烘烤命令时,一切正常。

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

I'm facing the same problem, checked PDO support the same way, same result. 我面临同样的问题,以相同的方式检查PDO支持,结果相同。

The following script (that I wrote) might help to make sure our settings are ok: 以下脚本(我写的)可能有助于确保我们的设置正常:

<?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();
}

?>

And it works fine : 它工作正常:

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

FYI, here are version details : 仅供参考,这是版本细节:

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

Would it be a problem with paths ? 它会成为路径的问题吗? Here's my bake interaction: 这是我的烘焙互动:

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}

I also tried : 我也尝试过:

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

with no more success. 没有更多的成功。

Now I suspect the Datasource itself ... 现在我怀疑数据源本身......

HTH HTH

Michelle 米歇尔

Just to help Ubuntu users out: I had the same error in my ubuntu 13.10 machine with the newest xampp downloaded directly from apachefriends. 只是为了帮助Ubuntu用户:我的ubuntu 13.10机器中出现了同样的错误,最新的xampp是直接从apachefriends下载的。 As a summary: 作为总结:

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“丢失,或无法创建。 ”。

change login => 'to your user created while setting up the database'. 将login =>'更改为在设置数据库时创建的用户'。 Just make sure the db configuration reflect exactly the same parameters used while creating the database and al should world fine. 只需确保db配置完全反映创建数据库时使用的相同参数,并且al应该世界正常。

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

相关问题 CakePHP烘焙错误:数据库连接“ Mysql”丢失,或无法创建 - CakePHP Bake Error: Database connection “Mysql” is missing, or could not be created CakePHP 数据库连接“Mysql”丢失,或无法创建 - CakePHP 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 蛋糕烘烤控制台错误:数据库连接丢失 - cake bake console error:Database connection missing CakePHP数据库连接“ Mysql”丢失,或无法创建。 core.php和bootstrap.php - CakePHP Database connection “Mysql” is missing, or could not be created. core.php and bootstrap.php 使用Mamp的Mac OS上的Cakephp控制台(烘焙):数据库连接错误 - Cakephp console (bake) on Mac OS with Mamp : database connection error 使用Cae Bake时错误数据库连接丢失 - Error database connection is missing while using cae bake 数据库连接“ Sqlserver”丢失,或无法创建 - Database connection “Sqlserver” is missing, or could not be created CakePHP WAMP-数据库Mysql连接丢失 - CakePHP WAMP - Database Mysql connection missing CakePHP - cake bake all error(说数据库中缺少 $default。php,但它存在) - CakePHP - cake bake all error (says missing $default in database.php, but it is present)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM