How to Install MySQL on Linux: A Fast, Step-by-Step Guide

How to Install MySQL on Linux: A Fast, Step-by-Step Guide

Want to install MySQL on Linux quickly and securely? This fast, step-by-step guide walks you through installation, hardening, and practical VPS tips to get a production-ready database up and running.

Installing MySQL on a Linux VPS is a common task for webmasters, developers, and businesses that need a reliable relational database engine. This guide walks you through practical, step-by-step instructions for installing and securing MySQL on popular Linux distributions, explains the underlying concepts, outlines typical application scenarios, compares advantages versus alternatives, and gives purchasing advice for selecting a VPS environment that suits database workloads.

Why MySQL on Linux?

MySQL is a widely adopted open-source relational database management system (RDBMS) known for its stability, performance, and ecosystem compatibility. Running MySQL on Linux is the dominant deployment model due to Linux’s performance, low overhead, package management, and strong automation tooling. For business-critical services—web backends, analytics, CMS platforms like WordPress—Linux + MySQL remains a pragmatic choice.

Preparation and principles

Before installing, consider these principles to ensure a stable production deployment:

  • Separation of roles: dedicate a VPS for database workloads when possible, or at least isolate resources (CPU, RAM, I/O).
  • Persistence and backups: plan storage (SSD preferred), snapshot backup cadence, and logical backups (mysqldump, mysqlpump) or physical backups (Percona XtraBackup).
  • Security: limit remote access, use strong passwords or key-based management, configure firewall and SELinux/AppArmor.
  • Monitoring and tuning: enable slow query logging, monitor buffer pool usage and connections, and adjust my.cnf according to workload.

Installation overview

The installation differs slightly across distributions. Below are fast, practical steps for Ubuntu/Debian and RHEL/CentOS/Fedora families. These use package repositories to ensure updates and security patches.

Ubuntu / Debian (apt-based)

1. Update the package index and install the server package:

  • sudo apt update
  • sudo apt install mysql-server

2. After installation, start and enable the service:

  • sudo systemctl start mysql
  • sudo systemctl enable mysql

3. Run the secure setup tool to set root password, remove anonymous users, and disable remote root login:

  • sudo mysql_secure_installation

4. Verify the server status and version:

  • sudo systemctl status mysql
  • mysql --version

RHEL / CentOS / Fedora (yum/dnf-based)

1. For CentOS/RHEL 7/8 or Fedora, add the official MySQL Yum repository if you want latest GA releases, or use the distro-provided MariaDB if preferred. To use Oracle’s MySQL:

  • Download the repo package from dev.mysql.com and install: sudo rpm -Uvh mysql80-community-release-el7-*.rpm

2. Install the server:

  • sudo yum install mysql-community-server or sudo dnf install mysql-community-server

3. Start and enable the service:

  • sudo systemctl start mysqld
  • sudo systemctl enable mysqld

4. Retrieve the temporary root password from the log and run secure installation:

  • sudo grep 'temporary password' /var/log/mysqld.log
  • sudo mysql_secure_installation

Containerized and alternative installs

For development or ephemeral workloads, consider Docker:

  • docker run --name mysql -e MYSQL_ROOT_PASSWORD=secret -d mysql:8.0

Using Docker simplifies reproducibility but requires separate strategies for persistent volumes and backup.

Securing MySQL

Security is paramount. After installation:

  • Run mysql_secure_installation to remove test databases and anonymous accounts and to set a strong root password.
  • Use least privilege: create database-specific users and grant only required privileges (GRANT SELECT, INSERT, UPDATE on db.table TO ‘user’@’host’;).
  • Disable remote root access: ensure root is limited to localhost or use socket authentication where appropriate.
  • Harden network access: bind MySQL to a private IP or localhost by configuring bind-address in /etc/mysql/my.cnf or relevant mysqld configuration files.
  • Enable firewall rules: open port 3306 only to trusted hosts; on Ubuntu use UFW (ufw allow from 203.0.113.5 to any port 3306), or configure iptables/nftables.
  • Consider TLS/SSL: enable encrypted connections with server certificates to protect data in transit.
  • SELinux/AppArmor: if enabled, ensure MySQL has the correct profiles or disable only if you understand the risk.

Post-install configuration and tuning

After installing and securing MySQL, tune it for your workload. Important parameters live in /etc/mysql/my.cnf or /etc/my.cnf. Key areas:

InnoDB and buffer pool

