Skip to content

Instantly share code, notes, and snippets.

@lucascaton
Forked from tomas-stefano/reinstall.sh
Last active June 3, 2025 23:02
Show Gist options
  • Save lucascaton/3373792 to your computer and use it in GitHub Desktop.
Save lucascaton/3373792 to your computer and use it in GitHub Desktop.
Reinstalling MySQL 5.x on macOS via Homebrew
brew remove mysql
brew cleanup
launchctl unload -w ~/Library/LaunchAgents/com.mysql.mysqld.plist
rm ~/Library/LaunchAgents/com.mysql.mysqld.plist
sudo rm -rf /usr/local/var/mysql
brew install mysql
unset TMPDIR
mysql_install_db --verbose --user=`whoami` --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp
mkdir -p ~/Library/LaunchAgents
cp /usr/local/Cellar/mysql/5.5.25a/homebrew.mxcl.mysql.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
# Success! :-)
@rajbdilip
Copy link

Looks like the script needs an update. I got the following error on macOS mojave.

reinstall-mysql.sh: line 16: mysql_install_db: command not found
cp: /usr/local/Cellar/mysql/5.5.25a/homebrew.mxcl.mysql.plist: No such file or directory

@kadlu
Copy link

kadlu commented Jul 31, 2019

Looks like the script needs an update. I got the following error on macOS mojave.

reinstall-mysql.sh: line 16: mysql_install_db: command not found
cp: /usr/local/Cellar/mysql/5.5.25a/homebrew.mxcl.mysql.plist: No such file or directory

replace the mysql version number with the version number of your installation.
cp: /usr/local/Cellar/mysql/{version_number}/homebrew.mxcl.mysql.plist

@r0binxp
Copy link

r0binxp commented Mar 31, 2022

Could it be that in M1 it is different?

@lucascaton
Copy link
Author

Could it be that in M1 it is different?

Yep, it should be /opt/homebrew/Cellar instead of /usr/local/Cellar/ when Homebrew is running on Apple Silicon.

@r0binxp
Copy link

r0binxp commented Mar 31, 2022

Ok Thanks @lucascaton

@MikKuba
Copy link

MikKuba commented Apr 10, 2024

Hi @lucascaton , please, can you review script for actualization for macos Sonoma with M1 chip? Will be very glad.
I just ruined my brew mysql service and anything can`t repair it.

Because I am working on several projects, for some I use brew mysql, for some MAMP mysql server and some Docker. When I came back to brew mysql after some days, mysql not running: "ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)"

@lucascaton
Copy link
Author

@MikKuba, I'm having exactly the same issue right now (with MySQL 8) 😞

@giovannievway
Copy link

@MikKuba @lucascaton have you found a solution?

@MikKuba
Copy link

MikKuba commented Apr 24, 2024

Hi @lucascaton , please, can you review script for actualization for macos Sonoma with M1 chip? Will be very glad. I just ruined my brew mysql service and anything can`t repair it.

Because I am working on several projects, for some I use brew mysql, for some MAMP mysql server and some Docker. When I came back to brew mysql after some days, mysql not running: "ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)"

My solution is to little change /etc/my.cnf. If I am using MAMP, I have to comment these rows:

[mysqld]
#socket = /tmp/mysql.sock
#datadir = /opt/homebrew/var/mysql
#tmpdir = /tmp

and MAMP is running.

To start mysql via brew, I have to uncomment rows above.
After that I have to delete files in /opt/homebrew/var/mysql/ib_logfile0 and ib_logfile1

And now its only to restart brew mysql and everything running fine.

@lucascaton
Copy link
Author

lucascaton commented Apr 29, 2024

@MikKuba, @giovannievway, I had to uninstall it, delete all the data, and re-install it to get it working again πŸ™‚

brew uninstall [email protected]
rm -rf /opt/homebrew/var/mysql
brew install [email protected]

Then, to check, I ran brew services list:

Name          Status     User       File
[email protected]     started    [myuser]   ~/Library/LaunchAgents/[email protected]
# (others)

Hope this solves the issue for you too πŸ€

@hendisantika
Copy link

I am using M3.
But I have this error: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

Can you help me with that?

@lucascaton
Copy link
Author

lucascaton commented Jun 3, 2025

@hendisantika, it seems your MySQL isn't running.

  • What's the output of brew services list?
  • What's the output when you start MySQL?
  • What steps have you tried so far? Maybe one listed above?

@hendisantika
Copy link

What's the output of brew services list?

brew services list

Name          Status   User         File
alloy         none
colima        error  1 hendisantika ~/Library/LaunchAgents/homebrew.mxcl.colima.plist
consul        started  hendisantika ~/Library/LaunchAgents/homebrew.mxcl.consul.plist
grafana-agent none
mysql         started  hendisantika ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
php           none
podman        none
postgresql@17 started  hendisantika ~/Library/LaunchAgents/[email protected]
redis         started  hendisantika ~/Library/LaunchAgents/homebrew.mxcl.redis.plist
unbound       none
vault         none

What's the output when you start MySQL?

hendisantika@Hendis-MacBook-Pro ~ % mysql -u root
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (38)

What steps have you tried so far? Maybe one listed above?

  1. Remove mysql
  2. Reinstall MySQL
  3. Still error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment