MySQL - installing and setting up MySQL on Windows, macOS, and Linux

1. Choose Your Installation Method

MySQL can be installed in several ways:

  1. MySQL Installer (Windows)

  2. DMG Package (macOS)

  3. APT/YUM (Linux)

  4. Docker (cross-platform)


2. Installation

A. Windows

  1. Download MySQL Installer from the official site:
    https://dev.mysql.com/downloads/installer/

  2. Run the installer and choose “Developer Default” (includes server, Workbench, Shell, connectors, etc.).

  3. During setup:

    • Choose Standalone MySQL Server.

    • Set a root password (remember this!).

    • Optionally, create additional user accounts.

  4. Complete the installation.


B. macOS

  1. Download the MySQL DMG archive:
    https://dev.mysql.com/downloads/mysql/

  2. Open the DMG and run the installer.

  3. After installation:

    • Start MySQL using System Preferences → MySQL.

    • Set root password during installation or via command line.

  4. Add MySQL to PATH (optional, for terminal use):

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

C. Linux

Ubuntu/Debian

sudo apt update
sudo apt install mysql-server
sudo systemctl start mysql
sudo systemctl enable mysql

CentOS/RHEL

sudo yum install mysql-server
sudo systemctl start mysqld
sudo systemctl enable mysqld

Secure installation:

sudo mysql_secure_installation
  • Set root password

  • Remove anonymous users

  • Disallow remote root login

  • Remove test database


D. Docker (Cross-platform)

docker pull mysql:latest
docker run --name mysql-server -e MYSQL_ROOT_PASSWORD=yourpassword -p 3306:3306 -d mysql:latest
  • Access MySQL: docker exec -it mysql-server mysql -uroot -p


3. Verify Installation

mysql -u root -p
  • Enter the root password.

  • If you see the MySQL prompt (mysql>), the installation was successful.


4. Basic Setup

  1. Create a database

CREATE DATABASE my_database;
  1. Create a user and grant privileges

CREATE USER 'my_user'@'localhost' IDENTIFIED BY 'mypassword';
GRANT ALL PRIVILEGES ON my_database.* TO 'my_user'@'localhost';
FLUSH PRIVILEGES;
  1. Connect to the database

mysql -u my_user -p my_database

5. Optional Tools

  • MySQL Workbench: GUI for database design and queries.

  • MySQL Shell: Advanced command-line client.

  • phpMyAdmin: Web-based management tool.