How to Install a MySQL Database on Ubuntu 18.04 – MySQL is the most popular open-source relational database management system in use today. This MySQL database is fast, easy to use, scalable, and an integral part of the stack
LEMP which is popular.
In this tutorial guide, I will show you how to install and secure a MySQL database on an Ubuntu 18.04 operating system. If you prefer to use the MariaDB database system, please read the following article → Guide on how to install MariaDB on Ubuntu 18.04.
Make sure you are Logged in as a user with sudo privileges. → How to Create a Sudo User and a Sudo Group on Ubuntu.
Install MySQL on Ubuntu
At the time of writing this article, the latest version of MySQL available from the official Ubuntu repositories is MySQL version 5.7.
To install MySQL on an Ubuntu server (Complete Guide: How to Install Ubuntu Server 18.04) You follow the steps below:
First, update the apt package index by typing:
$ sudo apt update
Then install the MySQL package with the following command:
$ sudo apt install mysql-server
After the installation is complete, the MySQL service will start working automatically. To check if the MySQL server is running, type:
$ sudo systemctl status mysql
Output 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
MySQL server packages come with a named script
mysql_secure_installation which can perform several security related operations.
Run the script by typing:
$ sudo mysql_secure_installation
You will be asked to configure
VALIDATE PASSWORD PLUGIN which is used to test the strength of a MySQL user password and improve security. There are three levels of password validation policies, low, medium and strong. Press
ENTER if you don’t want to set up a valid password plugin.
At the next prompt, you will be asked to reset the password for the MySQL root user. Once you do that, the script will also ask you to delete anonymous users, restrict root user access to the local machine and delete the test database. You must answer “
Y(Yes) to all questions.
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 of the MySQL server package.
In Ubuntu systems running MySQL 5.7 (and later), the root user is authenticated by a plugin
auth_socket by default.
auth_socket authenticates users connected from
localhost via a Unix socket file. This means that you cannot authenticate as root by providing a password.
To Log in to MySQL server as root user type type:
$ sudo mysql
You will be presented with the MySQL shell as shown below:
Output 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 of an external program like
phpMyAdmin You have two choices.
That first is to change the authentication method from
mysql_native_password. You can do this by running the following command:
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'very_strong_password'; mysql> FLUSH PRIVILEGES;
Option second, what is recommended is to create a new administrative user with access to all databases:
mysql> GRANT ALL PRIVILEGES ON *.* TO 'administrator'@'localhost' IDENTIFIED BY 'very_strong_password';
Now that your MySQL server is up and running and you know how to connect to a MySQL server from the command line as above, you may want to check out the following guide:
- Guide on How to Manage User Accounts and MySQL databases (coming soon)
- Guide on How to Reset MySQL root Password (Coming Soon)
- Guide on How to Create a MySQL Database (Coming Soon)
And that’s a guide on how to install MySQL on Ubuntu 18.04. If you prefer a web interface to running MySQL from the command line, you can install phpMyAdmin and manage your MySQL users and databases via phpMyAdmin which is obviously a lot easier.