How To Install MySQL On Ubuntu 22.04 | digital ocean (2023)


mysqlis an open source database management system, commonly installed as part of the popularLIGHTBULB(Linux, Apache, MySQL, PHP/Python/Perl). It implements the relational model and uses Structured Query Language (better known as SQL) to manage its data.

This tutorial will cover how to install MySQL version 8.0 on an Ubuntu 22.04 server. When complete, you'll have a working relational database that you can use to build your next website or app.

previous requirements

To follow this tutorial, you will need:

  • An Ubuntu 22.04 server with a non-root administrative user and a firewall configured with UFW. To set this up, follow ourinitial server setup guide for ubuntu 22.04.

Paso 1: Instalar MySQL

On Ubuntu 22.04 you can install MySQL using the APT package repository. At the time of writing this article, the version of MySQL available in the default Ubuntu repository is version 8.0.28.

To install it, update the package index on your server if you haven't done so recently:

  1. sudo fitupdate

Then install theservidor mysqlpackage:

  1. sudo fit installservidor mysql

Make sure the server is running using thestart systemctldomain:

  1. sudosystemctl start mysql.service

These commands will install and start MySQL, but will not prompt you to set a password or make any other configuration changes. Since this leaves your MySQL installation insecure, we'll cover it next.

Step 2: Configure MySQL

For new MySQL installations, you'll want to run the security script included with the database management system. This script changes some of the less secure default options for things like disallowing remote accessfuenteExample of user logins and deletion.

Notice: From July 2022, there will be an error when running themysql_secure_installationscript without any additional configuration. The reason is that this script will try to set a password for the installation process.fuenteMySQL account, but by default on Ubuntu installations this account is not configured to connect using a password.

Prior to July 2022, this script would fail silently after trying to configure thefuenteaccount password and continue with the rest of the prompts. However, at the time of writing, the script returns the following error after entering and confirming a password:


... Mistake! Error: SET PASSWORD has no meaning for user 'root'@'localhost' since the authentication method used does not store authentication data on the MySQL server. Consider using ALTER USER if you want to change the authentication parameters. New Password:

This will take the script through a recursive loop that you can only exit by closing the terminal window.

(Video) How to setup MySQL on Digital Ocean (or any Linux Server)

Because itmysql_secure_installationThe script performs a number of other actions that are useful for keeping your MySQL installation secure, however it is recommended that you run this script before you start using MySQL to manage your data. To avoid getting into this recursive loop, you'll first need to adjust howfuenteThe MySQL user is authenticated.

First, open the MySQL prompt:

  1. sudomysql

Then run the followingCHANGE USERCommand to change thefuenteuser authentication method to one that uses a password. The following example changes the authentication method tomysql_native_password:

  1. TO ALTEROF THE USER 'fuente'@'localhost'IDENTIFIED WITH mysql_native_password BY'password';

After making this change, exit the MySQL prompt:

  1. salida

After that you can run themysql_secure_installationsmooth script.

Run the security script withsudo:

  1. sudomysql_secure_installation

This will take you to a series of prompts where you can make some changes to the security options of your MySQL installation. The first prompt will ask if you want to configure the Validate Password plugin, which can be used to test the strength of new MySQL users' passwords before considering them valid.

If you choose to configure the password validation plugin, any MySQL user you create and authenticate with a password must have a password that complies with the selected policy:


Secure MySQL server implementation. Connecting to MySQL using a blank password. The VALIDATE PASSWORD COMPONENT can be used to test passwords and improve security. Verifies password strength and allows users to set only those passwords that are strong enough. Do you want to configure the VALIDATE PASSWORD component? Press y|Y for Yes, any other key for No:YThere are three levels of password validation policy: LOW Length >= 8 MEDIUM Length >= 8, numeric, upper and lower case letters, and special characters STRONG Length >= 8, numeric, upper and lower case letters, special characters, and dictionary file Enter 0 = LOW, 1 = MEDIUM and 2 = STRONG:2

Regardless of whether you choose to configure the password validation plugin, the next prompt will be to set a password for MySQLfuenteEnter and confirm a secure password of your choice:


Set the password for root here. New password: Retype the new password:

Note that even if you have set a password for thefuenteMySQL user, this user is not configured to authenticate with a password when connecting to the MySQL shell.

If you used the password validation plugin, you will receive feedback on the strength of your new password. The script will then ask you if you want to continue with the password you just entered or if you want to enter a new one. Assuming you're happy with the strength of the password you just entered, typeYto continue the script:


