# How to install MySQL on macOS

> Learn how to install MySQL on macOS with Homebrew, start the server with brew services, and lock it down by running mysql_secure_installation.

Author: Flavio Copes | Published: 2020-01-05 | Canonical: https://flaviocopes.com/mysql-how-to-install/

On macOS, you can install MySQL easily using [Homebrew](https://flaviocopes.com/homebrew/).

Run:

```bash
brew install mysql
```

The above command should take a while, then print something like this:

![Terminal output showing MySQL installation completion via Homebrew with initialization instructions](https://flaviocopes.com/images/mysql-how-to-install/Screen_Shot_2019-12-17_at_19.23.24.png)

You can now start the MySQL server by running:

```bash
brew services start mysql
```

Now we need to secure the MySQL server. By default the server comes without a root password, so we need to make sure it's protected.

Run:

```bash
mysql_secure_installation
```

The procedure can take a while, but it gives a lot of power to make sure you get the best defaults out of the box:

![MySQL secure installation wizard showing password validation, anonymous user removal, and test database cleanup](https://flaviocopes.com/images/mysql-how-to-install/Screen_Shot_2019-12-17_at_19.27.14.png)

Since we used `brew services start mysql` to start MySQL, your Mac will re-start it at reboot. You can run:

```bash
brew services stop mysql
```

to stop this from happening, and also to immediately stop MySQL.

You can also avoid this **daemon mode** (that's what we call programs that always run in the background and restart when the computer is restarted) by running:

```bash
mysql.server start
```

This will start MySQL and will keep it running until the computer is shut down, or until you run:

```bash
mysql.server stop
```

and it will not re-start it at reboot.

It's up to you to decide which one you prefer.

Now you can connect to the server using the command:

```bash
mysql -u root -p
```

You will need to type the `root` user password _after_ you run this command, and once you are done you should see this screen:

![MySQL command line interface showing successful login with welcome message and mysql prompt](https://flaviocopes.com/images/mysql-how-to-install/Screen_Shot_2019-12-18_at_19.07.11.png)

A great GUI (graphical) software we can use to interact with a SQLite database is TablePlus.

It comes with a free trial that's perfect for our usage, because it's not time-based but rather it limits the amount of concurrent connections you can make to the database.

Download it from <https://tableplus.com>. I know there are macOS, Windows and Linux versions.

![TablePlus welcome screen showing the main interface with backup, restore, and register options](https://flaviocopes.com/images/mysql-how-to-install/Screen_Shot_2019-12-16_at_21.12.18.png)

Click "Create a new connection..." and select MySQL in the list:

![TablePlus database selection dialog displaying various database type icons including MySQL highlighted in orange](https://flaviocopes.com/images/mysql-how-to-install/Screen_Shot_2019-12-17_at_19.31.29.png)

then set a name for the connection, and enter "root" and the password you set previously:

![TablePlus MySQL connection setup form with fields for name, host, port, user, and password configuration](https://flaviocopes.com/images/mysql-how-to-install/Screen_Shot_2019-12-17_at_19.32.12.png)

Click `Connect`, and you should be connected to MySQL!

![TablePlus main interface after successful MySQL connection showing empty database with central illustration](https://flaviocopes.com/images/mysql-how-to-install/Screen_Shot_2019-12-17_at_19.31.54.png)

Note that we are connected using the `root` user, which should only be used for administration purposes.

Day to day use of a database should be done using a normal user. We'll see it in a separate tutorial.
