Skip to content

Instantly share code, notes, and snippets.

@benlinton
Last active September 23, 2023 09:38
Show Gist options
  • Save benlinton/d24471729ed6c2ace731 to your computer and use it in GitHub Desktop.
Save benlinton/d24471729ed6c2ace731 to your computer and use it in GitHub Desktop.
Multiple MySQL Versions with Homebrew

Multiple MySQL Versions for Development

Options included below:

  • Using Docker docker-compose
  • Using Homebrew brew

Using Docker (recommended)

This gist was originally created for Homebrew before the rise of Docker, yet it may be best to avoid installing mysql via brew any longer. Instead consider adding a barebones docker-compose.yml for each project and run docker-compose up to start each project's mysql service.

docker-compose.yml

version: "3.7"
services:

  db:
    image: mysql:5.6
    restart: always
    ports:
      - "3306:3306"
    volumes:
      - "db_data:/var/lib/mysql"

volumes:
  db_data: null

Please visit https://hub.docker.com/_/mysql for environment variables that you can optionally set (e.g. username, password, etc.)

To start the your mysql service.

docker-compose up

To stop your mysql service.

docker-compose down  # or ctrl-c if service is running in foreground

Using Homebrew

Disclaimer: Your milage may vary, I've updated this gist based on comments below but haven't tested it myself since switching to Docker. Please see comments below and/or gist history for additional details.

I was using homebrew version 0.9.5 when writing this gist.

brew -v # => Homebrew 0.9.5

Install the current version of mysql.

# Install current mysql version
brew install mysql

# Start agent for current version of mysql
brew services start mysql

Install the older version of mysql.

# Install older mysql version
brew install mysql@5.6

# Start agent for older version of mysql
brew services stop mysql
brew services start mysql56

Then to switch to the older version.

# Unlink current mysql version
brew unlink mysql 

# Check older mysql version
ls /usr/local/Cellar/mysql56 # => 5.6.27

# Link the older version
brew switch mysql@5.6 5.6.27

And to switch back to the current version.

# Unlink older mysql version
brew unlink mysql56 

# Check current mysql version
ls /usr/local/Cellar/mysql # => 5.7.10

# Link the current version
brew switch mysql 5.7.10

To verify which mysql version you're on at any time.

# Check which version of mysql is currently symlinked
ls -l /usr/local/bin/mysql # => /usr/local/bin/mysql@ -> ../Cellar/mysql56/5.6.27/bin/mysql

# Or using the mysql command
mysql --version
@gerard-morera
Copy link

Love u

@maijaz01
Copy link

while giving command > mysql

ERROR 1045 (28000): Access denied for user 'sheikhaijaz'@'localhost' (using password: NO)
can you please help on this

@unlessbamboo
Copy link

Good.

@quberok
Copy link

quberok commented Jun 21, 2018

@jsweeney thanks, your solution is awesome. But before run the first, you must go to /usr/local/Cellar/mysql@5.6/5.6.36 dir, otherwise you will see mysql_install_db not found error.

cd /usr/local/Cellar/mysql@5.6/5.6.36

And it works only for mysql 5.6. for mysql 5.7 will be

/usr/local/Cellar/mysql@5.7/5.7.22/bin/mysqld --initialize --user=root --datadir=/usr/local/var/mysql57

And for mysql 5.5

cd /usr/local/Cellar/mysql@5.5/5.5.60
/usr/local/Cellar/mysql@5.5/5.5.60/scripts/mysql_install_db --user=root --datadir=/usr/local/var/mysql55

@rdp
Copy link

rdp commented Jul 12, 2018

these days it's more like brew install mysql@5.7 but got me going on the right track, thanks!

@ivange94
Copy link

ivange94 commented Jul 16, 2018

I followed this now I'm getting mysql command not found.

This helped me instead https://gist.github.com/6temes/3c52f8a472f61d9676e7218a98812286

@ricketybridge
Copy link

Why is it necessary to have the agents running before doing the relinking and such?

At any rate, this is giving me a "file not found" error:

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

@esirK
Copy link

esirK commented Feb 19, 2019

Received the error
Error: homebrew/versions was deprecated. This tap is now empty as all its formulae were migrated.
You need to use
brew install mysql@5.6 instead.

@ryanomor
Copy link

I had the same issue @esirK. I think this needs to be updated to reflect that change

@benlinton
Copy link
Author

@LoranceChen @geryit @ryanomor Thanks for you feedback! I edited the gist to apply your recommendations.

For everyone else, I did not test these changes out since I now use Docker to solve this problem. So your milage may vary.

@bpolster
Copy link

When switching back to mysql55 I needed to additionally do:

brew link mysql55 --force

Similar for me. brew link mysql@5.6 --force. Before, was getting mysql No such file or directory.

@4unkur
Copy link

4unkur commented Oct 4, 2019

Check https://dbngin.com/
It has MySQL 5.7 and 8. I'm using it only for 5.7 with different port, so I have both MySQL versions running at the same time

@f-l-s
Copy link

f-l-s commented Feb 3, 2021

The Homebrew section is still useful many years later... Thank you very much ! :)

Some changes in this section should be done with Homebrew 2.7.7 :

1- Use in every command the notation "mysql@5.6" instead of "mysql56"
For example :
brew services start mysql@5.7

2- Command brew Switch is disabled. You need to use :
# Unlink the actual linked version
brew unlink mysql@5.6
# Unlink the new version you want to use
brew link mysql@5.7

One useful command to add to this guide :
# List all installed versions of mysql
brew list --versions | grep mysql*

@pvangala-mdsol
Copy link

pvangala-mdsol commented Mar 10, 2021

Use brew link mysql@5.6 in case you have error like Error: Unknown command: switch

@wizhou
Copy link

wizhou commented Oct 12, 2021

Thanks a lot

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