CakePHP Bake Shell Error: Database connection “Mys

2019-04-24 15:55发布

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.

7条回答
迷人小祖宗
2楼-- · 2019-04-24 16:33

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.

查看更多
时光不老,我们不散
3楼-- · 2019-04-24 16:39

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.

查看更多
等我变得足够好
4楼-- · 2019-04-24 16:39

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.

查看更多
Melony?
5楼-- · 2019-04-24 16:40

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/

查看更多
劫难
6楼-- · 2019-04-24 16:48

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

查看更多
ゆ 、 Hurt°
7楼-- · 2019-04-24 16:51

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()
查看更多
登录 后发表回答