Skip to content

MySQL Installation

System Requirements

Before installing MySQL, ensure your system meets the minimum requirements:

Hardware Requirements

| Component |----------------|--------------|-----------------| | CPU | RAM | Disk Space | Network

Software Requirements

MySQL is available on multiple platforms:

  • Windows: Windows 10/11, Windows Server 2016+
  • macOS: macOS 10.15+
  • Linux: Most distributions (Ubuntu, Debian, CentOS, RHEL, etc.)
  • Unix: FreeBSD, Solaris

Windows Installation

  1. Download MySQL Installer

  2. Run the Installer

    • Double-click the downloaded .msi file
    • If prompted by User Account Control, click "Yes"
  3. Choose Setup Type

    - Developer Default: Includes MySQL Server, MySQL Workbench
    - Full: Includes all MySQL products
    - Server Only: MySQL Server only
    - Custom: Select specific components
  4. Check Requirements

    • Installer will check for missing prerequisites
    • Install any required components
  5. Installation

    • Review components to install
    • Click "Execute" to begin installation
    • Wait for download and installation
  6. Configuration

    • Configuration type: Standalone
    • Port: 3306 (default)
    • Authentication method: Strong password encryption
    • Set root password
    • Create user accounts (optional)
  7. Complete Installation

    • Click "Finish" when complete

Method 2: ZIP Archive

  1. Download MySQL ZIP Archive

  2. Extract Files

    powershell
    # Extract to desired location
    Expand-Archive mysql-8.0.x-winx64.zip -DestinationPath C:\mysql
  3. Initialize MySQL

    cmd
    cd C:\mysql
    mkdir data
    bin\mysqld --initialize-insecure --basedir=. --datadir=./data
  4. Start MySQL Service

    cmd
    bin\mysqld --install
    net start mysql
  5. Connect to MySQL

    cmd
    bin\mysql -u root -p

Method 3: Docker

powershell
# Pull MySQL image
docker pull mysql:8.0

