0
0
MysqlHow-ToBeginner · 3 min read

How to Install MySQL on Linux: Step-by-Step Guide

To install MySQL on Linux, use your distribution's package manager like apt for Ubuntu or yum for CentOS. Run sudo apt install mysql-server on Ubuntu or sudo yum install mysql-server on CentOS, then start the MySQL service with sudo systemctl start mysql.
📐

Syntax

Use your Linux distribution's package manager to install MySQL. The common commands are:

  • Ubuntu/Debian: sudo apt install mysql-server
  • CentOS/RHEL: sudo yum install mysql-server
  • After installation, start the MySQL service with sudo systemctl start mysqld or sudo systemctl start mysql.

Use sudo systemctl enable mysql to start MySQL automatically on boot.

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

Example

This example shows how to install MySQL on Ubuntu Linux, start the service, and check its status.

bash
sudo apt update
sudo apt install mysql-server
sudo systemctl start mysql
sudo systemctl status mysql
Output
● mysql.service - MySQL Community Server Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled) Active: active (running) since Thu 2024-06-20 10:00:00 UTC; 10s ago Main PID: 1234 (mysqld) Tasks: 29 (limit: 4915) Memory: 150.0M CGroup: /system.slice/mysql.service └─1234 /usr/sbin/mysqld
⚠️

Common Pitfalls

Common mistakes when installing MySQL on Linux include:

  • Not updating package lists before installation, causing outdated packages.
  • Forgetting to start the MySQL service after installation.
  • Using the wrong service name (mysqld vs mysql) depending on the Linux distribution.
  • Not securing MySQL after installation (run sudo mysql_secure_installation to set root password and remove test users).
bash
sudo apt install mysql-server
# Wrong: forgetting to start service
sudo systemctl start mysql
# Correct: always start and enable service
sudo systemctl start mysql
sudo systemctl enable mysql
📊

Quick Reference

CommandDescription
sudo apt updateRefresh package list on Ubuntu/Debian
sudo apt install mysql-serverInstall MySQL server on Ubuntu/Debian
sudo yum install mysql-serverInstall MySQL server on CentOS/RHEL
sudo systemctl start mysqlStart MySQL service
sudo systemctl enable mysqlEnable MySQL to start on boot
sudo mysql_secure_installationSecure MySQL installation

Key Takeaways

Use your Linux distribution's package manager to install MySQL.
Always update package lists before installing new software.
Start and enable the MySQL service after installation.
Run mysql_secure_installation to secure your MySQL server.
Service names may differ: use 'mysql' or 'mysqld' depending on your system.