Mac O’Clock
Published in

Mac O’Clock

Installing MySQL 5.7 using Homebrew

Photo by mnm.all on Unsplash

Install Homebrew

Installing Homebrew is probably the easiest step in the whole process. First, install xcode command line tools by running:

xcode-select --install
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew -v

Install MySQL

At the time of this writing, the default version of MySQL in Homebrew is 8.0. We can check this by running brew info mysql. However, we’re aiming to install version 5.7 so let's check which is the latest version by appending @5.7 to the default package key: brew info mysql@5.7. The expected output should look something like this:

mysql@5.7: stable 5.7.29 (bottled) [keg-only]
Open source relational database management system
https://dev.mysql.com/doc/refman/5.7/en/
Not installed

Starting/Stopping MySQL

Since MySQL runs as a service, we need to check if it is running: brew services list. Our output should look like this:

Name      Status  User Plist
mysql@5.7 stopped
brew services start mysql@5.7
  • % brew services list to list all running services
  • % brew services stop mysql@5.7 to stop MySQL
  • % brew services restart mysql@5.7 to restart MySQL

Securing your MySQL Installation

Once we have MySQL installed and our service is up and running, our next task is to secure the installation. This is done by running the command mysql_secure_installation.

  • Do we want to remove anonymous users? Yes. Since this is a local installation we have no need for anonymous users.
  • Disallow root login remotely? Yes. Let's follow the installer recommendation: "Normally, root should only be allowed to connect from ‘localhost’. This ensures that someone cannot guess at the root password from the network."
  • Remove test database and access to it? Yes. In MySQL, anyone can connect to the test database so it's a good idea to not worry about this.

Connect to MySQL

We are now ready to connect to MySQL and run a few commands. Type mysql -u root -p in your terminal and enter your password. If you followed the steps correctly, we should get the following output:

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.29 Homebrew
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.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>
mysql> CREATE DATABASE test_db;
mysql> USE test_db;
mysql> CREATE TABLE users (
user_id int(11) unsigned NOT NULL AUTO_INCREMENT,
last_name varchar(55) DEFAULT NULL,
first_name varchar(55) DEFAULT NULL,
PRIMARY KEY (user_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
mysql> DESCRIBE users;
mysql> quit

Uninstalling MySQL

In case you decide version 5.7 is not the one you needed, stop the service brew services stop mysql@5.7 and uninstall it by running: brew uninstall mysql@5.7. Finally, perform a cleanup brew cleanup.

mysqldump -u root -p test_db --single-transaction --quick --lock-tables=false > test_db-backup-$(date +%F).sql

Completely Remove MySQL

In most cases, the previous step should be enough to remove the software. You might, however, want to completely remove all the files that were installed. Since we used Homebrew, the only folder we need to worry about is /usr/local/var/mysql. We can quickly delete this folder by running:

sudo rm -rf /usr/local/var/mysql

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Diego Lorenesi

Diego Lorenesi

22 Followers

Father, husband, engineer, MBA, tech/taco enthusiast. Currently working for a Chocolate Factory 🍫.