php CakePHP Bake Shell 错误:数据库连接“Mysql”丢失,或无法创建
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17952880/
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
CakePHP Bake Shell Error: Database connection “Mysql” is missing, or could not be created
提问by Dan Hanly
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.
网络上的所有答案都指向未启用 PDO,或者 PHP 的路径不正确,但这些都不适用于我的情况。
采纳答案by tersmitten
回答by Todd
Another solution (Mac and MAMP) is to update your database connection HOST. I had specified localhostand received this error message. When I updated the host to 127.0.0.1cake was able to make the connection.
另一种解决方案(Mac 和 MAMP)是更新您的数据库连接 HOST。我已指定localhost并收到此错误消息。当我将主机更新为127.0.0.1 时,cake 能够建立连接。
The symlink method described by cfkane should also address the issue.
cfkane 描述的符号链接方法也应该解决这个问题。
回答by rockyroad
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. Here is my patch:
为了节省用户的时间,我建议异常处理程序可以显示来自 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)) {
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()
回答by cfkane
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
须藤 mkdir /var/mysql
Then this one
那么这个
sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock/var/mysql/mysql.sock
须藤 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/
回答by rockyroad
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
米歇尔
回答by FaustoW
Just to help Ubuntuusers out: I had the same error in my ubuntu 13.10 machine with the newest xampp downloaded directly from apachefriends. As a summary:
只是为了帮助Ubuntu用户:我在 ubuntu 13.10 机器上遇到了同样的错误,最新的 xampp 是直接从 apachefriends 下载的。作为总结:
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”丢失或无法创建的情况下执行。“。
回答by Lawrence Lakuma
change login => 'to your user created while setting up the database'. Just make sure the db configuration reflect exactly the same parameters used while creating the database and al should world fine.
更改登录 => '设置数据库时创建的用户'。只需确保 db 配置反映了创建数据库时使用的完全相同的参数,并且 al 应该没问题。