How to install/upgrade MySQL in Mac OS

Updated: January 25, 2024 By: Guest Contributor Post a comment

Introduction

Installing or upgrading MySQL on your Mac OS system is a crucial task if you’re working with databases or developing applications that require a database back-end. This tutorial will guide you through the process step-by-step, whether you’re a complete beginner or looking to update your current MySQL setup.

Before you start, make sure you have administrative access to your Mac, a reliable internet connection, and some basic knowledge of the terminal.

Installing MySQL on Mac OS

Method 1: Using Homebrew

Homebrew is a popular package manager for Mac that makes software installations quite easy. If you don’t have Homebrew installed, you can run the following command in your terminal to install it:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

After installing Homebrew, you can install MySQL by running:

brew install mysql

The above command will download and install the latest stable release of MySQL. Once installed, you can start the MySQL service with:

brew services start mysql

To verify that MySQL has successfully started, you can run:

mysql -u root -p

This command will prompt you for the MySQL root password. Since this is your first time running MySQL, the password will be empty — just press Enter to continue.

Method 2: Using the Official MySQL Installer

You can also install MySQL using the official installer from the MySQL website. Here’s how:

  1. Visit the MySQL downloads page and select the macOS version.
  2. Download the appropriate DMG archive for your machine.
  3. Open the DMG file and follow the installation prompts.
  4. During installation, take note of the temporary root password provided; you’ll need this to access MySQL after the installation.

After the installation completes, you can start MySQL through the system preferences or by using the following command:

sudo /usr/local/mysql/support-files/mysql.server start

Configuring MySQL Post Installation

After installing MySQL, you may want to secure your setup. Run the included security script:

mysql_secure_installation

This script will guide you through the process of setting a root password, removing anonymous users, disabling remote root logins, and removing the test database.

Upgrading MySQL on Mac OS

If you have an older version of MySQL and you need to upgrade to the latest version, the steps will depend on how you installed it originally.

Upgrading with Homebrew

If you installed MySQL via Homebrew, upgrading is straightforward:

brew update
brew upgrade mysql

If the service is running, you’ll want to stop it before upgrading:

brew services stop mysql

Then proceed with the update and upgrade commands. Once the upgrade is complete, you can start the service again:

brew services start mysql

Upgrading with the Official Installer

In case you installed MySQL using the official installer, you’ll need to download the new version from the MySQL website and run the installer:

  1. Visit the MySQL downloads page and select the latest macOS version.
  2. Download and run the DMG file.
  3. Follow the on-screen instructions to install the new version.

Note: It’s recommended that you backup your data before upgrading. You might also have to migrate your data to the new version if there are incompatibilities.

Working with MySQL

Once installed or upgraded, you might want to interact with MySQL. Here’s how to access the MySQL shell:

mysql -u root -p

You can create a new database by executing:

CREATE DATABASE my_database;

To select a database and work with it:

USE my_database;

And to retrieve information about users:

SELECT User, Host, authentication_string FROM mysql.user;

The MySQL world is quite extensive, and there are many more commands to explore. Consider looking into MySQL documentation for more advanced usage and functionalities.

Conclusion

Whether you’re a developer, a database administrator, or just curious, installing or upgrading MySQL on your Mac OS is important. By following the steps outlined in this tutorial, you should have MySQL up and running or your existing setup updated to the latest version. As always, be sure to back up your databases before performing major operations like upgrades.