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

14,356

Solution 1

In your /app/Config/database.php file, change the following line:

'host' => 'localhost',

to:

'host' => '127.0.0.1',

Solution 2

In case you haven't solved this yet: I had the same error in my ubuntu 13.10 machine with the newest xampp downloaded directly from apachefriends. Try this:

Find your mysql.sock:

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

Solution 3

In database.php you need to create a variable array for test database like so:

public $test = array(
    'datasource' => 'Database/Mysql',
    'persistent' => false,
    'host' => 'localhost',
    'login' => 'root', // what ever you have as user 
    'password' => '', // what ever you have as pass
    'database' => 'test',
    'prefix' => '',
    'encoding' => 'utf8',
);

Then crucially make sure you have an empty database called:

test

That error message is quite deceiving should really say "Missing database test" :) well in my case this was the issue.

Share:
14,356
Admin
Author by

Admin

Updated on June 05, 2022

Comments

  • Admin
    Admin almost 2 years

    following the installation of CakePHP 2.0 (modified, I said that I get the same problem on a clean install of cakephp 2.0) I found myself facing an error I have not been able to solve.

    configuration:

    server : debian weezy 7

    user : test

    structure : /wwwjail/siteroot/test/htdocs/[app/...]

    nginx configuration

    cat /etc/nginx/sites-available/test

    server {
        listen IP:80 default;
        server_name domain.fr;
        access_log /var/log/nginx/test.access.log;
        error_log  /var/log/nginx/test.error.log;
    
        autoindex off;
        index index.php index.html;
        root /wwwjail/siteroot/test/htdocs/;
    
        location ~ \.php$ {
                            try_files $uri =404;
    
                            fastcgi_intercept_errors on;
                            fastcgi_pass unix:/wwwjail/siteroot/test/php-fpm.sock;
    
                            include /etc/nginx/fastcgi_params;
                            fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
        }
    }
    

    php5-fpm configuration

    cat /etc/php5/fpm/pool.d/test.conf

    [test]
    listen = /wwwjail/siteroot/test/php-fpm.sock
    chroot = /wwwjail/siteroot/test/htdocs
    
    user = test
    group = test
    
    listen.allowed_clients = 127.0.0.1
    listen.owner = test
    listen.group = test
    listen.backlog = -1
    pm = dynamic
    pm.max_children = 5
    pm.start_servers = 2
    pm.min_spare_servers = 1
    pm.max_spare_servers = 3
    pm.max_requests = 200
    pm.status_path = /status_31337
    request_terminate_timeout = 30s
    catch_workers_output = yes
    php_value[session.save_path] = /tmp
    

    Visible errors on the page:

    Database connection "Mysql" is missing, or could not be created.
    
    Error: An Internal Error Has Occurred.
    Stack Trace
    
        ROOTlibROOTCakeROOTModelROOTDatasourceROOTDboSource.php line 262 → Mysql->connect()
        ROOTlibROOTCakeROOTModelROOTConnectionManager.php line 107 → DboSource->__construct(array)
        ROOTlibROOTCakeROOTModelROOTModel.php line 3221 → ConnectionManager::getDataSource(string)
        ROOTlibROOTCakeROOTModelROOTModel.php line 1108 → Model->setDataSource(string)
        ROOTlibROOTCakeROOTModelROOTModel.php line 3243 → Model->setSource(string)
        ROOTlibROOTCakeROOTModelROOTModel.php line 2696 → Model->getDataSource()
        ROOTappROOTControllerROOTAppController.php line 70 → Model->find(string, array)
        ROOTappROOTControllerROOTChallengesController.php line 57 → AppController->beforeFilter()
        [internal function] → ChallengesController->beforeFilter(CakeEvent)
        ROOTlibROOTCakeROOTEventROOTCakeEventManager.php line 248 → call_user_func(array, CakeEvent)
        ROOTlibROOTCakeROOTControllerROOTController.php line 670 → CakeEventManager->dispatch(CakeEvent)
        ROOTlibROOTCakeROOTRoutingROOTDispatcher.php line 184 → Controller->startupProcess()
        ROOTlibROOTCakeROOTRoutingROOTDispatcher.php line 162 → Dispatcher->_invoke(ChallengesController, CakeRequest, CakeResponse)
        ROOTappROOTwebrootROOTindex.php line 109 → Dispatcher->dispatch(CakeRequest, CakeResponse)
        ROOTindex.php line 43 → require(string)
    
    CakePHP: the rapid development php framework
    

    Nginx Error log nginx :

    2013/12/24 12:23:03 [error] 17792#0: *535 open() "/wwwjail/siteroot/test/htdocs/css/default.css" failed (2: No such file or directory), client: myIP, server: domain.fr, request: "GET /css/default.css HTTP/1.1", host: "IP:80", referrer: "http://IP:80/"
    2013/12/24 12:23:03 [error] 17792#0: *536 open() "/wwwjail/siteroot/test/htdocs/css/bootstrap.css" failed (2: No such file or directory), client: myIP, server: domain.fr, request: "GET /css/bootstrap.css HTTP/1.1", host: "IP:80", referrer: "http://IP:80/"
    2013/12/24 12:23:03 [error] 17792#0: *537 open() "/wwwjail/siteroot/test/htdocs/css/jqplot.css" failed (2: No such file or directory), client: myIP, server: domain.fr, request: "GET /css/jqplot.css HTTP/1.1", host: "IP:80", referrer: "http://IP:80/"
    2013/12/24 12:23:03 [error] 17792#0: *538 open() "/wwwjail/siteroot/test/htdocs/js/jqplot.js" failed (2: No such file or directory), client: myIP, server: domain.fr, request: "GET /js/jqplot.js HTTP/1.1", host: "IP:80", referrer: "http://IP:80/"
    2013/12/24 12:23:03 [error] 17792#0: *534 open() "/wwwjail/siteroot/test/htdocs/js/holder.js" failed (2: No such file or directory), client: myIP, server: domain.fr, request: "GET /js/holder.js HTTP/1.1", host: "IP:80", referrer: "http://IP:80/"
    2013/12/24 12:23:03 [error] 17792#0: *531 open() "/wwwjail/siteroot/test/htdocs/css/cake.generic.css" failed (2: No such file or directory), client: myIP, server: domain.fr, request: "GET /css/cake.generic.css HTTP/1.1", host: "IP:80", referrer: "http://IP:80/"
    2013/12/24 12:23:03 [error] 17792#0: *531 open() "/wwwjail/siteroot/test/htdocs/img/cake.power.gif" failed (2: No such file or directory), client: myIP, server: domain.fr, request: "GET /img/cake.power.gif HTTP/1.1", host: "IP:80", referrer: "http://IP:80/"
    2013/12/24 12:23:03 [error] 17792#0: *535 open() "/wwwjail/siteroot/test/htdocs/js/jqplot.js" failed (2: No such file or directory), client: myIP, server: domain.fr, request: "GET /js/jqplot.js HTTP/1.1", host: "IP:80", referrer: "http://IP:80/"
    2013/12/24 12:23:03 [error] 17792#0: *531 open() "/wwwjail/siteroot/test/htdocs/js/holder.js" failed (2: No such file or directory), client: myIP, server: domain.fr, request: "GET /js/holder.js HTTP/1.1", host: "IP:80", referrer: "http://IP:80/"
    2013/12/24 12:23:03 [error] 17792#0: *531 open() "/wwwjail/siteroot/test/htdocs/css/cake.generic.css" failed (2: No such file or directory), client: myIP, server: domain.fr, request: "GET /css/cake.generic.css HTTP/1.1", host: "IP:80", referrer: "http://IP:80/"
    2013/12/24 12:23:03 [error] 17792#0: *531 open() "/wwwjail/siteroot/test/htdocs/img/cake.power.gif" failed (2: No such file or directory), client: myIP, server: domain.fr, request: "GET /img/cake.power.gif HTTP/1.1", host: "IP:80", referrer: "http://IP:80/"
    

    On the mysql database credentials are correct, the user mysql user-test exist and full access to test.*

    cat /wwwjail/siteroot/test/htdocs/app/Config/database.php

    class DATABASE_CONFIG {
    
        public $default = array(
            'datasource' => 'Database/Mysql',
            'persistent' => false,
            'host' => '127.0.0.1',
            'login' => 'user-test',
            'password' => 'mypassword',
            'database' => 'test',
            'prefix' => '',
            'encoding' => 'utf8'
        );
    }
    

    Checking the connection with the config file of CakePHP (script found on stackoverflow)

    <?php
    include("app/Config/database.php");
    $config= new DATABASE_CONFIG();
    
    $name = 'default';
    
    $settings=$config->{$name};
    $dsn = 'mysql:dbname='.$settings['database'].';host='.$settings['host'];
    $user = $settings['login'];
    $password = $settings['password'];
    
    try {
        $dbh = new PDO($dsn, $user, $password);
        echo "Connection succeeded with dsn: ". $dsn . "\n";
        $sql = 'SELECT id, title FROM posts';
        echo "Here is the contents of the table `posts:";
        foreach ($dbh->query($sql) as $row) {
            print $row['id'] . "\t" . $row['title'] . "\n";
        }
    } catch (PDOException $e) {
        echo 'PDO error: ' . $e->getMessage();
    }
    
    ?>
    

    ====> return: Connection succeeded with dsn: mysql:dbname=test;host=127.0.0.1 Here is the contents of the tableposts:`

    Checking socket with php.ini

    *grep -i 'default_socket' /etc/php5/fpm/php.ini*

    default_socket_timeout = 60
    pdo_mysql.default_socket= /wwwjail/var/run/mysqld/mysqld.sock
    mysql.default_socket = /wwwjail/var/run/mysqld/mysqld.sock 
    mysqli.default_socket = /wwwjail/var/run/mysqld/mysqld.sock
    

    Verification of the existence of the PDO extension.

    var_dump( extension_loaded('pdo_mysql') );
    

    return => bool(true)

    I hope I have provided much information about my problem. I am willing to perform various manipulations to test. (I think a problem server side configuration necessarily, although this configuration works for other basic applications connecting to mysql also).

    Sincerely.

  • Admin
    Admin over 10 years
    i've the same problem with localhost. But with the test-file (Checking the connection with the config file of CakePHP (script found on stackoverflow)), now I have no "Connection succeeded with dsn: mysql:dbname=test;host=127.0.0.1 Here is the contents of the tableposts:" but "PDO error: SQLSTATE[HY000] [2002] No such file or directory" ..
  • J.D.
    J.D. about 10 years
    nice 1. Do you know why this is happening?