How to Set Up MySQL on macOS with Homebrew

Galaxy Glossary

How do I set up MySQL on macOS with Homebrew?

Install, secure, and run MySQL locally on macOS using Homebrew.

Sign up for the latest in SQL knowledge from the Galaxy Team!
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.

Description

How do I install MySQL on macOS with Homebrew?

Run brew update && brew install mysql. Homebrew downloads the latest GA version, compiles it when needed, and links binaries under /opt/homebrew/bin (Apple Silicon) or /usr/local/bin (Intel).

What does the installer put on my system?

The formula adds MySQL server, the client, utilities, a mysql@<version> directory, and a launchd plist for background startup.

How do I start and stop the MySQL server?

Use brew services start mysql to launch MySQL at login via launchd. Stop it with brew services stop mysql. For one-off sessions, run mysql.server start and mysql.server stop.

How do I secure the installation?

Execute mysql_secure_installation. The wizard sets a root password, removes anonymous accounts, disables remote root logins, and drops the test database.

How do I autostart MySQL at login?

brew services start mysql installs the launchd job automatically. Confirm with brew services list; status should read started.

How do I connect and create an ecommerce database?

Connect with mysql -u root -p. Then run:

CREATE DATABASE ecommerce;
USE ecommerce;
CREATE TABLE Products (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(80),
price DECIMAL(10,2),
stock INT
);

How do I verify everything works?

Insert a sample product: INSERT INTO Products(name,price,stock) VALUES ('Galaxy Tee',29.99,50);. Query it with SELECT * FROM Products;. If the row returns, your setup is complete.

Why How to Set Up MySQL on macOS with Homebrew is important

How to Set Up MySQL on macOS with Homebrew Example Usage


-- Verify installation and test data flow
INSERT INTO Products(name,price,stock) VALUES ('Galaxy Hoodie',59.99,25);
SELECT id,name,price FROM Products WHERE stock &gt; 0;

How to Set Up MySQL on macOS with Homebrew Syntax


brew install mysql
# Start/stop
brew services start mysql
brew services stop mysql
mysql.server start|stop  # ad-hoc

# Secure setup
mysql_secure_installation [--validate-password]

# Connect
mysql [-h host] -u user [-p[password]] [-P port]

# Example ecommerce schema
CREATE DATABASE ecommerce;
USE ecommerce;
CREATE TABLE Customers(
  id INT AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(100),
  email VARCHAR(150),
  created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE Orders(
  id INT AUTO_INCREMENT PRIMARY KEY,
  customer_id INT,
  order_date DATE,
  total_amount DECIMAL(10,2)
);
CREATE TABLE Products(
  id INT AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(80),
  price DECIMAL(10,2),
  stock INT
);
CREATE TABLE OrderItems(
  id INT AUTO_INCREMENT PRIMARY KEY,
  order_id INT,
  product_id INT,
  quantity INT
);

Common Mistakes

Frequently Asked Questions (FAQs)

Does Homebrew install the latest MySQL version?

Yes. The mysql formula tracks the current GA release. Pin a version with brew install mysql@<version> if you need stability.

Where are MySQL data files stored?

By default under /opt/homebrew/var/mysql (Apple Silicon) or /usr/local/var/mysql (Intel). Adjust datadir in my.cnf if required.

How do I uninstall MySQL completely?

Run brew services stop mysql, then brew uninstall mysql, and finally remove the data directory with rm -rf /opt/homebrew/var/mysql.

Want to learn about other SQL terms?

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie
BauHealth Logo
Truvideo Logo
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.