When connecting to MySQL if you specify host by ‘localhost’ it is treated as a switch to unix socket. Now a unix socket is a special file. It’s location is specified in the my.cnf file. I don’t use MySQL much but this error could refer to it.
There are many sections in the my.cnf file as it is used both by the server and all clients. Search for a [client] section and check the path there.
When you change ‘localhost’ to ‘127.0.0.1’ the driver will use a TCP socket, but the server could have it disabled for security reasons. It should listen only on 127.0.0.1 not to allow external connections or a good firewall should be installed and proper passwords set on superuser accounts.
Now this all doesn’t matter if your db component configuration is the same in both main.php and console.php and in the webapp the connection works. Maybe it has something to do with user permissions, the web server and CLI version of PHP may run under different users.
I would advise to check one thing at a time, maybe start with the TCP socket. Don’t rush into it and revert changes if it doesn’t resolve the issue. It’s good to know how services interact with each other, the apps we’re all writing sometimes do the same.
I’ve checked the path on the unix socket compared to the my.cnf file and that is right and I can’t see any change to my error when I add it.
When I change localhost to 127.0.0.1, the error changes to ‘connection refused’ instead of ‘No such file or directory’.
I’m not sure about the TCP socket, or where to check it really. I’m working solely on a localhost at the moment, and the firewall is currently off, so that shouldn’t be impacting anything.
That’s what doesn’t make sense to me. As far as I’m aware I’ve checked the user permissions for the database and everything seems to be correct. I’m not sure, but what strikes me here is the error message is ‘No such file or directory’. If I change the password in the console.php file so that it’s incorrect, I get the ‘connection refused’ error. That makes me think it’s connecting, but just not running something.
I’ve been fighting this all day and don’t feel like i’ve made any progress. I’m not sure what else to check?
I didn’t know you can pass unix socket path in the connection string Anyway, if you issue the command:
it will tell you which ini files it is using. If it points to /etc/php.ini but it doesn’t exist and there’s only /etc/php.ini.default I would assume you could copy it to /etc/php.ini and then apply your modifications.
The MAMP server uses a different php executable and setting. I suppose you are running [font="Courier New"]yiic migrate[/font] command directly (if so this will use the php executable from your PATH settings like /usr/bin which maybe different than the MAMP version and its ini settings). Instead try this command:
My Macbook produced the same error as above as the MacOS has another version of php installed. To get MAMP and Yii2 working it is necessary to define a few more variables in the connection string such as the port and which mysql.sock to use.
There is a more fuller article here describing why it is happening, and what you can do to keep your configuration the same. It also talks about the consequences of using 127.0.0.1 instead of localhost: