Home » Php » php – CakePHP Database connection "Mysql" is missing, or could not be created

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

Posted by: admin April 23, 2020 Leave a comment

Questions:

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?

How to&Answers:

Try adding the socket:

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

Answer:

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

Answer:

Edit php.ini and add:

extension=php_pdo_mysql.dll 

Then restart your web server

Answer:

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

Answer:

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.

Answer:

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.

Answer:

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:

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.

Answer:

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

 'unix_socket' => '/tmp/mysql.sock'

Answer:

It can be that mysql PDO support is missing.

as root (or using sudo):

apt-get install php5-mysql

Answer:

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:

[email protected] /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.“.

Answer:

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!

Answer:

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.

Answer:

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

Answer:

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

Answer:

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

Answer:

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

Answer:

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.

Answer:

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.