Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Install MySQL 5.7 on macOS using Homebrew

This is a fork of original gist https://gist.github.com/nrollr/3f57fc15ded7dddddcc4e82fe137b58e, with slight changes on pointing to 5.7 version branch, instead of 8 (latest default of MySQL in Hombrew).

Install MySQL 5.7 on macOS

This procedure explains how to install MySQL using Homebrew on macOS (Sierra 10.12 and up)

Install Homebrew

  • Installing Homebrew is effortless, open Terminal and enter :
    $ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
  • Note: Homebrew will download and install Command Line Tools for Xcode 8.0 as part of the installation process.

Install MySQL

At this time of writing, Homebrew has MySQL version 8 as default, but as we're aiming to get 5.7, we'll need to append @5.7 to the default package key:

  • Enter the following command : $ brew info mysql@5.7
  • Expected output: mysql@5.7: stable 5.7.22 (bottled) [keg-only]

To install MySQL enter : $ brew install mysql@5.7

Additional configuration

Homebrew

  • Install brew services first : $ brew tap homebrew/services

  • Load and start the MySQL service : $ brew services start mysql@5.7.
    Expected output : Successfully started mysql (label: homebrew.mxcl.mysql)

  • Check of the MySQL service has been loaded : $ brew services list 1

  • Force link 5.7 version - $ brew link mysql@5.7 --force

  • Verify the installed MySQL instance : $ mysql -V.
    Expected output : Ver 14.14 Distrib 5.7.22, for osx10.13 (x86_64)

MySQL

Open Terminal and execute the following command to set the root password:
mysqladmin -u root password 'yourpassword'

Important : Use the single ‘quotes’ to surround the password and make sure to select a strong password!

Database Management

To manage your databases, I recommend using Sequel Pro, a MySQL management tool designed for macOS.
Current version available: 1.1.2

Comments

1 The brew services start mysql@5.7 - instruction is equal to :

$ ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents
$ launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
@ricketybridge

This comment has been minimized.

Copy link

ricketybridge commented Jul 18, 2018

After following these instructions, the mysql command results in the following error:

ERROR 2059 (HY000): Authentication plugin 'caching_sha2_password' cannot be loaded: dlopen(/usr/local/Cellar/mysql@5.7/5.7.22/lib/plugin/caching_sha2_password.so, 2): image not found
@stevenchanin

This comment has been minimized.

Copy link

stevenchanin commented Aug 18, 2018

This worked great for me on a clean install of OS X 10.13.6

@rebz

This comment has been minimized.

Copy link

rebz commented Sep 1, 2018

Similar to @ricketybridge I also receive ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

@atul25khanduri

This comment has been minimized.

Copy link

atul25khanduri commented Sep 2, 2018

@rebz I'm also getting the same error.

@ashish-shrma

This comment has been minimized.

Copy link

ashish-shrma commented Sep 3, 2018

@rebz @atul did you guys find a solution?

@greggles

This comment has been minimized.

Copy link

greggles commented Sep 11, 2018

I had the same error ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) after following these instructions. I then found this set of steps to fully remove mysql and I did those steps to remove. Then I followed the steps in this gist and they worked fine. So...good luck :) @rebz @atul25khanduri @ashish-shrma also ping @operatino in case you want to update your gist to include some ideas about fully removing mysql first.

Edited to add: Coming back to this in April of 2019 and I had to also run mysqld --initialize

@Paveltarno

This comment has been minimized.

Copy link

Paveltarno commented Sep 17, 2018

@greggles Thanks a lot! Worked like a charm

@kezzico

This comment has been minimized.

Copy link

kezzico commented Oct 3, 2018

Still seeing the "Can't connect to local MySQL server..."

Tried Greggle's link to fully remove a previous install of MySQL 8, but after a restart the problem persists

# brew services start mysql@5.7
==> Successfully started `mysql@5.7` (label: homebrew.mxcl.mysql@5.7)

# launchctl list | grep mysql
-	0	homebrew.mxcl.mysql@5.7

Brew services is reporting the service started, but Launchctl is not showing a PID.

Here is the log output

lee@kezzico:~# tail /usr/local/var/mysql/kezzico.local.err 
2018-10-03T21:57:10.986146Z 0 [ERROR] Can't start server: can't check PID filepath: No such file or directory
2018-10-03T21:57:11.6NZ mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended

Finally got the service to start by creating a folder for the PID, and setting the permission on the folder to global read/write/x.

# sudo mkdir /var/run/mysqld
# chmod 777 /var/run/mysqld

Not sure why this is necessary. There may be security implications to this. Hopefully someone has a better solution.

@jcartw89

This comment has been minimized.

Copy link

