36

There have been several other posts about this, but none of the answers seemed to work for me.

When I navigate to the CakePHP page on my local machine, there is one error:

Cake is NOT able to connect to the database. Database connection "Mysql" is missing, or could not be created.

When I run this helpful code in my home.ctp, I get the following response:

Error!: SQLSTATE[42000] [1049] Unknown database 'test'

However, my Users/Ben/Sites/myapp/app/Config/database.php looks like this (I set MAMP to look for the document root in Users/Ben/Sites):

<?php
class DATABASE_CONFIG {

    public $default = array(
        'datasource' => 'Database/Mysql',
        'persistent' => false,
        'host' => 'localhost',
        'login' => 'Ben',
        'password' => 'mypass',
        'database' => 'CV',
    );
}

I have created a mysql user called Ben with password mypass and created a database called CV under that. Moreover, I can't find mention of a test database anywhere. Help?

hg8
  • 1,082
  • 2
  • 15
  • 28
Ben Caine
  • 1,128
  • 3
  • 15
  • 25
  • 1
    I was getting the same error message and in my case changing permissions for whole Model directory helped a lot. – bbbb Nov 22 '13 at 16:16

19 Answers19

50

Try adding the socket:

'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',
Domingo C.
  • 789
  • 4
  • 15
  • 7
    Just so you don't wonder where to add this, put is on the last line of your database configuration (for example default) ... I am for example using XAMPP and the file is here: /Applications/XAMPP/xamppfiles/var/mysql/mysql.sock – Ondrej Rafaj Dec 09 '13 at 14:23
  • Thats correct, I supposed to many things from reading the question. :) – Domingo C. Dec 09 '13 at 18:20
  • Adding this allowed my Cron job to execute on a Mac running XAMPP. Thanks! – adamup Apr 22 '14 at 20:36
  • 1
    FYI this worked on my XAMPP installation too except I had to put /Applications/XAMPP/xamppfiles/var/mysql/mysql.sock – khany May 21 '14 at 11:37
  • 3
    Not everyone is using xampp (especially for OS X people out there). Find out where your mysql.sock is. i'm using OS X 10.9+ and it was: 'unix_socket' => '/tmp/mysql.sock', Apple has been changing the location of mysql.sock. include that line as the last part of the $default array – moto Jul 18 '14 at 15:09
  • Had to do this for XAMPP on Arch Linux as well. – Christian Feb 18 '15 at 17:18
22

An alternative to unix_socket (especially for OS X people) is to replace localhost with 127.0.0.1

Would be as Follows :

public $default = array(
                'datasource' => 'Database/Mysql',
                'persistent' => false,
                'host' => '127.0.0.1',
                'login' => 'user',
                'password' => 'password',
                'database' => 'database-name',
                'prefix' => '',
                'encoding' => 'utf8',
        );
enagra
  • 2,296
  • 1
  • 18
  • 19
13

Edit php.ini and add:

extension=php_pdo_mysql.dll 

Then restart your web server

Simon East
  • 55,742
  • 17
  • 139
  • 133
King Jk
  • 1,069
  • 14
  • 22
10

On Mac, using MAMP as a development platform, for cake the correct solution is using Domingo Casarrubio solution.

Add the unix_socket parameter to your database configurations.

'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',
Rashad
  • 11,057
  • 4
  • 45
  • 73
DecoMartins
  • 131
  • 1
  • 5
  • 1
    Could you explain why? – Victor Augusto Apr 14 '14 at 20:52
  • 1
    @VictorAugusto using 'localhost' as host in database.php will cause CakePHP to try to connect to a local socket file. [Related](http://stackoverflow.com/questions/366550/how-do-i-get-cakephp-bake-to-find-mysql-sock-and-recognize-mysql-while-using-mam) – Domingo C. Jun 15 '14 at 17:16
6

This error can also be caused if your connecting database user doesn't have the proper privileges. I believe you only need a minimum of INSERT, SELECT, UPDATE, and DELETE.

Always check username/password and the user privileges first since CakePHP will most likely give a vague database connection error for either.

Scott
  • 61
  • 2
  • 3
5

I had the same problem and found out eventually that it was caused by CakePhp not accepting that I used a user with a password, even if that user was created in PHPMyAdmin. I had to use the user 'root' with no password.

I found this out after making the following change to the file /lib/Cake/Error/exceptions.php.

The original line:

protected $_messageTemplate = 'Database connection "%s" is missing, or could not be created.';

is changed into this instead (note the change from single to double quotes):

protected $_messageTemplate = "Database connection \"%s\" is missing, or could not be created:\n    %s";

This will give you the reason for the problem so that you may change the cause properly.

CODE-REaD
  • 2,819
  • 3
  • 33
  • 60
user3834255
  • 51
  • 1
  • 1
  • Amazing tip. This will tell you the exact reason for this error. This should have more votes. – Simon East Mar 23 '17 at 23:33
  • @SimonEast I agree. Why don't they include that in the original message out of the box? Any time you get an error, you need to have as much relevant information as possible so you can properly fix it. If you're afraid of giving away too much to a hacker, then write it out to a log-file somewhere. – UncaAlby Mar 05 '18 at 18:53
5

I noticed that you've had asked this an year ago, and most probably would've solved this by now. However, for those facing the same issues when attempting to install CakePHP on XAMPP, all you have to do is change the 'login' to 'root', i.e. the default login of XAMPP, and leave the 'password' as '', i.e. blank. The complete code in your database.php file should look like this:

public $default = array(
    'datasource' => 'Database/Mysql',
    'persistent' => false,
    'host' => 'localhost',
    'login' => 'root',
    'password' => '',
    'database' => 'ckblog',//replace with your own database name
    'prefix' => '',
    //'encoding' => 'utf8',
);

That's it.

Brajinder Singh
  • 159
  • 3
  • 8
  • Thanks! it helped! I am using Uniform Server. – Sid Dec 09 '14 at 15:11
  • Going in circles trying to get my Bitnami to talk to my windows MySql database. Thanks! I also used my vendors folder for the lib source for cakephp. – mmv_sat Sep 04 '15 at 19:33
2

I have had this problem since upgrading to OSX Yosemite and inserting following line did the trick for me:

 'unix_socket' => '/tmp/mysql.sock'
paulleephp
  • 21
  • 3
2

It can be that mysql PDO support is missing.

as root (or using sudo):

apt-get install php5-mysql
Simon East
  • 55,742
  • 17
  • 139
  • 133
Mike Rose
  • 21
  • 1
1

Just to help Ubuntu users out: I had the same error in my ubuntu 13.10 machine with the newest xampp downlaoded directly from apachefriends. Tried most of the stuff in every post I could find about this error, but not the mac-specific stuff. In the end, the fix happened to be the same as the elected answer here:

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

FaustoW
  • 632
  • 7
  • 15
1

Because, cake bake use unix socket for connecting to database
so that you need add unix_socket for connection string.
You have to confirm location that store mysql.sock in WAS
Example: in my case i'm using xampp on MACOS 10.11
(edit file Config/database.php)

public $default = array(
  ‘datasource’ => ‘Database/Mysql’,
  ‘persistent’ => false,
  ‘host’ => ‘localhost’,
  ‘login’ => ‘root’,
  ‘password’ => ‘root’,
  ‘database’ => ‘cakephp’,
  ‘encoding’ => ‘utf8’,
  ‘unix_socket’ => ‘/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock’
);

Finally, It's work for me!

Quy Le
  • 2,354
  • 25
  • 18
0

What did it for me in the end was that I had created a table in my database, but there was no data in it.

In order for CakePHP to recognize the MySql connection, there has to be a table with data in it.

Ben Caine
  • 1,128
  • 3
  • 15
  • 25
0

You might need to create the table in your php file... Open up phpMyAdmin and check to ensure that they database CV exists.

Dragonman117
  • 478
  • 4
  • 13
0

It's your model. Open that up and there must be the following line

public $useDbConfig = 'local';

This overwrites global config & set it back to local

Tanvir Gaus
  • 205
  • 1
  • 3
  • 10
0

I tried splicing the code from Example 2 of http://php.net/manual/en/pdo.connections.php into /app/View/Pages/home.ctp. I had to fix the arguments the PDO constructor and change the name of the table in the query. The example 2 code returned the error "Error!: could not find driver". Based on King Jk's answer I was attempting to modify the php.ini when I started to wonder where a php_pdo_mysql.so might live. http://php.net/pdo_mysql showed how it was compiled as part of PHP via the --with-pdo-mysql option to configure. Recompiling fixed my problem. Note I'm working on a Ubuntu 12.10 system with PHP 5.5.9 and Apache Webserver 2.4.6

jocassid
  • 4,429
  • 1
  • 13
  • 6
0

In my case it was because the database didn't exist. I expected running ./app/Console/cake schema create would create it but it did not. Creating it with create database <database name> in mysql did the trick (although I had already assigned privileges).

Tyler Collier
  • 11,489
  • 9
  • 73
  • 80
0

I've been struggling with this the whole weekend and finally solved it. Turns out that the php.ini is pointing to a non-existing "extensions dir". Create a phpinfo() file and look at the value of this field: extensions_dir

I noticed that in the mamp php installed folder there is a no-debug-non-zts-20131226 folder, which is different from the value shown in the phpinfo(). What I did was to clone this folder and changed the name to the value of the phpinfo(). Probably you could modify the php.ini file but I didn't want to.

I don't know if you solved your problem, but I'm posting this because my problem was different and google took me here, so I hope to help future googlers having a similiar issue.

Hope this helps.

chuysbz
  • 1,262
  • 6
  • 18
  • 47
0

If you're on Godaddy (or any other shared hosting for that matter), they may be limiting outgoing connections to ports 80 and 443 only.

ᴍᴇʜᴏᴠ
  • 4,804
  • 4
  • 44
  • 57
0

System configuration:

  • Fedora 32
  • php-fpm 7.4.13
  • mariadb 10.4.17
  • CAKE 2.10.17

Error message from CAKE:

Database connection "Mysql" is missing, or could not be created.

Enhanced error message using answer at https://stackoverflow.com/a/24722976/5025060

Database connection "Mysql" is missing, or could not be created: Selected driver is not enabled

My problem was no "connector" between PHP and SQL was installed. The solution was:

dnf install php-mysqlnd

This allowed PHP to connect to the database as specified in CAKE's database.php configuration file.

CODE-REaD
  • 2,819
  • 3
  • 33
  • 60