Introduction
MySQL is a popular relational database management system (RDBMS) widely used in web development. Installing MySQL on your macOS environment can streamline your development process. This guide will walk you through the installation process, checking if the server is running, and interacting with it through various methods.
How to Install MySQL Server on macOS
Method 1: Using Homebrew
Homebrew is a package manager for macOS that simplifies the installation of software. If you don’t have Homebrew installed, you can install it by running the following command in your terminal:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Once Homebrew is installed, you can use it to install MySQL:
brew install mysql
Method 2: Using MySQL Installer
Alternatively, you can download the MySQL installer directly from the MySQL website:
- Visit the MySQL Community Downloads page.
- Select macOS as your operating system.
- Download the DMG archive and open it.
- Follow the installation instructions provided by the installer.
How to Check if MySQL Server is Running
After installation, you need to start the MySQL server. You can do this using Homebrew services or directly from the terminal.
Using Homebrew Services
To start MySQL using Homebrew services, run:
brew services start mysql
To stop MySQL:
brew services stop mysql
Using Terminal
To start MySQL directly from the terminal:
mysql.server start
To stop MySQL:
mysql.server stop
Verifying the Server Status
To check if the MySQL server is running, you can use the following command:
mysqladmin -u root -p status
You’ll be prompted to enter the MySQL root password. If the server is running, you’ll see a status report.
Adding mysql.server to the PATH
Note that if you get a “command not found” error when you run the mysql.server command in the terminal, you need to add it to the PATH variable. The mysql.server binary file is in the /usr/local/mysql/support-files directory, so you can add it to PATH like this:
export PATH=$PATH:/usr/local/mysql/support-files
source $HOME/.zshrc
Adding mysqladmin to the PATH
Similarly, if you get a “command not found” error when you run the mysqladmin command, since the binary file is in the /usr/local/mysql/bin directory, you need to add that directory to your PATH variable.
export PATH=$PATH:/usr/local/mysql/bin
source $HOME/.zshrc
Different Ways to Interact with MySQL
There are several ways to interact with MySQL on macOS. Here, we’ll cover using the terminal and some popular programs.
Using Terminal
The terminal provides a direct way to interact with MySQL. To access the MySQL command-line interface (CLI), use:
mysql -u root -p
You’ll be prompted for the root password, and then you’ll have access to the MySQL shell, where you can run SQL commands.
Using MySQL Workbench
MySQL Workbench is a unified visual tool for database architects, developers, and DBAs. It provides a graphical interface to interact with MySQL databases.
- Download MySQL Workbench from the MySQL Community Downloads page.
- Open the DMG file and follow the installation instructions.
- Launch MySQL Workbench and configure a new connection to your local MySQL server.
Using TablePlus
TablePlus is a modern, native tool with an intuitive interface that allows developers to manage multiple databases efficiently. It supports various databases including MySQL, PostgreSQL, SQLite, Microsoft SQL Server, and more.
- Download TablePlus from the official website.
- Open the DMG file and move TablePlus to your Applications folder.
- Launch TablePlus and create a new connection to your MySQL server.