Tutorial

How To Install MySQL on Ubuntu 16.04

Published on November 24, 2016

staff technical writer

How To Install MySQL on Ubuntu 16.04
Not using Ubuntu 16.04?Choose a different version or distribution.
Ubuntu 16.04

Introduction

MySQL is an open-source database management system, commonly installed as part of the popular LAMP (Linux, Apache, MySQL, PHP/Python/Perl) stack. It uses a relational database and SQL (Structured Query Language) to manage its data.

The short version of the installation is simple: update your package index, install the mysql-server package, and then run the included security script.

  1. sudo apt-get update
  2. sudo apt-get install mysql-server
  3. mysql_secure_installation

This tutorial will explain how to install MySQL version 5.7 on a Ubuntu 16.04 server. However, if you’re looking to update an existing MySQL installation to version 5.7, you can read this MySQL 5.7 update guide instead.

Prerequisites

To follow this tutorial, you will need:

Step 1 — Installing MySQL

On Ubuntu 16.04, only the latest version of MySQL is included in the APT package repository by default. At the time of writing, that’s MySQL 5.7

To install it, simply update the package index on your server and install the default package with apt-get.

  1. sudo apt-get update
  2. sudo apt-get install mysql-server

You’ll be prompted to create a root password during the installation. Choose a secure one and make sure you remember it, because you’ll need it later. Next, we’ll finish configuring MySQL.

Step 2 — Configuring MySQL

For fresh installations, you’ll want to run the included security script. This changes some of the less secure default options for things like remote root logins and sample users. On older versions of MySQL, you needed to initialize the data directory manually as well, but this is done automatically now.

Run the security script.

  1. mysql_secure_installation

This will prompt you for the root password you created in Step 1. You can press Y and then ENTER to accept the defaults for all the subsequent questions, with the exception of the one that asks if you’d like to change the root password. You just set it in Step 1, so you don’t have to change it now. For a more detailed walkthrough of these options, you can see this step of the LAMP installation tutorial.

To initialize the MySQL data directory, you would use mysql_install_db for versions before 5.7.6, and mysqld --initialize for 5.7.6 and later. However, if you installed MySQL from the Debian distribution, like in Step 1, the data directory was initialized automatically; you don’t have to do anything. If you try running the command anyway, you’ll see the following error:

Output
2016-03-07T20:11:15.998193Z 0 [ERROR] --initialize specified but the data directory has files in it. Aborting.

Finally, let’s test the MySQL installation.

Step 3 — Testing MySQL

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

  1. systemctl status mysql.service

You’ll see output similar to the following:

Output
● mysql.service - MySQL Community Server
   Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: en
   Active: active (running) since Wed 2016-11-23 21:21:25 UTC; 30min ago
 Main PID: 3754 (mysqld)
    Tasks: 28
   Memory: 142.3M
      CPU: 1.994s
   CGroup: /system.slice/mysql.service
           └─3754 /usr/sbin/mysqld

If MySQL isn’t running, you can start it with sudo systemctl start mysql.

For an additional check, you can try connecting to the database using the mysqladmin tool, which is a client that lets you run administrative commands. For example, this command says to connect to MySQL as root (-u root), prompt for a password (-p), and return the version.

  1. mysqladmin -p -u root version

You should see output similar to this:

Output
mysqladmin  Ver 8.42 Distrib 5.7.16, 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.16-0ubuntu0.16.04.1
Protocol version	10
Connection		Localhost via UNIX socket
UNIX socket		/var/run/mysqld/mysqld.sock
Uptime:			30 min 54 sec

Threads: 1  Questions: 12  Slow queries: 0  Opens: 115  Flush tables: 1  Open tables: 34  Queries per second avg: 0.006

This means MySQL is up and running.

Conclusion

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

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
Default avatar

staff technical writer

hi! i write do.co/docs now, but i used to be the senior tech editor publishing tutorials here in the community.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
10 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!

If it occurs error because of broken MySQL package on Ubuntu 16.04. Just do this trick

# Purge all MySQL packages
sudo apt purge mysql*
sudo rm -rf /var/lib/mysql
sudo rm -rf /etc/mysql

# Reinstall MySQL
sudo apt install mysql-server mysql-client

This worked for me.

https://www.digitalocean.com/community/tutorials/how-to-add-swap-on-ubuntu-14-04

(use dd if=/dev/zero of=/swapfile bs=1M count=1024 if you have cheap plan, and don’t forget to purge and remove certain files as mentioned)

I get this error 100% of the time, I have purged/wiped/cleaned and re-installed about 50 times. Every single time i get the same thing.

Can anyone help??

Renaming removed key_buffer and myisam-recover options (if present) dpkg: error processing package mysql-server-5.7 (–configure): installed mysql-server-5.7 package post-installation script subprocess 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 Processing triggers for libc-bin (2.27-3ubuntu1) …No apport report written because the error message indicates its a followup error from a previous failure.

Processing triggers for ureadahead (0.100.0-20) … Processing triggers for systemd (237-3ubuntu10.15) … Errors were encountered while processing: mysql-server-5.7 mysql-server E: Sub-process /usr/bin/dpkg returned an error code (1)

I haven’t had to mess with the swap file to get mysql to install, as a couple of others have commented. Must thank my lucky stars. :-)

Thanks for taking the time to explain the differences in initializing mysql_install_db between the 5.6 and 5.7 versions of mysql. I find this level of attention of detail in most D.O tutorials and is the reason why I respect them so much.

Hey, There was no prompt for root password as mentioned in Step 1 :/(

hi I am absolutely new to this but my software instruction asks for: Database management system, MySQL, v >= 5.0 . The table engine must be “MyISAM”. With new MySQL versionit’s always InnoDB. Set “default-storage-engine” option to “MyISAM” in “/etc/mysql/my.cnf”

can you guide me on how to do that?

Thanks

mysql-secure-installation should also be started as sudo, otherwise it won’t work

sudo mysql_secure_installation

also the root password can then be set without any problems… ;-)

After I run ‘sudo apt-get install mysql-server’ Got this feedback:

Reading package lists… Done Building dependency tree
Reading state information… Done E: Unable to locate package mysql-server

What can be wrong here?

buen tutorial

It’s also worth mentioning that many consider MariaDB a much better alternative to MySQL. Not sure why DO doesn’t have a tutorial for MariaDB and Ubuntu, but they do have one for MariaDB and CentOS. But anyway, the installation instructions and everything else is almost identical. I wrote a tutorial for both MySQL and MariaDB on Ubuntu on my website

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.