Install MySQL 8 on Ubuntu
In this article, we show the detailed steps to install MySQL 8 on Ubuntu.
Ubuntu is a very widely used Linux distribution. Ubuntu Server is the most popular server operating system on the cloud.
Prerequisites
Please log in to the system as a root user or a user with administrator permissions.
Install MySQL in Ubuntu
Please follow the steps to install MySQL on Ubuntu.
Update System package index
Execute the following command to update the package index of the Ubuntu local software repository.
sudo apt update
Configure MySQLPPA
The most convenient way to install MySQL in Ubuntu is to use MySQL’s own APT repository. The APT repository contains software included MySQL server and tools. We need to add this MySQL APT repository to the system’s package source list.
-
use the
wget
command to download MySQL APT warehouse pack:wget -c https://repo.mysql.com//mysql-apt-config_0.8.13-1_all.deb
-
use the
dpkg
command to install MySQL APT warehouse pack:sudo dpkg -i mysql-apt-config_0.8.13-1_all.deb
Install MySQL
Execute the following command to start installing MySQL:
sudo apt install mysql-server
This will install a insecure MySQL server. We will secure MySQL server next.
You can start MySQL server now by running:
sudo systemctl start mysql
MySQL Security Configuration
Execute the following commands to adjust the security of the MySQL server:
sudo mysql_secure_installation
This will output:
Securing the MySQL server deployment.
Connecting to MySQL using a blank password.
VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?
Press y|Y for Yes, any other key for No:
Type ‘Y’ and press ENTER
key.
There are three levels of password validation policy:
LOW Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary file
Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG:
Choose the level of password validation policy. For Development and test, I choose 0
.
Please set the password for root here.
New password:
Re-enter new password:
Type your new password twice.
Estimated strength of the password: 25
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : Y
If you’re satisfied with the strength of the password, press Y
and ENTER
to continue.
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
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
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
- 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
Success.
All done!
Type Y
for all questions.
Manage MySQL server through Systemd
After the installation is complete, the MySQL service will start automatically. We can use the following commands to view the status of the MySQL service, start, stop, and restart the MySQL server:
- View the status of the MySQL server:
sudo systemctl status mysql
- Start the MySQL server:
sudo systemctl start mysql
- Stop the MySQL server:
sudo systemctl stop mysql
- Restart the MySQL server:
sudo systemctl restart mysql
- Configure the MySQL server to start automatically:
sudo systemctl enable mysql
Connect to the MySQL server
Please use the following command to connect to the MySQL server:
mysql -u root -p
Then enter the root account password, and press the Enter
key. After the verification is passed, the following output will be displayed:
mysql>
Use SHOW DATABASES
displays all the current database server:
mysql> show databases;
This is the output:
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.05 sec)
The databases shown above are come with the MySQL server.
Conclusion
In this tutorial, we showed the detailed steps of installing MySQL in Ubuntu, and introduced several commands for managing MySQL server. Finally, we tried to connect to the database using the mysql client command and showed a list of all databases.
If you use other operating system platforms, please use the following tutorial: