Cdbconnection Failed To Open The Db Connection.

Hello, I’m very new to yii framework. Following the blog tutorial of yii framework, i’m trying to install gii tool. [size=“5”]Actually i’m facing two major problems[/size]. [size=“5”]Firstly,[/size] When i try to access by:“http://localhost/yii/blog/index.php?r=gii”, i get the following error:

[size="5"][b]Error 500

CDbConnection failed to open the DB connection.[/b][/size]

I have enabled the gii and created the databse named "blog".

Here is my [size="5"]"/var/www/yii/demos/blog/protected/config/main.php[/size]" file:

<?php

// uncomment the following to define a path alias

// Yii::setPathOfAlias(‘local’,‘path/to/local-folder’);

// This is the main Web application configuration. Any writable

// CWebApplication properties can be configured here.

return array(

'basePath'=&gt;dirname(__FILE__).DIRECTORY_SEPARATOR.'..',


'name'=&gt;'Yii Blog Demo',





// preloading 'log' component


'preload'=&gt;array('log'),





// autoloading model and component classes


'import'=&gt;array(


	'application.models.*',


	'application.components.*',


),





'defaultController'=&gt;'post',





// application components


'components'=&gt;array(


	'user'=&gt;array(


		// enable cookie-based authentication


		'allowAutoLogin'=&gt;true,


	),


	'db'=&gt;array(


		'connectionString' =&gt; 'sqlite:protected/data/blog.db',


		'tablePrefix' =&gt; 'tbl_',


	),


	// uncomment the following to use a MySQL database


	


	'db'=&gt;array(


		'connectionString' =&gt; 'mysql:host=localhost;dbname=blog',


		'emulatePrepare' =&gt; true,


		'username' =&gt; 'root',


		'password' =&gt; '',


		'charset' =&gt; 'utf8',


		'tablePrefix' =&gt; 'tbl_',


	),


	


	'errorHandler'=&gt;array(


		// use 'site/error' action to display errors


		'errorAction'=&gt;'site/error',


	),


	'urlManager'=&gt;array(


		'urlFormat'=&gt;'path',


		'rules'=&gt;array(


			'post/&lt;id:&#092;d+&gt;/&lt;title:.*?&gt;'=&gt;'post/view',


			'posts/&lt;tag:.*?&gt;'=&gt;'post/index',


			'&lt;controller:&#092;w+&gt;/&lt;action:&#092;w+&gt;'=&gt;'&lt;controller&gt;/&lt;action&gt;',


		),


	),


	'log'=&gt;array(


		'class'=&gt;'CLogRouter',


		'routes'=&gt;array(


			array(


				'class'=&gt;'CFileLogRoute',


				'levels'=&gt;'error, warning',


			),


			// uncomment the following to show log messages on web pages


			/*


			array(


				'class'=&gt;'CWebLogRoute',


			),


			*/


		),


	),


),





// application-level parameters that can be accessed


// using Yii::app()-&gt;params['paramName']


'params'=&gt;require(dirname(__FILE__).'/params.php'),

);

[size=“5”]And i’m not sure where to create the databse, because simply i created that “blog” database in my phpmyadmin. I have not created any files in any of the folder in the directory for creating the databse. Should i create a php file to create a database? If so, Where to create it? i mean under which folder i should create the file?[/size]

[size="5"]Secondly,[/size]

Here is my [size="5"]"/var/www/yii/testdrive/protected/config/main.php"[/size] file:

<?php

// uncomment the following to define a path alias

// Yii::setPathOfAlias(‘local’,‘path/to/local-folder’);

// This is the main Web application configuration. Any writable

// CWebApplication properties can be configured here.

