tonydspaniard / yiinitializr-basic

Yii Framework project structure boilerplate for basic requirements
26 stars 16 forks source link

Got Error "CDbConnection failed to open the DB connection." #8

Open bamburillo opened 11 years ago

bamburillo commented 11 years ago

First of all, thanks for your great template which help a lot.

I met some error so reproduce step are:

  1. run command "php composer.phar self-update"
  2. run command "php composer.phar install" And after all this, I got this:

D:\OpenServer\domains\basic>php composer.phar install

Yiinitialzr 1.0.1

Start Installation? [y/n]: y
Loading composer repositories with package information
Installing dependencies

Please, enter your environment -ie. "dev | prod | stage": [dev]: dev

Building environment files.
Your environment configuration file has been created on D:\OpenServer\domains\basic\app\lib\Yiinitializr\config/../../../config .

'D:\OpenServer\domains\basic\app\lib\Yiinitializr\config/../../../config/env.php'
file already exists. No action has been executed.
'D:\OpenServer\domains\basic\app\lib\Yiinitializr\config/../../../config/env.php'
file already exists. No action has been executed.
Environment files creation process finished.

Building runtime 'runtime' folders.
Your runtime folder has been created on D:\OpenServer\domains\basic\app\lib\Yiinitializr\config/../../...
Runtime 'runtime' folders creation process finished.

Building runtime 'assets' folders.
Your assets folder has been created on D:\OpenServer\domains\basic\app\lib\Yiinitializr\config/../../../../www.
Runtime 'assets' folders creation process finished.
Script Yiinitializr\Composer\Callback::postPackageInstall handling the post-package-install event terminated with an exception

[CDbException]
CDbConnection failed to open the DB connection.

Do I miss something?

tonydspaniard commented 11 years ago

'D:\OpenServer\domains\basic\app\lib\Yiinitializr\config/../../../config/env.php'

file already exists. No action has been executed.

Remove your previous files installations or configure env.php database settings

bamburillo commented 11 years ago

My env.php in D:\OpenServer\domains\basic\app\config\

<?php
/**
 *
 * @author Antonio Ramirez <amigo.cobos@gmail.com>
 * @link http://www.ramirezcobos.com/
 * @link http://www.2amigos.us/
 * @copyright 2013 2amigOS! Consultation Group LLC
 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
 */
return array(
    'modules' => array(
        'gii' => array(
            'class' => 'system.gii.GiiModule',
            'password' => 'yii',
            'ipFilters' => array('127.0.0.1','::1'),
        ),
    ),
    'components' => array(
        'db' => array(
            'connectionString' => 'mysql:host=localhost;dbname=basic',
            'username' => 'root',
            'password' => '',
            'enableProfiling' => true,
            'enableParamLogging' => true,
            'charset' => 'utf8',
        ),
    ),
    'params' => array(
        'yii.handleErrors'   => true,
        'yii.debug' => true,
        'yii.traceLevel' => 3,
    )
);

Database "basic" in local MySQL exist.

And i still have error [CDbException]

CDbConnection failed to open the DB connection.

tonydspaniard commented 11 years ago

Whats in your other config files. Can I see? Enviado desde mi BlackBerry® de Vodafone

-----Original Message----- From: Vladimir Jucov notifications@github.com Date: Fri, 31 May 2013 01:18:52 To: tonydspaniard/yiinitializr-basicyiinitializr-basic@noreply.github.com Reply-To: tonydspaniard/yiinitializr-basic reply@reply.github.com Cc: Antonio Ramirezantonio@ramirezcobos.com Subject: Re: [yiinitializr-basic] Got Error "CDbConnection failed to open the DB connection." (#8)

My env.php in z:\openserver\domains\basic\app\config\

<?php
/**
 *
 * @author Antonio Ramirez <amigo.cobos@gmail.com>
 * @link http://www.ramirezcobos.com/
 * @link http://www.2amigos.us/
 * @copyright 2013 2amigOS! Consultation Group LLC
 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
 */
return array(
    'modules' => array(
        'gii' => array(
            'class' => 'system.gii.GiiModule',
            'password' => 'yii',
            'ipFilters' => array('127.0.0.1','::1'),
        ),
    ),
    'components' => array(
        'db' => array(
            'connectionString' => 'mysql:host=localhost;dbname=basic',
            'username' => 'root',
            'password' => '',
            'enableProfiling' => true,
            'enableParamLogging' => true,
            'charset' => 'utf8',
        ),
    ),
    'params' => array(
        'yii.handleErrors'   => true,
        'yii.debug' => true,
        'yii.traceLevel' => 3,
    )
);

And i still have error [CDbException]

CDbConnection failed to open the DB connection.


Reply to this email directly or view it on GitHub: https://github.com/tonydspaniard/yiinitializr-basic/issues/8#issuecomment-18731217

bamburillo commented 11 years ago

common.php

<?php
/**
 *
 * common.php configuration file
 *
 * @author Antonio Ramirez <amigo.cobos@gmail.com>
 * @link http://www.ramirezcobos.com/
 * @link http://www.2amigos.us/
 * @copyright 2013 2amigOS! Consultation Group LLC
 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
 */
return array(
    'basePath' => realPath(__DIR__ . '/..'),
    'preload' => array('log'),
    'aliases' => array(
        'vendor' => 'application.vendor'
    ),
    'import' => array(
        'application.controllers.*',
        'application.extensions.components.*',
        'application.extensions.behaviors.*',
        'application.helpers.*',
        'application.models.*',
        'vendor.2amigos.yiistrap.helpers.*',
        'vendor.2amigos.yiiwheels.helpers.*',
    ),
    'components' => array(
//      'db'=>array(
//          'connectionString' => 'sqlite:'.dirname(__FILE__).'/../data/testdrive.db',
//      ),
        'errorHandler' => array(
            'errorAction' => 'site/error',
        ),
        'log' => array(
            'class'  => 'CLogRouter',
            'routes' => array(
                array(
                    'class'        => 'CFileLogRoute',
                    'levels'       => 'error, warning',
                ),
            ),
        ),
    ),
    'params' => array(

        // php configuration
        'php.defaultCharset' => 'utf-8',
        'php.timezone'       => 'UTC',
    )
);

main.php

<?php
/**
 *
 * main.php configuration file
 *
 * @author Antonio Ramirez <amigo.cobos@gmail.com>
 * @link http://www.ramirezcobos.com/
 * @link http://www.2amigos.us/
 * @copyright 2013 2amigOS! Consultation Group LLC
 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
 */
defined('APP_CONFIG_NAME') or define('APP_CONFIG_NAME', 'main');

use Yiinitializr\Helpers\ArrayX;

// web application configuration
return ArrayX::merge(array(
    'name' => '{APPLICATION NAME}',

    // path aliases
    'aliases' => array(
        'bootstrap' => dirname(__FILE__) . '/../lib/vendor/2amigos/yiistrap',
        'yiiwheels' => dirname(__FILE__) . '/../lib/vendor/2amigos/yiiwheels',
    ),

    // application behaviors
    'behaviors' => array(),

    // controllers mappings
    'controllerMap' => array(),

    // application modules
    'modules' => array(),

    // application components
    'components' => array(

        'bootstrap' => array(
            'class' => 'bootstrap.components.TbApi',
        ),

        'clientScript' => array(
            'scriptMap' => array(
                'bootstrap.min.css' => false,
                'bootstrap.min.js' => false,
                'bootstrap-yii.css' => false
            )
        ),
        'urlManager' => array(
            // uncomment the following if you have enabled Apache's Rewrite module.
            'urlFormat' => 'path',
            'showScriptName' => false,

            'rules' => array(
                // default rules
                '<controller:\w+>/<id:\d+>' => '<controller>/view',
                '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
                '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
            ),
        ),
        'user' => array(
            'allowAutoLogin' => true,
        ),
        'errorHandler' => array(
            'errorAction' => 'site/error',
        )
    ),
    // application parameters
    'params' => array(),
), require_once('common.php'));

even if uncomment the db in common.php - the error remains.

bamburillo commented 11 years ago

if I just take all the default configuration, and doing composer install , I get this error.

tonydspaniard commented 11 years ago

Whats on your console? Did you setup your console properly? When you run composer install after installation it runs the composer callback hook, calling the console, thus its configuration.

Make sure it returns the correct configurations: https://github.com/tonydspaniard/yiinitializr-basic/blob/master/app/lib/Yiinitializr/config/console.php

bamburillo commented 11 years ago
<?php
/**
 *
 * console.php configuration file
 *
 * @author Antonio Ramirez <amigo.cobos@gmail.com>
 * @link http://www.ramirezcobos.com/
 * @link http://www.2amigos.us/
 * @copyright 2013 2amigOS! Consultation Group LLC
 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
 */
defined('APP_CONFIG_NAME') or define('APP_CONFIG_NAME', 'console');
return array(
    'commandMap' => array(
        'migrate' => array(
            'class' => 'system.cli.commands.MigrateCommand',
            'migrationPath' => 'application.cli.migrations'
        )
    )
);

This is var_dump() of configuration array appending composer callback methods

array(8) {
  ["commandMap"]=>
  array(1) {
    ["migrate"]=>
    array(2) {
      ["class"]=>
      string(34) "system.cli.commands.MigrateCommand"
      ["migrationPath"]=>
      string(26) "application.cli.migrations"
    }
  }
  ["basePath"]=>
  string(31) "Z:\openserver\domains\basic\app"
  ["preload"]=>
  array(1) {
    [0]=>
    string(3) "log"
  }
  ["aliases"]=>
  array(1) {
    ["vendor"]=>
    string(18) "application.vendor"
  }
  ["import"]=>
  array(7) {
    [0]=>
    string(25) "application.controllers.*"
    [1]=>
    string(35) "application.extensions.components.*"
    [2]=>
    string(34) "application.extensions.behaviors.*"
    [3]=>
    string(21) "application.helpers.*"
    [4]=>
    string(20) "application.models.*"
    [5]=>
    string(33) "vendor.2amigos.yiistrap.helpers.*"
    [6]=>
    string(34) "vendor.2amigos.yiiwheels.helpers.*"
  }
  ["components"]=>
  array(3) {
    ["errorHandler"]=>
    array(1) {
      ["errorAction"]=>
      string(10) "site/error"
    }
    ["log"]=>
    array(2) {
      ["class"]=>
      string(10) "CLogRouter"
      ["routes"]=>
      array(1) {
        [0]=>
        array(2) {
          ["class"]=>
          string(13) "CFileLogRoute"
          ["levels"]=>
          string(14) "error, warning"
        }
      }
    }
    ["db"]=>
    array(6) {
      ["connectionString"]=>
      string(33) "mysql:host=localhost;dbname=basic"
      ["username"]=>
      string(4) "root"
      ["password"]=>
      string(0) ""
      ["enableProfiling"]=>
      bool(true)
      ["enableParamLogging"]=>
      bool(true)
      ["charset"]=>
      string(4) "utf8"
    }
  }
  ["params"]=>
  array(6) {
    ["php.defaultCharset"]=>
    string(5) "utf-8"
    ["php.timezone"]=>
    string(3) "UTC"
    ["yii.handleErrors"]=>
    bool(true)
    ["yii.debug"]=>
    bool(true)
    ["yii.traceLevel"]=>
    int(3)
    ["composer.callbacks"]=>
    array(2) {
      ["post-update"]=>
      array(2) {
        [0]=>
        string(4) "yiic"
        [1]=>
        string(7) "migrate"
      }
      ["post-install"]=>
      array(2) {
        [0]=>
        string(4) "yiic"
        [1]=>
        string(7) "migrate"
      }
    }
  }
  ["modules"]=>
  array(1) {
    ["gii"]=>
    array(3) {
      ["class"]=>
      string(20) "system.gii.GiiModule"
      ["password"]=>
      string(3) "yii"
      ["ipFilters"]=>
      array(2) {
        [0]=>
        string(9) "127.0.0.1"
        [1]=>
        string(3) "::1"
      }
    }
  }
}
belkaaa commented 11 years ago

On "yiinitializr-basic" is the same problem as in "yiinitializr-intermediate"...

tonydspaniard commented 10 years ago

@bamburillo

But your database connection is set... I really do not understand how it doesn't work. We tested and work with all project templates without issues.

["db"]=>
    array(6) {
      ["connectionString"]=>
      string(33) "mysql:host=localhost;dbname=basic"
      ["username"]=>
      string(4) "root"
      ["password"]=>
      string(0) ""
      ["enableProfiling"]=>
      bool(true)
      ["enableParamLogging"]=>
      bool(true)
      ["charset"]=>
      string(4) "utf8"
    }
nicolasbotanic commented 10 years ago

Same here... I found that it was a driver issue, I just forgot to install php5-mysql in my debian box. But error message were not helpful because of this:

catch(PDOException $e) { if(YII_DEBUG) { throw new CDbException('CDbConnection failed to open the DB connection: '. $e->getMessage(),(int)$e->getCode(),$e->errorInfo); } else { Yii::log($e->getMessage(),CLogger::LEVEL_ERROR,'exception.CDbException'); throw new CDbException('CDbConnection failed to open the DB connection.',(int)$e->getCode(),$e->errorInfo); } }

... YII_DEBUG was false when I was running php composer.phar install.. so I couldn't see $e->getMessage() response and I don't know where is sent the message through Yii::log()

DenisOgr commented 10 years ago

I have this problem too... How it fix? I download basic from http://yiinitializr.2amigos.us/ I unzip in my root folder. Use denis@denis-linux:/home/www-data/www/yiin1$ php composer.phar self-update Updating to version a2a70380c14a20b3f611d849eae7342f2e35c763. Downloading: 100%
denis@denis-linux:/home/www-data/www/yiin1$ php composer.phar install PHP Warning: Module 'memcache' already loaded in Unknown on line 0 Yiinitialzr 1.0.1

Start Installation? [y/n]: y Start Installation? [y/n]: y Loading composer repositories with package information Installing dependencies (including require-dev)

Please, enter your environment -ie. "dev | prod | stage": [dev]:

Building environment files. Your environment configuration file has been created on /home/www-data/www/yiin1/app/lib/Yiinitializr/config/../../../config.

'/home/www-data/www/yiin1/app/lib/Yiinitializr/config/../../../config/env.php' file already exists. No action has been executed. '/home/www-data/www/yiin1/app/lib/Yiinitializr/config/../../../config/env.php' file already exists. No action has been executed. Environment files creation process finished.

Building runtime 'runtime' folders. Your runtime folder has been created on /home/www-data/www/yiin1/app/lib/Yiinitializr/config/../../... Runtime 'runtime' folders creation process finished.

Building runtime 'assets' folders. Your assets folder has been created on /home/www-data/www/yiin1/app/lib/Yiinitializr/config/../../../../www. Runtime 'assets' folders creation process finished.

Writing lock file Generating autoload files

Yii Migration Tool v1.0 (based on Yii v1.1.14) Script Yiinitializr\Composer\Callback::postInstall handling the post-install-cmd event terminated with an exception

[CDbException]
CDbConnection failed to open the DB connection.

install [--prefer-source] [--prefer-dist] [--dry-run] [--dev] [--no-dev] [--no-plugins] [--no-custom-installers] [--no-scripts] [--no-progress] [-v|vv|vvv|--verbose] [-o|--optimize-autoloader]

denis@denis-linux:/home/www-data/www/yiin1$

Avtonom commented 10 years ago

+1

YII_DEBUG: /console/config/env/prod.php

'params' => array( 'yii.handleErrors' => true, 'yii.debug' => true, 'yii.traceLevel' => 3, // 'yii.debug' => false, // 'yii.traceLevel' => 0, // 'yii.handleErrors' => APP_CONFIG_NAME !== 'test', )

but it did not help ((((

tonydspaniard commented 10 years ago

@DenisOgr before you run the installation script you need to setup the connection to the database for console app (check where to include that configuration)

khunemz commented 6 years ago

poor framework

rapttor commented 6 years ago

I got this problem with Yii 1.1.x project and almost got crazy:

First make sure you have

    sudo apt install php-xml php-mbstring php-pdo php-mysql 

installed, and restart apache

    sudo apachectl restart
    or: 
    sudo service apache2 restart

Detailed error was that database library cannot be found.

My solution was related to caching:

    cache' => array(
        'class' => 'system.caching.CDbCache',
        //'class' => 'system.caching.CFileCache',
        'connectionID'=>'db', // <<< THIS IS THE ISSUE
    ),

if connectionID is not set, db caching defaults to mysqli database in /protected/data directory which cannot be accessed if mysqli driver is not installed on system (common issue with dedicated servers, DO droplets, xampp/wamp...)

or, you can disable db caching and enable fileCache instead.