MySQL is a free, open-source database management system based on SQL or Structured Query Language with the current release is MySQL 8. The following tutorial will teach you how to install MySQL 8.0 Community Edition release on Ubuntu 22.04 LTS Jammy Jellyfish or Ubuntu 20.04 LTS Focal Fossa using the MySQL official APT repository, which will give you the latest version available on your system using the command line terminal instead of relying on Ubuntu to push updates to MySQL.
What is MySQL Community Edition?
MySQL Community Edition is a free, open-source database software package offered by Oracle. It is often seen in web-based applications, given its easy-to-use interface and comprehensive library of functions available. The MySQL Community Edition has no limitations on the number of databases or customers you can have, so it’s great for businesses that need to store large amounts of customer data without having to fork out extensive money. The software can be part of a distributed system with secure connectors, allowing users to access and manage multiple databases from different systems in different locations.
Recommended Steps Before Installation
First, update your system to ensure all existing packages are up to date to avoid potential conflict issues during the installation.
First, update your system to ensure all existing packages are up to date.
sudo apt update
Optionally, you can list the updates for users who require review or are curious to see what is available to update. This can be good if you have a specific you forgot to place; use the apt-hold command.
sudo apt --list upgradable
Proceed to upgrade any outdated packages using the following command.
sudo apt upgrade
Import the MySQL 8.0 Community Server Repository
Install Required Packages
The tutorial will require the following packages installed. These may be already present as they are widespread packages; if unsure, just run the command.
sudo apt install dirmngr ca-certificates software-properties-common apt-transport-https curl lsb-release gnupg -y
By default, MySQL 8.0 Community Edition is unavailable on Ubuntu LTS systems, given it’s a long-term release distribution repository; luckily, a repository from the MySQL official repository allows you to import the latest stable version.
First, import the GPG key using the following command.
curl -fsSL http://repo.mysql.com/RPM-GPG-KEY-mysql-2022 | sudo gpg --dearmor | sudo tee /usr/share/keyrings/mysql.gpg > /dev/null
Next, import the repository.
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/mysql.gpg] http://repo.mysql.com/apt/ubuntu $(lsb_release -cs) mysql-8.0" | sudo tee -a /etc/apt/sources.list.d/mysql.list
Optionally, if you need the source repository, you can also import the following. This should only be required if you are a developer or have a specific reason, and most users will not need the source repository.
echo "deb-src [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/mysql.gpg] http://repo.mysql.com/apt/ubuntu $(lsb_release -cs) mysql-8.0" | sudo tee -a /etc/apt/sources.list.d/mysql.list
Users that are using MySQL for development can additionally import the following repositories.
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/mysql.gpg] http://repo.mysql.com/apt/ubuntu $(lsb_release -cs) mysql-tools" | sudo tee -a /etc/apt/sources.list.d/mysql.list echo "deb-src [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/mysql.gpg] http://repo.mysql.com/apt/ubuntu $(lsb_release -cs) mysql-tools" | sudo tee -a /etc/apt/sources.list.d/mysql.list
Optionally, if you are unsure if you imported all the directories, just run a quick grep command to see what is imported. If you keep using the above commands, you will re-add the same directory repeatedly, leading you to delete and start again.
grep mysql /etc/apt/sources.list.d/mysql.list
[email protected]:~$ grep mysql /etc/apt/sources.list.d/mysql.list deb [arch=amd64 signed-by=/usr/share/keyrings/mysql.gpg] http://repo.mysql.com/apt/ubuntu jammy mysql-8.0 deb-src [arch=amd64 signed-by=/usr/share/keyrings/mysql.gpg] http://repo.mysql.com/apt/ubuntu jammy mysql-8.0 deb [arch=amd64 signed-by=/usr/share/keyrings/mysql.gpg] http://repo.mysql.com/apt/ubuntu jammy mysql-tools deb-src [arch=amd64 signed-by=/usr/share/keyrings/mysql.gpg] http://repo.mysql.com/apt/ubuntu jammy mysql-tools
As mentioned above, the tutorial uses a Jammy Jellyfish machine, so the command shows I have imported all the directories, or if I am missing one, I know now which one was not added. Remember, this is for demonstration purposes only. You do not need to import all the directories above.
Lastly, run an APT update.
sudo apt update
Install MySQL 8.0 Community Edition Server
Now that you have completed the import of the repository for MySQL, execute the command to install as follows:
sudo apt install mysql-community-server -y
A new pop-up will appear during the installation, prompting you to enter the database’s root password. Make sure this is secure and recorded. Once entered, press the enter key or the tab key to select the <Ok> and hit the enter button to proceed with the installation.
To confirm, you will be prompted to re-enter the root password a second time. REMEMBER THE PASSWORD FOR THE FUTURE!
Next, set the default selection for the authentication plugin as below.
If unsure, select “Use Strong Password Encryption,” recommended by MySQL.
The installation should finish after this point. To confirm it has been installed, run the following apt policy command.
apt policy mysql-community-server
Service Commands for MySQL 8.0 Community Server
The installer will automatically start your default MySQL service and configure itself to start automatically on system startup.
Verify that your MySQL service is operational after installation; type the following systemctl status command.
systemctl status mysql
For new installations, everything should be status ok. Proceed on to securing your MySQL instance.
Below are some of the most common system commands you will require to manage your MySQL systemd service.
Stop the MySQL service:
sudo systemctl stop mysqld
Start the MySQL service:
sudo systemctl start mysqld
Disable the MySQL service at system startup:
sudo systemctl disable mysqld
Activate the MySQL service at system startup:
sudo systemctl enable mysqld
Restart the MySQL service:
sudo systemctl restart mysqld
How to Secure MySQL 8.0 Community Edition
When installing MySQL, the new defaults are considered weak by most standards and raise concerns about the potential of allowing intrusion or exploitation by hackers. One solution is to run the installation security script with the MySQL installation.
First, use the following command to launch the (mysql_secure_installation).
Next, you will be prompted for your root password that was initially set, and then you will see a question about VALIDATE PASSWORD COMPONENT; this involves defining password complexity checks; for the most part, the default is correct.
Then follow below:
- Setting the password for root accounts.
- Setting the password for the accounts.
- Removal of root accounts accessible from outside the local host.
- Removal of anonymous user accounts.
- Removal of the test database, accessible by default to anonymous users.
Be careful; you use (Y) to delete everything. In addition, if you wish, you can reset your root password by creating a new one; you can ignore it if you want, as you already set it during the initial installation with the pop-ups.
[[email protected]~]$ mysql_secure_installation Securing the MySQL server deployment. Enter password for user root: The existing password for the user account root has expired. Please set a new password. New password: <---- SET NEW PASSWORD Re-enter new password: <---- RE-ENTER NEW PASSWORD Re-enter new password: The 'validate_password' component is installed on the server. The subsequent steps will run with the existing configuration of the component. Using existing password for root. Estimated strength of the password: 100 Change the password for root ? ((Press y|Y for Yes, any other key for No) : Y <---- Type Y then press the ENTER KEY (SKIP IF YOU ALREADY JUST SET) New password: Re-enter new password: Estimated strength of the password: 100 Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : Y <---- Type Y then press the ENTER KEY. By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment. Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y <---- Type Y then press the ENTER KEY. Success. Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network. Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y <---- Type Y then press the ENTER KEY. Success. By default, MySQL comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment. Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y <---- Type Y then press the ENTER KEY. - Dropping test database... Success. - Removing privileges on test database... Success. Reloading the privilege tables will ensure that all changes made so far will take effect immediately. Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y <---- Type Y then press the ENTER KEY. Success. All done!
Additional Commands & Tips
Update MySQL 8.0 Community Server
Since you have imported the official APT repository, updating is quick and straightforward; run the following standard APT commands as you would updating any other system package.
sudo apt update && sudo apt upgrade
Remove (Uninstall) MySQL 8.0 Community Server
First, stop the database if you no longer want to use the MySQL database.
sudo systemctl stop mysql --now
Use the following command to remove MySQL and any unused dependencies installed.
sudo apt autoremove mysql-community-server
The above command will blanket cover removing dependencies that are no longer required.
The tutorial has demonstrated how Ubuntu users can import the latest MySQL 8.0 community edition on their server or desktop using the MySQL APT repository and installing the required packages and tools. Overall, using this may be beneficial to keep up-to-date quicker with security and performance upgrades than waiting on Ubuntu. Given that MySQL builds to the actual distribution release, it should be very stable.