Go back to home page of Unsolicited Advice from Tiffany B. Brown

Can’t connect to local MySQL server error with MariaDB and Homebrew

An old fashioned, wooden card catalog that libraries used to store information about books and periodicals in their collection. A paper database, if you will.

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.

  1. Use brew uninstall mariadb to remove the latest-installed version.
  2. Use brew uninstall mysql to remove MySQL just in case.
  3. Run rm -rf /opt/homebrew/var/mysql
  4. Run rf -rm /opt/homebrew/Cellar/mariadb/ to remove all existing references to MariaDB. (If you see a mysql directory in /opt/homebrew/Cellar/ delete that too.)
  5. Run rm -rf /opt/homebrew/etc/my.cn* to delete existing configuration files.
  6. Run brew cleanup --prune=all to clear Homebrew's caches and clean up symlinks.
  7. Run brew install mariadb to reinstall MariaDB.
  8. 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.