jcartw89 commented Dec 2, 2018

I had to run mysqld --initialize in order to eliminate the ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) issue. Make sure your /usr/local/var/mysql directory is empty first though.

@ntwobike

This comment has been minimized.

Copy link

ntwobike commented Dec 11, 2018

Got the same issue ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' followed @greggles instructions, it works! mysql version 8.0.12

@sircEUNHO

This comment has been minimized.

Copy link

sircEUNHO commented Jan 8, 2019

@greggles you are my savior, haha

@n4ss1m

This comment has been minimized.

Copy link

n4ss1m commented Jan 28, 2019

I got the following error :
mysqladmin: unable to change password; error: 'The ACL operation failed due to the following error from SE: errcode 190 - Operation not allowed when innodb_forced_recovery > 0'

Google didn't help well : (

@rowild

This comment has been minimized.

Copy link

rowild commented Feb 21, 2019

How can I use mysqlsh in order to work with documents (nosql)?

@joeljpa

This comment has been minimized.

Copy link

joeljpa commented Mar 12, 2019

had the same issue which @greggles solved. This medium article was simple enough to solve it for me without going to different links.

@JSilversun

This comment has been minimized.

Copy link

JSilversun commented Mar 13, 2019

Thanks @joeljpa that article helped me

@fernyherrera

This comment has been minimized.

Copy link

fernyherrera commented Mar 26, 2019

Thank you, worked!

@derkhadim

This comment has been minimized.

Copy link

derkhadim commented Apr 1, 2019

Thank you, that work for me

@Heybluguy

This comment has been minimized.

Copy link

Heybluguy commented Apr 8, 2019

Nice! Before doing this I had 16 errors, now I'm down to 1 when I bundle. In a better spot than I was before. Here is my current error.
"mysql.c:1320:74: error: use of undeclared identifier 'rb_cFixnum'
else if (argv[i] == rb_cNumeric || argv[i] == rb_cInteger || argv[i] == rb_cFixnum)"
^

@jamesfacts

This comment has been minimized.

Copy link

jamesfacts commented Apr 26, 2019

I ran into a weird issue trying to connect to mysql@5.7 after installing with Homebrew. Similar to @kezzico, brew told me that the service was running but it had no PID. When I ran mysqld --initialize, I saw the message [ERROR] --initialize specified but the data directory has files in it. Aborting..

I think this error was caused by a previous install of mysql through MAMP. I followed this answer on SE. After running rm -rf /usr/local/var/mysql, I uninstalled and reinstalled mysql@5.7 and now when I check active services I do see a mysql PID:

 ~  launchctl list | grep mysql
54246	0	homebrew.mxcl.mysql@5.7
@dilip-k-m

This comment has been minimized.

Copy link

dilip-k-m commented May 16, 2019

mysqld stop
mysql.server start

@dschreck

This comment has been minimized.

Copy link

dschreck commented Jun 15, 2019

I ran into a weird issue trying to connect to mysql@5.7 after installing with Homebrew. Similar to @kezzico, brew told me that the service was running but it had no PID. When I ran mysqld --initialize, I saw the message [ERROR] --initialize specified but the data directory has files in it. Aborting..

I think this error was caused by a previous install of mysql through MAMP. I followed this answer on SE. After running rm -rf /usr/local/var/mysql, I uninstalled and reinstalled mysql@5.7 and now when I check active services I do see a mysql PID:

 ~  launchctl list | grep mysql
54246	0	homebrew.mxcl.mysql@5.7

This works.

@ringate

This comment has been minimized.

Copy link

ringate commented Jun 21, 2019

No idea why missed add the mysql path. I added by self.

$ mysql -V
-bash: mysql: command not found
$ echo 'export PATH="/usr/local/opt/mysql@5.7/bin:$PATH"' >> ~/.bash_profile
$ source ~/.bash_profile
$ mysql -V
mysql  Ver 14.14 Distrib 5.7.26, for osx10.14 (x86_64) using  EditLine wrapper
@filmo

This comment has been minimized.

Copy link

filmo commented Jun 27, 2019

In addition to the other fixes above, I also had to remove

/etc/my.cnf

Before I could get it to do the post install initialization.

@rsalimian

This comment has been minimized.

Copy link

rsalimian commented Aug 13, 2019

to fix the error:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock'

I had to also run:
sudo chown -R _mysql:mysql /usr/local/var/mysql

@Leo7654

This comment has been minimized.

Copy link

Leo7654 commented Sep 18, 2019

brew unlink mysql
brew link mysql@5.7 --force
mysql -V

@aneudysamparo

This comment has been minimized.

Copy link

aneudysamparo commented Sep 24, 2019

Hey! Te Amo <3

