Tutorial

How To Install the Latest MySQL on Ubuntu 16.04

Published on April 7, 2017
How To Install the Latest MySQL on Ubuntu 16.04
Not using Ubuntu 16.04?Choose a different version or distribution.
Ubuntu 16.04

Introduction

MySQL is a prominent open source database management system used to store and retrieve data for a wide variety of popular applications. MySQL is the M in the LAMP stack, a commonly used set of open source software that also includes Linux, the Apache web server, and the PHP programming language.

In order to use newly released features, it’s sometimes necessary to install a more up-to-date version of MySQL than that provided by your Linux distribution. Conveniently, the MySQL developers maintain their own software repository we can use to easily install the latest version and keep it up to date.

To install the latest version of MySQL, we’ll add this repository, install the MySQL software itself, secure the install, and finally we’ll test that MySQL is running and responding to commands.

Prerequisites

Before starting this tutorial, you will need:

Step 1 — Adding the MySQL Software Repository

The MySQL developers provide a .deb package that handles configuring and installing the official MySQL software repositories. Once the repositories are set up, we’ll be able to use Ubuntu’s standard apt-get command to install the software. We’ll download this .deb file with curl and then install it with the dpkg command.

First, load the MySQL download page in your web browser. Find the Download button in the lower-right corner and click through to the next page. This page will prompt you to log in or sign up for an Oracle web account. We can skip that and instead look for the link that says No thanks, just start my download. Right-click the link and select Copy Link Address (this option may be worded differently, depending on your browser).

Now we’re going to download the file. On your server, move to a directory you can write to:

  1. cd /tmp

Download the file using curl, remembering to paste the address you just copied in place of the highlighted portion below:

  1. curl -OL https://dev.mysql.com/get/mysql-apt-config_0.8.3-1_all.deb

We need to pass two command line flags to curl. -O instructs curl to output to a file instead of standard output. The L flag makes curl follow HTTP redirects, necessary in this case because the address we copied actually redirects us to another location before the file downloads.

The file should now be downloaded in our current directory. List the files to make sure:

  1. ls

You should see the filename listed:

Output
mysql-apt-config_0.8.3-1_all.deb . . .

Now we’re ready to install:

  1. sudo dpkg -i mysql-apt-config*

dpkg is used to install, remove, and inspect .deb software packages. The -i flag indicates that we’d like to install from the specified file.

During the installation, you’ll be presented with a configuration screen where you can specify which version of MySQL you’d prefer, along with an option to install repositories for other MySQL-related tools. The defaults will add the repository information for the latest stable version of MySQL and nothing else. This is what we want, so use the down arrow to navigate to the Ok menu option and hit ENTER.

The package will now finish adding the repository. Refresh your apt package cache to make the new software packages available:

  1. sudo apt-get update

Let’s also clean up after ourselves and delete the file we downloaded:

  1. rm mysql-apt-config*

Now that we’ve added the MySQL repositories, we’re ready to install the actual MySQL server software. If you ever need to update the configuration of these repositories, just run sudo dpkg-reconfigure mysql-apt-config, select new options, and then sudo apt-get update to refresh your package cache.

Step 2 — Installing MySQL

Having added the repository and with our package cache freshly updated, we can now use apt-get to install the latest MySQL server package:

  1. sudo apt-get install mysql-server

apt-get will look at all available mysql-server packages and determine that the MySQL provided package is the newest and best candidate. It will then calculate package dependencies and ask you to approve the installation. Type y then ENTER. The software will install. You will be asked to set a root password during the configuration phase of the installation. Be sure to choose a secure password, enter it twice, and the process will complete.

MySQL should be installed and running now. Let’s check using systemctl:

  1. systemctl status mysql
Output
● mysql.service - MySQL Community Server Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled) Active: active (running) since Wed 2017-04-05 19:28:37 UTC; 3min 42s ago Main PID: 8760 (mysqld) CGroup: /system.slice/mysql.service └─8760 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid

The Active: active (running) line means MySQL is installed and running. Now we’ll make the installation a little more secure.

Step 3 — Securing MySQL

MySQL comes with a command we can use to perform a few security-related updates on our new install. Let’s run it now:

  1. mysql_secure_installation

This will ask you for the MySQL root password that you set during installation. Type it in and press ENTER. Now we’ll answer a series of yes or no prompts. Let’s go through them:

First, we are asked about the validate password plugin, a plugin that can automatically enforce certain password strength rules for your MySQL users. Enabling this is a decision you’ll need to make based on your individual security needs. Type y and ENTER to enable it, or just hit ENTER to skip it. If enabled, you will also be prompted to choose a level from 0–2 for how strict the password validation will be. Choose a number and hit ENTER to continue.

Next you’ll be asked if you want to change the root password. Since we just created the password when we installed MySQL, we can safely skip this. Hit ENTER to continue without updating the password.

