Install MySQL on Linux: A Fast, Step-by-Step Guide
Ready to Install MySQL on Linux and get a production-grade database running on your VPS? This fast, step-by-step guide walks you through practical setup, tuning, and backup choices so you can deploy securely and confidently.
Installing and running a production-grade MySQL server on a Linux VPS is a fundamental skill for site owners, developers, and companies that depend on reliable data storage. This guide walks through practical, fast, and detailed steps to get MySQL up and running, while explaining underlying principles, common application scenarios, comparisons with alternatives, and purchase considerations for VPS hosting.
Why MySQL on Linux: core principles and architecture
MySQL is an open-source relational database management system (RDBMS) that follows the client-server model. At its core, a MySQL deployment consists of:
- mysqld — the server daemon that manages storage engines, query parsing, optimization, and connections.
- Storage engines — InnoDB (default), MyISAM (legacy), Memory, etc. InnoDB provides ACID compliance, transactions, row-level locking and crash recovery via redo logs (ib_logfile).
- Data files — tables and indexes stored on disk (datadir, commonly /var/lib/mysql).
- Binary logs — used for replication and point-in-time recovery.
- Configuration — my.cnf (or /etc/mysql/mysql.conf.d/) governs memory buffers, connections, logging, and security.
Understanding these components helps with tuning, backups, replication, and securing the server.
Common application scenarios
MySQL is widely used for:
- Web applications and CMS platforms (WordPress, Drupal, Magento).
- Transactional systems where ACID compliance is required.
- Analytics and logging for moderate-sized datasets (with careful indexing and partitioning).
- Replication topologies (master-slave, master-master) for high availability and scaling reads.
On VPS instances, MySQL often runs alongside web servers (Nginx/Apache) and application runtimes (PHP/Python/Node). Proper resource allocation is crucial to avoid contention between services.
Quick prerequisites and choices
Before installation, decide:
- Distribution — Debian/Ubuntu vs. RHEL/CentOS/Alma/Rocky. Package managers differ (apt vs dnf/yum).
- MySQL version — use the MySQL Community Server from Oracle for the latest MySQL releases, or the distro repository for convenience.
- Storage — prefer SSD-backed storage for I/O-heavy workloads. Ensure enough disk space for data, binlogs, and backups.
- Backup strategy — logical (mysqldump) vs physical (xtrabackup) and frequency.
Step-by-step installation (Debian/Ubuntu)
These steps install the official MySQL APT repo and MySQL server 8.x. Adjust versions as needed.
- Update packages and install prerequisites:
sudo apt update && sudo apt install -y wget lsb-release gnupg - Download and add the MySQL APT repository package:
wget https://dev.mysql.com/get/mysql-apt-config_0.8.24-1_all.deb
sudo dpkg -i mysql-apt-config_0.8.24-1_all.debDuring dpkg, choose the MySQL server & tools version you want.
- Update and install:
sudo apt update
sudo apt install -y mysql-server - Start and enable service:
sudo systemctl enable --now mysql - Run security script:
sudo mysql_secure_installationFollow prompts to set root password, remove anonymous users, disallow remote root login, remove test database, and reload privilege tables.
Verify operation
Connect locally to test:
sudo mysql -u root -p
Check server status and variables:
SHOW GLOBAL STATUS LIKE 'Threads_connected';
SHOW VARIABLES LIKE 'datadir';
Step-by-step installation (RHEL/CentOS/Alma/Rocky)
- Add the MySQL YUM repository:
Download RPM from MySQL repository and install:
sudo rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el8-3.noarch.rpm - Install server:
sudo dnf install -y mysql-server - Enable and start service:
sudo systemctl enable --now mysqld - Initial root password:
On first start, check
/var/log/mysqld.logfor the generated temporary root password, then runsudo mysql_secure_installationto set a new password and harden the instance.
Basic post-install configuration and hardening
- Bind address — in /etc/mysql/my.cnf or /etc/my.cnf, set
bind-address = 127.0.0.1to restrict remote access unless required. For remote access, use a specific private IP and firewall rules. - User management — prefer creating users with least privileges and strong passwords. Example:
CREATE USER 'appuser'@'10.0.0.5' IDENTIFIED BY 'S3cureP@ss';
GRANT SELECT, INSERT, UPDATE, DELETE ON mydb.* TO 'appuser'@'10.0.0.5'; - TLS/SSL — enable encrypted connections using server certificates (generated or from a CA) and enforce require_secure_transport where appropriate.
- Firewall — use UFW, firewalld, or cloud security groups to open port 3306 only to authorized application hosts.
Performance tuning basics
MySQL performance hinges on memory allocation, IO, and schema design. Key variables:
- innodb_buffer_pool_size — the primary InnoDB memory area. For dedicated DB servers, set to ~60-80% of available RAM.
- innodb_log_file_size — larger logs reduce checkpoint IO; balance recovery time vs throughput.
- max_connections — limit to realistic values and use connection pooling in application servers to avoid connection storms.
- query_cache — deprecated in modern MySQL; prefer application or proxy caching.
- slow_query_log — enable and analyze to find missing indexes and expensive queries.
Always monitor changes and benchmark (sysbench, mysqlslap) before and after tuning. Keep a copy of my.cnf backups so you can revert misconfigurations.
Backup and recovery
Implement a robust backup strategy:
- Logical backups — mysqldump is simple and portable, good for smaller datasets and schema migrations.
- Physical backups — Percona XtraBackup allows hot physical backups for large InnoDB datasets without downtime.
- Binary logs — enable binary logging and rotate logs; combine binlogs with full backups for point-in-time recovery.
- Automate and test — schedule backups (cron/systemd timers), transfer to offsite storage, and regularly restore to verify integrity.
High availability and scaling
Common options to increase availability and scale reads/writes:
- Replication — asynchronous replication (classic master->slave) is straightforward for read scaling. GTIDs simplify failover management.
- Group Replication / InnoDB Cluster — provides multi-primary or single-primary group-based high availability with automatic failover.
- Proxy layer — ProxySQL or HAProxy can be placed in front of MySQL replicas to route traffic, execute query routing, and handle failover logic.
- Sharding — manual or via middleware for very large datasets where horizontal partitioning is required.
Comparisons and when to choose MySQL
Picking a database involves trade-offs. Brief comparisons:
- MySQL vs MariaDB — MariaDB is a fork of MySQL that often includes additional storage engines and features, sometimes with different performance characteristics. MariaDB can be preferable for certain features or licensing preferences, but MySQL (Oracle) provides the latest MySQL-specific developments and broad ecosystem support.
- MySQL vs PostgreSQL — PostgreSQL emphasizes standards compliance, advanced SQL features, and extensibility (JSONB, PostGIS). For complex queries, analytics, and strict ACID behavior, PostgreSQL can be a better fit. For LAMP stacks, CMSs, and many web apps, MySQL remains a default choice due to compatibility and familiarity.
- Managed DB vs Self-Hosted — managed services remove operational burden (backups, patches, HA) at higher cost. Self-hosted on a VPS gives full control and cost-efficiency but requires DBA expertise.
VPS selection and provisioning advice
When running MySQL on a VPS consider these factors:
- CPU — MySQL benefits from single-thread performance for complex queries and multiple cores for concurrent connections. Choose modern CPUs with good clock speed.
- Memory — allocate enough RAM to fit your working set in innodb_buffer_pool. For databases, favor RAM over many CPU cores.
- Storage — prefer NVMe/SSD with provisioned IOPS. Use RAID or cloud block storage snapshots for redundancy.
- Network — low latency between app servers and DB is critical. Choose a VPS provider and plan with reliable networking and data centers near your users.
- Backups and snapshots — ensure the provider offers snapshotting and automated backups, and verify restore speed and retention policies.
Summary and next steps
Installing MySQL on Linux is straightforward, but running it reliably requires attention to configuration, security, and operational processes. Start with the official packages for the desired MySQL version, harden access, tune innodb_buffer_pool_size and log settings for your workload, and implement automated backups with periodic test restores. For scaling, evaluate replication, a proxy layer, or managed offerings if operational overhead is a concern.
For many site owners and developers, a well-provisioned VPS is the ideal platform to run MySQL. If you need reliable, low-latency infrastructure in the United States, consider the USA VPS plans available at VPS.DO — USA VPS. These plans offer configurable CPU, RAM, and SSD storage options suitable for MySQL workloads, along with snapshot and backup features to simplify operations.