Tutorial

How To Install MySQL on Ubuntu 14.04

Published on March 8, 2016
Default avatar

By Hazel Virdó

staff technical writer

How To Install MySQL on Ubuntu 14.04
Not using Ubuntu 14.04?Choose a different version or distribution.
Ubuntu 14.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 and database initialization scripts.

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

This tutorial will explain how to install MySQL version 5.5, 5.6, or 5.7 on a Ubuntu 14.04 server. If you want more detail on these installation instructions, or if you want to install a specific version of MySQL, read on. 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

There are two ways to install MySQL. You can either use one of the versions included in the APT package repository by default (which are 5.5 and 5.6), or you can install the latest version (currently 5.7) by manually adding MySQL’s repository first.

If you want to install a specific version of MySQL, follow the appropriate section below. To help you decide which version is best for you, you can read MySQL’s introduction to MySQL 5.5, then what’s new in MySQL 5.6 and what’s new in MySQL 5.7.

If you’re not sure, you can just use the mysql-server APT package, which just installs the latest version for your Linux distribution. At the time of writing, that’s 5.5, but you can always update to another version later.

To install MySQL this way, update the package index on your server and install the 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. Move on to step two from here.

Installing MySQL 5.5 or 5.6

If you want to install MySQL 5.5 or 5.6 specifically, the process is still very straightforward. First, update the package index on your server.

  1. sudo apt-get update

Then, to install MySQL 5.5, install the mysql-server-5.5 package.

  1. sudo apt-get install mysql-server-5.5

To install MySQL 5.6, install the mysql-server-5.6 package instead.

  1. sudo apt-get install mysql-server-5.6

For both options, 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.

Installing MySQL 5.7

If you want to install MySQL 5.7, you’ll need to add the newer APT package repository from the MySQL APT repository page. Click Download on the bottom right, then copy the link on the next page from No thanks, just start my download. Download the .deb package to your server.

  1. wget http://dev.mysql.com/get/mysql-apt-config_0.6.0-1_all.deb

Next, install it using dpkg.

  1. sudo dpkg -i mysql-apt-config_0.6.0-1_all.deb

You’ll see a prompt that asks you which MySQL product you want to configure. The MySQL Server option, which is highlighted, should say mysql-5.7. If it doesn’t, press ENTER, then scroll down to mysql-5.7 using the arrow keys, and press ENTER again.

Once the option says mysql-5.7, scroll down on the main menu to Apply and press ENTER again. Now, update your package index.

  1. sudo apt-get update

Finally, install the mysql-server package, which now contains MySQL 5.7.

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

Step 2 — Configuring MySQL

First, 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.

  1. sudo mysql_secure_installation

This will prompt you for the root password you created in step one. You can press 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 one, so you don’t have to change it now.

Next, we’ll initialize the MySQL data directory, which is where MySQL stores its data. How you do this depends on which version of MySQL you’re running. You can check your version of MySQL with the following command.

  1. mysql --version

You’ll see some output like this:

Output
mysql  Ver 14.14 Distrib 5.7.11, for Linux (x86_64) using  EditLine wrapper

If you’re using a version of MySQL earlier than 5.7.6, you should initialize the data directory by running mysql_install_db.

  1. sudo mysql_install_db

Note: In MySQL 5.6, you might get an error that says FATAL ERROR: Could not find my-default.cnf. If you do, copy the /usr/share/my.cnf configuration file into the location that mysql_install_db expects, then rerun it.

  1. sudo cp /etc/mysql/my.cnf /usr/share/mysql/my-default.cnf
  2. sudo mysql_install_db

This is due to some changes made in MySQL 5.6 and a minor error in the APT package.

 

The mysql_install_db command is deprecated as of MySQL 5.7.6. If you’re using version 5.7.6 or later, you should use mysqld --initialize instead.

However, if you installed version 5.7 from the Debian distribution, like in step one, the data directory was initialized automatically, so 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.

Step 3 — Testing MySQL

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

  1. service mysql status

You’ll see the following output (with a different PID).

Output
mysql start/running, process 2689

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

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.5.47, for debian-linux-gnu on x86_64
Copyright (c) 2000, 2015, 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.5.47-0ubuntu0.14.04.1
Protocol version	10
Connection		Localhost via UNIX socket
UNIX socket		/var/run/mysqld/mysqld.sock
Uptime:			4 min 15 sec

Threads: 1  Questions: 602  Slow queries: 0  Opens: 189  Flush tables: 1  Open tables: 41  Queries per second avg: 2.360

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 us


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?
 
9 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!

Thank so much :)

If anyone’s looking, DO have a tutorial MySQL and Ubuntu 16.04 Not sure why it didn’t show up in Related

Very useful guide. Thanks a lot.

It just worked flawless…

Lots of thanks !!!

Thank you for creating this wonderful tutorial. Much appreciated.

Hi guys,

I’m also getting the errors whilst attempting to install 5.5/5.6. eg. Errors were encountered while processing: /var/cache/apt/archives/mysql-server-5.5_5.5.49-0ubuntu0.14.04.1_amd64.deb E: Sub-process /usr/bin/dpkg returned an error code (1)

Then when running command mysql_secure_installation, it says command not found.

I have purged and retried many times with no luck.

I am gonna try other ways of getting it installed now, but i think tutorials need to cover all Linux versions. My droplet is Ubuntu Ubuntu 14.04.4 x64.

Will create a comment reply with the solution if I come right.

thanks! this guide is very useful! please add some error solving headers. see how to fix mysql installing error

2016-04-22 21:54:02 0 [Note] /usr/sbin/mysqld (mysqld 5.6.30-0ubuntu0.14.04.1) starting as process 5778 … start: Job failed to start invoke-rc.d: initscript mysql, action “start” failed. dpkg: error processing package mysql-server-5.6 (–configure): subprocess installed post-installation script returned error exit status 1 Setting up mysql-common-5.6 (5.6.30-0ubuntu0.14.04.1) … Processing triggers for libc-bin (2.19-0ubuntu6.7) … Processing triggers for ureadahead (0.100.0-16) … Errors were encountered while processing: mysql-server-5.6 E: Sub-process /usr/bin/dpkg returned an error code (1)

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!

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

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

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel