Popular Searches
Popular Course Categories
Popular Courses

Installing MySQL on macOS and Linux

Installing MySQL on macOS and Linux

MySQL Installation and Environment Setup

Installing MySQL on macOS and Linux

MySQL is a popular relational database management system (RDBMS) used to store, organize, retrieve, and manage structured data. Before working with SQL queries, databases, tables, and relationships, developers need to install MySQL Server on their computer or development environment.

macOS and Linux provide different installation methods for MySQL. On macOS, users can install MySQL through the official macOS package installer or a package manager such as Homebrew. On Linux, users can install MySQL using the distribution's package manager, the official MySQL APT or Yum repository, or other supported installation methods.

These notes explain installation prerequisites, step-by-step procedures, server configuration, starting and stopping the service, connecting through the MySQL client, testing the installation, and troubleshooting common errors.

Course Link: JustAcademy MySQL Training Course

Demo Link: Register for MySQL Course Demo


1. Learning Objectives

After completing this topic, learners will be able to:

  • Understand the requirements for installing MySQL on macOS and Linux.
  • Identify the difference between MySQL Server and MySQL Client.
  • Install MySQL on macOS using the official package installer or Homebrew.
  • Install MySQL on Ubuntu and other Debian-based Linux distributions.
  • Understand installation options for Fedora, RHEL, and related distributions.
  • Start, stop, restart, and check the status of the MySQL service.
  • Connect to MySQL Server using the command-line client.
  • Verify the installation by creating a database and running SQL queries.
  • Understand basic security configuration and troubleshooting.

2. Understanding MySQL Installation Components

Before installing MySQL, it is important to understand the main components that may be installed on your computer.

Component Description Purpose
MySQL Server The database server software. Stores data and processes SQL statements.
MySQL Client A command-line program or other client application. Connects to the server and executes queries.
MySQL Workbench A graphical database management tool. Creates databases, designs tables, and runs SQL queries.
MySQL Configuration Settings that control server behavior. Defines options such as port, storage paths, and logging.
MySQL Data Directory The location where database files are maintained. Stores persistent database information.
Service Manager A system utility that controls background services. Starts, stops, and monitors MySQL Server.

Important: Installing a client alone does not necessarily install MySQL Server. If you want to create and manage databases locally, ensure that the server component is installed and running.


3. System Requirements Before Installation

Before installing MySQL, check your operating system, hardware resources, permissions, and available disk space.

General Requirements

  • A supported version of macOS or a supported Linux distribution.
  • Sufficient free disk space for the software and database files.
  • Administrative privileges when required by the installer or package manager.
  • A stable internet connection for downloading packages.
  • Access to a terminal application.
  • A compatible processor architecture, such as Apple Silicon or Intel on macOS, where supported by the selected distribution.
  • A suitable package manager or the official installation package.

Check the Operating System

On macOS, open Terminal and execute:

sw_vers

This command displays information about the installed macOS version.

On Linux, execute:

cat /etc/os-release

This command displays information about the Linux distribution and version.

Check the Processor Architecture

On macOS and Linux, you can use:

uname -m

The output may include values such as arm64 or x86_64. Check that the MySQL package you choose supports your platform.


4. Installing MySQL on macOS

There are two common ways to install MySQL Server on macOS:

  • Official MySQL package installer: Download the macOS installer from the official MySQL website and follow the installation wizard.
  • Homebrew: Install MySQL using the Homebrew package manager from Terminal.

Choose one method for a straightforward development setup. Avoid installing multiple MySQL Server distributions simultaneously unless you understand their separate paths, services, and port configurations.

5. Method 1: Installing MySQL on macOS Using the Official Installer

Step 1: Visit the Official MySQL Website

Open the official MySQL downloads page:

Download MySQL Community Server

Select macOS as the operating system and choose a package compatible with your Mac and the MySQL version you intend to use.

Step 2: Download the Package

Download the appropriate macOS installer, commonly provided as a DMG package containing a PKG installer. Follow the official instructions for the selected version.

Step 3: Open the Installer

  1. Open the downloaded DMG file.
  2. Open the included package installer.
  3. Follow the installation wizard.
  4. Review the license and installation options.
  5. Authorize the installation if macOS requests administrator credentials.
  6. Complete the installation.

Step 4: Configure the MySQL Root Account

During installation or initial setup, follow the prompts for configuring the MySQL root account and authentication. Set a strong password where requested and keep it secure.

The MySQL root account is a database administrator account. It is separate from the administrator account used to sign in to macOS.

Step 5: Start MySQL Server

Depending on the package and version, MySQL may provide a configuration panel or service management option. Use the installed MySQL management interface to start the server.

You can also use the launch daemon or service mechanism installed by the package, following the official instructions for your version.

Step 6: Verify the Installation

Open Terminal and try connecting with the MySQL command-line client:

mysql -u root -p

Enter the root password when prompted. If the client is not found, you may need to use the full path to the installed client or configure your shell's PATH variable.

Step 7: Check the Server Version

After connecting successfully, execute:

SELECT VERSION();

The server returns the version of the MySQL Server to which you are connected.


6. Method 2: Installing MySQL on macOS Using Homebrew

Homebrew is a package manager that simplifies the installation and management of software on macOS. This method is convenient for developers who prefer working from the command line.

Step 1: Check Whether Homebrew Is Installed

Open Terminal and execute:

brew --version

If a Homebrew version is displayed, Homebrew is installed. If the command is not found, visit the official website for installation instructions:

Official Homebrew Website

Step 2: Update Homebrew Information

brew update

This command updates Homebrew's package information.

Step 3: Install MySQL

brew install mysql

This command installs the MySQL formula currently provided by Homebrew. The installed version may differ from the version provided by Oracle's native installer.

Step 4: Start the MySQL Service

brew services start mysql

This command asks Homebrew to start MySQL as a managed service for your macOS user environment. Follow the instructions shown by Homebrew if additional configuration is required.

Step 5: Check the Service

brew services list

Review the output to check whether the MySQL service is running.

Step 6: Connect to MySQL

mysql -u root

The initial authentication setup can vary by Homebrew formula version and existing configuration. If a password is required, use:

mysql -u root -p

Enter the password when prompted. If you cannot connect, check the installation messages and consult the official MySQL or Homebrew documentation for the installed version.

Step 7: Check the Version

SELECT VERSION();

Run this statement at the MySQL prompt after connecting successfully.

Useful Homebrew Commands

Command Purpose
brew install mysql Installs MySQL using Homebrew.
brew services start mysql Starts the Homebrew-managed MySQL service.
brew services stop mysql Stops the Homebrew-managed MySQL service.
brew services restart mysql Restarts the service.
brew services list Lists services managed by Homebrew.
brew info mysql Displays information about the MySQL formula.

Important: Homebrew installations and Oracle's native macOS package installer may use different locations and service mechanisms. Installing both without planning can lead to conflicts, particularly if both servers try to use the same port.


7. Installing MySQL on Linux

Linux provides multiple installation methods for MySQL. The exact commands depend on the distribution, available repositories, and required MySQL version.

Common Linux Installation Methods

Method Common Platforms Description
APT package manager Ubuntu and Debian Installs packages from configured APT repositories.
MySQL APT Repository Ubuntu and supported Debian-based systems Provides MySQL packages through Oracle's repository.
DNF or YUM Fedora, RHEL-compatible distributions, and other supported systems Installs RPM packages from configured repositories.
MySQL Yum Repository Supported RPM-based distributions Provides Oracle's MySQL packages for supported systems.
Zypper Supported SUSE Linux Enterprise systems Manages packages using SUSE's package system.
Generic binary packages Supported Linux environments Allows manual installation and configuration.
Docker Linux systems with Docker installed Runs MySQL Server in a container.

For beginners, the distribution's package manager is often the easiest option. However, the version supplied by a distribution may differ from the version offered through Oracle's official MySQL repository.

Official reference: MySQL Installation Guide for Linux


8. Installing MySQL on Ubuntu or Debian

Ubuntu and Debian use the APT package manager. The following steps demonstrate a common installation using the distribution's configured repositories.

Step 1: Update Package Information

sudo apt update

This command refreshes the local package index.

Step 2: Install MySQL Server

sudo apt install mysql-server

Review the package manager's proposed changes and confirm the installation when prompted. The package version depends on the configured repositories and the operating system release.

Step 3: Check the Service Status

sudo systemctl status mysql

This command displays the current status of the MySQL service on systems that use systemd and register the service under the name mysql.

Step 4: Start MySQL if Necessary

sudo systemctl start mysql

If the service is already running, starting it again is generally unnecessary.

Step 5: Enable Automatic Startup

sudo systemctl enable mysql

This command configures the service to start automatically when the system boots.

