CakePHP Database connection "Mysql" is missing, or could not be created
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.
Ben Caine
Updated on December 11, 2020Comments
-
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 passwordmypass
and created a database calledCV
under that. Moreover, I can't find mention of atest
database anywhere. Help? -
Ondrej Rafaj over 10 yearsJust 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. over 10 yearsThats correct, I supposed to many things from reading the question. :)
-
Victor Augusto about 10 yearsCould you explain why?
-
adamup about 10 yearsAdding this allowed my Cron job to execute on a Mac running XAMPP. Thanks!
-
khany almost 10 yearsFYI this worked on my XAMPP installation too except I had to put /Applications/XAMPP/xamppfiles/var/mysql/mysql.sock
-
AD7six almost 10 yearsThat is simply not true. -1.
-
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 almost 10 yearsNot 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 over 9 yearsThis 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 over 9 yearsThanks.. this is work for me. i am using cakePHP 2.5.4, IIS 8,PHP 5.4.24
-
matsjoyce over 9 yearsWhich is roughly what the top two answers say.
-
Sid over 9 yearsThanks! it helped! I am using Uniform Server.
-
Christian about 9 yearsHad to do this for XAMPP on Arch Linux as well.
-
mmv_sat over 8 yearsGoing 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 over 7 yearsThis has worked for me too with an old Cake 2.6.1 project with PHP 5.5.9.
-
Simon East about 7 yearsBe careful. Unless you know exactly what this line is doing, this may cause issues for you later.
-
Simon East about 7 yearsAmazing tip. This will tell you the exact reason for this error. This should have more votes.
-
UncaAlby about 6 yearsUnfortunately this is irrelevant if your database is hosted externally. :-(
-
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 about 6 years@UncaAlby, Obviously. What can we do! :-D The question concerned a local environment...