MySQLInstalling MySQL

Installing MySQL

MySQL can be installed on all major operating systems. This guide covers installation on Ubuntu/Debian Linux, macOS, and Windows — the three most common development environments. After installation you will verify the setup, secure the server, and connect for the first time.

Installing on Ubuntu / Debian

Ubuntu and Debian ship MySQL in their official package repositories. The easiest method is to use the APT package manager.

Bash
# Update package index
sudo apt update

# Install MySQL Server
sudo apt install mysql-server -y

# Verify the service started automatically
sudo systemctl status mysql
● mysql.service - MySQL Community Server
     Loaded: loaded (/lib/systemd/system/mysql.service; enabled)
     Active: active (running) since Mon 2024-01-15 10:23:45 UTC

If your distro's repository has an older version and you need MySQL 8.0 or 8.4 specifically, use Oracle's official APT repository:

Bash
# Download the MySQL APT config package (check dev.mysql.com for the latest URL)
wget https://dev.mysql.com/get/mysql-apt-config_0.8.29-1_all.deb

# Install the config package (opens a dialog to choose version)
sudo dpkg -i mysql-apt-config_0.8.29-1_all.deb

# Update and install
sudo apt update
sudo apt install mysql-server -y
Managing the MySQL Service on Linux

Bash
# Start MySQL
sudo systemctl start mysql

# Stop MySQL
sudo systemctl stop mysql

# Restart MySQL (after config changes)
sudo systemctl restart mysql

# Enable auto-start on boot
sudo systemctl enable mysql

# Disable auto-start on boot
sudo systemctl disable mysql

# Check service status
sudo systemctl status mysql
Installing on macOS (Homebrew)

Homebrew is the recommended way to install MySQL on macOS. If you don't have Homebrew, install it first from brew.sh.

Bash
# Install MySQL (latest stable)
brew install mysql

# Or install a specific major version
brew install mysql@8.4

# Start the MySQL service immediately and at login
brew services start mysql

# Stop MySQL
brew services stop mysql

# Restart MySQL
brew services restart mysql

After installation, Homebrew prints the data directory and socket path. The default socket on macOS Homebrew installs is at /tmp/mysql.sock.

Note
On macOS, the MySQL root user has no password by default after a Homebrew install. Run mysql_secure_installation immediately to set one.
Connecting After macOS Install

Bash
# Connect as root (no password yet on fresh Homebrew install)
mysql -u root

# If Homebrew installed to a custom prefix (Apple Silicon)
/opt/homebrew/bin/mysql -u root
Installing on Windows

The recommended approach on Windows is the MySQL Installer, available from dev.mysql.com. It provides a GUI wizard that installs MySQL Server, MySQL Workbench, and other tools.

Steps:

  • Download MySQL Installer from dev.mysql.com/downloads/installer/

  • Run the installer as Administrator

  • Choose "Developer Default" for a full install (server + workbench + connectors) or "Server Only" for minimal

  • Follow the Setup Wizard — accept defaults for most options

  • On the "Authentication Method" screen, choose "Use Strong Password Encryption" (caching_sha2_password) for new installs

  • Set a root password when prompted — store it securely

  • Complete the installation and start the server

After installation, MySQL is registered as a Windows Service and starts automatically. You can manage it via Services (services.msc) or the command line:

Bash
# Run in Command Prompt as Administrator
# Start MySQL service
net start MySQL80

# Stop MySQL service
net stop MySQL80

# Connect using mysql CLI (add MySQL bin to PATH first)
mysql -u root -p
Tip
Add MySQL's bin directory to your PATH during installation or manually. The default path is C:\Program Files\MySQL\MySQL Server 8.0\bin. This lets you run mysql from any terminal.
Verifying the Installation

After installing on any platform, verify that MySQL is running and accessible:

Bash
# Check the MySQL version
mysql --version
mysql Ver 8.4.0 Distrib 8.4.0, for Linux (x86_64) using EditLine wrapper

Bash
# Connect to the server (you will be prompted for the root password)
mysql -u root -p
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.4.0 MySQL Community Server - GPL

mysql> 

SQL
-- Inside the mysql prompt, run a quick test
SELECT VERSION(), NOW(), USER();
+-----------+---------------------+----------------+
| VERSION() | NOW()               | USER()         |
+-----------+---------------------+----------------+
| 8.4.0     | 2024-01-15 10:30:00 | root@localhost |
+-----------+---------------------+----------------+
Securing MySQL with mysql_secure_installation

