How to Install MySQL on Ubuntu 18.04

MySQL is a freely of the world’s most popular open source relational database management system. It is fast, simple to use, scalable, associated an integral a part of the popular LAMP(Linux+Apache+MySQL+PHP) and LEMP(Linux+Ngnix+MySQL+PHP) stacks.

Through this tutorial you will learn how to install MySQL on an Ubuntu 18.04.

Prerequisites

Before, you must have an account with sudo privileges and make sure your Ubuntu 18.04 server can access the Internet.

Install MySQL on Ubuntu 18.04

In this tutorial, we take the MySQL 5.7 installation as an example.Before starting the installation,you’d better update the apt package manager index.

Update the apt package manager index by following command:

sudo apt update

Install the MySQL package by typing the following command:

sudo apt install mysql-server

After completing installation MySQL will start automatically. Check MySQL version by typing:

mysql -v

For more details about mysql status, you can typing the following command:

sudo systemctl status mysql

The output should be:

mysql.service - MySQL Community Server
   Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
   Active: active (running) since Wed 2019-05-10 10:31:29 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

Configure MySQL

You can improve MySQL security by executing a script called mysql_secure_installation ,by typing command:

sudo mysql_secure_installation

You will be asked to configure the VALIDATE PASSWORD PLUGIN which is used to test the strength of the MySQL users’ passwords and improve the security. There are three levels of password validation policy, low, medium and strong. Press ENTER if you don’t want to set up the validate password plugin.

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

Login MySQL

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

In Ubuntu systems running MySQL 5.7 (and later), the root user is authenticated by the auth_socket plugin by default.

The auth_socket plugin authenticates users that connect from the localhost through the Unix socket file. This means that you can’t authenticate as root by providing a password.

To log in to the MySQL server as the root user type:

sudo mysql

The output should 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 login to your MySQL server as root from an external program such as [phpMyAdmin] you have two options.

The first one is to change the authentication method from auth_socketto mysql_native_password. You can do that 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 administrative user with access to all databases:

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

Conclusion

That’s It! In this article, we have explained how to install MySQL in Ubuntu 18.04. If you have any questions or thoughts to share, don’t forget to leave a comment to reach us.

Leave a Reply