How to install MySQL 8 on Ubuntu 20.04

Introduction

MySQL is a cornerstone of many applications and websites, providing a robust database management system to store and retrieve data efficiently. For users of Ubuntu 20.04, installing MySQL can be a straightforward process, thanks to the APT package repository. This guide will walk you through the steps to install MySQL on your Ubuntu 20.04 system, ensuring you have a secure and functioning database for your projects.

Prerequisites

Before you begin, ensure you have:

  • An Ubuntu 20.04 server set up with a non-root user with sudo privileges.
  • A basic firewall configured on your server.

Step 1: Update Package Index

Start by updating the package index on your Ubuntu system. This ensures you have the latest listings of available packages.

sudo apt update

Step 2: Install MySQL Server

With the package index updated, you can install the MySQL server package using the following command:

sudo apt install mysql-server

After you run this command, you will be prompted to confirm the installation. Press Y and then Enter to proceed. This command installs MySQL, but it does not secure the installation or prompt you to set a password.

....
After this operation, 398 MB of additional disk space will be used.
Do you want to continue? [Y/n] y

It will downloads and installs the MySQL server package and any dependencies required for the installation.

After the installation is complete, you will be prompted to select the geographic area in which you live. This will help set the time zone for MySQL like this:

Please select the geographic area in which you live. Subsequent configuration questions will narrow this down by presenting a list of cities, representing the time
zones in which they are located.

  1. Africa  2. America  3. Antarctica  4. Australia  5. Arctic  6. Asia  7. Atlantic  8. Europe  9. Indian  10. Pacific  11. SystemV  12. US  13. Etc
Geographic area: 2

You can select 2 for America, or choose the appropriate number for your geographic area.
After selecting the geographic area, you will be prompted to select the city in your time zone. Choose the appropriate city or select 0 for none of the above.

Geographic area: 2 

Please select the city or region corresponding to your time zone.

  1. Adak                     28. Boa_Vista      55. Fort_Nelson           82. Kentucky/Monticello  109. Noronha                 136. Scoresbysund
  2. Anchorage                29. Bogota         56. Fortaleza             83. Kralendijk           110. North_Dakota/Beulah     137. Shiprock
  3. Anguilla                 30. Boise          57. Glace_Bay             84. La_Paz               111. North_Dakota/Center     138. Sitka
  4. Antigua                  31. Cambridge_Bay  58. Godthab               85. Lima                 112. North_Dakota/New_Salem  139. St_Barthelemy
  5. Araguaina                32. Campo_Grande   59. Goose_Bay             86. Los_Angeles          113. Nuuk                    140. St_Johns
  6. Argentina/Buenos_Aires   33. Cancun         60. Grand_Turk            87. Lower_Princes        114. Ojinaga                 141. St_Kitts
  7. Argentina/Catamarca      34. Caracas        61. Grenada               88. Maceio               115. Panama                  142. St_Lucia
  8. Argentina/Cordoba        35. Cayenne        62. Guadeloupe            89. Managua              116. Pangnirtung             143. St_Thomas
  9. Argentina/Jujuy          36. Cayman         63. Guatemala             90. Manaus               117. Paramaribo              144. St_Vincent
  10. Argentina/La_Rioja      37. Chicago        64. Guayaquil             91. Marigot              118. Phoenix                 145. Swift_Current
  11. Argentina/Mendoza       38. Chihuahua      65. Guyana                92. Martinique           119. Port-au-Prince          146. Tegucigalpa
[More] 1

