Installing MySQL on macOS
macOS offers two solid paths to a running MySQL server: a direct installation via Homebrew or the official installer, and a Docker-based approach that mirrors the workflow on every other platform. This chapter covers both so you can pick the one that fits your workflow.
Prerequisites
- macOS 10.15 (Catalina) or later
- Administrator access
- Approximately 500 MB of free disk space
Option A: Homebrew (Recommended for Native Install)
Homebrew is the most popular package manager for macOS and the cleanest way to install MySQL without manual downloads.
Installing MySQL
Open Terminal and run:
# Update Homebrew's package list
brew update
# Install MySQL 8
brew install mysqlThe installation includes both the server and the command-line client. After it finishes, start the service:
# Start MySQL now and register it to launch on boot
brew services start mysqlVerify that the service is running:
# Check the MySQL service status
brew services list | grep mysqlYou should see mysql with status started.
Securing the Installation
MySQL installed via Homebrew does not set a root password by default. Run the security script to configure one:
# Interactive security configuration
mysql_secure_installationThe script asks a series of questions:
- Validate password component — choose
Yfor medium or strong password policy, orNto skip. - New root password — set a password you will remember.
- Remove anonymous users — choose
Y. - Disallow root login remotely — choose
Yfor safety. - Remove test database — choose
Y. - Reload privilege tables — choose
Y.
Verifying the Installation
# Check the installed version
mysql --version
# Log in as root
mysql -u root -pEnter the root password you just set. If you see the mysql> prompt, the server is running and accepting connections.
Updating and Uninstalling
# Upgrade to the latest MySQL version
brew upgrade mysql
# Stop the service
brew services stop mysql
# Uninstall completely
brew uninstall mysql
rm -rf /usr/local/var/mysqlTip
Apple Silicon Macs
On M1/M2/M3 Macs, Homebrew installs software to /opt/homebrew instead of /usr/local. The mysql binary is automatically added to your PATH by Homebrew's shell integration, so mysql --version should work immediately after installation.
Option B: Official DMG Installer
If you prefer not to use Homebrew, download the official installer:
- Visit the MySQL Community Downloads page.
- Select macOS and download the DMG archive for your architecture (x86_64 for Intel, ARM64 for Apple Silicon).
- Open the DMG and run the
.pkginstaller. - Follow the prompts. The installer places MySQL in
/usr/local/mysql. - Open System Settings → MySQL to start and stop the server, or use the preference pane in older macOS versions.
After installation, add MySQL to your PATH by editing your shell profile (~/.zshrc or ~/.bash_profile):
# Add MySQL to PATH
export PATH="/usr/local/mysql/bin:$PATH"Reload the profile:
source ~/.zshrcVerify with mysql --version.
Option C: Docker (Recommended for Team Consistency)
If your team uses Docker on Windows and Linux as well, running MySQL in a container on macOS keeps everyone's environment identical.
- Install Docker Desktop for Mac from docker.com.
- Pull and start the MySQL container:
# Pull the MySQL 8 image
docker pull mysql:8
# Run the container with port mapping and credentials
docker run -d \
--name jdbc-mysql \
-p 3306:3306 \
-e MYSQL_ROOT_PASSWORD=rootpass \
-e MYSQL_DATABASE=jdbc_demo \
-e MYSQL_USER=jdbc_user \
-e MYSQL_PASSWORD=jdbcpass \
mysql:8- Verify the connection from your Mac:
# Connect using the MySQL client inside the container
docker exec -it jdbc-mysql mysql -u root -pThe Docker steps match Installing MySQL via Docker on Windows and Linux. Use that chapter for detailed explanations of each flag, volume persistence, and container management.
Creating the Tutorial Database
Once MySQL is running — whether via Homebrew, DMG, or Docker — create the tutorial database and user. Open Terminal and log in:
mysql -u root -pRun:
For Docker installations, replace 'jdbc_user'@'localhost' with 'jdbc_user'@'%' so the user can connect from outside the container:
CREATE USER 'jdbc_user'@'%' IDENTIFIED BY 'jdbcpass';
GRANT ALL PRIVILEGES ON jdbc_demo.* TO 'jdbc_user'@'%';
FLUSH PRIVILEGES;FAQ
Homebrew says "mysql 8.x is already installed" but mysql command is not found.
Homebrew may have installed MySQL but not linked it due to a conflict. Run:
brew link mysql --forceOr check if the binary is in /opt/homebrew/bin/mysql (Apple Silicon) and add that directory to your PATH.
MySQL fails to start after a macOS update.
macOS updates sometimes reset file permissions in /usr/local/var/mysql. Run:
sudo chown -R $(whoami) /usr/local/var/mysql
brew services restart mysqlCan I use the same Docker commands on macOS that you showed for Windows?
Yes. Docker containers behave identically across platforms. The docker run, docker exec, and docker rm commands in chapter 04 work verbatim on macOS.
Should I use Homebrew or Docker?
Choose Homebrew if you want the fastest native performance and do not mind a small service running in the background. Choose Docker if you work on a team that standardizes on containers, or if you frequently need to wipe and recreate the database from scratch.