MySQL (https://www.mysql.com) is an Oracle-backed open source relational database management system based on Structured Query Language (SQL).
This tutorial will describe how to install MySQL database server on an Ubuntu 18.04 server.
To follow this tutorial, you will need:
One Ubuntu 18.04 server set up by following this initial server setup guide, including a sudo non-root user and a firewall.
Step 1 - Installing MySQL
On Ubuntu 18.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, update the package index on your server and install the default package with apt:
sudo apt update sudo apt install mysql-server
This will install MySQL, but will not prompt you to set a password or make any other configuration changes. Because this leaves your installation of MySQL insecure, we will address this next.
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.
Run the security script:
This will take you through a series of prompts where you can make some changes to your MySQL installation’s security options. The first prompt will ask whether you’d like to set up the Validate Password Plugin, which can be used to test the strength of your MySQL password. Regardless of your choice, the next prompt will be to set a password for the MySQL root user. Enter and then confirm a secure password of your choice.
From there, you can press Y and then ENTER to accept the defaults for all the subsequent questions. This will remove some anonymous users and the test database, disable remote root logins, and load these new rules so that MySQL immediately respects the changes you have made.
Step 3 - Testing MySQL
Regardless of how you installed it, MySQL should have started running automatically. To test this, check its status.
sudo systemctl status mysql.service
You'll see output similar to the following:
● mysql.service - MySQL Community Server Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: en Active: active (running) since Wed 2018-04-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.
sudo mysqladmin -p -u root version
You should see output similar to this:
mysqladmin Ver 8.42 Distrib 5.7.22, for Linux on x86_64 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 5.7.21-0ubuntu18.04.1 Protocol version 10 Connection Localhost via UNIX socket UNIX socket /var/run/mysqld/mysqld.sock Uptime: 1 hour 18 min 2 sec
Threads: 1 Questions: 9 Slow queries: 0 Opens: 107 Flush tables: 1 Open tables: 100 Queries per second avg: 0.001
This means MySQL is up and running.
You now have a basic MySQL setup installed on your server.