How to install MySQL on Ubuntu 24

To install MySQL on Ubuntu you need one package from Ubuntu’s own repository: MySQL 8.0 on both 24.04 and 22.04. Ubuntu sets it up a little differently from other systems: root has no password and logs in through sudo. This guide explains that, then covers a database and a user for your app and remote access done carefully.

Install MySQL on Ubuntu with apt

Refresh the package list and install the server. It starts at once and comes back after every reboot.

sudo apt update sudo apt install -y mysql-server mysql --version systemctl status mysql --no-pager

The version line shows Ver 8.0, and the service status shows Active: active (running). Settings live in /etc/mysql/, the databases in /var/lib/mysql/.

Run mysql_secure_installation

This script comes with MySQL and tightens a fresh install. It offers the password-strength check (the VALIDATE PASSWORD component), removes anonymous users, stops root from logging in from other hosts and deletes the test database. Answer y to each of these.

sudo mysql_secure_installation

On Ubuntu the script skips the root password, because root logs in through the system, and says so: Skipping password set for root as authentication with auth_socket is used by default. That is expected.

Older builds instead stopped with Failed! Error: SET PASSWORD has no significance for user 'root'@'localhost' and asked again in a loop. Press Control-C and update the system with: sudo apt upgrade Then run the script again.

How root logs in, and ERROR 1698

In Ubuntu’s MySQL the account root@localhost uses the auth_socket plugin: MySQL lets in the system’s root user and nobody else, with no password at all. So you open the MySQL console with sudo:

sudo mysql

Type exit to leave the console. This command lists the accounts without opening it:

sudo mysql -e "SELECT user, host, plugin FROM mysql.user;"

In its output, next to root you see auth_socket. If you log in as root without sudo, you get ERROR 1698 (28000): Access denied for user 'root'@'localhost'. That is not a broken install. For admin work, open the console with sudo as above, and give each app its own user with a password, as in the next section.

Create a database and a user

These commands create a database app and a user app who can do anything in it, and only in it. Replace choose-a-strong-password with a long password of your own. The command lines stay in the shell history: if that matters, type the same statements in the MySQL console instead.

sudo mysql -e "CREATE DATABASE app CHARACTER SET utf8mb4;" sudo mysql -e "CREATE USER 'app'@'localhost' IDENTIFIED BY 'choose-a-strong-password';" sudo mysql -e "GRANT ALL PRIVILEGES ON app.* TO 'app'@'localhost';" mysql -u app -p app

The last command asks for the password and opens the app database. Your app connects with the same user, password and database at localhost, port 3306.

Allow remote connections carefully

Out of the box MySQL listens only on 127.0.0.1, and that is the safest setup. If an app or a desktop client lives on another machine, an SSH tunnel is often enough and opens nothing. If you do need a direct connection, change bind-address in the server’s settings file:

/etc/mysql/mysql.conf.d/mysqld.cnf
[mysqld] bind-address = 0.0.0.0

Then restart MySQL, create a user for that one address and let only that address through the firewall. Replace 198.51.100.7 with the IP of the machine that connects.

sudo systemctl restart mysql sudo mysql -e "CREATE USER 'app'@'198.51.100.7' IDENTIFIED BY 'choose-a-strong-password';" sudo mysql -e "GRANT ALL PRIVILEGES ON app.* TO 'app'@'198.51.100.7';" sudo ufw allow from 198.51.100.7 to any port 3306 proto tcp

A UFW rule protects only while UFW is on: check it with: sudo ufw status Before turning it on over SSH, run: sudo ufw allow OpenSSH On a cloud server, open port 3306 in the provider’s firewall for the same single address. Never create root or other users with the host '%', which means any address.

If MySQL doesn’t start or let you in

  • ERROR 1698 (28000): Access denied for user 'root'@'localhost': root logs in only through the system. Open the console with: sudo mysql
  • ERROR 1045 (28000): Access denied for user 'app'@'localhost' (using password: YES): a wrong password, or the user exists for another host. Compare with the list of users: sudo mysql -e "SELECT user, host FROM mysql.user;"
  • ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2): the server isn’t running. These two show why: sudo journalctl -u mysql -n 50 sudo tail -n 50 /var/log/mysql/error.log A full disk is a common cause, check the free space with: df -h
  • ERROR 1819 (HY000): Your password does not satisfy the current policy requirements: the password check is on. Pick a longer password with upper and lower case letters, digits and a symbol.
  • Host '198.51.100.7' is not allowed to connect to this MySQL server: there is no user for that address. Create one as in the section above.

If you also work on a Mac

MacWashing, System Junk: Xcode junk, user caches, installers and logs found, 22.83 GB ready to be cleaned

System Junk in MacWashing finds the caches of every app it knows, including those hidden inside app data, plus logs, Xcode junk and leftovers of deleted apps. It shows how much each takes and clears them in one go. Anything debatable stays unchecked until you decide.

Free trial · macOS 14 or later

Questions and answers

How do I install MySQL on Ubuntu?

Ubuntu 24.04 and 22.04 both install MySQL 8.0 from their own repository. Refresh the package list: sudo apt update Install the server: sudo apt install mysql-server Then tighten it: sudo mysql_secure_installation

What is the MySQL root password on Ubuntu?

There is none. Root uses auth_socket and logs in through sudo. Apps should use their own user with a password, not root.

Where does MySQL keep its data and settings?

Databases are in /var/lib/mysql/, settings in /etc/mysql/, with the server’s own options in /etc/mysql/mysql.conf.d/mysqld.cnf. The error log is /var/log/mysql/error.log.