Skip to main content

How to Install MySQL 8 on macOS Using Homebrew

ยท 8 min read

A step-by-step guide ๐Ÿ“™ on how to install, configure and run MySQL 8 server on macOS using Homebrew ๐Ÿบ

I use Homebrew on macOS to manage all the packages I need for development. Once you understand how Homebrew works and how it helps you manage various packages on macOS, installing any dependency on your Mac becomes easy. We will install Homebrew and learn a few essential commands on how we can use Homebrew. Once through with installing Homebrew, we would then move on to install MySQL server using Homebrew and configure it.

What is Homebrew?โ€‹

Homebrew is a package manager for macOS and Linux (I know you would instead use apt-get or yum based on which Linux distro you are using), but just putting it on record that it works with Linux as well ๐Ÿ˜Š. Why would you use a package manager like Homebrew when all the packages come with their independent .pkg files, and these files can be used to install each package independently? We use a package manager because of a couple of advantages package managers have to offer over independently installing each page.

  • The main difference between Homebrew and the installer package is the fact that Homebrew takes care of the build time dependencies. If the package you are trying to install requires ruby and some of its dependencies, Homebrew does not require you to build/install ruby and libraries from scratch. Homebrew makes the process of installing packages easy.

  • Every installed package is cleanly sandboxed into its cellar (directory), so you don't have stray files all over your system, just symlinks from bin, man, etc. In the case of macOS with Apple Silicon, all the packages are installed in /opt/homebrew/cellar directory. Hence you know where all packages are installed.

  • Homebrew lets you install all the packages with root access and Homebrew installs without root access as well into /usr/local (Intel) or /opt/homebrew (Apple Silicon)

  • Uninstalling is hardly an issue anymore. Homebrew will manage the uninstall process and trim the run time dependencies if needed.

  • Homebrew provides you with many tools to manage all the packages installed on your machine, which includes various commands to list the packages installed, uninstall them, upgrade packages, search for packages, and so on.

Step One: Installing the Xcode Command Line Toolsโ€‹

Before we install any package or Homebrew itself, we first need to install the Xcode Command Line Tools. To do so open the Terminal app on your Mac and run the following command.

xcode-select --install

Step Two: Install Homebrewโ€‹

If you do not have Homebrew installed on your machine, you can follow along, but if you have Homebrew installed and already working with it, you can move on to the next step. You can check if Homebrew is installed on your machine by running the following command in your Terminal.

brew --version

If Homebrew is installed on your Mac, you should get a response containing the version of the Homebrew installed on your machine, which should look like this.

Homebrew >=3.x.x

// Where 3.x.x is the version of the Homebrew installed on your machine.
// Anything above 3 is good

If Homebrew is already installed, you can move to the next step. If not, hang on while we install Homebrew. To install Homebrew, run the following command in your Terminal.

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

The above command should install Homebrew for you and add it to the Path. If you are curious about Homebrew's steps while installing your machine, you can read them here. Once Homebrew is installed restart the Terminal app and rerun the following command to check if Homebrew was installed successfully on your machine.

brew --version

If Homebrew is installed on your Mac, you should get a response that will contain the homebrew version installed on your machine and should look like this.

Homebrew >=3.x.x

// Where 3.x.x is the version of the Homebrew installed on your machine.
// Anything above 3 is good

Step Three: Install MySQL 8โ€‹

Once Homebrew is installed, we will use it to install MySQL 8 on our machine. We would use the [MySQL formula] (https://formulae.brew.sh/formula/mysql) to install MySQL on our Mac. Still, run the following command in your Terminal to start the MySQL installation.

brew install mysql

Step Four: Configure MySQLโ€‹

The above command will install the latest version of MySQL on your Mac. Once MySQL is installed, we need to run the secure installation to assign a password to the root user and create and connect to databases using the root user. You might decide to create another user and not use the root user. But, for this tutorial and assuming that you are installing MySQL on macOS for development workloads, I will stick to using the root user. To secure your MySQL installation, run the following command.

mysql_secure_installation

Once you run this above command, you will be requested to assign a password to the root user, and you will see the following prompt in your terminal window. You can enter your preferred password for the root user. This will be the password you will use to connect to your database and perform all operations with MySQL.

Assign Mysql Password

Next, MySQL asks you if you want to enable the Validate Password Component. The validate password component helps you ensure that users of MySQL use passwords with minimum strength and characters. This is super helpful for production loads where many users are using MySQL, and you want to force strong passwords for all the users. Feel free to use it for production environments, but I will skip it for this tutorial because I am assuming you are configuring MySQL for development and not production.

Validate Password Component

So as a response to this question, I will type No and press return. At this point, MySQL will ask you whether you want to keep the password you just entered for the root user or you want to set a new password. Since we want to keep the password we already assigned to the root user, type in No in your Terminal and press enter.

Use Existing MySQL Password

By default, MySQL creates anonymous users, and now it will ask you whether you want to remove the anonymous users. We can remove the anonymous users as we don't need them and reply to this prompt with a Yes and press the return key.

Disable Anonymous Users

Then you get a prompt asking whether you want to disable the remote login for the root user. We will go ahead and disable the remote access for the root user, reply to this prompt with a Yes, and press the return key.

Disable Remote Access

As the next step, you are asked if you want to remove the test database MySQL created. Reply to this with a Yes as well, which should remove the test database.

Remove Test database

Finally, you are prompted to reload the privileges table for all the changes to take effect. Reply to this with a Yes and press the return key.

Reload MySQL Privileges Table

With this, we have successfully installed MySQL 8 using Homebrew. Now let's go ahead and create a new database.

Step Five: Create a New Databaseโ€‹

To create a new database with MySQL, we need to sign in as a root user to MySQL in our Terminal. To sign in as a root user, use the following command:

mysql -u root -p

Once you use this command, you will be prompted to enter your password. Enter the root password you created during the installation process. Once you enter the password, you should be logged into MySQL, and your terminal prompt should now show mysql like so:

Logging Into MySQL

Once you see the above prompt, you can create a new database using the following command.

CREATE DATABASE db_name;
// db_name is the name of your database; name it whatever you want to

You should get the following response from MySQL:

Query OK, 1 row affected (0.01 sec)

That's it; you have a new database that you can use with the root as the username and password assigned to the root user.

What Can You Do Next ๐Ÿ™๐Ÿ˜Šโ€‹

If you liked the article, consider subscribing to Cloudaffle, my YouTube Channel, where I keep posting in-depth tutorials and all edutainment stuff for ssoftware developers.

YouTube @cloudaffle