How to Install MySQL 8 on MacOS with Homebrew

This step-by-step tutorial will guide you through the installation process using Homebrew (Brew).

Posted on

MySQL is a popular open-source relational database management system that allows you to store and manage data efficiently. If you’re a MacOS user and want to set up MySQL 8 on your system, this step-by-step tutorial will guide you through the installation process using Homebrew (Brew). Homebrew is a package manager that simplifies the installation of software on macOS, making it an excellent choice for setting up MySQL.

Preconditions

Before you begin the installation process, make sure you have the following prerequisites in place:

  1. Homebrew

    Ensure that you have Homebrew installed on your macOS system. If you haven’t already installed Homebrew, you can do so by running the following command in your terminal:

    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
    
  2. Xcode Command Line Tools

    You’ll need Xcode Command Line Tools for some of the dependencies during the MySQL installation. You can install them using the following command:

    xcode-select --install
    

With these prerequisites in place, you’re ready to proceed with the MySQL 8 installation.

Installing MySQL 8 with Homebrew

Follow these steps to install MySQL 8 on your macOS using Homebrew:

1. Update Homebrew

First, make sure your Homebrew installation is up-to-date by running:

brew update

2. Install MySQL 8

Now, you can install MySQL 8 by running the following command:

brew install [email protected]

This command will download and install MySQL 8 on your macOS system.

3. Start MySQL Service

To start the MySQL service, use the following command:

brew services start [email protected]

MySQL will now be running as a background service.

4. Secure MySQL Installation

For security purposes, it’s recommended to run the MySQL secure installation script:

mysql_secure_installation

Follow the prompts to set a root password and secure your MySQL installation.

Creating a Database and Table

Now that MySQL is up and running, you can create a database and a table to get started:

1. Access MySQL

To access the MySQL command-line interface, use:

mysql -u root -p

Enter the root password you set during the secure installation.

2. Create a Database

You can create a new database with a name of your choice. Replace your_database with your preferred database name:

CREATE DATABASE your_database;

3. Create a Table

Let’s create a sample table within your database:

USE your_database;

CREATE TABLE sample_table (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(255),
    age INT
);

You’ve now created a database and a table within it.

Managing the MySQL Service

You may need to manage the MySQL service at times, such as starting, stopping, or restarting it. Here are the commands to do so:

  • Start MySQL:

    brew services start [email protected]
    

    This command brings that MySQL runs in the background and restart when the computer is restarted.

    Or you can use brew services run [email protected]. With this command, Mysql doesn’t restart when the computer is restarted.

  • Stop MySQL:

    brew services stop [email protected]
    
  • Restart MySQL:

    brew services restart [email protected]
    

Conclusion

Congratulations! You’ve successfully installed MySQL 8 on your macOS using Homebrew. You’ve also created a database, a table, and learned how to manage the MySQL service. Now you’re ready to start using MySQL for your data storage and management needs on your macOS system. Happy querying!

If you want to learn more about MySQL, please use our MySQL tutorials and MySQL Reference.