How to Install MySQL on Linux, Windows, and macOS
Install MySQL on Linux, Windows, and macOS, then verify the service, run `mysql_secure_installation`, and create a safer app user.
How to Install MySQL on Linux, Windows, and macOS
Installing MySQL gives your app a local or server-side relational database to store users, orders, logs, and other structured data. The steps differ across Linux, Windows, and macOS, and the small setup details matter: service names, root authentication, and post-install security are not identical everywhere.
This guide shows the usual install path for MySQL Community Server on each platform, then walks through a quick verification and a safer application user.
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: The server package is modest, but your database files, binary logs, and backups can grow quickly.
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. Ubuntu and Debian often include MySQL or MariaDB packages in their default repositories. Use the official MySQL APT repository when you specifically need Oracle's MySQL Community Server packages or a version not provided by your distribution.
Step 1: Download and Add the MySQL APT Repository
Download the current repository setup package from the official MySQL APT repository page, then install it. The filename changes over time, so replace the example below with the package you downloaded.
sudo dpkg -i mysql-apt-config_*_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
Depending on the package and distribution, you may be prompted for a MySQL root password, or the server may use socket authentication for the local administrative account. Check your distribution's install notes if password login as root is not enabled.
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 from the MySQL Yum repository page. Pick the package that matches your OS major version. For example:
sudo dnf install ./mysql*-community-release-*.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 may help set or change the root password, remove anonymous users, disable remote root login, and remove the test database. The exact prompts depend on your MySQL version and package source.
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
Start the service with Homebrew:
# Start the service if it's not running
brew services start mysql
# Run the security script
mysql_secure_installation
If this is a fresh Homebrew install, the local root account may initially have no password or may use setup guidance printed by brew. Read the caveats shown after brew install mysql, then run mysql_secure_installation once the server is running.
B. Using the DMG Archive (Official Installer)
You can also download the macOS Installer (.dmg) from the MySQL website. Follow the on-screen prompts. The installer can add a preference pane or startup configuration depending on the package version.
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>).
Run a simple query:
SHOW DATABASES;
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:
CREATE USER 'app_user'@'localhost' IDENTIFIED BY 'secure_password';
GRANT ALL PRIVILEGES ON my_application_db.* TO 'app_user'@'localhost';
FLUSH PRIVILEGES;
Install the server from the package source that matches your operating system, confirm the service is running, run the security script, and create a dedicated user before connecting your application. That gives you a cleaner setup than building on the administrative root account.