How to Install (and Uninstall) MySQL on a Mac
Tags: mysql, macos, beginners, homebrew
Published: 2024-06-10 00:15:51

# Introduction
In this tutorial, we will walk you through the actions necessary to perform on a Mac to install MySQL.

There are two methods that can be used:
* download the installation package from Oracle and launch the installer file, or
* install using `homebrew`.

Since the first method is pretty straight-forward, we will take up the second one and describe it in more detail - installation of MySQL using `homebrew`. Even though it may seem a bit more complicated, after that you will have more control over your installation and various options.

At the end of the tutorial, we will also learn to clean MySQL out of your system on a Mac.

# Installing and / or Updating Homebrew

If you don't have Homebrew yet on your Mac, please install it using the following command:
```
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
```
Before any new installation, it is recommended to update Homebrew package manager itself. The below command fetches the latest version of the Homebrew core and all the formulae (package definitions) from the official repository. This ensures that you have the most recent information about available software packages and their versions.

To update Homebrew - execute the following command:
```
brew update
```

# Installing and Running MySQL Using Homebrew

To install MySQL using Homebrew, run the following command:
```
brew install mysql
```
Once the installation is complete, use the following to run MySQL:
```
brew services start mysql
```
And, here are the corresponding commands to stop and to restart MySQL:
```
brew services stop mysql
```
```
brew services restart mysql
```
# Securing MySQL Installation

Next, you must secure your MySQL installation, this is a required step.

To do this, execute the following command:

```
mysql_secure_installation
```
Here is how I ran this command in my terminal and what options I used / how MySQL replied:
```
olgastrijewski@MacBook-Pro ~ % mysql_secure_installation

Securing the MySQL server deployment.

Enter password for user root: 

VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?

Press y|Y for Yes, any other key for No: No
Using existing password for root.
Change the password for root ? ((Press y|Y for Yes, any other key for No) : No

 ... skipping.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : Yes
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Yes
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Yes
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Yes
Success.

All done! 
```
## Command `mysql_secure_installation` not Available
Sometimes, after the installation of MySQL, you will find that this command is not available. Then you can restart your Terminal, to make sure that all settings are applied, or even restart your computer if needed. If this doesn't help, then try searching for the `mysql_secure_installation` script in the `/usr/local` directory as follows:
```
find /usr/local -name "mysql_secure_installation"
```
If you can't find it, then try searching in other common locations:
```
ls /usr/local/opt/mysql@8.0/bin
ls /usr/local/bin
```
If you still can't find it, then it is possible to secure the installation without this script, by issuing direct commands in MySQL command-line interface. Here are the commands that I issued together with MySQL responses:
```
olgastrijewski@MacBook-Pro ~ % mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.37 Homebrew

Copyright (c) 2000, 2024, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'my-password';
Query OK, 0 rows affected (0.01 sec)

mysql> DELETE FROM mysql.user WHERE User='';
Query OK, 0 rows affected (0.00 sec)

mysql> DELETE FROM mysql.user WHERE User='root' AND Host!='localhost';
Query OK, 0 rows affected (0.00 sec)

mysql> DROP DATABASE IF EXISTS test;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%';
Query OK, 0 rows affected (0.00 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

mysql> exit;
Bye
```
# Installing MySQL Workbench

MySQL Workbench makes it easy and visual to work with the MySQL installation, creating database schemas and issuing SQL queries to them.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/279vs83z7cqyrhqldi3y.png)

Again, you can install MySQL Workbench by downloading it from Oracle. But here we will install it using Homebrew.

MySQL Workbench is a "cask" in Homebrew, meaning it is a visual UI application and not a command-line tool.

To install MySQL Workbench using Homebrew, issue the following command:
```
brew install --cask mysqlworkbench
```
Then you can run it from your Applications folder as usual:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p6gcadjtl72dgo58mffl.png)

# Installing a Specific Version of MySQL

The command from the previous section - will install the latest version of MySQL. Sometimes you may want to install one of the previous versions, or even have several versions installed, so you can choose which one to run.

