install mysql on ubuntu is the APT-based deployment of MySQL 8.x server on Ubuntu 20.04/22.04, followed by mysql_secure_installation and root authentication plugin configuration.
# Update package index and install MySQL server
sudo apt update
sudo apt install mysql-server -y
install mysql on ubuntu Syntax Reference
The commands below represent a complete installation workflow. Copy and paste the block into your terminal on a fresh Ubuntu system.
# Update package index and install MySQL server
sudo apt update
sudo apt install mysql-server -y
# Check service status
sudo systemctl status mysql
# Secure the installation (interactive)
sudo mysql_secure_installation
# Log in as root (if auth_socket is default)
sudo mysql
# Check bind address (default 127.0.0.1)
sudo ss -tlnp | grep 3306
install mysql on ubuntu Rapid Reference Cheat Sheet
| Installation Method | Command | Key Flag | Use Case |
|---|---|---|---|
| Ubuntu native APT | sudo apt install mysql-server-8.0 |
-y |
Standard production, security-patched by Ubuntu repos |
| MySQL APT repository | dpkg -i mysql-apt-config_0.8.30-1_all.deb && sudo apt upgrade |
N/A | Need latest minor release (e.g., 8.0.37 on Ubuntu 22.04) |
| Docker container | docker run --name mysql -e MYSQL_ROOT_PASSWORD=secret -d mysql:8.0 |
--name, -e, -d |
Isolated dev/test or microservice environments |
Advanced Implementation & Parameters
Choosing the exact MySQL version
# See available versions in native repos
apt-cache policy mysql-server-8.0
# For Ubuntu 22.04, the native package is 8.0.36-0ubuntu0.22.04.1
# To get 8.0.37, add the official MySQL APT repo:
wget https://dev.mysql.com/get/mysql-apt-config_0.8.30-1_all.deb
sudo dpkg -i mysql-apt-config_0.8.30-1_all.deb
sudo apt update
sudo apt install mysql-server
Root authentication plugin behavior
On Ubuntu, the default mysql-server package configures root to use auth_socket. This means only the OS root user can authenticate without a password via sudo mysql. To enable password-based login for remote or application access, alter the plugin:
-- Connect via socket as OS root
sudo mysql
-- Switch to caching_sha2_password and set a password
ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'YourStr0ngP@ss';
FLUSH PRIVILEGES;
Bind address and remote access
# Default: only loopback
sudo grep bind-address /etc/mysql/mysql.conf.d/mysqld.cnf
# To allow remote connections, change to 0.0.0.0 or a specific IP:
sudo sed -i 's/^bind-address.*/bind-address = 0.0.0.0/' /etc/mysql/mysql.conf.d/mysqld.cnf
sudo systemctl restart mysql
# Verify listening on all interfaces
sudo ss -tlnp | grep mysql
Creating a dedicated application user
-- Inside mysql shell
CREATE USER 'app_user'@'192.168.0.%' IDENTIFIED BY 'AppP@ss2025';
GRANT SELECT, INSERT, UPDATE, DELETE ON mydb.* TO 'app_user'@'192.168.0.%';
FLUSH PRIVILEGES;
Error Resolution & Troubleshooting
| Error / Symptom | Root Cause | Remediation |
|---|---|---|
Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' |
Service not running or socket path wrong | sudo systemctl start mysql then check sudo journalctl -u mysql -n 20 |
Access denied for user 'root'@'localhost' (using password: NO) |
auth_socket plugin active; trying to connect with password | Use sudo mysql instead of mysql -u root -p |
Failed to start mysql.service: Unit mysql.service not found. |
MySQL not installed or package name mismatch | Verify with apt list --installed | grep mysql-server; reinstall mysql-server-8.0 |
ERROR 1410 (42000): You are not allowed to create a user with GRANT |
Attempting to grant privileges on a user that does not exist | Create user first with CREATE USER, then GRANT |
Binding to 0.0.0.0 causes connection hangs |
Firewall (ufw) blocking port 3306 | sudo ufw allow 3306/tcp |
Checking logs quickly
# General error log
sudo grep -i 'error' /var/log/mysql/error.log
# Service logs from systemd
sudo journalctl -u mysql --since "5 minutes ago"
# Test connectivity
mysql -u root -h 127.0.0.1 -P 3306 -p --protocol=tcp
Production-Grade Implementation
Security hardening after installation
# Run the interactive secure script
sudo mysql_secure_installation
# Remove anonymous users
# Disallow root login remotely
# Remove test database
# Reload privilege tables
Performance tuning (MySQL 8.0)
Modify /etc/mysql/mysql.conf.d/mysqld.cnf with realistic values. Example for a 4 vCPU, 16 GB RAM server:
[mysqld]
innodb_buffer_pool_size = 8G
innodb_log_file_size = 2G
max_connections = 200
table_open_cache = 4000
query_cache_type = 0 # deprecated in 8.0
Apply and restart: sudo systemctl restart mysql.
Automated deployment (Ansible snippet)
- name: Install MySQL on Ubuntu
hosts: dbservers
become: yes
tasks:
- name: Install mysql-server
apt:
name: mysql-server-8.0
state: present
update_cache: yes
- name: Start and enable mysql
systemd:
name: mysql
state: started
enabled: yes
- name: Run mysql_secure_installation non-interactively
expect:
command: mysql_secure_installation
responses:
'Enter current password for root': ''
'Switch to unix_socket': 'y'
'Change the root password?': 'y'
'New password:': 'SecretP@ss123'
'Re-enter new password:': 'SecretP@ss123'
'Remove anonymous users?': 'y'
'Disallow root login remotely?': 'y'
'Remove test database?': 'y'
'Reload privilege tables now?': 'y'
Backup strategy
# Full backup using mysqldump
mysqldump -u root --all-databases --routines --triggers --single-transaction > full_backup.sql
# Restore
mysql -u root < full_backup.sql
# Physical backup (using XtraBackup for large datasets)
xtrabackup --backup --target-dir=/backup/mysql
Tested on Ubuntu 22.04 LTS with mysql-server-8.0 (8.0.36) from native repos.
Frequently Asked Questions
What is the difference between apt install mysql-server and apt install mysql-server-8.0?
Answer: mysql-server is a meta-package that installs the default MySQL version for the Ubuntu release. mysql-server-8.0 pins the major version to 8. On Ubuntu 22.04 both resolve to 8.0.36, but on older releases mysql-server may point to 5.7. Use mysql-server-8.0 for deterministic behavior. Verify with:
apt show mysql-server 2>/dev/null | grep Version
apt show mysql-server-8.0 2>/dev/null | grep Version
When should I use the --defaults-file flag with mysqld?
Answer: Use --defaults-file to force MySQL to read a specific configuration file, overriding all other config paths. By default, MySQL reads /etc/mysql/my.cnf and ~/.my.cnf. For isolated instances or testing alternate configs, specify the exact file:
mysqld --defaults-file=/etc/mysql/custom.cnf --user=mysql
This prevents inadvertent inclusion of /etc/my.cnf or other system files.
How do I fix the error "E: Unable to locate package mysql-server" on Ubuntu 20.04?
Answer: First run sudo apt update to refresh package lists. If still missing, ensure the universe repository is enabled:
sudo add-apt-repository universe
sudo apt update
If the package remains unavailable, add the official MySQL APT repository:
wget -c 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.deb
sudo apt update && sudo apt install mysql-server
Does mysql-server installation from default Ubuntu repos work on AWS EC2 (Ubuntu 22.04) and Azure VMs?
Answer: Yes, both platforms provide the same default repositories. However, on minimal cloud images (e.g., Ubuntu Minimal), the universe repository may be disabled. Ensure it is enabled before installing:
sudo apt update && sudo apt install -y software-properties-common
sudo add-apt-repository universe && sudo apt install mysql-server
What is the fastest way to install MySQL 8.0 on Ubuntu 22.04 with automated, non-interactive configuration?
Answer: Use DEBIAN_FRONTEND=noninteractive apt-get install -y mysql-server and preset the root password with debconf-set-selections:
sudo debconf-set-selections <<< 'mysql-server mysql-server/root_password password MySecurePass1!'
sudo debconf-set-selections <<< 'mysql-server mysql-server/root_password_again password MySecurePass1!'
sudo DEBIAN_FRONTEND=noninteractive apt-get install -y mysql-server
After install, run mysql_secure_installation to harden, or inject SQL directly.

Command Line Expert & Software Engineer
Welcome! I’m Thomas Heinrich, a software engineer and system administrator with a deep passion for the Command Line Interface (CLI). With years of experience navigating the terminal, building backend architectures, and automating server deployments, I created this space to share practical, real-world terminal knowledge.
Whether you are a beginner taking your first steps in a Linux environment or a seasoned DevOps engineer looking to optimize your deployment scripts, you will find actionable solutions here. My goal is to help you ditch the mouse, speed up your workflow, and harness the full power of the command line.