Yii Framework Forum: CDbException: CDbConnection failed to open the DB connection. - Yii Framework Forum

Jump to content

  • (2 Pages)
  • +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

CDbException: CDbConnection failed to open the DB connection. Unit tests cannot reach the db connection? Rate Topic: **--- 1 Votes

#21 User is offline   orca.clock 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 13-April 11

Posted 13 April 2011 - 09:31 PM

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

#22 User is offline   orca.clock 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 13-April 11

Posted 14 April 2011 - 01:02 AM

try yum install php-mysql on fedora
or apt-get install php-mysql

than change host=localhost to host=127.0.0.1


View Postortenheim, on 29 August 2010 - 07:29 AM, said:

Hi i have installed yii and i am doing the Yii book "Agile with Yii 1.1 and PHP5: The TrackStar Application" and setting up the phpunit and selenium tests.
I manage to setup a database and get and set data throught the models however when trying to test the database it doesnt work?!

DbTest.php

<?php
class DbTest extends CTestCase
{
    public function testConnection()
    {

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


this gives me:
DbTest::testConnection()
CDbException: CDbConnection failed to open the DB connection.
...../framework/db/CDbConnection.php:275
...../framework/db/CDbConnection.php:242
...../framework/db/CDbConnection.php:221
...../framework/base/CModule.php:363
...../framework/base/CModule.php:86
...../YII/trackstar/protected/tests/unit/DbTest.php:10


i have also setup the bootstrap and the xml configuration to use the relevant files in the trackstar application.

this is my main.php config
/*

		'db'=>array(
			'connectionString' => 'sqlite:'.dirname(__FILE__).'/../data/testdrive.db',
		),
 */
            
		// uncomment the following to use a MySQL database
		
		'db'=>array(
                        //'class'=>'CDbConnection',
			'connectionString' => 'mysql:host=localhost;dbname=yii_trackstar_dev',
			'emulatePrepare' => true,
			'username' => 'root',
			'password' => 'root',
			'charset' => 'utf8',
		),


note that i have disabled the sqlite. and also note that if i desable mysql and use sqlite the test runs with success...this is making me crazy since the application is clearly working with CRUD its only the tests that are failing....

please help me with this.

0

#23 User is offline   Mukesh 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 107
  • Joined: 17-May 11
  • Location:India

Posted 17 May 2011 - 11:51 AM

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

#24 User is offline   Trenton T. Scott 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 12
  • Joined: 19-May 11

Posted 21 May 2011 - 06:25 PM

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
0

#25 User is offline   Nguyen Thanh Hai 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 27-May 11

Posted 23 July 2011 - 03:22 AM

View PostMythriel, on 01 March 2011 - 04:43 PM, said:

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

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 ^^
0

#26 User is offline   kadishmal 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 44
  • Joined: 09-February 11
  • Location:South Korea

Posted 23 August 2011 - 01:43 AM

View Postmdomba, on 13 September 2010 - 05:22 AM, said:

One difference is that phpunit is run from command line, have you checked the PHP configuration for CLI ?

Run php --ini (from command line) to get the location of the configuration file, and take a look there if all needed extensions are enabled


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!
0

#27 User is offline   Vivek A 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 04-September 11

Posted 04 September 2011 - 04:05 AM

View PostNguyen Thanh Hai, on 23 July 2011 - 03:22 AM, said:

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 ^^

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"
4. Voila!

THANKS!!!!!
0

#28 User is offline   buknoybutikoy 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 09-January 12

Posted 10 January 2012 - 12:14 AM

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?
0

#29 User is offline   macdemac 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 14-December 11

Posted 12 January 2012 - 11:03 PM

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

#30 User is offline   Rafael Lima 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 18
  • Joined: 30-January 12

Posted 30 January 2012 - 11:25 AM

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
0

#31 User is offline   Rafael Lima 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 18
  • Joined: 30-January 12

Posted 01 February 2012 - 12:34 PM

View PostRafael Lima, on 30 January 2012 - 11:25 AM, said:

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
0

#32 User is offline   Jimi 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 23-April 12

Posted 25 April 2012 - 12:13 PM

View PostAlan Cheung, on 22 December 2010 - 05:26 AM, said:

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)


Thanks Alan!!

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

--> Chapter 4, DbTest failed!
0

#33 User is offline   JohnCardozo 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 19-July 12

Posted 19 July 2012 - 05:38 PM

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

#34 User is offline   stormscratcher 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 22-December 12

Posted 29 December 2012 - 11:07 AM

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
0

Share this topic:


  • (2 Pages)
  • +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users