PDO connect in console command or web app

Hello,

I use a Yii command to regularly load CSV files into the database.

From today, the command doesn’t work anymore. The error msg. is




PHP Error[2]: PDO::__construct(): [2002] Ein Verbindungsversuch ist fehlgeschlag

en, da die Gegenstelle na (trying to connect via tcp://HOSTNAME:3306)

	in file C:\Frameworks\yii\db\CDbConnection.php at line 405

#0 C:\Frameworks\yii\db\CDbConnection.php(405): PDO->__construct()

#1 C:\Frameworks\yii\db\CDbConnection.php(356): CDbConnection->createPdoInstance

()

#2 C:\Frameworks\yii\db\CDbConnection.php(313): CDbConnection->open()

#3 C:\Frameworks\yii\db\CDbConnection.php(291): CDbConnection->setActive()

#4 C:\Frameworks\yii\base\CModule.php(372): CDbConnection->init()

#5 C:\Frameworks\yii\base\CModule.php(86): CConsoleApplication->getComponent()

#6 C:\xampp\htdocs\project\protected\commands\LoadCommand.php(41): CConsol

eApplication->__get()

#7 C:\Frameworks\yii\console\CConsoleCommandRunner.php(63): LoadCommand->run()

#8 C:\Frameworks\yii\console\CConsoleApplication.php(88): CConsoleCommandRunner-

>run()

#9 C:\Frameworks\yii\base\CApplication.php(158): CConsoleApplication->processReq

uest()

#10 C:\Frameworks\yii\yiic.php(34): CConsoleApplication->run()

#11 C:\xampp\htdocs\project\protected\yiic.php(7): require_once()



Line 42 is:




echo "Connection String: ".Yii::app()->db->connectionString."\n";



I have tested the connection with some "raw" PDO commands, and this code works:




<?php

try {

	$dbh = new PDO('mysql:host=HOSTNAME;dbname=DBNAME', 'USERNAME','PASSWORD');

	foreach($dbh->query('SELECT * FROM tbl_user ORDER BY lastName, firstName') as $row) {

    	print_r($row);

	}

	$dbh = null;

} catch (PDOException $e) {

	print "Error!: " . $e->getMessage() . "<br/>";

	die();

}

?> 



What could be the difference between the Yii command db connection and the "raw" PDO connection?

Thanks in advance + regards,

Joachim

Try to change the hostname to the IP address… for example instead of "localhost" use "127.0.0.1"

But why does the raw PDO work with the real host name?

So… did it work with the IP ?

Not sure why it does not work… I just know that "some" problems can be solved by using the IP address…

It now works using the IP address.

However, this is still rather strange, as well as the web app running locally against the same MySQL server (remote) also works using the host name.

It’s very different if you access mysql locally or remotely… that could be the difference ;)

Check this link for more info - http://stackoverflow.com/questions/1819592/error-when-connecting-to-mysql-using-php-pdo