Step 6: Open the MySQL Client

sudo mysql

On some Ubuntu and Debian installations, the administrative MySQL account may use socket-based authentication, allowing the operating-system administrator to connect through sudo mysql. Other configurations may require a username and password.

Step 7: Verify the Version

SELECT VERSION();

Execute this statement at the MySQL prompt. It confirms that the client is communicating with a MySQL Server.

Alternative: Install the Client Only

If you need only the command-line client and do not want to run a local database server, the client package may be installed separately:

sudo apt install mysql-client

Package names and availability vary by distribution version. A client-only installation requires access to an existing MySQL Server to perform database operations.


9. Installing MySQL on Fedora or RHEL-Compatible Linux

RPM-based distributions commonly use DNF or YUM. Depending on the distribution and the desired MySQL version, you may use the native repositories or Oracle's MySQL Yum repository.

Method A: Install from the Configured Distribution Repository

On systems where the package is available under this name, use:

sudo dnf install mysql-server

On older systems that use YUM, the corresponding command may be:

sudo yum install mysql-server

These commands install the package provided by the enabled repositories. Confirm the package identity and version before proceeding, because some Linux distributions provide a compatible database server such as MariaDB under related package names.

Method B: Use Oracle's MySQL Yum Repository

  1. Open the official MySQL Linux installation documentation.
  2. Select the instructions for your specific distribution and release.
  3. Install and configure the appropriate MySQL repository package.
  4. Refresh package metadata if required.
  5. Install the MySQL Server package according to the official instructions.
  6. Review the installation output for initial setup instructions.

Read the Official MySQL Linux Installation Guide

Start the Service

sudo systemctl start mysqld

Oracle MySQL packages on RPM-based systems commonly use the service name mysqld. The exact service name depends on the package and distribution.

Check the Service

sudo systemctl status mysqld

Enable Automatic Startup

sudo systemctl enable mysqld

Connect to MySQL

mysql -u root -p

Enter the configured root password when prompted. If the package provides a temporary initial password or setup procedure, follow the instructions for that version.


10. Installing MySQL on SUSE Linux

Supported SUSE Linux Enterprise systems use the Zypper package manager. The exact installation steps depend on the selected MySQL distribution and the repositories configured on the system.

Step 1: Refresh Repository Information

sudo zypper refresh

Step 2: Check Available Packages

zypper search mysql

Review the available package names and versions before installing. Confirm that you are selecting Oracle MySQL Server if that is your requirement.

Step 3: Install the Appropriate Server Package

Install the verified package using the name supplied by the selected repository. Package names and service configuration can differ by distribution and repository.

Step 4: Start and Verify the Service

After installation, use the service name documented by the installed package:

sudo systemctl status mysql

If the service is registered under a different name, substitute that name in the command. Consult the official MySQL installation guide for supported SUSE instructions.


11. Managing MySQL Server on Linux

On most modern Linux distributions, systemd is used to manage background services. The commands below apply when MySQL is registered with systemd under the specified service name.

Task Ubuntu/Debian Example Common Oracle RPM Example
Start sudo systemctl start mysql sudo systemctl start mysqld
Stop sudo systemctl stop mysql sudo systemctl stop mysqld
Restart sudo systemctl restart mysql sudo systemctl restart mysqld
Check status sudo systemctl status mysql sudo systemctl status mysqld
Enable at boot sudo systemctl enable mysql sudo systemctl enable mysqld
Disable at boot sudo systemctl disable mysql sudo systemctl disable mysqld

Note: The service name is not universal. If a command reports that the unit cannot be found, inspect the installed package and available services rather than repeatedly running the same command.


12. Connecting to MySQL After Installation

Once the MySQL Server is running, connect to it through the command-line client or a graphical tool such as MySQL Workbench.

Connect as Root with a Password

mysql -u root -p

The client prompts for the password. This is a common connection method, but authentication depends on the account configuration.

Connect to a Specific Database Server

mysql -h 127.0.0.1 -P 3306 -u root -p
  • -h specifies the host.
  • -P specifies the TCP port.
  • -u specifies the username.
  • -p prompts for the password.

Check the Server Version

SELECT VERSION();

List Available Databases

SHOW DATABASES;

Exit the Client

EXIT;

If the command-line client is not found, verify that the client package is installed and that the directory containing the executable is included in your PATH.


13. Verifying the Installation with a Practice Database

After installing MySQL, create a small database and table to confirm that the server can process SQL statements.

Step 1: Connect to MySQL

mysql -u root -p

Use the authentication method configured for your installation.

Step 2: Create a Database

CREATE DATABASE practice_db;

Step 3: Select the Database

USE practice_db;

Step 4: Create a Table

CREATE TABLE students (
    student_id INT PRIMARY KEY AUTO_INCREMENT,
    student_name VARCHAR(100) NOT NULL,
    course_name VARCHAR(100) NOT NULL
);

Step 5: Insert Sample Records

INSERT INTO students (student_name, course_name)
VALUES
('Aarav Sharma', 'MySQL'),
('Priya Verma', 'Python'),
('Rohan Mehta', 'Java');

Step 6: Retrieve the Records

SELECT * FROM students;

Step 7: Verify the Table Structure

DESCRIBE students;

If the queries execute successfully and the inserted records appear, you have verified that the server is accepting connections and performing basic database operations.


14. Securing a New MySQL Installation

A new database server should be configured carefully before it is used for real applications. Security steps depend on the installation method, MySQL version, and operating system.

Recommended Security Practices

  • Configure a strong password or an appropriate secure authentication method for administrative accounts.
  • Use the installation's supported security configuration procedure.
  • Remove or disable insecure accounts and unnecessary test resources where applicable.
  • Create separate application accounts instead of using the root account for routine work.
  • Grant only the database privileges required by each account.
  • Keep MySQL Server and its client tools updated.
  • Limit remote connections to trusted systems and networks.
  • Use TLS for connections that carry sensitive data over networks.
  • Maintain regular backups and test recovery procedures.
  • Protect configuration files, database files, logs, and backup files.

Security Configuration Utility

On installations that provide it, the following command may be available:

sudo mysql_secure_installation

This utility guides users through supported security-related setup tasks. The available options vary by MySQL version and distribution. Review each prompt and follow the documentation for your installed version.

Example: Creating a Restricted Application Account

CREATE USER 'app_user'@'localhost'
IDENTIFIED BY 'ReplaceWithAStrongUniquePassword';

GRANT SELECT, INSERT, UPDATE, DELETE
ON practice_db.*
TO 'app_user'@'localhost';

This example creates a local account and grants selected data access privileges on the practice database. Replace the example password with a strong, unique secret before use. Grant additional privileges only when required.


15. Understanding MySQL Configuration Files

MySQL uses configuration files to define server and client settings. The file locations and names can differ depending on the operating system, installation package, and version.

Common Configuration Options

Option Purpose
port Defines the TCP port used by the server.
bind-address Controls the network addresses on which the server listens, where supported.
datadir Specifies the database data directory.
socket Specifies a local socket path on supported systems.
log_error Configures the error log location.
max_connections Controls the configured maximum number of client connections.

Do not edit configuration files blindly. First identify which configuration files your installation reads, make a backup, and change only the required settings. Incorrect configuration can prevent the server from starting.


16. Checking MySQL Installation Paths

The location of MySQL executables and data files varies by installation method.

Check the MySQL Client Location on macOS or Linux

which mysql

This command prints the path of the executable found through your shell's PATH setting.

Check the Client Version

mysql --version

This command displays information about the installed command-line client. It does not necessarily prove that a server is running.

Check the Server Version Through SQL

SELECT VERSION();

This query requires a successful connection and returns the version of the server, which may differ from the client version.

Check the Service on Linux

systemctl list-units --type=service | grep -i mysql

This command searches active service units for names containing “mysql”. Depending on the installation, the service may be named mysql or mysqld, and inactive or differently named units may require additional inspection.


17. Troubleshooting Common Installation Problems

Problem Possible Cause Solution
mysql: command not found The client is not installed or its directory is not in PATH. Install the client package or configure the correct executable path.
MySQL service is not running The server has not started or failed during startup. Check service status and review the error logs.
Access denied for user Incorrect credentials or an authentication/permission mismatch. Verify the username, password, account host, and configured authentication method.
Can't connect to local MySQL server The server is stopped or the connection endpoint is incorrect. Start the service and verify the host, port, or socket configuration.
Port 3306 is already in use Another server process is using the port. Identify the existing process and resolve the conflict before starting another server.
Package not found The package is unavailable in the configured repositories. Refresh repository metadata and check the official instructions for your distribution.
Permission denied during installation The command requires administrative privileges. Use the appropriate administrator authorization for the operation.
Service unit not found The package is not installed or the service has a different name. Check installed packages and available service units.
Server fails after configuration changes An invalid option or incorrect configuration value. Review the error log and carefully revert or correct the invalid setting.
Two MySQL servers conflict Multiple installation methods have configured separate servers on the same port. Identify both installations and stop or reconfigure the unintended service.

