How to Install MySQL 8 on macOS Using Homebrew
Tags: mysql, tutorial, database, beginners
Published: 2022-10-31 13:52:02

> 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](https://support.apple.com/en-in/guide/terminal/apd5265185d-f365-44cb-8b09-71a064a42125/mac) on your Mac and run the following command. 

```bash
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.  

```bash
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. 

```bash
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. 

```bash
/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](https://docs.brew.sh/Installation). Once Homebrew is installed **restart the Terminal app** and rerun the following command to check if Homebrew was installed successfully on your machine. 

```bash
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. 

```bash
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. 

```bash
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.

```bash
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/67qs1gdxwts2xo1i36qb.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7wztx1n2vyx0rpdijdzk.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l6w4g0pqyzjjsuk7tavw.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/51p0vi5lj852tuklbvgi.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oli933djx3gwu1fozrvw.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lbdro6zax7cccws8ht9r.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fgx9f14toz574dkfdqmz.png)

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:

```bash
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fdylxmnocrv5krbrsf97.png)

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

```bash
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: 

```bash
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. 


## Subscribe 🙏😊
If I could help you, consider subscribing to [Cloudaffle, my YouTube Channel] (https://www.youtube.com/c/cloudaffle), where I keep posting in-depth tutorials, and all stuff is edutainment for software developers. You can also follow me on Hashnode; here is my profile handle @[Cloudaffle](https://hashnode.com/@cloudaffle) Leave a like if you liked the article; it keeps my motivation high 👍.