How To Install MySQL on Ubuntu 22.04

MySQL Installation on Ubuntu 22.04

Default MySQL Version in Ubuntu 22.04

As of Ubuntu 22.04, the default MySQL version available in the official Ubuntu repositories is MySQL 8.0. This version comes with several enhancements over previous versions, including improved performance, security features, and JSON support.

Installing MySQL

  1. Update Package Index:
    Open a terminal and update your package list to ensure you get the latest version available.

    sudo apt update
  2. Install MySQL:
    • To install the default version (MySQL 8.0), use the following command:

      sudo apt install mysql-server
    • For MySQL 5.7, you'll need to add a specific repository since it's not available in the default Ubuntu 22.04 repositories:
      • First, add the MySQL APT repository:

        wget https://dev.mysql.com/get/mysql-apt-config_0.8.15-1_all.deb sudo dpkg -i mysql-apt-config_0.8.15-1_all.deb
      • During the configuration dialog, select MySQL 5.7 for installation.
      • Update the package index:

        sudo apt update
      • Install MySQL 5.7:

        sudo apt install mysql-server
  3. Initial Root Password:
    1. For MySQL 8.0, during the installation process, a temporary root password is generated and stored in the MySQL error log. You can retrieve it using:

      sudo grep 'temporary password' /var/log/mysqld.log
    2. For MySQL 5.7, you might be prompted to set the root password during the installation process.
  4. Securing MySQL Installation:
    After the installation, it's recommended to run the included security script.

    sudo mysql_secure_installation

    This script will guide you through setting up a root password, removing anonymous users, disallowing root login remotely, and more.

  5. Initial Root Password:
    • For MySQL 8.0, during the installation process, a temporary root password is generated and stored in the MySQL error log. You can retrieve it using:

      sudo grep 'temporary password' /var/log/mysqld.log
    • For MySQL 5.7, you might be prompted to set the root password during the installation process.
  6. Changing the Root Password:
    • Log in to MySQL with the initial root password:

      mysql -u root -p
    • Change the root password:

      sqlCopy code

      ALTER USER 'root'@'localhost' IDENTIFIED BY 'NewPassword'; FLUSH PRIVILEGES;

      Replace NewPassword with your new password.

  7. Verifying Installation: Check if MySQL is running:

    bashCopy code

    systemctl status mysql.service
  8. Accessing MySQL: To access the MySQL shell, use:

    mysql -u root -p

Tips

  • Always use a strong, unique password for the MySQL root user.
  • Regularly update your MySQL installation for security and performance improvements.
  • Consider backing up your MySQL data regularly, especially before upgrades or significant changes.

This tutorial provides a general guideline for installing MySQL on Ubuntu 22.04. For specific requirements or configurations, you might need to consult additional resources or the official MySQL documentation.