How to Install MySQL on Linux, Windows, and macOS
Installing MySQL Community Server is the foundational step for building any application that relies on a robust, open-source relational database. Whether you are setting up a local development environment or deploying a production system, understanding the correct installation procedure for your operating system is crucial for a smooth start. This comprehensive guide walks you through the exact steps required to install MySQL on the three major platforms: Linux (using common package managers), Windows, and macOS.
By following these platform-specific instructions, you will successfully set up the necessary database server, initialize the system, and secure the installation, preparing your environment for database management and application connectivity.
Prerequisites for Installation
Before starting the installation process on any operating system, ensure you meet these basic requirements:
- Administrative Privileges: You must have root (Linux/macOS) or Administrator (Windows) access to install software and configure system services.
- Internet Connection: Required to download the necessary packages or installers from the official MySQL repository.
- Sufficient Disk Space: While modern installations are small, ensure you have adequate space for your database files.
1. Installing MySQL on Linux (Debian/Ubuntu & RHEL/CentOS)
Linux distributions typically use package managers to handle software installation, updates, and dependency resolution. We will cover the two most common families: Debian-based (like Ubuntu) and Red Hat-based (like CentOS/Fedora).
A. Debian/Ubuntu Installation (using APT)
For Debian and Ubuntu systems, the apt package manager is used. It is generally recommended to use the official MySQL APT repository for the latest, supported version.
Step 1: Download and Add the MySQL APT Repository
Download the repository setup package from the official MySQL website and install it:
# Download the repository setup file
wget https://dev.mysql.com/get/mysql-apt-config_0.8.29-1_all.deb
# Install the repository configuration package
sudo dpkg -i mysql-apt-config_0.8.29-1_all.deb
During the installation, a configuration screen will appear, allowing you to select the specific MySQL Server version you wish to install. Choose the desired version and continue.
Step 2: Update the Package List and Install MySQL Server
Update your local package cache and then install the server package:
# Update package list
sudo apt update
# Install the server package
sudo apt install mysql-server
During this process, you will be prompted to set the root password for the database instance. Remember this password.
B. RHEL/CentOS/Fedora Installation (using YUM/DNF)
For Red Hat-based systems, use yum (older systems) or dnf (newer systems).
Step 1: Download and Add the MySQL Repository
Download and install the appropriate repository RPM file (check the MySQL download page for the absolute latest version):
# For RHEL/CentOS 8+ or Fedora
sudo dnf install https://dev.mysql.com/get/mysql80-community-release-el8-1.noarch.rpm
# For older RHEL/CentOS 7
sudo yum install https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm
Step 2: Install MySQL Server
Once the repository is configured, install the server package:
# Install MySQL Server
sudo dnf install mysql-server
Step 3: Start and Enable the MySQL Service
After installation, start the service and configure it to launch automatically on boot:
# Start the MySQL service
sudo systemctl start mysqld
# Enable it to start on boot
sudo systemctl enable mysqld
# Check status
sudo systemctl status mysqld
Step 3 (Universal): Securing the Installation
Regardless of the Linux flavor, run the security script immediately after installation to secure default settings:
sudo mysql_secure_installation
This script guides you through setting the root password (if not set during installation), removing anonymous users, disabling remote root login, and removing the test database.
2. Installing MySQL on Windows
On Windows, the recommended method is using the MySQL Installer for Windows, which provides a graphical interface (GUI) to manage the installation and configuration of various MySQL products (Server, Workbench, Shell, Connectors).
Step 1: Download the MySQL Installer
Navigate to the official MySQL Downloads page and download the MySQL Installer for Windows (usually the web-community-installer).
Step 2: Run the Installer
- Run the downloaded
.msifile. - Choosing a Setup Type:
- Developer Default: Recommended for local development, installs Server, Workbench, Shell, and essential connectors.
- Server Only: Installs only the database engine.
- Custom: Allows you to select specific components.
- Review the prerequisites check and click Next.
- The installer will list required components. Click Execute to download and install them.
Step 3: Configuration
After installation, the setup wizard will guide you through configuration steps:
- Type and Networking: Usually, leave the default settings (Standalone Server).
- Authentication Method: Select Use Strong Password Encryption for Authentication (recommended for modern applications).
- Root Password: Set a strong password for the
rootuser. - Windows Service: Configure MySQL to run as a Windows service, often set to start automatically.
- Apply Configuration: Execute the configuration steps.
Step 4: Finalizing and Testing
Complete the setup. You can now launch MySQL Workbench to connect to your newly installed server instance using localhost and the root credentials you defined.
3. Installing MySQL on macOS
Installation on macOS can be achieved via the official DMG Archive installer or using the popular Homebrew package manager.
A. Using Homebrew (Recommended for macOS Users)
Homebrew simplifies installation and management on macOS. Ensure Homebrew is installed first (/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)").
Step 1: Install MySQL
Use the brew install command:
# Install the latest stable version of MySQL Server
brew install mysql
Step 2: Start and Secure the Installation
Homebrew often starts the service automatically after installation, but you should ensure it is running:
# Start the service if it's not running
brew services start mysql
# Run the security script
mysql_secure_installation
When running mysql_secure_installation, you may need to enter the temporary default password if prompted, or if this is a fresh installation, it might prompt you to set the root password immediately. For recent Homebrew installations, the initial setup often handles this, prompting you to use the mysqladmin command if needed, or directly launching the security script.
B. Using the DMG Archive (Official Installer)
Similar to Windows, you can download the macOS Installer (.dmg) from the MySQL website. Follow the on-screen installation prompts. During configuration, the installer often provides options to launch MySQL Startup Item, which configures it to start automatically upon system boot.
After installation, you must manually run the security script from the Terminal:
/usr/local/mysql/bin/mysql_secure_installation
Post-Installation Verification and Next Steps
Once installed, verify that the server is running and accessible.
Connecting to the Database
Use the MySQL Command Line Client to test connectivity:
mysql -u root -p
Enter the root password you established during setup. If successful, you will see the MySQL prompt (mysql>).
Verification Command:
mysql> SHOW DATABASES;
mysql> EXIT;
Best Practice: Creating a Dedicated User
For security, avoid using the root user for daily application tasks. Create a dedicated user with restricted permissions:
-- Log in as root first
mysql -u root -p
-- Create a new user and grant privileges (replace 'app_user' and 'secure_password')
CREATE USER 'app_user'@'localhost' IDENTIFIED BY 'secure_password';
-- Grant necessary permissions (e.g., all privileges on a specific database)
GRANT ALL PRIVILEGES ON my_application_db.* TO 'app_user'@'localhost';
FLUSH PRIVILEGES;
This completes the core installation process across major platforms, setting a secure foundation for your database work.