How to Install MySQL on Ubuntu 25.04 Complete Guide
In this comprehensive step-by-step guide, let's see how to install MySQL 8.4 on Ubuntu 25.04 via apt, the .deb package manager, default for Debian-based distributions
It is very common in our Linux Server Support interventions, being the base for LAMP (Linux, Apache, MySQL, PHP), LEMP (NginX) stacks and several others, necessary for webapps, CMS and e-commerce, let's see how to install it
Install MySQL on Ubuntu
As the first step for installing MySQL in Ubuntu 25.04 we update the list of available packages with:
sudo apt update
We proceed to install MySQL on Ubuntu with this command
sudo apt install mysql-server
The server should already be up, let's check if the process is active
sudo service mysql status
In case it is not active we can restart MySQL and repeat the check
sudo service mysql restart
* Stopping MySQL database server mysqld [ OK ]
* Starting MySQL database server mysqld [ OK ]
Let's check MySQL's default port, 3306 TCP, which should be listening at this point
sudo apt install net-tools
sudo netstat -tnplu |grep 3306
The command should return the following output, showing port 3306 listening locally
tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN -.
tcp6 0 0 :::33060 :::* LISTEN -
Configure MySQL
The server is up, for a new installation the configurations used are the default, security is poor, at this point it is recommended to run the DBMS configuration script to set authentications and remove anonymous accesses
sudo mysql_secure_installation.
The script will guide us in configuring the server's security options
We first enable password control for users, and select strong passwords, with dictionary lookup of common passwords
Securing the MySQL server deployment.
Connecting to MySQL using a blank password.
VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of passwords
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?
Press y|Y for Yes, any other key for No: Y
There are three levels of password validation policy:
LOW Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary file
Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2
Skipping password set for root as authentication with auth_socket is used by default.
If you would like to use password authentication instead, this can be done with the "ALTER_USER" command.
See https://dev.mysql.com/doc/refman/8.4/en/alter-user.html#alter-user-password-management for more information.
Let's remove anonymous users, to increase security
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y
We configure local authentication of the root
user, which will be via socket, only on the local machine
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y
Remove test databases, useless in production
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y
Let's reload the privilege tables, so that we can apply our changes immediately.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y
Accessing MySQL from the console
The installation is finished and we can log in with the root
user from our server's console, and list the databases present, for example
sudo mysql -u root
Connecting to the database, from MySQL 8.4 you may see an error, due to a missing plugin:
ERROR 1524 (HY000): Plugin 'mysql_native_password' is not loaded
MySQL 8.4 and above implement
mysql_native_password
, if you display this error, follow our guide to enable the mysql_native_password authentication plugin
show databases;
+--------------------+
| Databases |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
The output shows us the databases present, we have succeeded in installing MySQL in Ubuntu 25.04, we can use with the usual syntax our MySQL, connect our web apps and CMS
MySQL root
password
To log in remotely we will need to set the MySQLroot
password. We can use this command, but we recommend using a "strong" password, with numbers, upper and lower case, and special characters.
ALTER USER 'root'@'localhost' IDENTIFIED BY 'LA_MIA_PASSWORD';
Accessing remote MySQL
To access a remote MySQL, it is recommended to use an SSH tunnel by redirecting our server's remote port 3306
, which listens only in localhost
, to our local port 33006
ssh root@IP-o-DNS -L 33006:localhost:3306
Thanks to the tunnel we could connect directly to our port 33006 in localhost
or 127.0.0.1
with the root
credentials created earlier
For that we can use one of these commands:
mysql -h localhost -P 33006 -u root -p
mysql -h 127.0.0.1 -P 33006 -u root -p
Accessing with MySQL Workbench
MySQL Workbench is a great tool for MySQL Database Admin (D.B.A.) and Operations, it allows you to create DBs, manage server configuration, administer users, make backups and more, and also incorporates the ability to tunnel SSH already described above
- Let's create a new connection
- We select Standard TCP/IP over SSH
- We enter the SSH configurations for our server of
- SSH Host name
- SSH Username
- SSH password or key
- MySQL Database configurations
- Hostname
- Server Port
- Username
- Password
- Let's test the connection
Want support in setting up your database in production? We are by your side with our Linux Consulting