A prominent open source database management system, MySQL is used to store and recover data for a several popular applications. M means MySQL in the LAMP stack, a commonly used set of open source software that comprises of Linux, the Apache web server, and the PHP programming language other than MySQL.
For using the newly released features, it is often essential to install a more up-to-date version of MySQL than that is offered by your Linux distribution. It is convenient for the MySQL developers to maintain their own software repository that we can use to install the latest version easily and keep it up to date.
For installing the latest version of MySQL, we will add this repository, install the MySQL software itself, secure the install, and lastly test that MySQL is running and is responsive to commands.
Prerequisites
Prior to starting this tutorial, you will need:
• An Ubuntu 18.04 server with a non-root and sudo-enabled user.
Step 1 — Adding the MySQL Software Repository
You get a .deb package offered by the MySQL developers that manages the configuration and installation of the official MySQL software repositories. After setting up the repositories, user can use Ubuntu’s standard apt command for software installation. Then download this .deb file with curl and then install it with the dpkg command.
Now, load the MySQL download page in your web browser. The Download button is in the lower-right corner, click via to the next page. You will be prompted for log in or sign up for an Oracle account. Skip this and find the link that says No thanks, just start my download. Now, right-click the link and select Copy Link Address (this option may be different, based on your browser).
Now, you will be downloading the file. Move to a directory that you can write to on your server with the below command:
$ cd /tmp
Use curl to download the file and paste the address you just copied in place of the highlighted portion below:
It is important to pass two command line flags to curl. -0 asks curl to output to a file rather than the standard output.
The L flag helps curl follow HTTP redirects, which is essential in this case as the address you copied actually redirects you to another location prior to the file downloads.
With this, the file should be downloaded in your current directory. For ensuring, list the files:
$ 1s
You will see the filename listed:
Output mysql-apt-config_0.8.10-1_all.deb . . .
Now you ready to install:
$ sudo dpkg -i mysql-apt-config*
With dpkg you can install, remove, and inspect .deb software packages. The -i flag signifies that you would install from the specified file.
While the installation continues, you will get a configuration screen displayed where you need to enter the version of MySQL of your choice in addition to an option for installing repositories for other MySQL-related tools. The repository information for the latest stable version of MySQL will be added by the defaults. This is what is expected so, use the down arrow to navigate to the OK menu option and hit ENTER.
Now, the package will finish adding the repository. To make the new software packages available, refresh your apt package cache:
$ sudo apt update
Also, it is important to clean up and delete the file that you downloaded:
$ rm mysql-apt-config*
Now, you have added the MySQL repositories and so, you are ready to install the actual MySQL server software. In case, you need to update the configuration of these repositories, simply run sudo dpkg-reconfigure mysql-apt-config, select new options, and then sudo apt update to refresh your package cache.
Step 2 — Installing MySQL
After adding the repository and with your package cache freshly updated, you can now use apt to install the latest MySQL server package:
$ sudo apt install mysql-server
All available mysql-server packages will be looked at by apt and verified that the MySQL provided package is the newest and best candidate. After that the package dependencies will be calculated and you will be asked to approve the installation. Type y then press ENTER. The software will get installed.
Then you will need to set a root password during the configuration phase of the installation. Make sure you select a secure password. After entering it twice, hit ENTER. You will be asked to configure an authentication plugin. The Use Strong Password Encryption is recommended to be default, so hit ENTER to select it. Now the installation process will continue until it gets completed.
You will see MySQL is installed and running. To check it use systemctl:
$ systemctl status mysql
The Active: active (running) line indicates MySQL is installed and running. Now you will need to make the installation a little more secure.
Step 3 — Securing MySQL
A command is included in MySQL that you can use for performing a few security related updates on your new install.
Run it now with the below command:
$ mysql_secure_installation
Now you will be asked for the MySQL root password that you set while the installation. Type it and press ENTER. Now you will need to answer a series of yes or no prompts. Go through them:
First, you will be asked about the validate password plugin, a plugin that can automatically implement certain password strength rules for your MySQL users. When you enable this, it is a decision you’ll need to make depending on your individual security needs. To enable it, type y and ENTER or simply hit ENTER to skip it. If you enable it, you will also be asked to select a level from 0–2 for the amount of strictness of the password validation. Select a number and press ENTER to continue.
Next you’ll be asked for changing the root password, if required. Since you have just created the password while installing the MySQL, you can skip this. Press ENTER to continue without changing the password.
Answer ‘yes’ to the rest of the prompts. Now, you will be prompted for removing the anonymous MySQL user, removing the test database, disallowing remote root login, and reloading privilege tables to make sure that the previous changes take effect. All these are a good idea. Type y and press ENTER for each.
After all the prompts are answered, the script will exit. Your MySQL installation is reasonably secured now. It is important to test it again by running a client that connects to the server and returns some information.
Step 4 – Testing MySQL
A command line administrative client for MySQL is termed as mysqladmin. You can use it to connect to the server and output some version and status information:
$ mysqladmin -u root -p version
The -u root portion indicates mysqladmin to log in as the MySQL root user, -p indicates the client to prompt for a password, and version is the actual command you want to run.
With the output you will know the version of the MySQL server, its uptime, and some other status information:
Output mysqladmin Ver 8.0.11 for Linux on x86_64 (MySQL Community Server - GPL) Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. 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.11 Protocol version 10 Connection Localhost via UNIX socket UNIX socket /var/run/mysqld/mysqld.sock Uptime: 2 min 21 sec Threads: 2 Questions: 10 Slow queries: 0 Opens: 136 Flush tables: 2 Open tables: 112 Queries per second avg: 0.070 If you get the similar output, it means you have successfully installed the latest MySQL server and secured it.
Conclusion
You have completed the basic installation of the MySQL’s latest version that should work for various applications. If you have some advanced requirements, you may continue with some other configuration tasks:
- If you want a graphical interface for administering your MySQL server, phpMyAdmin is a popular web-based solution.
- Currently, your database can only access the applications that are running on the same server. Often you’ll require separate database and application servers, for performance and storage reasons.
- The next common configuration is to change the directory where MySQL stores its data. If you want your data stored on a different storage device than the default directory, then only you will need to do this.