How To

Install MySQL on Ubuntu 20.04: Setup, Security, and Root Access

MySQL is the most popular open-source relational database management system. It is fast, reliable, and a core part of both the LAMP and LEMP stacks. WordPress, Drupal, Joomla, and most PHP applications are built to work with MySQL out of the box.

This guide shows you how to install MySQL on Ubuntu 20.04, run the security script, and set up root access for both local and external connections.

<strong>Prerequisite:</strong>&nbsp;You need a user account with sudo access to follow these steps.

Install MySQL on Ubuntu

MySQL 8.0 is available in Ubuntu’s default repositories. Install it with:

bashsudo apt updatesudo apt install mysql-server

The service starts automatically after install. Confirm it is running:

bashsudo systemctl status mysql

Look for active (running) in the output. Your database server is up and ready.

Secure the MySQL Installation

MySQL includes a built-in script called mysql_secure_installation. It walks you through several settings that harden your server against common vulnerabilities.

Run it:

bashsudo mysql_secure_installation

The script will ask you to:

  • Set up the password validation plugin (choose LOW, MEDIUM, or STRONG)
  • Set a strong password for the MySQL root user
  • Remove anonymous user accounts
  • Restrict root login to localhost only
  • Remove the test database

Go through all the prompts and answer y to each one. These steps are standard for any production database server. Skipping them leaves your server with default settings that are easy to exploit.

Log In as Root

MySQL 8.0 uses the auth_socket plugin for the root user by default. This means root does not use a password to log in locally. Instead, MySQL checks that the Linux system user running the command matches the database user name.

Log in to the MySQL shell:

bashsudo mysql

You will see the MySQL prompt:

mysql>

Type exit or press CTRL+D to close the session.

You can run a few quick checks after logging in:

  • SELECT VERSION(); – shows the running MySQL version
  • SHOW DATABASES; – lists all databases on the server

This default setup is secure for managing MySQL from the command line. However, it does not work with external tools like phpMyAdmin, which need a username and password to connect.

Set Up Admin Access for External Tools

If you need to connect to MySQL from an external program, you have two options.

Option 1: Change root to use a password.

Run these commands from inside the MySQL shell:

sqlALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_strong_password';FLUSH PRIVILEGES;

Option 2 (recommended): Create a new admin user.

This keeps root on auth_socket and gives you a separate account for external tools:

sqlGRANT ALL PRIVILEGES ON *.* TO 'admin'@'localhost' IDENTIFIED BY 'your_strong_password';

You can name this user anything. Always use a strong, unique password for any admin account.

<strong>Tip:</strong>&nbsp;For each web application you run, create a dedicated MySQL user with access to only that application's database. Do not use the admin account for app connections.

MySQL is now installed and running on your Ubuntu server. The security script reduces your exposure to common database vulnerabilities, and the admin user setup gives you flexible access for different tools and applications. Got questions? Leave a comment below.

Cyber Defence

Recent Posts

groupdel Command in Linux: Remove a Group and Audit Files

The groupdel command in Linux removes a group from the system. It deletes the group's entry from /etc/group and /etc/gshadow,…

15 hours ago

wc Command in Linux: Count Lines, Words, Characters, and Bytes

The wc command in Linux counts lines, words, characters, and bytes in files or standard input. It…

15 hours ago

top Command in Linux: Monitor Processes and Resource Usage

The top command in Linux provides a real-time view of running processes and system resource usage. From…

15 hours ago

usermod Command in Linux: Modify User Accounts and Groups

The usermod command in Linux modifies existing user account attributes. You can use it to manage group…

15 hours ago

sort Command in Linux: Sort Text, Numbers, Columns, and More

The sort command in Linux reads lines from files or standard input and writes them to standard…

2 days ago

wall Command in Linux: Broadcast Messages to Logged-In Users

The wall command in Linux sends a message to the terminals of all currently logged-in users. The…

2 days ago