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.
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.
/opt/homebrew/cellar
directory. Hence you know where all packages are installed./usr/local
(Intel) or /opt/homebrew
(Apple Silicon)
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
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
Once Homebrew is installed, we will use it to install MySQL 8 on our machine. We would use the MySQL formula to install MySQL on our Mac. Still, run the following command in your Terminal to start the MySQL installation.
brew install 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.
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.
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.
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.
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.
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.
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.
With this, we have successfully installed MySQL 8 using Homebrew. Now let's go ahead and 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:
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.
If I could help you, consider subscribing to Cloudaffle, my YouTube Channel, 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. Leave a like if you liked the article; it keeps my motivation high 👍.
Also published here.