Installing MySQL can be done using the Terminal on a Mac using something called
“homebrew”. In this video, I’ll show you how to do this and connect to the MySQL database.
There are two ways you can install MySQL on a Mac. The first is using an installation file,
or DMG file, which I have covered in another video. The second is using a
tool called Homebrew, which we’ll cover in this video.
Homebrew is a package manager for macOS that makes it easy to install software on a Mac.
Let’s get started.
The first step is to open Terminal. You can do this on a Mac by pressing Command and Space,
and start typing the word Terminal. Once it appears, press Enter to open it.
A Terminal window will open and should look something like this.
Next, we will install Homebrew. Your Mac may already have it installed,
so we’ll check that first. If it’s not installed, we can install it.
To check if Homebrew is installed, you can type. “brew help”.
If Homebrew is installed, you’ll get an output with some help on different homebrew commands.
If you get an error message such as “command not found”, then Homebrew is not installed.
Alternatively, you could type “which brew”, which will show the path to homebrew if it’s installed,
or the message of “brew not found” if it’s not installed.
How can we install it?
We can install it with a single command here.
You can get this command from the description below. Or, you can go to the Homebrew website
at [brew.sh](http://brew.sh) to see this script to copy in this text box.
Copy this into the Terminal and press Enter. This will download
and run the installation script for Homebrew.
Once you press Enter, Homebrew will start to install.
Once it is complete, your screen will look like this.
Now that you have Homebrew set up, it’s time to install MySQL.
To do this, run this command: brew install mysql
The brew means you are using the Homebrew tool. Install means you want to install something,
and MySQL is the software to install. Homebrew knows what this means and where to get it from.
It will install the most recent version of the MySQL package that is available.
After a little while, the package will be installed. There’s a lot of output here
from the different steps that Homebrew takes. You can read through it if you’re interested,
but at the end of the output you’ll see the prompt again.
And that’s the installation process complete!
Now it’s installed, you can check the version that was installed if you’re curious. To do this, type
“brew info mysql”. This will show the information for the mysql package you just installed.
The output of this statement is shown here. At the top it shows the version,
which is 8.0.28 in this video.
You’ll then see a range of other output such as conflicts, dependencies, and other information.
There are two things to notice here. The first is this line here about mysql_secure_installation.
It says that the root account is set up with no password, but we can secure it
by running this mysql_secure_installation command. We’ll do this later in the video.
The second is the connect string, which we’ll run shortly as well.
But first, we need to run the mysql server. It’s installed but it’s not running.
To start the mysql server, we run this command: “brew services start mysql”.
After a moment, it will be running.
How can we tell it’s running? There are a few ways. One way is to see a list of
services that homebrew is managing. We can run “brew services list”.
This will show a list of packages installed by homebrew. You’ll see MySQL listed here. The
status will say “started” if it’s running. If it says something else, it means it’s not running.
So, now that MySQL is running, let’s try to connect.
As we saw earlier, there is no password to the root account.
So we can login to mysql by running this command: mysql -u root.
This means the username to use is root, and no password is specified.
After you press Enter, you’ll be logged in to MySQL.
If you get an error message, it could be because the server is not running,
or the command is incorrect. So, I would suggest using Google to search for the error message.
If you get an error that says “bash mysql command not found”, you can resolve this
in a few ways. one of which is to create a bash profile, which I’ve detailed the steps
in my other MySQL Install video. See the link here or in the description for how to do this.
Now that the server is running and we have connected to it,
let’s run a simple query to check the database.
We’ll run the command “show databases;” with a semicolon at the end, to see what databases exist.
When we press enter to run it, we’ll see a list of a few databases that come with MySQL.
So we can see it all looks OK.
Type exit and press enter to exit the MySQL prompt and return to the Terminal.
So, we’ve logged into MySQL using the root account, but there is no password.
This was also mentioned when we ran the brew info mysql command earlier.
We can run this mysql_secure_installation command
to set a password for the root account. We don’t have to,
if you’re just running this on your own computer, but it’s a good idea to make it more secure.
Run the command like this. You’ll be prompted for a few settings to change.
Set a root password as part of this process.
Once this command is complete, you can connect to the database again.
However you’ll need to specify a password for the root account, because we just set one.
So, connect using this command: mysql -u root -p.
You’ll be prompted to enter a password, so enter the password you just provided and press Enter.
You’ll be taken to the MySQL prompt. Your login was successful! Type the exit command to exit.
So that’s how we can install MySQL on our Mac computer using Homebrew.
Now you’ve got that set up, you can do a range of things on the database. You
could set up MySQL Workbench and get started using that,
which is a popular IDE for MySQL. Check out my tutorial video here on how to use it.
You can then start creating your own database on MySQL.
If you want to learn more about database design and SQL, visit my website at databasestar.com.
If you liked this video, consider subscribing to my channel.
Thanks for watching.