(Video) How to install mysql and phpmyadmin on ubuntu 22.04

Estimated password strength: 100Do you want to continue with the provided password? (Press y|Y for Yes, any other key for No):Y

From there you can pressYand thenTO TYPEto accept the defaults for all subsequent questions. This will remove some anonymous users and the test database, disable remote root logins, and load these new rules so MySQL will immediately honor any changes you make.

Observation:Once the security script completes, you can reopen MySQL and change thefuentethe user authentication method returns to its default value,auth_socket. To authenticate asfuenteMySQL user using a password, run this command:

  1. mysql -u raíz -p

Then, go back to using the default authentication method with this command:

  1. TO ALTEROF THE USER 'fuente'@'localhost'IDENTIFIED WITH auth_socket;

This means that you can reconnect to MySQL as yourfuenteuser using thesudo mysqldomain.

Once the script is complete, your MySQL installation is safe. You can now create a dedicated database user with the MySQL client.

Step 3 – Create a dedicated MySQL user and grant privileges

After installation, MySQL creates afuenteuser account that you can use to manage your database. This user has full privileges on the MySQL server, which means they have full control over all databases, tables, users, etc. Therefore, it is best to avoid using this account outside of administrative functions. This step describes how to use thefuenteMySQL User to create a new user account and grant it privileges.

On Ubuntu systems running MySQL 5.7 (and later versions), thefuenteThe MySQL user is configured to authenticate using theauth_socketdefault plugin instead of a password. This plugin requires the username of the operating system calling the MySQL client to match the MySQL username specified in the command, so it must callmysqlcomsudoaccess privileges tofuenteMySQL user:

  1. sudomysql

Observation:If you installed MySQL with another tutorial and enabled password authentication forfuente, you will need to use a different command to access the MySQL shell. The following will run your MySQL client with regular user privileges and only gain administrator privileges on the database upon authentication:

  1. mysql -u raíz -p

Once you have access to the MySQL prompt, you can create a new user with aCREATE USERstatement. These follow this general syntax:

  1. CREATEOF THE USER 'Username'@'host'IDENTIFIED WITHplugin_authenticationBY'password';

AfterCREATE USER, specifies a username. This is immediately followed by a@sign and then the hostname from which this user will connect. If you plan to access this user only locally from your Ubuntu server, you can specifylocal host. It is not always necessary to enclose the username and host in single quotes, but it can help prevent errors.

You have several options when it comes to choosing your user authentication plugin. EITHERauth_socketThe aforementioned plugin can be convenient as it provides great security without requiring valid users to enter a password to access the database. But it also prevents remote connections, which can complicate things when external programs need to interact with MySQL.

Alternatively, you can omit theCOMplugin_authenticationpart of the syntax entirely for the user to authenticate with the standard MySQL plugin,cache_sha2_password.MySQL documentation recommends this pluginfor users who want to log in with a password due to its strong security features.

(Video) How To Install Apache, MySQL, PHP on Ubuntu Digital Ocean

Run the following command to create a user that authenticates withcache_sha2_password. be sure to changesammyto your preferred username andpasswordto a secure password of your choice:

  1. CREATEOF THE USER 'sammy'@'localhost'IDENTIFIED BY'password';

Observation: There is a known issue with some versions of PHP that causes problems withcache_sha2_password. If you plan to use this database with a PHP application (eg phpMyAdmin), you may want to create a user that authenticates with the old, though still secure,mysql_native_passwordplugin instead:

  1. CREATEOF THE USER 'sammy'@'localhost'IDENTIFIED WITH mysql_native_password BY'password';

If you're not sure, you can always create a user who authenticates withhidden_sha2_pluginand thenTO ALTERlater with this command:

  1. TO ALTEROF THE USER 'sammy'@'localhost'IDENTIFIED WITH mysql_native_password BY'password';

After creating your new user, you can grant them the appropriate privileges. The general syntax for granting user privileges is as follows:

  1. GRANTPRIVILEGEABOUTdatabase.low hillFOR'Username'@'host';

OPRIVILEGEThe value in this example syntax defines what actions the user can perform on thedatabasemilow hill. You can grant multiple privileges to the same user in a single command, separating them with a comma. You can also grant user privileges globally by entering asterisks (*) instead of the database and table names. In SQL, asterisks are special characters used to represent "all" databases or tables.