After select city, you will see the time zone list like this:

  12. Argentina/Rio_Gallegos  39. Ciudad_Juarez  66. Halifax               93. Matamoros            120. Port_of_Spain           147. Thule
  13. Argentina/Salta         40. Coral_Harbour  67. Havana                94. Mazatlan             121. Porto_Acre              148. Thunder_Bay
  14. Argentina/San_Juan      41. Costa_Rica     68. Hermosillo            95. Menominee            122. Porto_Velho             149. Tijuana
  15. Argentina/San_Luis      42. Creston        69. Indiana/Indianapolis  96. Merida               123. Puerto_Rico             150. Toronto
  16. Argentina/Tucuman       43. Cuiaba         70. Indiana/Knox          97. Metlakatla           124. Punta_Arenas            151. Tortola
  17. Argentina/Ushuaia       44. Curacao        71. Indiana/Marengo       98. Mexico_City          125. Rainy_River             152. Vancouver
  18. Aruba                   45. Danmarkshavn   72. Indiana/Petersburg    99. Miquelon             126. Rankin_Inlet            153. Virgin
  19. Asuncion                46. Dawson         73. Indiana/Tell_City     100. Moncton             127. Recife                  154. Whitehorse
  20. Atikokan                47. Dawson_Creek   74. Indiana/Vevay         101. Monterrey           128. Regina                  155. Winnipeg
  21. Atka                    48. Denver         75. Indiana/Vincennes     102. Montevideo          129. Resolute                156. Yakutat
  22. Bahia                   49. Detroit        76. Indiana/Winamac       103. Montreal            130. Rio_Branco              157. Yellowknife
  23. Bahia_Banderas          50. Dominica       77. Inuvik                104. Montserrat          131. Santa_Isabel
  24. Barbados                51. Edmonton       78. Iqaluit               105. Nassau              132. Santarem
  25. Belem                   52. Eirunepe       79. Jamaica               106. New_York            133. Santiago
  26. Belize                  53. El_Salvador    80. Juneau                107. Nipigon             134. Santo_Domingo
  27. Blanc-Sablon            54. Ensenada       81. Kentucky/Louisville   108. Nome                135. Sao_Paulo
Time zone: 98

This command installs MySQL, but it does not secure the installation or prompt you to set a password.

Step 3: Secure MySQL Installation

After installation, it's crucial to secure your MySQL installation. Run the included security script that comes with MySQL:

sudo mysql_secure_installation

This script will prompt you to configure security options, including setting up a root password and removing anonymous users.

If you elect to set up the Validate Password Plugin, you will be prompted to select a password policy level. Choose the desired level and press Enter.

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: 1

You will then be prompted to set a password for the MySQL root user. Enter a secure password and press Enter.

Please set the password for root here.

New password:

Re-enter new password:

After setting the root password, you will be asked to remove anonymous users, disallow root login remotely, and remove the test database. Answer Y to all of these prompts to secure your MySQL installation.

Remove anonymous users? [Y/n] Y
Disallow root login remotely? [Y/n] Y
Remove test database and access to it? [Y/n] Y
Reload privilege tables now? [Y/n] Y

Step 4: Verify Installation

Once secured, verify that the MySQL service is running:

sudo systemctl status mysql.service

You should see an active status indicating that MySQL is functioning correctly.

● mysql.service - MySQL Community Server
   Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2021-09-14 15:00:00 UTC; 1min 30s ago
 Main PID: 1234 (mysqld)
   Status: "Server is operational"
    Tasks: 38 (limit: 1137)
   Memory: 332.0M
   CGroup: /system.slice/mysql.service
           └─1234 /usr/sbin/mysqld

Step 5: Access MySQL

To access the MySQL shell, use the following command:

sudo mysql

You will be prompted to enter the root password you set during the installation process. Once you enter the password, you will be granted access to the MySQL shell.

Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.21-0ubuntu0.20.04 (Ubuntu)

Copyright (c) 2000, 2021, 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.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

You can now interact with the MySQL database using SQL commands. Try running a simple query to test the connection:

SELECT version();

This command will return the MySQL version you have installed.

+-----------+
| version() |
+-----------+
| 8.0.21    |
+-----------+
1 row in set (0.00 sec)

Additional Configuration

For fresh installations, you might encounter an error when running the mysql_secure_installation script due to changes in how MySQL handles root authentication. If you face this issue, consider using the ALTER USER command to change authentication parameters.

Conclusion

By following these steps, you have installed and secured MySQL on your Ubuntu 20.04 server. You now have a relational database ready to manage your data for websites or applications.

Remember, managing a database system like MySQL requires regular maintenance and updates to ensure security and performance. Stay informed about the latest MySQL updates and best practices to keep your database running smoothly. Happy databasing!