A fresh MySQL install has insecure defaults: anonymous users, a test database accessible to everyone, and remote root login may be enabled. Run the security script to fix these:

Bash
sudo mysql_secure_installation

The script walks you through these steps:

  • VALIDATE PASSWORD component: Enables password strength enforcement. Choose "MEDIUM" or "STRONG" for production servers.

  • Set root password: Set a strong password for the root account if not already set.

  • Remove anonymous users: Anonymous users let anyone connect without a username — always remove them.

  • Disallow root login remotely: Root should only connect from localhost. External access should use a named user with minimal privileges.

  • Remove test database: The test database is accessible to anonymous users. Remove it.

  • Reload privilege tables: Apply all changes immediately.

Warning
Never skip mysql_secure_installation on a production server. An unsecured MySQL instance with a known root account and anonymous user access is a frequent cause of database breaches.
Checking the MySQL Configuration File

MySQL reads its configuration from a file called my.cnf (Linux/macOS) or my.ini (Windows). Common locations:

  • Linux: <code>/etc/mysql/mysql.conf.d/mysqld.cnf</code> or <code>/etc/my.cnf</code>

  • macOS Homebrew: <code>/opt/homebrew/etc/my.cnf</code>

  • Windows: <code>C:\ProgramData\MySQL\MySQL Server 8.0\my.ini</code>

Bash
# See which config files MySQL is reading
mysql --help | grep "Default options" -A1
Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf ~/.my.cnf
Creating Your First User

You should never use the root account for application connections. Create a dedicated user with only the privileges your application needs:

SQL
-- Connect as root first
-- Create a new user (only accessible from localhost)
CREATE USER 'appuser'@'localhost' IDENTIFIED BY 'SecurePassword123!';

-- Create the application database
CREATE DATABASE myapp_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- Grant all privileges on the app database only
GRANT ALL PRIVILEGES ON myapp_db.* TO 'appuser'@'localhost';

-- Apply privilege changes
FLUSH PRIVILEGES;

-- Verify the user was created
SELECT user, host FROM mysql.user WHERE user = 'appuser';

Bash
# Connect as the new user to verify it works
mysql -u appuser -p myapp_db
Tip
For production applications, grant only the minimum required privileges. A read-only reporting user needs only SELECT. An application user typically needs SELECT, INSERT, UPDATE, DELETE on its own database — not CREATE, DROP, or GRANT.
Installing MySQL with Docker

Docker is excellent for development environments — no installation pollution, easy version switching, and disposable instances.

Bash
# Pull and run MySQL 8.4
docker run --name mysql-dev \
  -e MYSQL_ROOT_PASSWORD=rootpassword \
  -e MYSQL_DATABASE=myapp_db \
  -e MYSQL_USER=appuser \
  -e MYSQL_PASSWORD=apppassword \
  -p 3306:3306 \
  -d mysql:8.4

# Connect to the container's MySQL instance
docker exec -it mysql-dev mysql -u root -p

# Or connect from your host machine (port is mapped to 3306)
mysql -h 127.0.0.1 -P 3306 -u root -p

Bash
# docker-compose.yml for a persistent dev database
# Save as docker-compose.yml and run: docker compose up -d

Bash
# docker-compose.yml
# version: "3.9"
# services:
#   mysql:
#     image: mysql:8.4
#     environment:
#       MYSQL_ROOT_PASSWORD: rootpassword
#       MYSQL_DATABASE: myapp_db
#     ports:
#       - "3306:3306"
#     volumes:
#       - mysql_data:/var/lib/mysql
# volumes:
#   mysql_data:
Note
The -p 3306:3306 flag maps the container's MySQL port to your host. This lets MySQL Workbench and your application connect to 127.0.0.1:3306 as if MySQL were installed locally.
Checking the MySQL Data Directory

MySQL stores all database files in a data directory. Knowing its location is useful for backups, checking disk usage, and troubleshooting.

SQL
-- Find the data directory location
SHOW VARIABLES LIKE 'datadir';

-- Find the socket file location
SHOW VARIABLES LIKE 'socket';

-- Find the error log location
SHOW VARIABLES LIKE 'log_error';
+---------------+--------------------------+
| Variable_name | Value                    |
+---------------+--------------------------+
| datadir       | /var/lib/mysql/          |
+---------------+--------------------------+