@JavierPons

This comment has been minimized.

Copy link

JavierPons commented Oct 4, 2019

Works fine, no error. Perfect. Thnx!

@Adrian-Sanchez

This comment has been minimized.

Copy link

Adrian-Sanchez commented Oct 10, 2019

For those that are still looking for a solution...this worked for me. I'd appreciate an upvote if it works.
Error relating to: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

https://stackoverflow.com/a/58315841/2209519

@nstanard

This comment has been minimized.

Copy link

nstanard commented Oct 17, 2019

Is there any way to get mysql to start on system startup installing via this approach?

@ChildishBob

This comment has been minimized.

@gieart87

This comment has been minimized.

Copy link

gieart87 commented Nov 3, 2019

mysql.server start

Thanks!

@huangyisan

This comment has been minimized.

Copy link

huangyisan commented Dec 1, 2019

I had to run mysqld --initialize in order to eliminate the ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) issue. Make sure your /usr/local/var/mysql directory is empty first though.

thanks a lot!!!!!!!!!!!!!!!!!!!!!!!!!!!

@Catalin-Ionut

This comment has been minimized.

Copy link

Catalin-Ionut commented Dec 28, 2019

Every step works great and as intended on OSX Catalina.

@filmo

This comment has been minimized.

Copy link

filmo commented Jan 15, 2020

So I can get it running on the server and connect as local host. (in my case 192.168.5.3)

However when I connect from a remote machine on the same LAN it doesn't seem like port 3306 is accepting connections anymore.

mysql -p -u my_user_name -h -192.168.5.3

ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.5.3' (61)

I test with telnet and it also fails to connect.

$telnet 192.168.5.3 3306
Trying 192.168.5.3...
telnet: connect to address 192.168.5.3: Connection refused
telnet: Unable to connect to remote host

On the server (192.168.5.3), I can connect and interact with the databases as I normally would. I've not changed any firewall permissions, so they should be the same as they previously were, which allowed 3306 and worked with 5.6

When I roll the server back to 5.6 via a restore, both telnet and mysql are able to connect (as they had previously before)??

I've tried putting a my.cnf file in /etc/my.cnf and setting the bind-address to each of the following with no luck either.

127.0.0.1
192.168.5.3
0.0.0.0

Any thoughts on why mysql@5.7 wouldn't allow connections from the LAN ?? (seems like it's not listening on 3306 rather than a database password error because even telnet can't reach the port, whereas it could previously.)

@Adrian-Sanchez

This comment has been minimized.

Copy link

Adrian-Sanchez commented Jan 15, 2020

@filmo
for me, my cnf file is in /usr/local/etc/my.cnf

@Adrian-Sanchez

This comment has been minimized.

Copy link

Adrian-Sanchez commented Jan 15, 2020

If that didnt work, the next step is user privaledges for remote access
CREATE USER ‘USERNAME‘@’REMOTE_IP‘ IDENTIFIED BY ‘PASSWORD‘;
CREATE DATABASE ‘EXAMPLE‘;
GRANT ALL ON EXAMPLE.* TO ‘USERNAME‘@’REMOTE_IP‘;
FLUSH PRIVILEGES;

@omerwwazap

This comment has been minimized.

Copy link

omerwwazap commented Feb 7, 2020

Thank you, worked as intended on OSX Catalina.

@ZEdgars

This comment has been minimized.

Copy link

ZEdgars commented Feb 28, 2020

Thanks for this post!!!! It solved by sql schema error and after some days of struggling, I was able to connect Laravel with SQL/SequelPro and migrate databases.

@michaelklopf

This comment has been minimized.

Copy link

michaelklopf commented Mar 11, 2020

We had the ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) issue too when accessing the db with Sequel Pro and we solved it by adding user=root (which is our standard user we use in Sequel Pro) to the my.cnf file under the bind-address.

# Default Homebrew MySQL server config
[mysqld]
# Only allow connections from localhost
bind-address = 127.0.0.1
user=root
@ewhicher

This comment has been minimized.

Copy link

ewhicher commented Mar 19, 2020

This and @greggles suggestion saved my day, thanks!

@rsalimian

This comment has been minimized.

Copy link

rsalimian commented May 11, 2020

this is what I did to fix: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

  • uninstalled MySQL
  • when you run $ brew install mysql@5.7 make sure it installs with no Error/Warning
  • I got error Warning: The post-install step did not complete successfully
    to fix the error I create /usr/local/var/mysql manually:
  • $ mkdir /usr/local/var/mysql
  • $ chmod 777 mkdir /usr/local/var/mysql
    and then
  • $ brew postinstall mysql@5.7
    and then just follow up rest of instruction to start the MySQL service.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.