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

71,333

Solution 1

Try adding the socket:

'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',

Solution 2

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',
        );

Solution 3

Edit php.ini and add:

extension=php_pdo_mysql.dll 

Then restart your web server

Solution 4

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

Solution 5

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.

Share:
71,333
Ben Caine
Author by

Ben Caine

Updated on December 11, 2020

Comments

  • Ben Caine
    Ben Caine over 3 years

    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?

  • Ondrej Rafaj
    Ondrej Rafaj over 10 years
    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
  • Domingo C.
    Domingo C. over 10 years
    Thats correct, I supposed to many things from reading the question. :)
  • Victor Augusto
    Victor Augusto about 10 years
    Could you explain why?
  • adamup
    adamup about 10 years
    Adding this allowed my Cron job to execute on a Mac running XAMPP. Thanks!
  • khany
    khany almost 10 years
    FYI this worked on my XAMPP installation too except I had to put /Applications/XAMPP/xamppfiles/var/mysql/mysql.sock
  • AD7six
    AD7six almost 10 years
    That is simply not true. -1.
  • Domingo C.
    Domingo C. almost 10 years
    @VictorAugusto using 'localhost' as host in database.php will cause CakePHP to try to connect to a local socket file. Related
  • moto
    moto almost 10 years
    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
  • Michael Ozeryansky
    Michael Ozeryansky over 9 years
    This fixed my issue. CakePHP had the right credentials but I the database did not exist. If you look through Cake's source you'll see there isn't a check in case the Database doesn't exist.
  • riad
    riad over 9 years
    Thanks.. this is work for me. i am using cakePHP 2.5.4, IIS 8,PHP 5.4.24
  • matsjoyce
    matsjoyce over 9 years
    Which is roughly what the top two answers say.
  • Sid
    Sid over 9 years
    Thanks! it helped! I am using Uniform Server.
  • Christian
    Christian about 9 years
    Had to do this for XAMPP on Arch Linux as well.
  • mmv_sat
    mmv_sat over 8 years
    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.
  • adriaroca
    adriaroca over 7 years
    This has worked for me too with an old Cake 2.6.1 project with PHP 5.5.9.
  • Simon East
    Simon East about 7 years
    Be careful. Unless you know exactly what this line is doing, this may cause issues for you later.
  • Simon East
    Simon East about 7 years
    Amazing tip. This will tell you the exact reason for this error. This should have more votes.
  • UncaAlby
    UncaAlby about 6 years
    Unfortunately this is irrelevant if your database is hosted externally. :-(
  • UncaAlby
    UncaAlby about 6 years
    @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.
  • enagra
    enagra about 6 years
    @UncaAlby, Obviously. What can we do! :-D The question concerned a local environment...