<p>If you're using 'localhost' for the 'server' in <code>craft/config/db.php</code>, try '127.0.0.1' and visa-versa.</p>

<p>For some reason, some environments are picky about that when connecting to MySQL from the CLI.</p>

CC BY-SA 3.0
2015-03-05T11:18:01.740Z
I still have this issue, any other ideas to try?
Luke Holder
CC BY-SA 3.0
2015-03-05T16:18:42.420Z
@LukeHolder I'm out of ideas. Those are usually the 2 reasons I've seen it happen.
Brad Bell
CC BY-SA 3.0
2015-03-06T03:37:26.843Z
had to drop using yiic... manual migration creation for me!
Luke Holder
CC BY-SA 3.0
2015-06-17T03:49:01.233Z
I tried setting the unixSocket though, didn't seem to work ([link to code](https://github.com/pixelandtonic/Craft-Release/blob/master/app/etc/config/defaults/db.php#L53-L58))
Natetronn
CC BY-SA 3.0
2015-02-12T02:35:55.503Z
That was it (sort of). I had multiple environments in the config file, but yiic needed something in the default (*) array.
jjmartucci
CC BY-SA 3.0
2015-02-12T03:02:52.747Z
Ahh, yeah.  There is no domain to match, so it needs something for the default.  Nice catch.
Brad Bell
CC BY-SA 3.0
2016-06-15T08:36:28.343Z
It seems you need every config line of your local environment in the default `'*' => array(`. So don't forget `user`, `password` and `database`...
luwes
CC BY-SA 3.0
2018-03-09T09:25:28.887Z
unix_socket=/Applications/MAMP/tmp/mysql/mysql.sock did the trick for me. Check https://stackoverflow.com/questions/41852050/yii-console-command-cant-connect-to-database?rq=1
Jan
CC BY-SA 3.0
2015-02-12T00:01:58.000Z
2015-02-12T00:01:58.000Z
Brad Bell
<p>I've been trying to follow the docs for creating a plugin migration (<a href="https://craftcms.com/docs/plugins/migrations" rel="nofollow noreferrer">https://craftcms.com/docs/plugins/migrations</a>) but any attempts to run yiic seem to die with:</p>

<pre><code>CDbConnection failed to open the DB connection.
</code></pre>

<p>My Google-fu has not come up with any relevant answers. This is with Craft setup in a Vagrant box, if that makes a difference. </p>

CC BY-SA 3.0
2015-06-17T02:59:18.180Z
I've run into this as well when following [this tutorial](http://spin.atomicobject.com/2015/06/16/craft-console-plugin/)
Natetronn
<plugin-development><migration>
Using Yiic for plugin migration
# Using Yiic for plugin migration

(Asked by: username_0 on 2015-02-11)

I've been trying to follow the docs for creating a plugin migration (https://craftcms.com/docs/plugins/migrations) but any attempts to run yiic seem to die with:
CDbConnection failed to open the DB connection.

My Google-fu has not come up with any relevant answers. This is with Craft setup in a Vagrant box, if that makes a difference.

**Tags:** plugin-development, migration
> **Comments:**
> **username_1**: I've run into this as well when following [this tutorial](http://spin.atomicobject.com/2015/06/16/craft-console-plugin/)  

---

## 1 answers

### Answer by username_2 on 2015-02-12. Score: 2

If you're using 'localhost' for the 'server' in `craft/config/db.php`, try '127.0.0.1' and visa-versa.

For some reason, some environments are picky about that when connecting to MySQL from the CLI.

> **Comments:**
> **username_0**: That was it (sort of). I had multiple environments in the config file, but yiic needed something in the default (*) array.  
> **username_2**: Ahh, yeah.  There is no domain to match, so it needs something for the default.  Nice catch.  
> **username_3**: I still have this issue, any other ideas to try?  
> **username_2**: @LukeHolder I'm out of ideas. Those are usually the 2 reasons I've seen it happen.  
> **username_3**: had to drop using yiic... manual migration creation for me!  
> **username_1**: I tried setting the unixSocket though, didn't seem to work ([link to code](https://github.com/pixelandtonic/Craft-Release/blob/master/app/etc/config/defaults/db.php#L53-L58))  
> **username_4**: It seems you need every config line of your local environment in the default `'*' => array(`. So don't forget `user`, `password` and `database`...  
> **username_5**: unix_socket=/Applications/MAMP/tmp/mysql/mysql.sock did the trick for me. Check https://stackoverflow.com/questions/41852050/yii-console-command-cant-connect-to-database?rq=1  

---

