Skip to content

MySQL - Installation

Installing MySQL: A Modern Developer’s Guide

Section titled “Installing MySQL: A Modern Developer’s Guide”

Setting up a MySQL server is the first step in any database-driven project. Today, developers have several excellent options, from traditional native installers to lightweight, isolated containers with Docker. We’ll cover the most effective methods for development and production.

Section titled “Method 1: Using Docker (Recommended for Development)”

Docker provides a clean, isolated, and reproducible environment. It’s the fastest way to get a MySQL instance running for development without affecting your host operating system. It works identically on Windows, macOS, and Linux.

  • Docker Desktop installed and running on your system.

Open your terminal and run the following command:

docker run --name mysql-dev -p 3306:3306 -e MYSQL_ROOT_PASSWORD=your_strong_password -d mysql:8

Let’s break that down:

  • --name mysql-dev: Gives your container a memorable name.
  • -p 3306:3306: Maps port 3306 on your host machine to port 3306 inside the container, allowing you to connect to it as if it were running locally.
  • -e MYSQL_ROOT_PASSWORD=...: Sets the root password via an environment variable. Use a secure password!
  • -d: Runs the container in detached mode (in the background).
  • mysql:8: Specifies the official MySQL image, version 8.

Your MySQL server is now running! You can connect to it using any MySQL client (like MySQL Workbench, DataGrip, or the command line) with host 127.0.0.1, port 3306, user root, and the password you set.

If you prefer a traditional installation on your host OS, follow the guide for your system.

  • Download: Go to the official MySQL Community Downloads page and download the ‘MySQL Installer for Windows’.
  • Run Installer: Launch the downloaded MSI file.
  • Choose Setup Type: For most developers, ‘Developer Default’ is a good choice as it includes the Server, Workbench, and Shell. Otherwise, choose ‘Custom’ to select specific products.
  • Installation: The installer will download and install the selected products.
  • Configuration: This is the key part. The wizard will guide you through:
    • Type and Networking: Keep the defaults (Standalone, Port 3306) unless you have a specific need.
    • Authentication Method: Choose ‘Use Strong Password Encryption’ (recommended).
    • Accounts and Roles: Set a strong password for the root user. This is critical.
    • Windows Service: Configure MySQL to run as a Windows Service. This is convenient as it will start automatically.
  • Apply Configuration: The installer will apply all your settings. Once complete, MySQL is installed and running.

Homebrew is the de facto package manager for macOS and the easiest way to install MySQL.

# 1. Install MySQL
brew install mysql
# 2. Start the MySQL service
brew services start mysql
# 3. Secure your installation (IMPORTANT!)
mysql_secure_installation

The mysql_secure_installation script will guide you through setting a root password, removing anonymous users, and other crucial security steps.

Using your distribution’s native package manager is the recommended way to install, manage, and update MySQL.

For Debian/Ubuntu:

sudo apt update
sudo apt install mysql-server
# Then, secure the installation
sudo mysql_secure_installation

For RHEL/CentOS/Fedora:

sudo dnf install mysql-server
# Or 'yum' on older systems
# Start and enable the service
sudo systemctl start mysqld
sudo systemctl enable mysqld
# Then, secure the installation
sudo mysql_secure_installation

After any native installation, you can connect using the MySQL command-line client.

mysql -u root -p

It will prompt you to enter the root password you set during the installation. If you see the mysql> prompt, congratulations, your installation was successful!