MySQL is the most popular open-source relational database management system. It is fast, reliable, and scales from small development projects to large production workloads. MySQL is a core component of both the LAMP and LEMP stacks and is used by millions of websites and applications worldwide.
Ubuntu 18.04’s official repositories include MySQL 5.7. This guide shows you how to install MySQL on Ubuntu 18.04, run the security hardening script, and connect to the server from the command line.
Prerequisite: You need sudo access.
Install the mysql-server package from the Ubuntu repositories:
bashsudo apt updatesudo apt install mysql-server
The mysql-server package also pulls in the MySQL client utility, a set of administration tools, and creates the system users and data directories MySQL needs to operate.
MySQL starts automatically after installation. Verify it is running:
bashsudo systemctl status mysql
Look for Active: active (running) in the output. If you see it, MySQL is up and ready to accept connections.
The mysql-server package ships with a script called mysql_secure_installation. Run it immediately after a fresh install to close common security gaps:
bashsudo mysql_secure_installation
The script walks you through several prompts.
VALIDATE PASSWORD PLUGIN. This plugin enforces password strength rules on MySQL user accounts. There are three levels: LOW (minimum length), MEDIUM (length, mixed case, numbers, and special characters), and STRONG (adds a dictionary file check). If you enable MEDIUM or STRONG, weak passwords will be rejected when you create new MySQL users. Press ENTER to skip this plugin and manage password rules manually later.
Root password. If you skipped the validate plugin, set a strong root password at this prompt.
Remaining prompts. Answer Y to all of them. The script removes the default anonymous user account, restricts root login to the local machine only, drops the test database, and reloads the privilege tables. Each step removes a default setting that is fine for development but should not exist on a production server.
On Ubuntu systems running MySQL 5.7 and later, the root account uses the auth_socket plugin instead of a password. The plugin checks the identity of the Linux system user making the connection. Because the Linux root user matches the MySQL root user, the connection is allowed automatically via sudo:
bashsudo mysql
You land at the MySQL shell prompt. Type exit to leave the shell when you are done. This method works well for command-line administration, but external tools like phpMyAdmin cannot use socket authentication and need a password-based account.
For external access, you have two options.
Option 1 – Change root to password authentication (not recommended):
sqlALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'very_strong_password';FLUSH PRIVILEGES;
Option 2 – Create a dedicated admin user (recommended):
sqlGRANT ALL PRIVILEGES ON *.* TO 'administrator'@'localhost' IDENTIFIED BY 'very_strong_password';
The second option leaves the root account untouched and gives external tools a named, password-authenticated account to connect with. This is cleaner and more secure than modifying root’s authentication method.
MySQL is now installed, secured, and ready on your Ubuntu 18.04 server. Your next steps are to create databases and user accounts for your applications, or install phpMyAdmin for a web-based management interface. Leave a comment below if you run into any issues.
Apache Cassandra is a free, open-source NoSQL database designed for high availability and linear scalability with…
Rocket.Chat is a free, open-source team communication platform built with the Meteor framework. It is a…
Apache is the most widely used web server in the world. It is free, open-source, and…
NetBeans is a free, open-source, cross-platform IDE developed by the Apache Software Foundation. It was one…
Pip is the official package manager for Python and the standard way to install libraries from…
R is an open-source programming language and environment built for statistical computing and data visualization. It…