# Run MySQL container
docker run --name mysql-dev `
  -e MYSQL_ROOT_PASSWORD=my-secret-pw `
  -p 3306:3306 `
  -d mysql:8.0

# Connect to MySQL
docker exec -it mysql-dev mysql -u root -p

macOS Installation

  1. Install Homebrew

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

    bash
    brew install mysql@8.0
    
    # Start MySQL service
    brew services start mysql@8.0
    
    # Or start manually
    mysql.server start
  3. Initial Setup

    bash
    # Secure installation
    mysql_secure_installation
    
    # Or connect directly
    mysql -u root
  4. Add to PATH

    bash
    echo 'export PATH="/usr/local/opt/mysql@8.0/bin:$PATH"' >> ~/.zshrc
    source ~/.zshrc

Method 2: DMG Package

  1. Download DMG Package

  2. Run Installation

    • Open the downloaded .dmg file
    • Double-click mysql-8.0.x.pkg
    • Follow installation wizard
    • Set temporary password (shown in installer)
  3. Start MySQL

    bash
    # Using System Preferences
    # Open MySQL preference pane
    # Click "Start MySQL Server"
    
    # Or using command line
    sudo mysql.server start
  4. Change Root Password

    bash
    sudo mysql -u root -p
    # Use temporary password from installation
    ALTER USER 'root'@'localhost' IDENTIFIED BY 'NewPassword123!';

Method 3: Docker

bash
# Pull MySQL image
docker pull mysql:8.0

# Run MySQL container
docker run --name mysql-dev \
  -e MYSQL_ROOT_PASSWORD=my-secret-pw \
  -p 3306:3306 \
  -d mysql:8.0

# Connect to MySQL
docker exec -it mysql-dev mysql -u root -p

Linux Installation

Ubuntu / Debian

Step 1: Update Package Index

bash
sudo apt update

Step 2: Install MySQL

bash
# Install MySQL Server
sudo apt install mysql-server

# Install MySQL Client
sudo apt install mysql-client

# Install MySQL development files
sudo apt install libmysqlclient-dev

Step 3: Secure MySQL Installation

bash
sudo mysql_secure_installation

The secure installation script will guide you through:

Securing the MySQL server deployment.

VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks password strength and
allows setting only passwords which meet the current
policy requirements.

Would you like to setup VALIDATE PASSWORD component? [Y/N]: Y

Please set the password for root here.

New password: ********
Confirm new password: ********

Remove anonymous users? [Y/N]: Y
Disallow root login remotely? [Y/N]: Y
Remove test database and access to it? [Y/N]: Y
Reload privilege tables now? [Y/N]: Y

Step 4: Start MySQL Service

bash
# Start MySQL service
sudo systemctl start mysql

# Enable MySQL to start on boot
sudo systemctl enable mysql

# Check service status
sudo systemctl status mysql

Step 5: Connect to MySQL

bash
sudo mysql -u root -p

CentOS / RHEL / Rocky Linux

Step 1: Add MySQL Repository

bash
# Download MySQL repository package
sudo dnf localinstall https://dev.mysql.com/get/mysql80-community-release-el8-7.noarch.rpm

# Enable MySQL 8.0 repository
sudo dnf module enable mysql:8.0

# Or for CentOS 7
sudo yum localinstall https://dev.mysql.com/get/mysql80-community-release-el7-7.noarch.rpm

Step 2: Install MySQL

bash
# Install MySQL community server
sudo dnf install mysql-community-server

# Install MySQL client
sudo dnf install mysql-community-client

Step 3: Start MySQL Service

bash
# Start MySQL service
sudo systemctl start mysqld

# Enable MySQL to start on boot
sudo systemctl enable mysqld

# Check temporary password
sudo grep 'temporary password' /var/log/mysqld.log

Step 4: Secure Installation

bash
# Get temporary password
TEMP_PASSWORD=$(sudo grep 'temporary password' /var/log/mysqld.log | awk '{print $NF}')

# Run secure installation
sudo mysql -u root -p"$TEMP_PASSWORD" -e "
ALTER USER 'root'@'localhost' IDENTIFIED BY 'NewStrongPassword123!';
FLUSH PRIVILEGES;
"

# Or run full secure installation
sudo mysql_secure_installation

Fedora / Fedora

bash
# Install MySQL
sudo dnf install mysql mysql-server

# Start and enable service
sudo systemctl start mysqld
sudo systemctl enable mysqld

# Secure installation
sudo mysql_secure_installation

Verification

Check MySQL Version

bash
# Command line
mysql --version

# In MySQL
mysql -u root -p
SELECT VERSION();

Test Connection

bash
# Local connection
mysql -u root -p

# Test basic commands
SHOW DATABASES;
CREATE DATABASE test_db;
DROP DATABASE test_db;

Check Service Status

bash
# Linux (systemd)
sudo systemctl status mysql
sudo systemctl status mysqld

# macOS
brew services list | grep mysql

# Check if MySQL is running
sudo netstat -tlnp | grep 3306

Post-Installation Configuration

Setting Root Password

sql
-- Method 1: ALTER USER
ALTER USER 'root'@'localhost' IDENTIFIED BY 'YourStrongPassword123!';

-- Method 2: SET PASSWORD
SET PASSWORD FOR 'root'@'localhost' = 'YourStrongPassword123!';

-- Method 3: Using mysqladmin
mysqladmin -u root password 'YourStrongPassword123!'

Creating a New User

sql
-- Create a user with full privileges
CREATE USER 'username'@'localhost' IDENTIFIED BY 'StrongPassword123!';
GRANT ALL PRIVILEGES ON *.* TO 'username'@'localhost';
FLUSH PRIVILEGES;

-- Create a user with specific privileges
CREATE USER 'app_user'@'localhost' IDENTIFIED BY 'AppPassword123!';
GRANT SELECT, INSERT, UPDATE, DELETE ON myapp.* TO 'app_user'@'localhost';
FLUSH PRIVILEGES;

Configuring Remote Access

sql
-- Allow remote connections
CREATE USER 'remote_user'@'%' IDENTIFIED BY 'RemotePassword123!';
GRANT ALL PRIVILEGES ON *.* TO 'remote_user'@'%';
FLUSH PRIVILEGES;

-- Exit MySQL and edit configuration
-- In my.cnf or my.ini
[mysqld]
bind-address = 0.0.0.0

Configuration File Location

| Platform |---------------|----------------| | Windows | C:\ProgramData\MySQL\MySQL Server 8.0\my.ini | | macOS | /etc/my.cnf or /usr/local/etc/my.cnf | | Linux (Ubuntu/Debian) | /etc/mysql/mysql.conf.d/mysqld.cnf | | Linux (CentOS/RHEL) | /etc/my.cnf |

Docker Installation

Install Docker

Windows / macOS

  1. Download Docker Desktop from https://www.docker.com/products/docker-desktop
  2. Install and start Docker Desktop

Linux

bash
# Ubuntu
sudo apt install docker.io docker-compose
sudo systemctl start docker
sudo systemctl enable docker

# Add user to docker group
sudo usermod -aG docker $USER

Run MySQL with Docker

bash
# Pull MySQL image
docker pull mysql:8.0

# Run MySQL container
docker run --name mysql-dev \
  -e MYSQL_ROOT_PASSWORD=my-secret-pw \
  -p 3306:3306 \
  -v mysql_data:/var/lib/mysql \
  -d mysql:8.0

# View running containers
docker ps

# Connect to MySQL
docker exec -it mysql-dev mysql -u root -p

# Stop container
docker stop mysql-dev

# Start container
docker start mysql-dev

# Remove container
docker rm mysql-dev

Docker Compose / Docker Compose

Create docker-compose.yml:

yaml
version: '3.8'

services:
  mysql:
    image: mysql:8.0
    container_name: mysql-dev
    environment:
      MYSQL_ROOT_PASSWORD: my-secret-pw
      MYSQL_DATABASE: myapp
      MYSQL_USER: appuser
      MYSQL_PASSWORD: apppassword
    ports:
      - "3306:3306"
    volumes:
      - mysql_data:/var/lib/mysql
    restart: always
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
      interval: 10s
      timeout: 5s
      retries: 5

volumes:
  mysql_data:

Run with:

bash
docker-compose up -d
docker-compose down

Troubleshooting

Common Installation Issues

MySQL Service Won't Start

bash
# Check error logs
sudo tail -f /var/log/mysql/error.log

# Check port availability
sudo netstat -tlnp | grep 3306

# Try starting with different configuration
sudo mysqld --defaults-file=/etc/my.cnf --user=mysql

Access Denied Errors

bash
# Reset root password
# Stop MySQL service
sudo systemctl stop mysql

# Start MySQL in safe mode
sudo mysqld_safe --skip-grant-tables &

# Connect and reset password
mysql -u root
FLUSH PRIVILEGES;
ALTER USER 'root'@'localhost' IDENTIFIED BY 'NewPassword123!';

Port Already in Use

bash
# Find process using port 3306
sudo lsof -i :3306

# Change MySQL port in my.cnf
[mysqld]
port = 3307

Check MySQL Logs

bash
# Error log locations
Windows: C:\ProgramData\MySQL\MySQL Server 8.0\Data\*.err
Linux: /var/log/mysql/error.log
macOS: /usr/local/mysql/data/*.err

# View logs
sudo tail -f /var/log/mysql/error.log

Uninstallation

Windows

  1. Use Control Panel > Programs and Features
  2. Find MySQL and click "Uninstall"
  3. Use MySQL Installer to remove all components
  4. Delete remaining files manually

macOS

bash
# Stop and remove service
brew services stop mysql@8.0
brew uninstall mysql@8.0

# Remove files
sudo rm -rf /usr/local/var/mysql
sudo rm -rf /usr/local/etc/my.cnf

Linux

bash
# Ubuntu/Debian
sudo apt remove mysql-server mysql-client
sudo apt autoremove
sudo rm -rf /var/lib/mysql
sudo rm -rf /etc/mysql

# CentOS/RHEL
sudo dnf remove mysql-community-server mysql-community-client
sudo rm -rf /var/lib/mysql
sudo rm -rf /etc/my.cnf

Summary

MySQL installation varies by platform:

  • Windows: Use MySQL Installer for easy setup
  • macOS: Use Homebrew or DMG package
  • Linux: Use package manager (apt, dnf, yum)
  • Cross-platform: Use Docker for consistent environment

Key steps after installation:

  1. Secure installation - Run mysql_secure_installation
  2. Set root password - Create a strong password
  3. Create users - Create users with appropriate privileges
  4. Configure remote access - If needed
  5. Backup configuration - Keep configuration files safe

Next Steps

Continue to MySQL Administration to learn how to manage and configure MySQL server.


Previous: Introduction

Next: Administration

Content is for learning and research only.