How to Set Up MySQL on Linux

Galaxy Glossary

How do I set up MySQL on Linux quickly and securely?

Install, secure, and start a MySQL server on any major Linux distribution so you can create and manage databases locally or in production.

Sign up for the latest in SQL knowledge from the Galaxy Team!
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.

Description

Which Linux package manager commands install MySQL?

Use your distro’s native package manager for a clean install. On Debian-based systems run sudo apt update && sudo apt install mysql-server. On Red Hat, CentOS, or Fedora execute sudo dnf install mysql-server. These packages include the server daemon, client tools, and the systemd service unit.

How do I start and enable the MySQL service?

After installation, activate MySQL with sudo systemctl start mysqld. Enable auto-start on boot using sudo systemctl enable mysqld.Verify status via systemctl status mysqld and check the listening port (3306 by default) with ss -tunlp | grep 3306.

What is the fastest way to secure the server?

Run sudo mysql_secure_installation. The script sets a root password, removes anonymous accounts, disallows remote root login, removes the test database, and reloads privilege tables.Always complete this step before exposing the server to a network.

How do I create an e-commerce database and user?

Log in with sudo mysql -u root -p, then execute:

CREATE DATABASE ecommerce CHARACTER SET utf8mb4;
CREATE USER 'app_user'@'%' IDENTIFIED BY 'StrongP@ss!';
GRANT ALL PRIVILEGES ON ecommerce.* TO 'app_user'@'%';
FLUSH PRIVILEGES;

This isolates application access and follows the principle of least privilege.

How do I allow remote connections safely?

In /etc/mysql/mysql.conf.d/mysqld.cnf (Debian) or /etc/my.cnf (RHEL), set bind-address = 0.0.0.0 to listen on all interfaces.Open TCP 3306 in your firewall: sudo ufw allow 3306/tcp or sudo firewall-cmd --add-service=mysql --permanent && sudo firewall-cmd --reload. Restrict access to trusted IPs whenever possible.

What’s the recommended backup strategy?

Use mysqldump --single-transaction --routines --triggers ecommerce > ecommerce.sql for logical backups. For large datasets leverage mysqlpump or filesystem snapshots combined with --flush-logs.Automate backups with cron and store them off-site.

How do I test the setup with a sample query?

Create retail tables and run a join to confirm everything works:

USE ecommerce;
CREATE TABLE Customers(id INT PK AUTO_INCREMENT, name VARCHAR(100), email VARCHAR(100), created_at DATETIME);
CREATE TABLE Orders(id INT PK AUTO_INCREMENT, customer_id INT, order_date DATE, total_amount DECIMAL(10,2));
INSERT INTO Customers(name,email,created_at) VALUES ('Alice','alice@example.com',NOW());
INSERT INTO Orders(customer_id,order_date,total_amount) VALUES (1,CURDATE(),199.99);
SELECT c.name,o.total_amount FROM Customers c JOIN Orders o ON c.id=o.customer_id;
.

Why How to Set Up MySQL on Linux is important

How to Set Up MySQL on Linux Example Usage


USE ecommerce;
SELECT c.name, SUM(oi.quantity*p.price) AS lifetime_value
FROM Customers c
JOIN Orders o   ON o.customer_id = c.id
JOIN OrderItems oi ON oi.order_id  = o.id
JOIN Products p ON p.id = oi.product_id
GROUP BY c.id, c.name
ORDER BY lifetime_value DESC;

How to Set Up MySQL on Linux Syntax


# Debian/Ubuntu
sudo apt update && sudo apt install mysql-server

# Red Hat/CentOS/Fedora
a) sudo dnf install @mysql
b) sudo systemctl start mysqld
c) sudo systemctl enable mysqld

# Secure root and remove defaults
sudo mysql_secure_installation

# Connect as root
sudo mysql -u root -p

# Create database & user (e-commerce example)
CREATE DATABASE ecommerce CHARACTER SET utf8mb4;
CREATE USER 'app_user'@'%' IDENTIFIED BY 'StrongP@ss!';
GRANT ALL PRIVILEGES ON ecommerce.* TO 'app_user'@'%';
FLUSH PRIVILEGES;

# Firewall example (UFW)
sudo ufw allow 3306/tcp

# Backup command
mysqldump --single-transaction --routines --triggers ecommerce > ecommerce.sql

Common Mistakes

Frequently Asked Questions (FAQs)

Does MySQL install a default root password?

On most modern packages, root is created with auth_socket or a random password shown in /var/log/mysqld.log. Set your own password during mysql_secure_installation.

How can I change the data directory?

Stop MySQL, copy /var/lib/mysql to the new location, update datadir in my.cnf, adjust SELinux/AppArmor contexts, then start MySQL and verify.

Is MariaDB a drop-in replacement?

For most applications yes, but certain MySQL-specific features (e.g., InnoDB Cluster, Group Replication) require Oracle MySQL. Evaluate compatibility before switching.

Want to learn about other SQL terms?

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie
BauHealth Logo
Truvideo Logo
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.