MySQL connection error generating model

Hi all,

Going through “Creating first yii application” I’m running into this problem when I’m trying to generate the User model using the yiic tool:

>> model User tbl_user

Warning: PDO::__construct(): [2002] No such file or directory (trying to connect via unix:///tmp/mysql.sock) in /Applications/MAMP/htdocs/yii/framework/db/CDbConnection.php on line 297

exception ‘CDbException’ with message ‘CDbConnection failed to open the DB connection: SQLSTATE[HY000] [2002] No such file or directory’ in /Applications/MAMP/htdocs/yii/framework/db/CDbConnection.php:262

Stack trace:

#0 /Applications/MAMP/htdocs/yii/framework/db/CDbConnection.php(237): CDbConnection->open()

#1 /Applications/MAMP/htdocs/yii/framework/db/CDbConnection.php(216): CDbConnection->setActive(true)

#2 /Applications/MAMP/htdocs/yii/framework/base/CModule.php(357): CDbConnection->init()

#3 /Applications/MAMP/htdocs/yii/framework/base/CApplication.php(391): CModule->getComponent(‘db’)

#4 /Applications/MAMP/htdocs/yii/framework/cli/commands/shell/ModelCommand.php(260): CApplication->getDb()

#5 /Applications/MAMP/htdocs/yii/framework/cli/commands/ShellCommand.php(144): ModelCommand->run(Array)

#6 /Applications/MAMP/htdocs/yii/framework/cli/commands/ShellCommand.php(99): ShellCommand->runShell()

#7 /Applications/MAMP/htdocs/yii/framework/console/CConsoleCommandRunner.php(62): ShellCommand->run(Array)

#8 /Applications/MAMP/htdocs/yii/framework/console/CConsoleApplication.php(88): CConsoleCommandRunner->run(Array)

#9 /Applications/MAMP/htdocs/yii/framework/base/CApplication.php(135): CConsoleApplication->processRequest()

#10 /Applications/MAMP/htdocs/yii/framework/yiic.php(31): CApplication->run()

#11 /Applications/MAMP/htdocs/yiitest/protected/yiic.php(7): require_once(’/Applications/M…’)

#12 /Applications/MAMP/htdocs/yiitest/protected/yiic(4): require_once(’/Applications/M…’)

#13 {main}

Running the same command again results in this:

>> model User tbl_user

Error: an active ‘db’ connection is required.

If you already added ‘db’ component in application configuration,

please quit and re-enter the yiic shell.

And of course I’ve quit, re-entered but the result stays the same.

I’ve checked php settings, PDO is there with mysql driver. Trying to connect from a simple PHP script works fine with the same settings I’ve put in main.php, being:

	'db'=>array(


		'connectionString' => 'mysql:host=localhost;dbname=yiitest',


		'emulatePrepare' => true,


		'username' => 'root',


		'password' => '****',


		'charset' => 'utf8',


	),

Any help is much appreciated!

btw: I’m running all this on MAMP…

Try 127.0.0.1 instead of localhost

/Tommy

May be it works .But why?

Hello,

you have to define the path of the unix socket. See here

127.0.0.1 may work for you because windows may assume to use localhost as ::1 for IPv6 (depending on the version of windows we are talking about) and entering the IP of localhost (127.0.0.1) will force it to use IPv4

Cheers