Can’t connect to local MySQL server error with MariaDB and Homebrew
Photo by Jan Antonin Kolar on Unsplash
I encountetered this error message with MariaDB installed using Homebrew.
ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’ (2)
I had previously installed a version of MySQL. Apparently that was the cause. One tell-tale sign that you may be having a similar problem is if you receive a Homebrew Caveat about a /etc/my.cnf file after installing MariaDB. Even though I'd uninstalled MySQL, there were still some files hanging around and causing trouble.
I ran a bunch of commands in order to resolve the error. Hopefully this works for you too.
- Use
brew uninstall mariadbto remove the latest-installed version. - Use
brew uninstall mysqlto remove MySQL just in case. - Run
rm -rf /opt/homebrew/var/mysql - Run
rf -rm /opt/homebrew/Cellar/mariadb/to remove all existing references to MariaDB. (If you see amysqldirectory in/opt/homebrew/Cellar/delete that too.) - Run
rm -rf /opt/homebrew/etc/my.cn*to delete existing configuration files. - Run
brew cleanup --prune=allto clear Homebrew's caches and clean up symlinks. - Run
brew install mariadbto reinstall MariaDB. - Run
brew postinstall mariadb.
As Homebrew instructs you after installing: use brew services start mariadb and brew services stop mariadb to start and stop the server. Then use mariadb to connect to the server from a terminal window.