return array(

'basePath'=&gt;dirname(__FILE__).DIRECTORY_SEPARATOR.'..',


'name'=&gt;'My Web Application',





// preloading 'log' component


'preload'=&gt;array('log'),





// autoloading model and component classes


'import'=&gt;array(


	'application.models.*',


	'application.components.*',


),





'modules'=&gt;array(


	// uncomment the following to enable the Gii tool


	


	'gii'=&gt;array(


		'class'=&gt;'system.gii.GiiModule',


		'password'=&gt;'vediclabs',


		// If removed, Gii defaults to localhost only. Edit carefully to taste.


		'ipFilters'=&gt;array('127.0.0.1','::1'),


	),


	


),





// application components


'components'=&gt;array(


	'user'=&gt;array(


		// enable cookie-based authentication


		'allowAutoLogin'=&gt;true,


	),


	// uncomment the following to enable URLs in path-format


	


	'urlManager'=&gt;array(


		'urlFormat'=&gt;'path',


		'rules'=&gt;array(


			'&lt;controller:&#092;w+&gt;/&lt;id:&#092;d+&gt;'=&gt;'&lt;controller&gt;/view',


			'&lt;controller:&#092;w+&gt;/&lt;action:&#092;w+&gt;/&lt;id:&#092;d+&gt;'=&gt;'&lt;controller&gt;/&lt;action&gt;',


			'&lt;controller:&#092;w+&gt;/&lt;action:&#092;w+&gt;'=&gt;'&lt;controller&gt;/&lt;action&gt;',


		),


	),


	


	'db'=&gt;array(


		'connectionString' =&gt; 'sqlite:'.dirname(__FILE__).'/&#46;&#46;/data/testdrive.db',


	),


	// uncomment the following to use a MySQL database


	


	'db'=&gt;array(


		'connectionString' =&gt; 'mysql:host=localhost;dbname=testdrive',


		'emulatePrepare' =&gt; true,


		'username' =&gt; 'root',


		'password' =&gt; '',


		'charset' =&gt; 'utf8',


	),


	


	'errorHandler'=&gt;array(


		// use 'site/error' action to display errors


		'errorAction'=&gt;'site/error',


	),


	'log'=&gt;array(


		'class'=&gt;'CLogRouter',


		'routes'=&gt;array(


			array(


				'class'=&gt;'CFileLogRoute',


				'levels'=&gt;'error, warning',


			),


			// uncomment the following to show log messages on web pages


			/*


			array(


				'class'=&gt;'CWebLogRoute',


			),


			*/


		),


	),


),





// application-level parameters that can be accessed


// using Yii::app()-&gt;params['paramName']


'params'=&gt;array(


	// this is used in contact page


	'adminEmail'=&gt;'webmaster@example.com',


),

);

[size=“5”]And i’m not sure where to create the databse, because simply i created that “testdrive” database in my phpmyadmin. I have not created any files in any of the folder in the directory for creating the databse. Should i create a php file to create a database? If so, Where to create it? i mean under which folder i should create the file?[/size]

And When i try to access gii tool by [size="5"][b]"http://localhost/yii/framework/gii/views/default/", its not taking me to login screen. In otherwords, its not asking me to enter password. Directly it shows:

Welcome to Yii Code Generator!

You may use the following generators to quickly build up your Yii application:

And its not showing any generators below…[/b][/size]

I’m using Mysql database server…

Please help me to solve both the issues…

Here you are using mySql as your server right? then no need to enable sqlite credentials from the array. Comment the following from your [color="#1C2837"][size="2"]"/var/www/yii/demos/blog/protected/config/main.php"[/size][size="2"] file [/size][/color]




// 'db' => array(

	//'connectionString' => 'sqlite:protected/data/blog.db',

	//'tablePrefix' => 'tbl_',

	//),




[size="2"][color="#1c2837"]Now it will work!..[/color][/size]

[size="2"] [/size]

[size="2"][color="#1c2837"]No need to create any files if you are using mysql as your server. If you use sqlite database then you need to create a file namely YOUR_DB_NAME.db [/color][/size]

[size="2"][color="#1c2837"][b][i]

[/i][/b][/color][/size]

[size="2"][color="#1c2837"]Cheers![/color][/size]

Also, when you post configuration files, remember to remove the passwords. Make that a good habit. Even if your database is not reachable from the internet right now, it could be in the future. Just never post passwords.

Thanks a lot…

Hi, yeah i’m using mysql server. I have commented the sqlite credentials from the array. But again the same issue. Its not redirecting to login screen for gii tool and not showing any generators…and when i try:

"http://localhost/yii/blog/index.php?r=gii",

i get the following error:

Error 500

CDbConnection failed to open the DB connection. Even after commenting the sqlite credentials from the array, Again the same issue which is in my previous post…

Do you login to your phpMysqlAdmin using same credentials you put in your config/main.php?

Does the error message contains any details? Maybe you’re trying to connect using an unix socket and it’s default location is invalid in PHP.

Yes i logged in into my phpmyadmin using the same credentials as i put in my config/main.php file.

No, it does not contain any details… Just it shows:

Error 500

CDbConnection failed to open the DB connection.when i try to access…

See i always connect to phpmyadmin using the same username and password which i have put in config/main.php file. And now i have added the following lines in "/var/www/yii/demos/blog/protected/data/schema.mysql.sql"

<?php

mysqli_connect("localhost","root","","blog");

if(mysqli_connect_errno())

{

echo &quot;Failed to connect to Mysql:&quot; . mysqli_connect_error();

}

?>

but no use. Again the same problem…

If you would enable debug mode by defining a YII_DEBUG const to true you would see a full stacktrace along with the error message. Anyway, this should be logged in protected/runtime/application.log.

The file protected/data/schema.mysql.sql is not used.

Save it as test.php in your app root dir and open it via browser. If it displays a similar error you probably have a unix socket issue I mentioned earlier.