To illustrate, the following command grants a user global privileges toCREATE,TO ALTER, miSHOOT DOWNdatabases, tables, and users, as well as the power ofINSERT,UPDATE, miDELETEdata from any table on the server. It also grants the user the ability to query data withSELECT, create foreign keys with theREFERENCESkeyword and executeBLUSHoperations with theRECHARGEprivilege. However, you should only grant users the permissions they need, so feel free to adjust your own user privileges as needed.

You can find the full list of privileges available atthe official MySQL documentation.

spin thisGRANTstatement, replacingsammywith your own MySQL username, to grant your user these privileges:


Please note that this statement also includesWITH CONCESSION OPTION. This will allow your MySQL user to grant whatever permissions they have to other users on the system.

Notice: Some users may wish to grant the MySQL user theALL PRIVILEGESprivilege, which will give them extensive superuser privileges similar to thosefuenteuser privileges, like so:


Privileges so wideshould not be taken lightly, since anyone with access to that MySQL user will have full control over all databases on the server.

After that, it is good practice to run theRELEASE PRIVILEGESdomain. This will free any memory that the server may have cached as a result of the above procedure.CREATE USERmiGRANTstatements:

(Video) DigitalOcean MySQL on 18.04 - Connect with MySQL WorkBench (Remote Connection)


You can then exit the MySQL client:

  1. salida

In the future, to log in as your new MySQL user, you would use a command like the following:

  1. mysql-usammy-page

O-pagewill cause the MySQL client to request your MySQL user password to authenticate.

Finally, let's test the MySQL installation.

Step 4: Test MySQL

Regardless of how you installed it, MySQL should have started running automatically. To test this, check your status.

  1. systemctl status mysql.service

The output will be similar to the following:


● mysql.service - MySQL Community Server Loaded: Loaded (/lib/systemd/system/mysql.service; enabled; Vendor Preconfiguration: enabled) Active: active (running) since Monday 11/04/2022 16:04: 39 UTC; 2 h 36 min ago Process: 2593 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exit, status=0/SUCCESS) Main PID: 2601 (mysqld) Status: "Server is operational" Tasks : 38 ( limit: 1119) Memory: 354.3M CPU: 19.944s CGroup: /system.slice/mysql.service └─2601 /usr/sbin/mysqld

If MySQL is not running, you can start it withsudo systemctl iniciar mysql.

For further verification, you can try connecting to the database using themysqladmintool, which is a client that allows you to run administrative commands. For example, this command tells you to log in as a MySQL user namedsammy(- yousammy), prompt for a password (-page) and return the version. be sure to changesammyto the name of your dedicated MySQL user and enter that user's password when prompted:

  1. sudomysqladmin -p -usammyversion

Below is an example of the output:


mysqladmin Ver 8.0.28-0ubuntu4 for Linux on x86_64 ((Ubuntu))Copyright (c) 2000, 2022, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.Server version 8.0.28-0ubuntu4Protocol version 10Localhost connection via UNIX socketUNIX socket /var/run/mysqld/mysqld.sockActive time: 2 hours 31 min 57 secThreads: 2 Questions: 25 Queries slow: 0 Openings: 160 Tables Level: 3 Open Tables: 79 Queries Per Second Average: 0.000

This means that MySQL is working.


You now have a basic MySQL setup installed on your server. Here are some examples of next steps you can take:

  • Setting up a LAMP stackoa LEMP stack
  • Practice running queries with SQL
  • Manage your MySQL installation with phpMyAdmin


1. Install Apache MySQL PHP and phpMyAdmin in Digital Ocean
2. How to install MySql on Ubuntu 22.04 using PuTTY.
(Rinkesh p)
3. How to Install MySQL on Ubuntu 22.10 ✔️
(Solvetic English)
4. Install database mysql VPS Digital Ocean
5. Digital Ocean install mysql server
(WEB Programming)
6. Installing LAMP (Linux, Apache, MySQL & PHP) Stack On DigitalOcean


Top Articles
Latest Posts
Article information

Author: Kimberely Baumbach CPA

Last Updated: 16/06/2023

Views: 6686

Rating: 4 / 5 (41 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Kimberely Baumbach CPA

Birthday: 1996-01-14

Address: 8381 Boyce Course, Imeldachester, ND 74681

Phone: +3571286597580

Job: Product Banking Analyst

Hobby: Cosplaying, Inline skating, Amateur radio, Baton twirling, Mountaineering, Flying, Archery

Introduction: My name is Kimberely Baumbach CPA, I am a gorgeous, bright, charming, encouraging, zealous, lively, good person who loves writing and wants to share my knowledge and understanding with you.