How to Set Up MySQL on Linux: A Comprehensive Guide
Introduction
MySQL is a powerful, open-source relational database management system (RDBMS) widely used in web applications and enterprise environments. Installing MySQL on a Linux server is a fundamental skill for developers and system administrators. This guide provides a detailed, step-by-step walkthrough on how to install and configure MySQL on various Linux distributions, including Ubuntu, CentOS, and Fedora.
Prerequisites
Before proceeding, ensure you have:
- A Linux server: This guide covers Ubuntu 20.04/22.04, CentOS 7/8, and Fedora 33/34.
- Root or sudo privileges: Necessary for installation and configuration.
- Internet connectivity: Required for downloading packages and updates.
Table of Contents
- How to Set Up MySQL on Linux: A Comprehensive Guide
- Introduction
- Prerequisites
- Table of Contents
- 1. Updating System Packages
- 2. Installing MySQL
- 3. Securing the MySQL Installation
- 4. Managing the MySQL Service
- 5. Testing the MySQL Installation
- 6. Configuring Remote Access (Optional)
- 7. Setting Up a Sample Database (Optional)
- 8. Scheduling Regular Backups
- 9. Updating MySQL
- 10. Conclusion
1. Updating System Packages
Before installing new software, update your system packages to the latest versions.
Ubuntu
sudo apt update
sudo apt upgrade -y
CentOS
sudo yum update -y
Fedora
sudo dnf update -y
2. Installing MySQL
On Ubuntu
Ubuntu's default repositories include MySQL packages.
Install MySQL Server:
bashsudo apt install mysql-server -y
Verify Installation:
bashmysql --version
On CentOS and Fedora
CentOS and Fedora use the YUM and DNF package managers, respectively.
Add the MySQL Repository:
Download the MySQL Community RPM package:
bashwget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm # For CentOS 7 wget https://dev.mysql.com/get/mysql80-community-release-el8-3.noarch.rpm # For CentOS 8
Install the RPM package:
bashsudo rpm -Uvh mysql80-community-release-el7-3.noarch.rpm # CentOS 7 sudo rpm -Uvh mysql80-community-release-el8-3.noarch.rpm # CentOS 8
Install MySQL Server:
bashsudo yum install mysql-server -y # CentOS sudo dnf install mysql-server -y # Fedora
Verify Installation:
bashmysql --version
3. Securing the MySQL Installation
Run the mysql_secure_installation
script to enhance security.
Run the Security Script:
bashsudo mysql_secure_installation
Follow the Prompts:
- Set the root password: Enter a strong password.
- Remove anonymous users: Yes.
- Disallow root login remotely: Yes.
- Remove test database and access to it: Yes.
- Reload privilege tables now: Yes.
4. Managing the MySQL Service
Ensure that MySQL starts on boot and is currently running.
Start MySQL Service:
bashsudo systemctl start mysql # Ubuntu sudo systemctl start mysqld # CentOS/Fedora
Enable MySQL to Start on Boot:
bashsudo systemctl enable mysql # Ubuntu sudo systemctl enable mysqld # CentOS/Fedora
Check Service Status:
bashsudo systemctl status mysql # Ubuntu sudo systemctl status mysqld # CentOS/Fedora
5. Testing the MySQL Installation
Log In to MySQL Shell:
bashsudo mysql -u root -p
- Enter the root password when prompted.
Run a Test Query:
sqlSHOW DATABASES;
Exit the MySQL Shell:
sqlEXIT;
6. Configuring Remote Access (Optional)
By default, MySQL listens only on localhost
. To allow remote connections:
Edit MySQL Configuration File:
Open the file in a text editor.
bashsudo nano /etc/mysql/mysql.conf.d/mysqld.cnf # Ubuntu sudo nano /etc/my.cnf # CentOS/Fedora
Modify Bind Address:
Locate the line:
inibind-address = 127.0.0.1
Change it to:
inibind-address = 0.0.0.0
Create a Remote User:
bashsudo mysql -u root -p
In the MySQL shell:
sqlCREATE USER 'remote_user'@'%' IDENTIFIED BY 'strong_password'; GRANT ALL PRIVILEGES ON *.* TO 'remote_user'@'%' WITH GRANT OPTION; FLUSH PRIVILEGES; EXIT;
Adjust Firewall Settings:
Ubuntu with UFW:
bashsudo ufw allow 3306/tcp sudo ufw reload
CentOS/Fedora with firewalld:
bashsudo firewall-cmd --permanent --add-port=3306/tcp sudo firewall-cmd --reload
Restart MySQL Service:
bashsudo systemctl restart mysql # Ubuntu sudo systemctl restart mysqld # CentOS/Fedora
7. Setting Up a Sample Database (Optional)
Log In to MySQL Shell:
bashsudo mysql -u root -p
Create a New Database:
sqlCREATE DATABASE testdb;
Create a New User and Grant Permissions:
sqlCREATE USER 'testuser'@'localhost' IDENTIFIED BY 'user_password'; GRANT ALL PRIVILEGES ON testdb.* TO 'testuser'@'localhost'; FLUSH PRIVILEGES;
Use the New Database:
sqlUSE testdb;
Create a Table:
sqlCREATE TABLE users ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100) NOT NULL, email VARCHAR(100) NOT NULL UNIQUE );
Insert Data:
sqlINSERT INTO users (name, email) VALUES ('Alice Smith', '[email protected]');
Query the Data:
sqlSELECT * FROM users;
Exit the MySQL Shell:
sqlEXIT;
8. Scheduling Regular Backups
Regular backups are essential for data recovery.
Create a Backup Directory:
bashsudo mkdir /var/backups/mysql sudo chown $(whoami):$(whoami) /var/backups/mysql
Create a Backup Script:
bashnano ~/mysql_backup.sh
Add the following content:
bash#!/bin/bash TIMESTAMP=$(date +"%F") BACKUP_DIR="/var/backups/mysql/$TIMESTAMP" mkdir -p "$BACKUP_DIR" mysqldump -u root -p'root_password' --all-databases > "$BACKUP_DIR/all_databases.sql"
Replace
'root_password'
with your MySQL root password.
Make the Script Executable:
bashchmod +x ~/mysql_backup.sh
Schedule the Script with Cron:
bashcrontab -e
Add the following line to run the backup daily at 2 AM:
bash0 2 * * * /home/your_username/mysql_backup.sh
Replace
/home/your_username/
with your actual username.
9. Updating MySQL
Keep MySQL updated for security and performance improvements.
Update Package Lists and Upgrade Packages:
Ubuntu:
bashsudo apt update sudo apt upgrade -y
CentOS:
bashsudo yum update -y
Fedora:
bashsudo dnf upgrade -y
Check MySQL Version:
bashmysql --version
10. Conclusion
You have successfully installed and configured MySQL on your Linux server. This guide covered the essential steps, from installation and securing the server to managing databases and scheduling backups. Regular maintenance and updates will keep your MySQL server running smoothly and securely.
Note: Always ensure that your MySQL server is properly secured, especially if it's accessible over a network. Use strong passwords, restrict user privileges, and keep your software up to date to protect against vulnerabilities.