Install, secure, and start a MySQL server on any major Linux distribution so you can create and manage databases locally or in production.
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.
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
.
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.
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.
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.
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.
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;
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
.
Stop MySQL, copy /var/lib/mysql
to the new location, update datadir
in my.cnf
, adjust SELinux/AppArmor contexts, then start MySQL and verify.
For most applications yes, but certain MySQL-specific features (e.g., InnoDB Cluster, Group Replication) require Oracle MySQL. Evaluate compatibility before switching.