sodcalculator

10 Tech How-To

How to Install MySQL on Ubuntu 20.04 LTS

Guide 10-12420 · Updated · Step by step

This guide walks you through installing MySQL on Ubuntu 20.04 LTS, covering both the default repository version and the official MySQL APT repository. You’ll end with a running MySQL server, a secured root account, and a test database you can query.

What You’ll Need

  • A machine running Ubuntu 20.04 LTS with an internet connection.
  • sudo access (a user in the sudo group).
  • Basic comfort with the terminal.

Step-by-Step

  1. Update package lists
    Open a terminal and run:
    sudo apt update
    This refreshes the list of available packages from the repositories.
  2. Install MySQL server
    Run:
    sudo apt install mysql-server -y
    The -y automatically confirms the installation. This installs MySQL 8.0 from Ubuntu’s default repositories.
  3. Check the service status
    After installation, verify the service is running:
    sudo systemctl status mysql
    You should see active (running). If it’s not running, start it with sudo systemctl start mysql.
  4. Run the security script
    MySQL ships with a security script that helps you set a root password and remove insecure defaults. Run:
    sudo mysql_secure_installation
    Follow the prompts: set a password for root (if you want), remove anonymous users, disallow root login remotely, remove the test database, and reload privileges. The script asks about VALIDATE PASSWORD COMPONENT – you can skip it or enable it, but if you enable it, choose a strong password that meets the policy.
  5. Log in to MySQL
    To access the MySQL shell as root, use:
    sudo mysql
    On a fresh Ubuntu installation, root uses the auth_socket plugin by default, so you don’t need a password when using sudo. You’ll see the mysql> prompt.
  6. Create a test database and user (optional but recommended)
    At the MySQL prompt, run:
    CREATE DATABASE testdb;
    CREATE USER 'testuser'@'localhost' IDENTIFIED BY 'YourStrongPassword';
    GRANT ALL PRIVILEGES ON testdb.* TO 'testuser'@'localhost';
    FLUSH PRIVILEGES;
    Exit with EXIT;.
  7. Test the new user
    Log out of the sudo session and try:
    mysql -u testuser -p
    Enter the password you set. You should get a MySQL prompt. Run SHOW DATABASES; to see testdb.
  8. Enable MySQL to start on boot (usually automatic)
    If you want to be sure, run:
    sudo systemctl enable mysql
    This ensures MySQL starts automatically when the system boots.

Common Mistakes

  • Forgetting to run the security script – leaving anonymous users and the test database can be a security risk.
  • Assuming root uses a password – on Ubuntu, root uses auth_socket, so sudo mysql works without a password. If you try mysql -u root -p without sudo, it will fail.
  • Using a weak password – even if you skip the validate component, choose a strong password for any user you create.
  • Not checking the service status – if MySQL doesn’t start, you might miss an error. Always check with systemctl status.
  • Editing config files without backup – if you modify /etc/mysql/mysql.conf.d/mysqld.cnf, make a backup first (sudo cp).

Tips

  • Use a dedicated user for applications – don’t use root for your web app; create a user with only the privileges it needs.
  • Back up your database regularly – use mysqldump to export databases: mysqldump -u user -p dbname > backup.sql.
  • Monitor logs – MySQL logs are in /var/log/mysql/. Check error.log if something goes wrong.
  • Consider the official MySQL repository – if you need a newer version than 8.0.25 (the default in 20.04), download the .deb from MySQL’s site and install it with dpkg. But for most users, the default is fine.
  • Use MySQL Workbench or phpMyAdmin – for a GUI, install mysql-workbench or phpmyadmin for easier management.

Troubleshooting

  • Can’t connect to the MySQL shell – If sudo mysql fails, try sudo service mysql start first. If you get an error about the socket, check /var/run/mysqld/ exists and is writable.
  • Access denied for user ‘root’@’localhost’ – This usually means you’re not using sudo. Use sudo mysql. If you’ve already set a root password and want to switch back to socket auth, run ALTER USER 'root'@'localhost' IDENTIFIED WITH auth_socket; while logged in as root.
  • MySQL won’t start – Check the error log: sudo tail -f /var/log/mysql/error.log. Common causes: disk full, permission issues on the data directory (/var/lib/mysql), or a misconfigured config file. Try sudo systemctl status mysql for more details.
  • Forgot the root password – If you set a password and forgot it, you can reset it by starting MySQL in safe mode: stop the service (sudo systemctl stop mysql), start it with sudo mysqld_safe --skip-grant-tables &, then log in without password and update the root password. Be careful: this is a security risk, so do it only on a local machine.
  • Port 3306 already in use – Another MySQL instance or service might be running. Check with sudo lsof -i :3306 and stop the conflicting process.

Frequently Asked Questions

Do I need to install MySQL or MariaDB?

MySQL and MariaDB are compatible alternatives. Ubuntu 20.04 offers both; this guide covers MySQL. If you prefer MariaDB, the steps are similar (sudo apt install mariadb-server).

How do I change the root password?

Log in with sudo mysql, then run: ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'NewPassword'; Then FLUSH PRIVILEGES;. After that, you’ll need to use mysql -u root -p with the new password.

Can I install MySQL from the official MySQL site instead?

Yes. Download the MySQL APT repository .deb from dev.mysql.com, install it with sudo dpkg -i mysql-apt-config_*.deb, then run sudo apt update and sudo apt install mysql-server. This gives you the latest version, but you must manage the repository yourself.

How do I allow remote connections to MySQL?

Edit /etc/mysql/mysql.conf.d/mysqld.cnf, change bind-address to 0.0.0.0, restart MySQL, and create a user with a host of % or a specific IP. Also ensure your firewall allows port 3306. This is not recommended for production without proper security.

Why does mysql_secure_installation ask about the validate password plugin?

That plugin enforces password strength. You can skip it, but if you enable it, you must choose a password that meets the length and complexity requirements. You can later disable it if it causes issues, but it’s a good security practice.

How do I uninstall MySQL completely?

Run sudo apt purge mysql-server mysql-client mysql-common, then sudo apt autoremove. Also remove the data directory with sudo rm -rf /var/lib/mysql if you want to erase all databases. Be careful: this is irreversible.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →