DEV Community

Leon Wong 282
Leon Wong 282

Posted on

Installing MySQL 8.0 on macOS with Homebrew: A Complete Guide

Installing MySQL 8.0 on macOS with Homebrew: A Complete Guide

Introduction

Recently, I needed to set up a development environment on my Mac and chose to use Homebrew to install MySQL 8.0. The process was smoother than expected, so I'm sharing the complete installation steps and some practical tips here.

Prerequisites

Make sure Homebrew is already installed on your system. If not, run this command in Terminal:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Enter fullscreen mode Exit fullscreen mode

Installation Steps

1. Update Homebrew

First, update Homebrew to the latest version:

brew update
Enter fullscreen mode Exit fullscreen mode

2. Install MySQL 8.0

Run the following command to install MySQL 8.0:

brew install mysql@8.0
Enter fullscreen mode Exit fullscreen mode

The installation process will automatically download and configure MySQL, usually taking a few minutes. Homebrew automatically handles environment variable configuration, so you can use the mysql command directly after installation.

Install MySQL8.0.jpg

3. Start MySQL Service

There are two ways to start the MySQL service:

Option 1: Start immediately (without auto-start on boot)

brew services start mysql@8.0
Enter fullscreen mode Exit fullscreen mode

Option 2: Manual start

mysql.server start
Enter fullscreen mode Exit fullscreen mode

4. Security Configuration

Run MySQL's security configuration script:

mysql_secure_installation
Enter fullscreen mode Exit fullscreen mode

This script will guide you through the following configurations:

  • Set root password
  • Remove anonymous users
  • Disallow root login remotely
  • Remove test database
  • Reload privilege tables

It's recommended to select "Yes" for all options to enhance security.

Start and mysql_secure_installation.jpg

Verify Installation

Log into MySQL

Enter the password you just set to log in.

mysql -u root -p
Enter fullscreen mode Exit fullscreen mode

Test MySQL.jpg

Option: DataGrip config

If you prefer to operate the database using a GUI tool, you can configure the DataGrip connect MySQL.

Use DataGrip config.jpg

Test DataGrip.jpg

Common Commands

Service Management

# Start service
brew services start mysql@8.0

# Stop service
brew services stop mysql@8.0

# Restart service
brew services restart mysql@8.0

# Check service status
brew services list
Enter fullscreen mode Exit fullscreen mode

Connect to MySQL

# Connect as root user
mysql -u root -p

# Connect to specific database
mysql -u root -p database_name

# Specify host and port
mysql -h localhost -P 3306 -u root -p
Enter fullscreen mode Exit fullscreen mode

Uninstalling MySQL

If you need to uninstall:

# Stop service
brew services stop mysql@8.0

# Uninstall
brew uninstall mysql@8.0

# Delete data (optional)
rm -rf /opt/homebrew/var/mysql
Enter fullscreen mode Exit fullscreen mode

Conclusion

Installing MySQL 8.0 with Homebrew is very straightforward - the entire process requires just a few commands. Homebrew automatically handles dependencies and configuration, making it much easier than manual installation. Remember to complete the security configuration after installation, and you're ready to start using MySQL!

I hope this tutorial helps! Feel free to share in the comments if you encounter any other issues.

Top comments (0)