5

I'm trying to use the migration tool, but am getting the following error:

exception 'CDbException' with message 'CDbConnection failed to open the DB connection: SQLSTATE[HY000] [2002] Connection refused' in /Applications/MAMP/htdocs/yii-sandbox/framework/db/CDbConnection.php:382
Stack trace:
#0 /Applications/MAMP/htdocs/yii-sandbox/framework/db/CDbConnection.php(330): CDbConnection->open()
#1 /Applications/MAMP/htdocs/yii-sandbox/framework/db/CDbConnection.php(308): CDbConnection->setActive(true)
#2 /Applications/MAMP/htdocs/yii-sandbox/framework/base/CModule.php(387): CDbConnection->init()
#3 /Applications/MAMP/htdocs/yii-sandbox/framework/cli/commands/MigrateCommand.php(442): CModule->getComponent('db')
#4 /Applications/MAMP/htdocs/yii-sandbox/framework/cli/commands/MigrateCommand.php(451): MigrateCommand->getDbConnection()
#5 /Applications/MAMP/htdocs/yii-sandbox/framework/cli/commands/MigrateCommand.php(482): MigrateCommand->getMigrationHistory(-1)
#6 /Applications/MAMP/htdocs/yii-sandbox/framework/cli/commands/MigrateCommand.php(84): MigrateCommand->getNewMigrations()
#7 [internal function]: MigrateCommand->actionUp(Array)
#8 /Applications/MAMP/htdocs/yii-sandbox/framework/console/CConsoleCommand.php(172): ReflectionMethod->invokeArgs(Object(MigrateCommand), Array)
#9 /Applications/MAMP/htdocs/yii-sandbox/framework/console/CConsoleCommandRunner.php(71): CConsoleCommand->run(Array)
#10 /Applications/MAMP/htdocs/yii-sandbox/framework/console/CConsoleApplication.php(92): CConsoleCommandRunner->run(Array)
#11 /Applications/MAMP/htdocs/yii-sandbox/framework/base/CApplication.php(180): CConsoleApplication->processRequest()
#12 /Applications/MAMP/htdocs/yii-sandbox/framework/yiic.php(33): CApplication->run()
#13 /Applications/MAMP/htdocs/yii-sandbox/projects/trackstar/protected/yiic.php(7): require_once('/Applications/M...')
#14 /Applications/MAMP/htdocs/yii-sandbox/projects/trackstar/protected/yiic(4): require_once('/Applications/M...')

I have checked the DB settings in console.php, which are as follows:

'db'=>array(
    'connectionString' => 'mysql:host=127.0.0.1;dbname=yii_trackstar, unix_socket=/Applications/MAMP/tmp/mysql/mysql.sock',
    'emulatePrepare' => true,
    'username' => 'yii',
    'password' => 'xxx',
    'charset' => 'utf8',
),

As you can see, I've tried setting the host to 127.0.0.1 and setting the unix socket as per other suggestions.

I'm using MAMP (as you can see). from the protected dir, I'm running the command: ./yiic migrate

No matter what I change, I get the same error message.

*Update: *

I've just realized something. Changing the host=localhost to host=127.0.0.1 actually gives me a different error of connection refused. If I set that back to localhost the error is as follows:

exception 'CDbException' with message 'CDbConnection failed to open the DB connection: SQLSTATE[HY000] [2002] No such file or directory' in /Applications/MAMP/htdocs/yii-sandbox/framework/db/CDbConnection.php:382
TH1981
  • 3,105
  • 7
  • 42
  • 78
  • Do you need to use the `unix_socket` in the connection? My connection string typically works like this: `'mysql:host=localhost;dbname=yii_trackstar'` – Tim Withers Sep 12 '13 at 17:24
  • tried it both with and without. I added the unix_socket at the suggestion that it's sometimes needed. same w/ changing localhost to 127.0.0.1 – TH1981 Sep 12 '13 at 18:37
  • Using Finder or Terminal, are you able to navigate to the folder `/Applications/MAMP/tmp/mysql`? If not, update that path in your connection string to the actual location of that folder on your system. – Willem Renzema Sep 13 '13 at 14:28

11 Answers11

6

You can check the yii framework forum, there is already similar question posted at http://www.yiiframework.com/forum/index.php/topic/16-db-connection-string

NOTE

Specifically, what solved it is this from that thread:

