CDbException: CDbConnection failed to open the DB connection.

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.