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. This guide walks you through installing MySQL 5.7 on Ubuntu 18.04 and securing your installation with recommended security settings.

Prerequisites

  • Ubuntu 18.04 Linux operating system
  • A root or non-root user with sudo privileges
  • Stable internet connection
  • Terminal window / command line access

How to Install MySQL

  1. Step 1: Update local repositories

    Update the local package index with the apt-get command. Open a terminal window and run:

    sudo apt-get update

    How to install MySQL On Ubuntu 18.04 - TechvBlogs

  2. Step 2: Install MySQL 5.7 Server

    Run the following command as sudo to install MySQL from APT-GET repositories:

    sudo apt-get install mysql-server

    How to install MySQL On Ubuntu 18.04 - TechvBlogs

    A [Y/n] question to continue with the installation may appear during the installation process. Press Y and hit Enter to proceed.

  3. Step 3: Verify MySQL installation

    After the installation, the MySQL server should start automatically. To check the installed version, run:

    mysql --version
  4. Step 4: Manage the MySQL process

    Now that MySQL is up and running, here are basic management commands:

    To stop MySQL:

    sudo systemctl stop mysql

    To start MySQL:

    sudo systemctl start mysql

    To check MySQL status:

    sudo systemctl status mysql

    How to install MySQL On Ubuntu 18.04 - TechvBlogs

  5. Step 5: Configure MySQL security

    By default, MySQL lacks many basic security features. Use the mysql_secure_installation script to configure security settings:

    sudo mysql_secure_installation

    You will be asked to configure the VALIDATE PASSWORD PLUGIN, which tests the strength of MySQL user passwords. Type Y to enable it:

    How to install MySQL On Ubuntu 18.04 - TechvBlogs

    Enter the number for the password strength level and press Enter:

    How to install MySQL On Ubuntu 18.04 - TechvBlogs

    Enter and re-enter your password:

    How to install MySQL On Ubuntu 18.04 - TechvBlogs

    The system will display the password strength and ask if you want to continue. Type Y and press Enter.

    How to install MySQL On Ubuntu 18.04 - TechvBlogs

    Answer the following security questions:

    Remove anonymous users?

    How to install MySQL On Ubuntu 18.04 - TechvBlogs

    Type Y and press Enter.

    Disallow root login remotely? It is recommended to allow root connections only from the local system.

    How to install MySQL On Ubuntu 18.04 - TechvBlogs

    Type Y and press Enter.

    Remove test database?

    How to install MySQL On Ubuntu 18.04 - TechvBlogs

    Type Y and press Enter if you want to remove it.

    Reload privilege tables?

    How to install MySQL On Ubuntu 18.04 - TechvBlogs

    Type Y and press Enter to apply all security changes.

  6. Step 6: Log in to MySQL and configure authentication

    Log in to MySQL with the root user:

    mysql -u root -p

    How to install MySQL On Ubuntu 18.04 - TechvBlogs

    Check which authentication method each MySQL user uses:

    SELECT user,authentication_string,plugin,host FROM mysql.user;

    How to install MySQL On Ubuntu 18.04 - TechvBlogs

    Change the authentication method from auth_socket to mysql_native_password:

    ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'YOUR_STRONG_PASSWORD';

    After executing the ALTER USER command, flush privileges:

    FLUSH PRIVILEGES;

    Verify the authentication method has changed:

    SELECT user,authentication_string,plugin,host FROM mysql.user;

    How to install MySQL On Ubuntu 18.04 - TechvBlogs

    You should now see that the root user is using the mysql_native_password plugin for authentication.

    How to install MySQL On Ubuntu 18.04 - TechvBlogs