MySQL’s default storage engine is InnoDB. For transactional workloads, tune:

  • innodb_buffer_pool_size: set to ~60–80% of available RAM on dedicated DB servers. This is the single most important parameter for InnoDB performance.
  • innodb_log_file_size: larger log files can improve write throughput; consider 512M–1G depending on WAL activity.
  • innodb_flush_log_at_trx_commit: 1 is safest (ACID-compliant); 2 or 0 can increase throughput with risk of losing last transactions during a crash.

Connection and thread handling

  • max_connections: adjust to expected concurrency and ensure the server has RAM headroom; too high values may cause OOM.
  • thread_cache_size: reduces overhead from creating threads for new connections.

Logging and slow queries

  • Enable slow query log (slow_query_log=ON) and set long_query_time to a threshold (e.g., 1s) to capture inefficient queries for optimization.
  • Rotate logs and monitor disk usage.

Backup, replication, and high availability

Plan backups and HA depending on RPO/RTO requirements:

  • Logical backups: mysqldump --single-transaction for InnoDB-heavy databases; smaller and easier to transport but slower for large data sets.
  • Physical backups: tools like Percona XtraBackup enable hot backups with minimal downtime for large datasets.
  • Replication: set up asynchronous replication for read scaling and offsite backups; consider semi-sync or Group Replication for stronger durability.
  • Clustering: solutions like Galera Cluster provide multi-master synchronous replication but come with complexity and specific workload considerations.

Application considerations and connectors

Most web stacks interact with MySQL through connectors/drivers. Consider:

  • Use the native client libraries for your language (e.g., Connector/Python, Connector/J for Java, mysqli or PDO_MYSQL for PHP).
  • Check authentication plugin compatibility—MySQL 8 introduced caching_sha2_password as default; some older clients expect mysql_native_password. You can create users specifying the plugin: CREATE USER 'app'@'%' IDENTIFIED WITH mysql_native_password BY 'strongpwd';
  • Ensure connection pooling at application level to reduce overhead (e.g., PgBouncer equivalent for MySQL or connection pooling built into frameworks).

When to choose MySQL vs alternatives

Choosing between MySQL, MariaDB, PostgreSQL, or NoSQL depends on requirements:

  • MySQL: excellent for web applications, broad hosting support, and mature ecosystem. Strong read performance and replication tools.
  • MariaDB: community-driven fork with additional storage engines and features; often a drop-in replacement for MySQL in many distros.
  • PostgreSQL: better suited for advanced SQL features, complex queries, and strict ACID compliance in certain modes; consider it for analytics, GIS, and complex transactions.
  • NoSQL: use when schema-less, horizontal scalability, or document-based access patterns are primary concerns (e.g., MongoDB, Cassandra).

Choosing the right VPS for MySQL

For realistic database performance, VPS selection matters. Evaluate:

  • CPU: prioritize single-threaded performance for query processing; higher clock speeds help OLTP workloads.
  • Memory: InnoDB buffer pool benefits from more RAM—plan buffer pool size to fit your working set.
  • Disk I/O: choose SSD-backed storage with predictable IOPS and low latency; avoid noisy-neighbor shared HDD tiers for DB servers.
  • Network: if you replicate across nodes or have application servers, ensure low-latency networking between components.
  • Backups and snapshots: confirm snapshot frequency and retention, and prefer providers that offer automated backups and restore options.

Example quick checklist for production deployment

  • Install MySQL from trusted repository
  • Run mysql_secure_installation and set strong credentials
  • Restrict network exposure and configure firewall
  • Tune innodb_buffer_pool_size and log file sizes
  • Enable slow query log and set up monitoring
  • Implement regular backups and verify restores
  • Consider replication or clustering based on SLA

Summary

Installing MySQL on Linux is straightforward, but a production-ready deployment requires planning around security, backups, monitoring, and performance tuning. Start with a secure base install, configure network and access controls, tune key InnoDB parameters to match your RAM and workload, and implement backup and replication strategies that meet your recovery objectives. For many webmasters and enterprises, placing MySQL on a dedicated or well-provisioned VPS with SSD storage and sufficient RAM provides predictable performance and cost efficiency.

If you need a reliable VPS to host your MySQL instance—with options tuned for US-based deployments and SSD-backed storage—consider checking out VPS.DO’s offerings, including the USA VPS plans which are designed to provide the CPU, memory, and storage profiles suitable for database workloads.

Fast • Reliable • Affordable VPS - DO It Now!

Get top VPS hosting with VPS.DO’s fast, low-cost plans. Try risk-free with our 7-day no-questions-asked refund and start today!