General Troubleshooting Checklist

  1. Confirm that the correct MySQL Server package is installed.
  2. Verify the operating system and MySQL version compatibility.
  3. Check the service name and current status.
  4. Review the installation output for warnings or required setup steps.
  5. Check the server error log for startup failures.
  6. Confirm that the host, port, socket, and account settings are correct.
  7. Check for other database servers using the same port.
  8. Verify that required directories have appropriate permissions.
  9. Follow the official documentation for the exact version and platform.

18. macOS and Linux Installation Comparison

Feature macOS Linux
Common Installation Methods Official DMG/PKG installer or Homebrew. APT, DNF/YUM, Zypper, official MySQL repositories, or other supported methods.
Terminal Utility Terminal Distribution terminal or shell
Package Manager Homebrew, if selected APT, DNF, YUM, or Zypper, depending on distribution
Service Management Installer-provided service controls or Homebrew services. Often systemd on modern distributions.
Common Service Name Depends on installation method. Often mysql or mysqld.
Client Connection mysql -u root -p, subject to account configuration. mysql -u root -p or distribution-specific administrative authentication.
Version Source Depends on the selected installer or Homebrew formula. Depends on configured repositories and package selection.

19. Frequently Asked Questions (FAQs)

Q1. Can MySQL be installed on macOS?

Yes. MySQL can be installed on supported macOS versions using the official MySQL package installer or a compatible package manager such as Homebrew.

Q2. How do I install MySQL on Ubuntu?

For a standard installation using the configured Ubuntu repositories, update the package index with sudo apt update and install the server package with sudo apt install mysql-server. Follow the package's setup instructions afterward.

Q3. What is the difference between MySQL Server and MySQL Client?

MySQL Server manages the database and executes SQL statements. The client connects to the server and provides a way to send queries and display results.

Q4. How can I check whether MySQL is installed?

Run mysql --version to check for the command-line client. To verify a server connection, connect successfully and run SELECT VERSION();.

Q5. How do I start MySQL on macOS when installed using Homebrew?

Use brew services start mysql for a Homebrew-managed installation. The official package installer uses its own service-management mechanism.

Q6. How do I start MySQL on Linux?

On systems using systemd, the command is commonly sudo systemctl start mysql or sudo systemctl start mysqld, depending on the installed package.

Q7. Why does the MySQL client say “command not found”?

The command-line client may not be installed or its executable directory may not be included in the shell's PATH.

Q8. Is port 3306 always used by MySQL?

No. Port 3306 is the common default TCP port, but the server can be configured to use another port.

Q9. Should I install both MySQL Server and MySQL Workbench?

If you want a graphical interface, you can install Workbench as well as MySQL Server. Workbench is a client tool and does not replace the server.

Q10. Is it safe to use the root account for applications?

No. Routine applications should use separate accounts with only the privileges they need. The root account should be reserved for administrative tasks.


20. Practice Questions

  1. What are the main prerequisites for installing MySQL?
  2. Explain two methods of installing MySQL on macOS.
  3. Write the commands to install MySQL Server on Ubuntu using APT.
  4. What is the purpose of the systemctl command?
  5. How can you verify the MySQL client version?
  6. What is the difference between mysql --version and SELECT VERSION();?
  7. Explain the role of a MySQL data directory.
  8. What could cause a conflict on port 3306?
  9. List five common MySQL installation problems and their solutions.
  10. Why should database administrators configure secure authentication and restricted accounts?
  11. Write SQL statements to create a practice database and a students table.
  12. Explain why different Linux distributions may use different service names for MySQL.

21. Conclusion

Installing MySQL on macOS and Linux is an important first step toward learning database development and administration. macOS users can choose the official installer or Homebrew, while Linux users can select an appropriate package manager or the official MySQL repository for their distribution.

After installation, verify the server status, connect through the MySQL client, check the server version, create a practice database, and run basic SQL queries. Also configure secure authentication, use restricted accounts, maintain backups, and follow the documentation for your specific operating system and MySQL version.

Learn More: Explore JustAcademy MySQL Training

Register for a Demo: Register for MySQL Course Demo

whatsapp