Skip to main content

How To Install MySQL on Ubuntu 16.04

How To Install MySQL on 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. 

* sudo apt-get update
* sudo apt-get install mysql-server
* sudo mysql_secure_installation


This tutorial will explain how to install MySQL version 5.7 on a Ubuntu 16.04 server.

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

    * sudo apt-get update
* 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. 

    * sudo 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.
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. 

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

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

Comments

Popular posts from this blog

How To Configure Nginx as a Web Server and Reverse Proxy for Apache on One Ubuntu 16.04 Server

Introduction Apache and Nginx are two popular open source web servers often used with PHP. It can be useful to run both of them on the same virtual machine when hosting multiple websites which have varied requirements. The general solution for running two web servers on a single system is to either use multiple IP addresses or different port numbers. Droplets which have both IPv4 and IPv6 addresses can be configured to serve Apache sites on one protocol and Nginx sites on the other, but this isn't currently practical, as IPv6 adoption by ISPs is still not widespread. Having a different port number like 81 or 8080 for the second web server is another solution, but sharing URLs with port numbers (such as http://example.com:81 ) isn't always reasonable or ideal. This tutorial will show you how to configure Nginx as both a web server and as a reverse proxy for Apache – all on one Droplet. Depending on the web application, code changes might be required to keep Apache rev...

How to Tame the Brother 161xNW Over the Network Without Losing Your Mind

  The Definitive Arch Linux/CachyOS Guide Some hardware feels like it was designed to test the patience of Linux users. The Brother DCP-1610NW —and its close relatives in the 161x family— fits perfectly into that category. It is a monochrome laser multifunction printer: cheap to run, physically tough, reliable, and clearly built more like a small office tank than a delicate modern gadget. The problem is not the printer. The problem is making it work cleanly on Arch Linux or CachyOS over the network, especially when we want both sides of the device to behave properly: the printer and the scanner. The traditional instinct is to go straight to Brother’s official Linux drivers, hunt for old .deb or .rpm packages, look for AUR wrappers, and start installing model-specific packages until something works. That path exists. But on my system, it was not the right first move. The cleaner solution was this: CUPS + brlaser for printing. SANE + sane-airscan + Skanlite for scanning. No driver ...

Backups with rclone: Synchronizing Without Making Life Complicated

A simple strategy to avoid losing your work environment One of the most common mistakes in computing is remembering backups only when it is already too late. When the disk fails. When an update breaks something. When we accidentally delete a folder. When a laptop stops booting. When that “temporary” file turns out to be important. In the world of development and system administration, we usually spend a lot of time fine-tuning our environment: configurations, scripts, keys, projects, documents, dotfiles, profiles, tools, notes, and small adjustments that make a machine truly ours. The problem is that, many times, all of that lives in only one place. And if that place fails, we lose much more than files: we lose time. Backup as a habit, not as an event A backup should not be a heroic task we perform once every six months. It should be something simple, repeatable, and easy to run. That is where rclone becomes a very interesting tool. rclone allows you to synchronize files between a lo...