A note on PHP and Mac OSX: A version of PHP4 comes bundled on Mac OSX and is typically what is executed when you try the php command from the command line. You need to have the php command execute a version of PHP5.x or higher. Usually, you have already installed a version of PHP5.x or higher, but need to tell the php command to execute the newer version rather than the older one. There are certainly many ways to achieve this, but here is one:

At any terminal prompt, type in:

prompt>which php

this should tell you where the OS is looking for the php command tells me /usr/bin/php. If I navigate to /usr/bin and issue an ls *php* I will see three files:

php
php-config
phpize

These are all for the bundled version PHP4.x Rename these files to use a 4 in their names, to remember these are the executables for PHP4

mv php php4
mv php-config php-config4
mv phpize phpize4

Then, create sym links for each of these these to point to the PHP5.x or higher version you have installed. In my case these are located in /usr/local/apache/php/bin. So I would issue from the /usr/bin directory

ln -s [absolute path to your php5.x] php

and similarly for the other files.

kachar
  • 2,310
  • 30
  • 32
  • Really helps, thanks. Who have console.php with correct DB connection data and also have PDO installed and get such error - this is your solution. Only one thing if you use MAMP: your php5 will not in /usr/bin. In my case these files are located in /Applications/MAMP/bin/php/PHP VERSION/bin/ – LIAL Oct 04 '13 at 16:07
2

While running yyic I've got problems until I've changed localhost to 127.0.0.1 in the connection string.

Aldekein
  • 3,538
  • 2
  • 29
  • 33
2

Try specifying the port in the host string. So localhost:8889 instead of just localhost or 127.0.0.1 — whichever you use.

This fixed my issue.

Igbanam
  • 5,904
  • 5
  • 44
  • 68
1

You can try setting the unix_socket with the correct keyword Socket and in the same way as the other vars in the string with ; and no spaces:

'connectionString' => 'mysql:host=localhost;dbname=yii_trackstar;Socket=/Applications/MAMP/tmp/mysql/mysql.sock',

You can check more connection strings over here: http://www.connectionstrings.com/mysql

kachar
  • 2,310
  • 30
  • 32
  • 'connectionString' => 'mysql:host=127.0.0.1;dbname=yii_trackstar;unix_socket=/Applications/MAMP/tmp/mysql/mysql.sock', – IVsevolod Jan 16 '16 at 07:27
1

Try setting host=127.0.0.1 on your db.php file

Jordi Corominas
  • 1,244
  • 10
  • 15
1

Simple Solution:

Just open the  mail-local.php  page inside  backend/common/config  directory.

Then change localhost   to 127.0.0.1 .

Now you run php yii migrate .

Hope, It will successfully create the tables in Databse

Deepak swain
  • 3,380
  • 1
  • 30
  • 26
0

change connection string to this

'connectionString' => 'mysql:host=localhost;dbname=yii_trackstar',
Neophile
  • 1,519
  • 12
  • 15
0

Open your terminal and use this

echo "export PATH=/Applications/MAMP/bin/php/php5.x.x/bin:$PATH" >> ~/.profile

5.x.x is your version of PHP you use

This is because Yii framework use your default PHP come with OSX, not the PHP of MAMP. To make sure, type "which php" to see if it's /Applications/MAMP/bin/php/php5.x.x/bin/php then you are done.

Hope this help :)

0xh8h
  • 3,271
  • 4
  • 34
  • 55
0

It's a mac problem where it uses an older version of php that is installed on the machine as opposed to the php5 that comes with MAMP. So I had to force it to use PHP version 5 that comes with MAMP.

So I had to write this in the Terminal and it worked:

/Applications/MAMP/bin/php/php5.5.3/bin/php /Applications/MAMP/htdocs/projectname/protected/yiic migrate

zeckdude
  • 15,877
  • 43
  • 139
  • 187
0

Edit file config/db.php

return [
    'class' => 'yii\db\Connection',
    'dsn' => 'mysql:host=localhost;dbname=namedb',
    'username' => 'root',
    'password' => 'root',
    'charset' => 'utf8',
];
Mr X
  • 27
  • 2
0

MAMP PRO click Enable Allow access to MySql at Mysql Menu.

surdet
  • 91
  • 1
  • 2
  • some explanation of why there's an error and why does this fix it would be probably welcome – YakovL Nov 08 '16 at 07:01