How To Install MySQL on Ubuntu 22.04

Installing MySQL on Ubuntu 22.04 is a straightforward process. Let's walk through the steps:

  1. Update the package index to ensure you have the latest information about available packages: bash sudo apt update

  2. Install MySQL using the following command: bash sudo apt install mysql-server

  3. After installation, start the MySQL service: bash sudo systemctl start mysql.service

  4. For fresh installations of MySQL, run the security script to configure it securely: bash sudo mysql_secure_installation

  5. Log in to MySQL Server: sudo mysql or mysql -u root -p

This script will guide you through several prompts to set up security options for your MySQL installation. You'll be asked to set a root password, remove anonymous users, and disallow remote root login.

Please note that there might be an error when running the mysql_secure_installation script due to recent changes. If you encounter this, you can use the following steps to address it:

  • Log in to MySQL as root: bash sudo mysql -u root -p

  • Change the authentication method for the root user: sql ALTER USER 'root'@'localhost' IDENTIFIED WITH 'mysql_native_password' BY 'your_new_password';

  • Exit the MySQL shell: sql exit;

  • Verify that MySQL is running and explore its features!

You now have MySQL installed and ready to use on your Ubuntu 22.04 server.