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

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:

bash
# Update Homebrew's package list
brew update
 
# Install MySQL 8
brew install mysql

The installation includes both the server and the command-line client. After it finishes, start the service:

bash
# Start MySQL now and register it to launch on boot
brew services start mysql

Verify that the service is running:

bash
# Check the MySQL service status
brew services list | grep mysql

You 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:

bash
# Interactive security configuration
mysql_secure_installation

The script asks a series of questions:

  1. Validate password component — choose Y for medium or strong password policy, or N to skip.
  2. New root password — set a password you will remember.
  3. Remove anonymous users — choose Y.
  4. Disallow root login remotely — choose Y for safety.
  5. Remove test database — choose Y.
  6. Reload privilege tables — choose Y.

Verifying the Installation

bash
# Check the installed version
mysql --version
 
# Log in as root
mysql -u root -p

Enter the root password you just set. If you see the mysql> prompt, the server is running and accepting connections.

Updating and Uninstalling

bash
# 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/mysql

Tip

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:

  1. Visit the MySQL Community Downloads page.
  2. Select macOS and download the DMG archive for your architecture (x86_64 for Intel, ARM64 for Apple Silicon).
  3. Open the DMG and run the .pkg installer.
  4. Follow the prompts. The installer places MySQL in /usr/local/mysql.
  5. 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):

bash
# Add MySQL to PATH
export PATH="/usr/local/mysql/bin:$PATH"

Reload the profile:

bash
source ~/.zshrc

Verify with mysql --version.

If your team uses Docker on Windows and Linux as well, running MySQL in a container on macOS keeps everyone's environment identical.

  1. Install Docker Desktop for Mac from docker.com.
  2. Pull and start the MySQL container:
bash
# 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
  1. Verify the connection from your Mac:
bash
# Connect using the MySQL client inside the container
docker exec -it jdbc-mysql mysql -u root -p

The 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:

bash
mysql -u root -p

Run:

For Docker installations, replace 'jdbc_user'@'localhost' with 'jdbc_user'@'%' so the user can connect from outside the container:

sql
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:

bash
brew link mysql --force

Or 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:

bash
sudo chown -R $(whoami) /usr/local/var/mysql
brew services restart mysql

Can 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.