MySQL on macOS, when installed via Homebrew, doesn’t actually run on your Mac. It runs inside a Docker container, and Homebrew just orchestrates the setup.
Let’s see it in action.
First, ensure you have Homebrew installed. If not, grab it from brew.sh.
Now, install MySQL:
brew install mysql
This command downloads the MySQL package and its dependencies. It’s not just a simple binary; Homebrew handles the complexity of setting up the necessary environment.
Next, start the MySQL service:
brew services start mysql
This is where the magic happens. Instead of directly launching a mysqld process on your host machine, Homebrew, through its services mechanism, interacts with a system that manages a containerized MySQL instance. For most modern Homebrew installations of MySQL, this means it’s leveraging Docker or a similar containerization technology under the hood. Your brew services start mysql command tells the Homebrew service manager to ensure the MySQL container is running and accessible.
To verify it’s running, you can check the Homebrew services status:
brew services list
You should see mysql listed with a started status.
Now, to actually connect to your MySQL server, you’ll use the standard MySQL client. The Homebrew installation configures it to listen on 127.0.0.1 (localhost) on the default MySQL port 3306.
mysql -u root -p
When prompted for a password, if this is your first time connecting after installation, you likely haven’t set one. Press Enter to proceed without a password. The default configuration for a fresh Homebrew MySQL install often allows passwordless root access initially for ease of setup.
To secure your installation, the very first thing you should do is set a root password.
mysql -u root
Then, inside the MySQL prompt:
ALTER USER 'root'@'localhost' IDENTIFIED BY 'your_strong_password_here';
FLUSH PRIVILEGES;
EXIT;
Replace 'your_strong_password_here' with a strong, unique password. This command directly modifies the user authentication settings within the MySQL server running inside its container.
You can create databases, tables, and users just as you would with any other MySQL installation. For example:
mysql -u root -p
Enter your password.
CREATE DATABASE myapp_db;
CREATE USER 'myapp_user'@'localhost' IDENTIFIED BY 'another_strong_password';
GRANT ALL PRIVILEGES ON myapp_db.* TO 'myapp_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;
This entire setup is designed to isolate MySQL from your main macOS environment, preventing conflicts with other software and making uninstallation clean. The Homebrew services command is essentially a wrapper around container orchestration, ensuring your MySQL instance starts when your machine boots (if you use brew services start) and stops gracefully when you tell it to.
The real power of this setup is its reproducibility and isolation. If you need to reset your MySQL environment, you can simply stop and remove the service, and Homebrew can manage the cleanup of its associated data directories (though be careful, this deletes your data).
What most people don’t realize is that Homebrew doesn’t just install MySQL; it installs a managed service. This service manager is responsible for starting, stopping, and keeping the MySQL process (or rather, its container) alive. When you run brew services start mysql, you’re not just running a command; you’re telling a background daemon managed by Homebrew to ensure that the MySQL service is active. This daemon uses system-level integration (like launchd on macOS) to manage the lifecycle of the service, ensuring it starts on login or boot if configured.
The next hurdle you’ll likely encounter is configuring your application to connect to this specific MySQL instance, especially if you’re dealing with multiple MySQL versions or other database services running on your machine.