How to Install MySQL on Mac: Step-by-Step Guide
To install
MySQL on a Mac, you can use Homebrew by running brew install mysql in the Terminal. Alternatively, download the official MySQL installer from the MySQL website and follow the setup wizard to complete the installation.Syntax
Here are the main commands and steps to install MySQL on Mac:
brew install mysql: Installs MySQL using Homebrew package manager.brew services start mysql: Starts the MySQL server as a background service.mysql_secure_installation: Runs a script to secure your MySQL installation by setting root password and removing test databases.- Alternatively, download the
.dmginstaller from the official MySQL website and follow the graphical setup wizard.
bash
brew install mysql brew services start mysql mysql_secure_installation
Example
This example shows how to install MySQL using Homebrew and start the server:
bash
brew install mysql brew services start mysql mysql -u root -p
Output
==> Downloading https://homebrew.bintray.com/bottles/mysql-8.0.33.catalina.bottle.tar.gz
==> Pouring mysql-8.0.33.catalina.bottle.tar.gz
==> Caveats
MySQL is configured to only allow connections from localhost by default.
To start mysql now and restart at login:
brew services start mysql
To connect:
mysql -u root
Warning: The server is running with the default root password.
Run 'mysql_secure_installation' to improve security.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.33 Homebrew
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
Common Pitfalls
Common mistakes when installing MySQL on Mac include:
- Not starting the MySQL server after installation, so
mysqlcommands fail. - Skipping
mysql_secure_installation, leaving the root user without a password. - Confusing the Homebrew installation with the official installer, which have different setup steps.
- Not adding MySQL to your PATH, causing command not found errors.
Always verify the server is running and secure your installation.
bash
brew install mysql # Wrong: Forgetting to start the server mysql -u root -p # Right: Start server before connecting brew services start mysql mysql -u root -p
Quick Reference
Summary of key commands for installing and managing MySQL on Mac:
| Command | Description |
|---|---|
| brew install mysql | Install MySQL using Homebrew |
| brew services start mysql | Start MySQL server as a background service |
| mysql_secure_installation | Secure MySQL by setting root password and removing test data |
| mysql -u root -p | Connect to MySQL server as root user |
| brew services stop mysql | Stop MySQL server |
Key Takeaways
Use Homebrew with 'brew install mysql' for a quick MySQL installation on Mac.
Always start the MySQL server with 'brew services start mysql' before using it.
Run 'mysql_secure_installation' to set a root password and secure your database.
You can also use the official MySQL installer from the MySQL website for a graphical setup.
Check that MySQL commands work by connecting with 'mysql -u root -p' after installation.