Skip to content

Installing MySQL on Ubuntu the right way

A reliable Ubuntu install of MySQL 8 covering apt sources, the secure-install bootstrap, systemd integration, and what to verify before opening port 3306.

A MySQL install that survives its first six months looks almost identical to one that goes sideways in week three — the difference is in the small checks performed before the database accepts its first connection. This article walks through installing MySQL 8 on Ubuntu 22.04 or 24.04 from the distribution packages, validating systemd integration, and applying the hardening that every production install needs before it is reachable over the network.

How to verify

Before you install, confirm Ubuntu version, available disk, and that no prior MySQL/MariaDB packages are squatting on the data directory.

lsb_release -a
df -h /var/lib /var/log
dpkg -l | grep -E 'mysql|mariadb'
ss -tlnp | grep -E '3306|33060'
ls -la /var/lib/mysql 2>/dev/null

If /var/lib/mysql exists and contains ibdata1 or mysql.ibd, there is already a data directory — do not install over it. Either back it up and remove, or hand the server to whoever owns the existing database.

What’s happening

Ubuntu ships MySQL Server in its universe repository, currently at version 8.0 on 22.04 and 8.0/8.4 on 24.04. The mysql-server package pulls in the server binary, the mysql client, the mysqlsh shell, and a systemd unit that starts the daemon at boot. Data lives in /var/lib/mysql, configuration in /etc/mysql/, logs in /var/log/mysql/, and the server listens on 127.0.0.1:3306 and :33060 (the X protocol) by default.

The package’s postinst script initializes the data directory with mysqld --initialize-insecure and starts the daemon, leaving the root@localhost account with auth_socket authentication — meaning only the system root user can connect as MySQL root without a password. That is the right default, but it surprises operators who expect a password prompt during installation.

The procedure

  1. Update the package index and install MySQL Server.

    sudo apt update
    sudo apt install -y mysql-server
  2. Confirm the daemon is running and listening on localhost.

    sudo systemctl status mysql --no-pager
    ss -tlnp | grep mysqld
    sudo mysqladmin --defaults-file=/etc/mysql/debian.cnf ping
  3. Run the secure-install bootstrap. Set a root password, drop the anonymous accounts, remove the test database, and forbid remote root login. See the companion article mysql-secure-installation for the full prompt walkthrough.

    sudo mysql_secure_installation
  4. Decide on root authentication. auth_socket is the most secure default — keep it on production hosts. On developer machines you may prefer a password:

    sudo mysql
    ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'strong-password';
    FLUSH PRIVILEGES;
  5. Tune the listen address. By default MySQL binds to 127.0.0.1. If the application server is on the same host, leave it. If you need to expose the port to a private subnet, edit /etc/mysql/mysql.conf.d/mysqld.cnf:

    [mysqld]
    bind-address = 10.0.0.5
    mysqlx-bind-address = 10.0.0.5

    Then sudo systemctl restart mysql and validate with ss -tlnp | grep mysqld. Never expose 3306 to the internet — front it with a private subnet, a VPN, or an SSH tunnel.

  6. Set the InnoDB buffer pool. The single most impactful tuning knob. Default is 128 MiB which is wrong for almost any real workload. Edit mysqld.cnf:

    [mysqld]
    innodb_buffer_pool_size = 4G
    innodb_log_file_size    = 512M
    innodb_flush_method     = O_DIRECT

    A common rule: 60-70 percent of system RAM on a dedicated DB host. See mysql-mycnf-tuning for the full tuning pass.

  7. Enable automatic startup and verify the unit is enabled.

    sudo systemctl enable mysql
    systemctl is-enabled mysql
  8. Configure the firewall. If MySQL needs to accept connections from app hosts in the same VPC:

    sudo ufw allow from 10.0.0.0/24 to any port 3306 proto tcp
    sudo ufw status verbose

Common pitfalls

  • The debian.cnf maintenance user is what the package’s own scripts use — never delete or rename it, or apt upgrade will fail on the next package update.
  • The /var/lib/mysql directory should live on a filesystem with noatime mounted, on storage with consistent IOPS — bursty cloud volumes throttle InnoDB checkpoints and produce mysterious latency spikes.
  • AppArmor on Ubuntu confines mysqld to specific paths; if you move the data directory you must also update /etc/apparmor.d/local/usr.sbin.mysqld and reload the profile.
  • A first-boot mysqld writes to /var/log/mysql/error.log even before networking is up — that is the file to tail when the daemon refuses to start, not journalctl -u mysql.
  • The X protocol on :33060 is rarely used by application stacks but is bound by default; disable it in mysqld.cnf (mysqlx = 0) if you don’t need it.

In the engagements we run, the first 48 hours of a new MySQL host are spent setting up backups (mysqldump cron + offsite copy), wiring slow-query log to a parser, and confirming the buffer pool size against SHOW ENGINE INNODB STATUS. We operate this lifecycle under Managed Operations so clients don’t carry the install-day checklist as tribal knowledge.