The rest of the prompts can be answered yes. You will be asked about removing the anonymous MySQL user, disallowing remote root login, removing the test database, and reloading privilege tables to ensure the previous changes take effect properly. These are all a good idea. Type y and hit ENTER for each.

The script will exit after all the prompts are answered. Now our MySQL installation is reasonably secured. Let’s test it again by running a client that connects to the server and returns some information.

Step 4 – Testing MySQL

mysqladmin is a command line administrative client for MySQL. We’ll use it to connect to the server and output some version and status information:

  1. mysqladmin -u root -p version

The -u root portion tells mysqladmin to log in as the MySQL root user, -p instructs the client to ask for a password, and version is the actual command we want to run.

The output will let us know what version of the MySQL server is running, its uptime, and some other status information:

Output
mysqladmin Ver 8.42 Distrib 5.7.17, for Linux on x86_64 Copyright (c) 2000, 2016, 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 5.7.17 Protocol version 10 Connection Localhost via UNIX socket UNIX socket /var/run/mysqld/mysqld.sock Uptime: 58 min 28 sec Threads: 1 Questions: 10 Slow queries: 0 Opens: 113 Flush tables: 1 Open tables: 106 Queries per second avg: 0.002

If you received similar output, congrats! You’ve successfully installed the latest MySQL server and secured it.

Conclusion

You’ve now completed a basic install of the latest version of MySQL, which should work for many popular applications. If you have more advanced needs you might continue with some other configuration tasks:

  • If you’d like a graphical interface for administering your MySQL server, phpMyAdmin is a popular web-based solution. Our tutorial How To Install and Secure phpMyAdmin can get you started.
  • Currently, your database is only accessible to applications running on the same server. Sometimes you’ll want separate database and application servers, for performance and storage reasons. Take a look at How To Configure SSL/TLS for MySQL to learn how to set up MySQL for secure access from other servers.
  • Another common configuration is to change the directory where MySQL stores its data. You’ll need to do this if you want your data stored on a different storage device than the default directory. This is covered in How To Move a MySQL Data Directory to a New Location.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
3 Comments


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Was facing issue as MYSQL8 was not getting installed. So before following these steps (As part of Step 1) “During the installation, you’ll be presented with a configuration screen where you can specify which version of MySQL you’d prefer, along with an option to install repositories for other MySQL-related tools. The defaults will add the repository information for the latest stable version of MySQL and nothing else. This is what we want, so use the down arrow to navigate to the Ok menu option and hit ENTER.” I had to choose option 1 and click ENTER and then had to SELECT MYSQL8

Setting up mysql-server-5.7 (5.7.20-0ubuntu0.16.04.1) … update-alternatives: using /etc/mysql/mysql.cnf to provide /etc/mysql/my.cnf (my.cnf) in auto mode Renaming removed key_buffer and myisam-recover options (if present) Job for mysql.service failed because the control process exited with error code. See “systemctl status mysql.service” and “journalctl -xe” for details. invoke-rc.d: initscript mysql, action “start” failed. ● mysql.service - MySQL Community Server Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled) Active: activating (auto-restart) (Result: exit-code) since Mon 2018-01-01 21:18:00 EET; 11ms ago Process: 14264 ExecStartPost=/usr/share/mysql/mysql-systemd-start post (code=exited, status=0/SUCCESS) Process: 14263 ExecStart=/usr/sbin/mysqld (code=exited, status=1/FAILURE) Process: 14255 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/SUCCESS) Main PID: 14263 (code=exited, status=1/FAILURE)

Jan 01 21:18:00 juhfark2 systemd[1]: mysql.service: Unit entered failed state. Jan 01 21:18:00 juhfark2 systemd[1]: mysql.service: Failed with result ‘exit-code’. dpkg: error processing package mysql-server-5.7 (–configure): subprocess installed post-installation script returned error exit status 1 dpkg: dependency problems prevent configuration of mysql-server: mysql-server depends on mysql-server-5.7; however: Package mysql-server-5.7 is not configured yet.

dpkg: error processing package mysql-server (–configure): dependency problems - leaving unconfigured No apport report written because the error message indicates its a followup error from a previous failure. Processing triggers for systemd (229-4ubuntu21) … Processing triggers for ureadahead (0.100.0-19) … Errors were encountered while processing: mysql-server-5.7 mysql-server E: Sub-process /usr/bin/dpkg returned an error code (1)

Greetings

as in this step

" Step 3 — Securing MySQL MySQL comes with a command we can use to perform a few security-related updates on our new install. Let’s run it now:

mysql_secure_installation This will ask you for the MySQL root password that you set during installation. Type it in and press ENTER. Now we’ll answer a series of yes or no prompts. Let’s go through them: " The System didn’t ask me to input any password and i’ve tried

  • uninstalling and installing
  • reseting the root password

can someone help me please

thank you

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.