I have recently upgraded my macOS to Monterey (version 12.6) and mariadb (mysql) stopped working. I tried to run brew services restart mariadb and it didn’t show any error so I thought everything works fine.
However, when I tried to access the mysql using either mariadb or mysql I was getting this ERROR 2002 (HY000): Can’t connect to local server through socket ‘/tmp/mysql.sock’ (2).
I spent a lot of time trying to fix the error but I couldn’t. However, someone said in a comment on this post https://getgrav.org/blog/macos-monterey-apache-mysql-vhost-apc that it will help to backup then remove the contents of the mysql folder.
Solution for ERROR 2002 (HY000): Can’t connect to local server through socket ‘/tmp/mysql.sock’ (2)
This worked for me on macOS Monterey 12.6:
Step 1. Backup the mysql folder and your databases
In your terminal, copy the contents of mysql folder:
$ mkdir /usr/local/var/mysql__BKP $ cp -R /usr/local/var/mysql/* /usr/local/var/mysql__BKP $ mysqldump --all-databases > sql_file.sql
Step 2. Uninstall (remove) using homebrew and remove the content from the mysql folder
$ brew services stop mariadb $ brew uninstall mariadb $ brew cleanup $ rm -rf /usr/local/var/mysql/*
Step 3. Install mariadb using homebrew and start the service
$ brew install mariadb $ brew start mariadb
Once you do that, a popup should open asking you to allow network connections:
Step 4. Copy your databases back to mysql
That should be all you need to fix the connection error.