If you'd like to install a particular version, which is not the latest version, then you can do this as follows (for example, to install MySQL version 8.0):
```
brew install mysql@8.0
```
You will then need to specify which version of MySQL you need to start, stop or restart in the following commands:
```
brew services start mysql@8.0
```
```
brew services stop mysql@8.0
```
```
brew services restart mysql@8.0
```
If you want to see which versions are available for installation, use the following:
```
brew search mysql
```
This command will list all formulae related to MySQL. You might see output similar to this:
```
==> Formulae
mysql        mysql@5.6    mysql@5.7    mysql@8.0

==> Casks
mysqlworkbench
```
In the next section, we will be entering the MySQL command-line interface (CLI), and if you did install a specific non-latest version of MySQL, then you will need to run the following command before entering the CLI:
```
brew link --force --overwrite mysql@8.0
```
This will ensure that the MySQL binaries are properly linked in your system's PATH, making it easier to run MySQL commands from the command line without specifying the full path.

# Command-Line Interface (CLI)
When you want to connect to MySQL command-line interface, use the following command:
```
mysql -u root -p
```
It will ask you for a password - enter the one that you provided when you were securing MySQL installation.

If the login is successful, then you’ll see a prompt like this:
```
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.25 Homebrew

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>
```
In the MySQL console, you can run SQL commands. For example:
```
SHOW DATABASES;
```
This command lists all databases on the MySQL server. Each command should end with a semicolon (;).

To exit the MySQL console, you can type:
```
exit;
```
Or use the shortcut:
```
\q
```
# Uninstalling MySQL
Sometimes you will want to clean MySQL out of your MacOS system completely. For example, I did that when I had MySQL installed using the package installer, but I wanted to reinstall it using Homebrew.

Here are the commands you will need to run to do this:
```
sudo rm -rf /usr/local/mysql*
sudo rm -rf /Library/PreferencePanes/My*
sudo rm -rf /etc/my.cnf
sudo rm -rf /etc/my.cnf.d
sudo rm -rf /usr/local/var/mysql
sudo rm -rf /Library/Receipts/mysql*
sudo rm -rf /Library/Receipts/MySQL*
sudo rm -rf /private/var/db/receipts/*mysql*
sudo rm -rf /Library/Preferences/com.oracle.mysql.*
sudo rm -rf /Library/Logs/MySQL*
```
Then remove any references to MySQL from your `~/.bash_profile` and `~/.zshrc`:
```
sudo nano ~/.bash_profile
sudo nano ~/.zshrc
```
Check your `$PATH` variable to make sure it doesn't have any references to MySQL:
```
olgastrijewski@MacBook-Pro ~ % echo $PATH
/Users/olgastrijewski/.rvm/gems/ruby-2.7.2/bin:/Users/olgastrijewski/.rvm/gems/ruby-2.7.2@global/bin:/Users/olgastrijewski/.rvm/rubies/ruby-2.7.2/bin:/Users/olgastrijewski/.gem/ruby/2.7.0/bin:/usr/local/opt/openssl@1.1/bin:/Users/olgastrijewski/.sdkman/candidates/maven/current/bin:/Users/olgastrijewski/.sdkman/candidates/java/current/bin:/Users/olgastrijewski/.sdkman/candidates/ant/current/bin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/Users/olgastrijewski/.cargo/bin:/Users/olgastrijewski/.rvm/bin
olgastrijewski@MacBook-Pro ~ %
```
These commands will effectively clean MySQL completely out of your system.
# Conclusion
Installing MySQL on a Mac using Homebrew provides a flexible and powerful method for managing your database setup. While the initial steps might seem more complex than using a standard installer package, the control and customization options available through Homebrew are invaluable. This tutorial has guided you through the entire process, from installing Homebrew itself to securing your MySQL installation, installing MySQL Workbench, and even cleaning MySQL from your system if needed. By following these steps, you ensure a robust and secure MySQL environment on your Mac, tailored to your specific development needs.