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

一世执手 提交于 2019-12-05 03:32:09
tersmitten

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.

Todd

Another solution (Mac and MAMP) is to update your database connection HOST. I had specified localhost and received this error message. When I updated the host to 127.0.0.1 cake was able to make the connection.

The symlink method described by cfkane should also address the issue.

I finally found out what was the problem, at least for me: In database.php, I specified:

'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:

--- 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.

From the command prompt, I had to run this sudo

sudo mkdir /var/mysql

Then this one

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/

I'm facing the same problem, checked PDO support the same way, same result.

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

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. As a summary:

Find the socket that mysqld creates for programs to connect to:

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

'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.".

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!