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.
Method 1: Using Docker (Recommended for Development)
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.
Prerequisites
Section titled “Prerequisites”- Docker Desktop installed and running on your system.
One-Command Setup
Section titled “One-Command Setup”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:8Let’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.
Method 2: Native Installation
Section titled “Method 2: Native Installation”If you prefer a traditional installation on your host OS, follow the guide for your system.
On Windows (using MySQL Installer)
Section titled “On Windows (using MySQL Installer)”- 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
rootuser. This is critical.
- Accounts and Roles: Set a strong password for the
-
- 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.
On macOS (using Homebrew)
Section titled “On macOS (using Homebrew)”Homebrew is the de facto package manager for macOS and the easiest way to install MySQL.
# 1. Install MySQLbrew install mysql
# 2. Start the MySQL servicebrew services start mysql
# 3. Secure your installation (IMPORTANT!)mysql_secure_installationThe mysql_secure_installation script will guide you through setting a root password, removing anonymous users, and other crucial security steps.
On Linux (using a Package Manager)
Section titled “On Linux (using a Package Manager)”Using your distribution’s native package manager is the recommended way to install, manage, and update MySQL.
For Debian/Ubuntu:
sudo apt updatesudo apt install mysql-server# Then, secure the installationsudo mysql_secure_installationFor RHEL/CentOS/Fedora:
sudo dnf install mysql-server# Or 'yum' on older systems
# Start and enable the servicesudo systemctl start mysqldsudo systemctl enable mysqld
# Then, secure the installationsudo mysql_secure_installationPost-Installation: First Connection
Section titled “Post-Installation: First Connection”After any native installation, you can connect using the MySQL command-line client.
mysql -u root -pIt will prompt you to enter the root password you set during the installation. If you see the mysql> prompt, congratulations, your installation was successful!