How to install MySQL on Ubuntu 18.04

s2 0

MySQL
is the most popular open source relational database management system. It’s fast, easy to use, scalable, and an integral part of the popular LAMP and LEMP Stack.

In this tutorial, we are going to show you how to install and secure MySQL on an Ubuntu 18.04 machine.

requirements

Make sure you are logged in as a user with sudo permissions.

Install MySQL on Ubuntu

At the time of this writing, MySQL Version 5.7 is the latest version of MySQL available in the official Ubuntu repositories.

To install MySQL on your Ubuntu server, do the following:

  1. First update the apt package index by typing:

    sudo apt update
  2. Then install the MySQL package with the following command:

    sudo apt install mysql-server
  3. As soon as the installation is complete, the MySQL service will start automatically. Enter the following to verify that the MySQL server is running:

    sudo systemctl status mysql
    ● mysql.service - MySQL Community Server
       Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
       Active: active (running) since Wed 2018-06-20 11:30:23 PDT; 5min ago
     Main PID: 17382 (mysqld)
        Tasks: 27 (limit: 2321)
       CGroup: /system.slice/mysql.service
               `-17382 /usr/sbin/mysqld --daemonize --pid-file=/run/mysqld/mysqld.pid

Back up MySQL

The MySQL server package contains a script called mysql_secure_installation that can perform several security-related operations.

Run the script by typing:

sudo mysql_secure_installation

You will be prompted for the VALIDATE PASSWORD PLUGIN

which is used to test the strength of the MySQL users’ passwords and improve security. There are three levels of password validation policy: low, medium, and strong. Press ENTER if you don’t want to set up the Validation Password Plugin.

At the next prompt, you will be asked to set a password for the MySQL root user. Once you do, the script will also prompt you to remove the anonymous user, restrict root user access to the local computer, and remove the test database. You should answer all questions with “Y” (yes).

Log in as root

To interact with the MySQL server from the command line, you can use the MySQL client utility, which is installed as a dependency on the MySQL server package.

On Ubuntu systems running MySQL 5.7 (and higher), the root user is created by the. authenticated auth_socket Plugin by default.

the auth_socket Plugin authenticates users who use the localhost via the Unix socket file. This means that you cannot authenticate as root by providing a password.

To log into the MySQL server as the root user, enter:

sudo mysql

You will see the MySQL shell as shown below:

Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 8
Server version: 5.7.22-0ubuntu18.04.1 (Ubuntu)

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.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

If you want to log into your MySQL server as root from an external program, such as phpMyAdmin
you have two options.

The first is to change the authentication method from auth_socket to mysql_native_password. You can do this by running the following command:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'very_strong_password';FLUSH PRIVILEGES;

The second recommended option is to create a new administrator with access to all databases:

GRANT ALL PRIVILEGES ON *.* TO 'administrator'@'localhost' IDENTIFIED BY 'very_strong_password';

Conclusion

Now that your MySQL server is up and running and you know how to connect to the MySQL server from the command line, you should read the following guides:

  • How to manage MySQL user accounts and databases
  • How to reset a MySQL root password
  • How to create a MySQL database

If you prefer a web interface to the command line, you can install phpMyAdmin and use it to manage your MySQL databases and users.

This post is part of the how-installed-lemp-stack-on-ubuntu-18-04 Series.
Further articles in this series:


How to install Nginx on Ubuntu 18.04

How to set up Nginx server blocks on Ubuntu 18.04

Secure Nginx with Let’s Encrypt on Ubuntu 18.04

How to install MySQL on Ubuntu 18.04

How to install PHP on Ubuntu 18.04

This post is part of the how-installed-lemp-stack-on-ubuntu-18-04 Series.
Further articles in this series:


How to install Nginx on Ubuntu 18.04

How to set up Nginx server blocks on Ubuntu 18.04

Secure Nginx with Let’s Encrypt on Ubuntu 18.04

How to install MySQL on Ubuntu 18.04

How to install PHP on Ubuntu 18.04

How to install Nginx on Ubuntu 18.04

How to set up Nginx server blocks on Ubuntu 18.04

Secure Nginx with Let’s Encrypt on Ubuntu 18.04

How to install MySQL on Ubuntu 18.04

How to install PHP on Ubuntu 18.04