Databases · Beginner · 12 min
How to Install MySQL 9.7 LTS on Ubuntu 26.04
By Padding · Updated 15 Nov 2026
Ubuntu 26.04.1 LTS · MySQL 9.7 LTS (MySQL apt repo)
You will install MySQL 9.7 LTS from Oracle's official apt repository, close it to the network, create a database and a limited user for your app, and connect with a password.
On this page
MySQL is one of the most widely used open-source relational databases. Ubuntu 26.04 ships MySQL 8.4 in its own archive. MySQL 9.7 is a long-term support release, and Oracle publishes it in its own apt repository, which is what this tutorial uses.
You will add that repository, install the server, close it to the network, create a database and a user that can touch only that database, and connect as that user. A fresh MySQL install listens on every network interface until you tell it not to, so the lock-down step matters.
Step 1 · Add the MySQL apt repository
Install the tools that fetch and check the repository key:
sudo apt-get update
sudo apt-get install -y curl gnupg ca-certificatesDownload MySQL's signing key and look at its fingerprint before you trust it. The key file name comes from MySQL's repository root at repo.mysql.com, and the mysql-9.7-lts component name from the repository's own resolute release listing at repo.mysql.com/apt/ubuntu/dists/resolute/:
curl -fsSL https://repo.mysql.com/RPM-GPG-KEY-mysql-2025 -o /tmp/mysql-key.asc
gpg --show-keys --with-fingerprint /tmp/mysql-key.ascThe output includes the fingerprint BCA4 3417 C3B4 85DD 128E C6D4 B7B3 B788 A8D3 785C. Compare it with the one on dev.mysql.com (search for "MySQL Software Package Signature Verification") before you continue. Then store the key and add the repository for the 9.7 LTS release:
sudo gpg --dearmor --yes -o /usr/share/keyrings/mysql.gpg /tmp/mysql-key.asc
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/mysql.gpg] http://repo.mysql.com/apt/ubuntu resolute mysql-9.7-lts mysql-tools" | sudo tee /etc/apt/sources.list.d/mysql.list
sudo apt-get updateresolute is the code name of Ubuntu 26.04, and mysql-9.7-lts is the repository component that carries 9.7.
Check it worked
apt-cache policy mysql-serverThe Candidate line shows a version starting with 9.7., from repo.mysql.com. The older 8.4 packages from the Ubuntu archive are listed below it, and apt will not pick them, because the higher version wins.
Step 2 · Install the server
sudo apt-get install -y mysql-serverThe package may ask you to set a root password. Leave it empty and press Enter. The root database account then signs in through the local Linux account instead (the auth_socket plugin), so in practice only people who can run sudo on the server get in, and there is no root password to leak.
Confirm the version, then check that the service is running:
mysql --versionThe output reads mysql Ver 9.7. followed by the patch number.
systemctl status mysqlThe service is called mysql. It starts when the package is installed. If it is not running, sudo systemctl enable --now mysql starts it and turns it on for every boot.
Check it worked
Open a session as the database root user with sudo:
sudo mysql -e "SELECT VERSION();"The output shows 9.7. and a patch number. In a session you opened with sudo mysql, the prompt is mysql>, and exit leaves it.
Step 3 · Close the server to the network
By default MySQL listens on every network interface, on port 3306 for the classic protocol and 33060 for the X protocol. Look at what it is doing now:
sudo mysql -e "SHOW VARIABLES LIKE 'bind_address';"
sudo mysql -e "SHOW VARIABLES LIKE 'mysqlx_bind_address';"Both are *, which means all interfaces. Unless another machine must connect, limit both to the local machine. Add a settings file in the folder MySQL already reads:
sudo tee /etc/mysql/mysql.conf.d/local-only.cnf > /dev/null <<'CNF'
[mysqld]
bind-address = 127.0.0.1
mysqlx-bind-address = 127.0.0.1
CNFRestart MySQL so it reads the file:
sudo systemctl restart mysqlCheck it worked
sudo ss -ltn | grep -E ':(3306|33060)'Both lines show 127.0.0.1 as the local address, so nothing outside the server can reach the database. If an app on another machine must connect later, bind to the server's private address instead and allow only that machine in the firewall.
Step 4 · Create a database and a user
Do not let your app connect as root. Create a database and a user that has rights on that database only. Replace appdb and appuser with your own names. Open a session:
sudo mysqlAt the mysql> prompt, run the statements below. Choose a long random password in place of the example one. The mysql client does not save statements that contain IDENTIFIED BY to its history file, so the password does not end up in ~/.mysql_history.
CREATE DATABASE appdb CHARACTER SET utf8mb4;
CREATE USER 'appuser'@'localhost' IDENTIFIED BY 'replace-with-a-long-random-password';
GRANT ALL PRIVILEGES ON appdb.* TO 'appuser'@'localhost';Each statement replies Query OK. utf8mb4 is the character set that stores every Unicode character, including emoji. The grant covers appdb.* only, so the user cannot read or change any other database. A grant of *.* would hand over the whole server, so avoid it for app accounts. MySQL 9 uses the caching_sha2_password login method for the new user. The older mysql_native_password method was removed in MySQL 9.0.
Check it worked
sudo mysql -e "SHOW GRANTS FOR 'appuser'@'localhost';"
sudo mysql -e "SELECT user, host, plugin FROM mysql.user WHERE user IN ('root', 'appuser');"The grants list shows GRANT USAGE ON *.* and GRANT ALL PRIVILEGES ON appdb.*. The user table shows root with auth_socket and appuser with caching_sha2_password.
Step 5 · Connect as the new user
Connect as appuser. The -p option makes the client ask for the password, so it never appears in your shell history or the process list:
mysql -u appuser -p appdbAt the mysql> prompt, make a table, add a row and read it back:
CREATE TABLE notes (id INT AUTO_INCREMENT PRIMARY KEY, body VARCHAR(200) NOT NULL);
INSERT INTO notes (body) VALUES ('first note');
SELECT * FROM notes;Check it worked
The SELECT prints a table with one row, 1 | first note. Type SHOW DATABASES; to see what appuser can see: only appdb and information_schema, because the account has no rights on anything else. Type exit to leave.
Troubleshooting
ERROR 1698 (28000): Access denied for user 'root'@'localhost': you ranmysqlwithoutsudo. The root account only signs in through the local Linux account, so usesudo mysql.ERROR 1045 (28000): Access denied for user 'appuser'@'localhost' (using password: YES): the password is wrong. As root, reset it withALTER USER 'appuser'@'localhost' IDENTIFIED BY 'new-password';.ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock': the server is not running. Checksystemctl status mysqland read/var/log/mysql/error.log.E: Unable to locate package mysql-serveror only the 8.4 version appears: the repository file is missing or the machine is not x86-64. Check/etc/apt/sources.list.d/mysql.listand rununame -m, which must printx86_64.
Step 6 · Know where things live
| Path | What it holds |
|---|---|
| /etc/mysql/mysql.conf.d/ | Server settings. Your local-only.cnf is here |
| /etc/mysql/conf.d/ | Client settings and extra server settings |
| /var/lib/mysql | The data directory |
| /var/log/mysql/error.log | The server's error log |
| /var/run/mysqld/mysqld.sock | The local socket that mysql uses |
sudo mysql -e "SELECT @@datadir, @@port, @@character_set_server;"
sudo ls /var/log/mysqlThe first command prints /var/lib/mysql/, 3306 and utf8mb4.
Do not edit /etc/mysql/my.cnf or mysql.cnf by hand. Add your own file next to local-only.cnf so package upgrades never overwrite your changes.
Next steps
- Close every port you do not use:
/tutorials/set-up-ufw-firewall-ubuntu-26-04. - Harden how you log in to the server:
/tutorials/harden-ssh-on-ubuntu-26-04. - The repository setup is documented at dev.mysql.com/doc/refman/9.7/en/linux-installation-apt-repo.html.
Related tutorials
- How to Find and Fix Slow PostgreSQL Queries with EXPLAIN ANALYZEYou will read a PostgreSQL 18 query plan, add an index that turns a 40 ms table scan into a 0.2 ms lookup, and use pg_stat_statements to find which queries deserve that effort.
- How to Install Valkey 9.1 (a Redis-Compatible Cache) on Ubuntu 26.04You will install Valkey 9.1 from the project's official binary release, run it as its own user with a password and a memory limit, and use it as a cache from the command line.
- How to Install PostgreSQL 18 on Ubuntu 26.04 and Create Your First DatabaseYou will install PostgreSQL 18 from the official PostgreSQL apt repository, create a database and a user for your app, and connect to it with a password.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials