How to Install MySQL on Ubuntu in 2025: Complete Step-by-Step Guide

MySQL remains the world’s most popular open-source database management system, powering millions of web applications and enterprise solutions. If you’re running Ubuntu and need to install MySQL, this comprehensive guide will walk you through the entire process, from installation to initial configuration.

Why Choose MySQL for Ubuntu?

MySQL offers several advantages for Ubuntu users:

  • High Performance: Optimized for speed and reliability
  • Cross-Platform Compatibility: Works seamlessly with Ubuntu’s ecosystem
  • Strong Community Support: Extensive documentation and active forums
  • Enterprise Features: Advanced security, backup, and monitoring tools
  • Cost-Effective: Free and open-source with optional commercial support

Prerequisites

Before installing MySQL on Ubuntu, ensure you have:

  • Ubuntu 20.04 LTS, 22.04 LTS, or 24.04 LTS
  • Root or sudo privileges
  • At least 512MB RAM (2GB recommended)
  • 200MB free disk space minimum
  • Active internet connection

Method 1: Installing MySQL Using APT Repository (Recommended)

This is the easiest and most straightforward method for most users.

Step 1: Update Package Index

sudo apt update
sudo apt upgrade -y

Step 2: Install MySQL Server

sudo apt install mysql-server -y

Step 3: Check MySQL Service Status

sudo systemctl status mysql

You should see output indicating the service is active and running.

Step 4: Start and Enable MySQL Service

sudo systemctl start mysql
sudo systemctl enable mysql

Method 2: Installing MySQL from Official MySQL Repository

For the latest MySQL version or specific version requirements, use the official MySQL repository.

Step 1: Download MySQL APT Repository

wget https://dev.mysql.com/get/mysql-apt-config_0.8.29-1_all.deb

Step 2: Install Repository Package

sudo dpkg -i mysql-apt-config_0.8.29-1_all.deb

Step 3: Update Package List

sudo apt update

Step 4: Install MySQL Server

sudo apt install mysql-server -y

Securing Your MySQL Installation

Run MySQL Security Script

Execute the built-in security script to improve your installation’s security:

sudo mysql_secure_installation

The script will prompt you to:

  1. Set up VALIDATE PASSWORD component: Choose Y for enhanced password security
  2. Set root password strength: Select from LOW (0), MEDIUM (1), or STRONG (2)
  3. Enter root password: Create a strong password
  4. Remove anonymous users: Choose Y
  5. Disallow root login remotely: Choose Y for better security
  6. Remove test database: Choose Y
  7. Reload privilege tables: Choose Y

Initial MySQL Configuration

Step 1: Access MySQL Shell

sudo mysql -u root -p

Enter your root password when prompted.

Step 2: Create a New Database User

CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'strong_password';

Step 3: Grant Privileges to New User

GRANT ALL PRIVILEGES ON *.* TO 'newuser'@'localhost';
FLUSH PRIVILEGES;

Step 4: Create a Sample Database

CREATE DATABASE sample_db;
USE sample_db;

Step 5: Exit MySQL Shell

EXIT;

Managing MySQL Service

Essential MySQL Service Commands

  1. Start MySQL service: sudo systemctl start mysql
  2. Stop MySQL service: sudo systemctl stop mysql
  3. Restart MySQL service: sudo systemctl restart mysql
  4. Check service status: sudo systemctl status mysql
  5. Enable auto-start on boot: sudo systemctl enable mysql

Configuring MySQL Settings

Main Configuration File Location

The primary MySQL configuration file is located at:

/etc/mysql/mysql.conf.d/mysqld.cnf

Common Configuration Changes

  1. Edit configuration file: sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
  2. Key settings to consider:
    • bind-address: Set to 0.0.0.0 for remote connections
    • max_connections: Adjust based on your needs
    • innodb_buffer_pool_size: Optimize for available RAM
  3. Restart MySQL after changes: sudo systemctl restart mysql

Testing Your MySQL Installation

Test 1: Check MySQL Version

mysql --version

Test 2: Connect to MySQL

mysql -u root -p

Test 3: Show Databases

SHOW DATABASES;

Test 4: Check User Accounts

SELECT user, host FROM mysql.user;

Enabling Remote Access (Optional)

If you need to connect to MySQL from remote machines:

Step 1: Edit MySQL Configuration

sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

Change:

bind-address = 127.0.0.1

To:

bind-address = 0.0.0.0

Step 2: Create Remote User

CREATE USER 'remote_user'@'%' IDENTIFIED BY 'strong_password';
GRANT ALL PRIVILEGES ON *.* TO 'remote_user'@'%';
FLUSH PRIVILEGES;

Step 3: Configure Firewall

sudo ufw allow 3306/tcp

Step 4: Restart MySQL

sudo systemctl restart mysql

Troubleshooting Common Issues

Issue 1: MySQL Service Won’t Start

Solution:

sudo systemctl status mysql
sudo journalctl -u mysql

Check logs for specific error messages and address accordingly.

Issue 2: “Access Denied” Error

Solution:

sudo mysql -u root
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'new_password';
FLUSH PRIVILEGES;

Issue 3: Port 3306 Already in Use

Solution:

sudo netstat -tlnp | grep 3306
sudo kill -9 [PID]

Issue 4: Disk Space Issues

Solution:

sudo du -sh /var/lib/mysql/
sudo mysql -u root -p -e "SHOW VARIABLES LIKE 'datadir';"

Performance Optimization Tips

1. Optimize MySQL Configuration

Key parameters to tune:

  • innodb_buffer_pool_size: Set to 70-80% of available RAM
  • query_cache_size: Enable for read-heavy applications
  • max_connections: Balance based on your application needs
  • innodb_log_file_size: Increase for write-heavy workloads

2. Regular Maintenance Tasks

# Optimize tables
mysqlcheck -u root -p --optimize --all-databases

# Check for table corruption
mysqlcheck -u root -p --check --all-databases

# Repair corrupted tables
mysqlcheck -u root -p --repair --all-databases

3. Monitor Performance

-- Show processlist
SHOW PROCESSLIST;

-- Check slow queries
SHOW VARIABLES LIKE 'slow_query_log';

-- View table status
SHOW TABLE STATUS;

Backup and Recovery

Create Database Backup

mysqldump -u root -p database_name > backup_file.sql

Restore Database from Backup

mysql -u root -p database_name < backup_file.sql

Automated Backup Script

Create a simple backup script:

#!/bin/bash
DATE=$(date +%Y%m%d_%H%M%S)
mysqldump -u root -p[PASSWORD] --all-databases > /backup/mysql_backup_$DATE.sql

Installing MySQL Workbench (GUI Tool)

For users who prefer a graphical interface:

sudo apt update
sudo apt install mysql-workbench-community -y

Uninstalling MySQL

If you need to completely remove MySQL:

sudo systemctl stop mysql
sudo apt remove --purge mysql-server mysql-client mysql-common -y
sudo apt autoremove -y
sudo rm -rf /etc/mysql /var/lib/mysql

Best Practices for MySQL on Ubuntu

Security Best Practices

  1. Use strong passwords for all MySQL accounts
  2. Limit user privileges to minimum required access
  3. Regularly update MySQL to latest stable version
  4. Enable SSL connections for remote access
  5. Monitor access logs for suspicious activity

Performance Best Practices

  1. Regular database maintenance (optimize, repair, analyze)
  2. Monitor slow query log and optimize problematic queries
  3. Implement proper indexing strategies
  4. Use connection pooling in applications
  5. Regular backup scheduling with tested recovery procedures

Conclusion

Installing MySQL on Ubuntu in 2025 is a straightforward process that can be completed in minutes. Whether you choose the standard APT repository method or the official MySQL repository, you’ll have a robust database system ready for your applications.

Remember to:

  • Secure your installation immediately after setup
  • Configure appropriate user accounts and privileges
  • Implement regular backup procedures
  • Monitor performance and optimize as needed
  • Keep your MySQL installation updated

With MySQL properly installed and configured on your Ubuntu system, you’re ready to build powerful, data-driven applications that can scale with your needs.

Frequently Asked Questions

Q: What’s the default root password for MySQL on Ubuntu? A: Recent MySQL versions on Ubuntu don’t set a default password. Use sudo mysql -u root initially, then set a password using the security script.

Q: Can I run multiple MySQL versions on the same Ubuntu system? A: Yes, but it requires careful configuration of different ports, data directories, and socket files.

Q: How do I check which MySQL version is installed? A: Use mysql --version or connect to MySQL and run SELECT VERSION();

Q: Is MySQL free to use on Ubuntu? A: Yes, MySQL Community Edition is completely free and open-source.

Q: How much RAM does MySQL need on Ubuntu? A: Minimum 512MB, but 2GB+ is recommended for production use.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top