可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I recently tried installing MySQL with homebrew (brew install mysql
) and when I try to run it I get the following error:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
There is no /tmp/mysql.sock
nor a /var/lib/mysql.sock
.
I've searched and haven't found any mysql.sock
file.
How can I fix this?
回答1:
When you got the server running via
mysql.server start
you should see the socket in /tmp/mysql.sock. However, the system seems to expect it in /var/mysql/mysql.sock. To fix this, you have to create a symlink in /var/mysql:
sudo mkdir /var/mysql
sudo ln -s /tmp/mysql.sock /var/mysql/mysql.sock
This solved it for me. Now my phpMyAdmin works happily with localhost and 127.0.0.1.
Credit goes to Henry
回答2:
Looks like your mysql server is not started. I usually run the stop command and then start it again:
mysqld stop mysql.server start
Same error, and this works for me.
回答3:
Try to connect using "127.0.0.1" instead "localhost".
回答4:
1) If you are able to start mysql with below command;
mysql server start
2) and if you are able to see "mysql stopped" when you run below command;
brew services list
than, adding mysql to services will fix your problem. With this method, mysql will start as service when your system started. To do so you can run below command;
brew services start mysql
After that, you can restart your system and try connecting to mysql. I did the same and stop receiving below error;
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
I hope this helps.
回答5:
I got the same error and this is what helped me:
$ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents $launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist $mysql -uroot mysql>
回答6:
You'll need to run mysql_install_db
- easiest way is if you're in the install directory:
$ cd /usr/local/Cellar/mysql// $ mysql_install_db
Alternatively, you can feed mysql_install_db
a basedir
parameter like the following:
$ mysql_install_db --basedir="$(brew --prefix mysql)"
回答7:
I faced the same problem on my mac and solved it, by following the following tutorials
https://mariadb.com/resources/blog/installing-mariadb-10116-mac-os-x-homebrew
But don't forget to kill or uninstall the old version before continuing.
Commands:
brew uninstall mariadb xcode-select --install ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" - See more at: https://mariadb.com/resources/blog/installing-mariadb-10116-mac-os-x-homebrew#sthash.XQoxRoJp.dpuf brew doctor brew update brew info mariadb brew install mariadb mysql_install_db mysql.server start
回答8:
Just to add to these answers, In my case I had no local mySQL server, it was running inside a docker container. So the socket file does not exist and will not be accessible for the "mysql" client.
The sock file gets created by mysqld and mysql uses this to communicate with it. However if your mySql server is not running local, it does not require the sock file.
By specifying a host name/ip the sock file is not required e.g.
mysql --host=127.0.0.1 --port=3306 --user=xyz --password=xyz
回答9:
just to complete this thread. therefore MAMP (PRO) is used pretty often
the path here is
/Applications/MAMP/tmp/mysql/mysql.sock
回答10:
The socket isn't created if you restart mysqld too quickly after stopping it:
brew services stop mysql;brew services start mysql ls /tmp/mysql.sock // socket not created
Waiting 3 seconds before restarting mysql fixes this:
brew services stop mysql;sleep 3;brew services start mysql sleep 1.5 // Note that it takes a second after mysql starts to create the socket ls /tmp/mysql.sock // socket created!