CDbException: CDbConnection failed to open the DB connection.

thanks! finally :)

my MAMP config can be found here: http://stackoverflow.com/questions/4232084/phpunit-xdebug-codecoverage-not-working-in-console-mac-mamp-1-9-pro

protected/config/main.php


<?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'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',

	'name'=>'My Web Application',


	// preloading 'log' component

	'preload'=>array('log'),


	// autoloading model and component classes

	'import'=>array(

		'application.models.*',

		'application.components.*',

	),


        'modules'=>array(

                'gii'=>array(

                    'class'=>'system.gii.GiiModule', 'password'=>'1234qwer',

                ),

        ),

	// application components

	'components'=>array(

		'user'=>array(

			// enable cookie-based authentication

			'allowAutoLogin'=>true,

		),

               

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

		/*

		'urlManager'=>array(

			'urlFormat'=>'path',

			'rules'=>array(

				'<controller:\w+>/<id:\d+>'=>'<controller>/view',

				'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',

				'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',

			),

		),

		*/


                'db'=>array(

                        //'class'=>'CDbConnection',

			'connectionString' => 'mysql:host=localhost;dbname=yii_trackstar_dev;port=8889;',

			'emulatePrepare' => true,

			'username' => 'root',

			'password' => 'root',

			'charset' => 'utf8',

		),

            /*

		'db'=>array(

			'connectionString' => 'sqlite:'.dirname(__FILE__).'/../data/testdrive.db',

		),

              */

            

		// uncomment the following to use a MySQL database

		

		

                

                

            

		'errorHandler'=>array(

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

            'errorAction'=>'site/error',

        ),

		'log'=>array(

			'class'=>'CLogRouter',

			'routes'=>array(

				array(

					'class'=>'CFileLogRoute',

					'levels'=>'error, warning',

				),

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

				/*

				array(

					'class'=>'CWebLogRoute',

				),

				*/

			),

		),

	),


	// application-level parameters that can be accessed

	// using Yii::app()->params['paramName']

	'params'=>array(

		// this is used in contact page

		'adminEmail'=>'test@test.no',

	),

);

protected/config/test.php


<?php


return CMap::mergeArray(

	require(dirname(__FILE__).'/main.php'),

	array(

		'components'=>array(

			'fixture'=>array(

				'class'=>'system.test.CDbFixtureManager',

			),

                      

                         'db'=>array(

                            'connectionString' => 'mysql:host=localhost;dbname=yii_trackstar_dev;port=8889;',

                            'emulatePrepare' => true,

                            'username' => 'root',

                            'password' => 'root',

                            'charset' => 'utf8',

                        ),

		),

	)

);



DbTest.php


<?php

class DbTest extends CTestCase

{

    /*

     * Test if we have database driver active

     */

    public function testConnection()

    {

        

        $this->assertNotEquals(NULL, Yii::app()->db);

    }

}

Spent a good time to find a solution. Here it is:

‘connectionString’ => ‘mysql:host=localhost;dbname=trackstar;unix_socket=/Applications/MAMP/tmp/mysql/mysql.sock’

it will work (or change the mysql.sock to the path that you’re running)

Hello everyone,

you don’t need to specify the unix socket. Just use “localhost” instead of the IP as host, this will do the trick.

So the following line is absolutely enough to make it work:

‘connectionString’ => ‘mysql:host=localhost;dbname=trackstar_dev;’,

Explanation:

MAMP by default doesn’t allow TCP connections. You can either turn it on or use sockets. Using “localhost” is actually like using a socket.

(later I might be allowed to post a link here, for now just search it on stackoverflow)

Here a snippet from the mysql documentation

(Again, you have to search it yourself, sorry!)

"On Unix, MySQL programs treat the host name localhost specially, in a way that is likely different from what you expect compared to other network-based programs. For connections to localhost, MySQL programs attempt to connect to the local server by using a Unix socket file. This occurs even if a --port or -P option is given to specify a port number. To ensure that the client makes a TCP/IP connection to the local server, use --host or -h to specify a host name value of 127.0.0.1, or the IP address or name of the local server. You can also specify the connection protocol explicitly, even for localhost, by using the --protocol=TCP option."

Hope this helps,

Cheers,

Peter

I have the same problem… and I am running lampp under Ubuntu, and I get the same error , can someone fix this?

ryznar.wordpress.com/2010/06/ :rolleyes:

‘db’ => array(

        'connectionString' =&gt; [i][b]'mysql:dbname=yii_trackstar_dev;host=localhost; '[/b][/i], <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/smile.gif' class='bbc_emoticon' alt=':)' />  


         …

),

vim /usr/bin/phpunit

change the first line #!/usr/bin/php to your php path under xamp soft.

eg #!/usr/bin/php ==> #!/opt/lampp/bin/php

edit the php.ini file ,add correct include_path to your PHPUnit.

in my php.ini file,wo just add /usr/share/pear to my include path.

try yum install php-mysql on fedora

or apt-get install php-mysql

than change host=localhost to host=127.0.0.1

Try this. May work -

Edit your my.cnf file (mysql configuration file. Search within MAMP)

Find ‘skip-networking’. If non commented, comment it out. This will allow network access to mysql. Currently your mysql setup might only be allowing local access.

I had similar issues on MAMP. (1) You must use ‘localhost’, not ‘127.0.0.1’ – don’t know why; (2) make sure that your firewall (if enabled) isn’t blocking port 3306; and (3) I had to explicitly state the MySQL port (3306), see my code below:


	'db'=>array(

			'connectionString' => 'mysql:host=localhost;port=3306;dbname=trackstar_dev',

			'emulatePrepare' => true,

			'username' => 'root',

			'password' => 'root',

			'charset' => 'utf8',

		),

TTS

I got the same error, but with some hack I got it working ^^

First, remove the original php executable: rm /usr/bin/php

Then symlink the php of lampp to /usr/bin/php: ln -s /opt/lampp/bin/php /usr/bin/php

Then edit the lampp’s php.ini to add /usr/share/php to include_path.

That did the trick ^^

This did the trick for me. I didn’t know there are two php.ini config files under Ubuntu. I have previously configured the standard /etc/php5/apache2/php.ini. This is the one used by Apache to serve PHP script.

However, phpunit is happened to use /etc/php5/cli/php.ini, i.e. the php.ini for client applications. Well, it does make sense. However, I didn’t know this. So, I just copied configs that I added to the standard php.ini related to my pdo_cubrid and xdebug.so, and I scored a goal! Hope this helps to others, too!

I had the same problem in Fedora 15. I am using LAMPP and was struggling with the problem for more than a day. Your solution fixed my problem. For the benefit of everyone, here is my fix:

  1. Remove the default PHP executable: rm /usr/bin/php

  2. Add a symbolic link to LAMPP PHP installation and make that your default LINUX PHP installation: ln -s /opt/lampp/bin/php-5.3.5 /usr/bin/php

  3. Modify the /opt/lampp/etc/php.ini file: uncomment the include_path

; UNIX: "/path1:/path2"

include_path = ".:/usr/share/php:/usr/share/pear"

  1. Voila!

THANKS!!!!!

guys im having the same issue. CDbConnection failed to open db connection.

im using XAMPP on mac lion

  • created the trackstar_dev database using phpmyadmin()

  • i already configure my /protected/config/main.php

  • i can connect to mysql using just php mysql_connect() to check if mysql is working properly

is there something i missed?

I am using MAMP on Mac os x Lion.

I tried what Nguyen Thanh Hai suggested and it worked fine for me.

Thanks to Nguyen Thanh Hai.

Hi everyone,

I don’t have any problem in my Snow Leopard at home, everything works perfectly fine, but at work I use an ubuntu vm machine and I have the same problem: I’m able to connect through the yii application, but phpunit gives me the “CDbConnection failed to open db connection” error.

I have done every single suggestion, but none of them worked. Does anyone have another idea?

appreciate it

It turned out that it was due to the vm I was using. I got mad and decided to install the server in my windows workstation and it worked fine.

thanks

Thanks Alan!!

I have been stuck on this for a while and I found your solution worked!!

–> Chapter 4, DbTest failed!

I had the same problem and I changed "localhost" to "127.0.0.1" in the connection string and it worked.

I have observed same problem with unit tests :

The solution was to add the line below in php-cli.ini :


extension=php_pdo_mysql.dll

Hope this help

Hi.

I changed it and it works.

Thanks.