Upgrade MySQL from 5.x to 8.x with Homebrew

05 Dec 2025

  1. [Optinal] Backup Your Databases:
    # better make backup only for necessary databases, 
    # you don't need system databases, 
    # as mysql 5 and 8 have quite different internal structure
    mysqldump -u root -p --databases db1 db2 db3 > data_dump.sql
    # !!! AVOID DOING FULL DB BACKUP !!!
    # System tables of MySQL 5 and 8 are incompatible
    # If you have only all-databases backup ->
    # -> open it with text editor and leave only DBs that you need migrate
    mysqldump -u root -p --all-databases > all_databases_backup.sql
    
  2. Stop and Uninstall MySQL 5:
    brew services stop [email protected] # Replace 5.x with your specific MySQL 5 version
    brew uninstall [email protected]
    # and delete old mysql data, to avoid any conflicts in future
    rm -rf /opt/homebrew/var/mysql
    
  3. Install MySQL 8:
    brew install [email protected]
    brew services list
    
  4. [Optional] Restore Your Databases:
    # need --force to skip all incompatibility errors and skip backupf system tables
    mysql -u root --force < all_databases_backup.sql
    # if only user databases were backed up --> you don't need to use --force
    
  5. [Optional] Secure the MySQL 8 Installation (set root pass and other sec options):
    mysql_secure_installation
    

DONE !

==> Summary
🍺  /opt/homebrew/Cellar/[email protected]/8.4.7_2: 322 files, 316.8MB
==> Running `brew cleanup [email protected]`...
Disable this behaviour by setting `HOMEBREW_NO_INSTALL_CLEANUP=1`.
Hide these hints with `HOMEBREW_NO_ENV_HINTS=1` (see `man brew`).
==> Caveats
==> [email protected]
We've installed your MySQL database without a root password. To secure it run:
    mysql_secure_installation

MySQL is configured to only allow connections from localhost by default

To connect run:
    mysql -u root

[email protected] is keg-only, which means it was not symlinked into /opt/homebrew,
because this is an alternate version of another formula.

If you need to have [email protected] first in your PATH, run:
  echo 'export PATH="/opt/homebrew/opt/[email protected]/bin:$PATH"' >> ~/.zshrc

For compilers to find [email protected] you may need to set:
  export LDFLAGS="-L/opt/homebrew/opt/[email protected]/lib"
  export CPPFLAGS="-I/opt/homebrew/opt/[email protected]/include"

To start [email protected] now and restart at login:
  brew services start [email protected]
Or, if you don't want/need a background service you can just run:
  /opt/homebrew/opt/[email protected]/bin/mysqld_safe --datadir\=/opt/homebrew/var/mysql