How to Install MySQL on Ubuntu 20.04 LTS?
MySQL is an open-source relational database management system (RDBMS) that is used to store data into tables. In this tutorial, we will install MySQL on Ubuntu 20.04 LTS. So, let's get started and follow the following steps.
Step 1: Update Package index
Before installing, let's first update the package index by using the
sudo apt update command.
Step 2: Install the MySQL Server
After updating the package index, now we can install the MySQL server by using the below command.
$ sudo apt install mysql-server
It will install the database into our local system. We can see the teaching message on the console screen.
Installing the MySQL server package will take a few minutes based on your internet speed.
Step 3: Configuring MySQL
After successful installation, we need to run the security script, especially If we are installing it the first time.
This will take us through a series of prompts.
Select y or Y for password setup as shown in the above image.
As shown in the above image password plugin, for authentication what kind of password you want to choose there are three levels of password
While entering the password no sign appears like * sign. Your password is invisible. After the script, it will ask if you want to continue with the password you entered or you want to enter a new one. If you are satisfied enter y to continue.
After entering the password, It will prompt you with some security options that you should choose in order to secure the MySQL server.
- Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
- Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
- Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
- Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
After completion of the script move on to creating a dedicated database user with MySQL client.
Step 4: Connect to MySQL server
$ sudo mysql
After completing all the configuration and setup let's run the MySQL by using the above command. Run this command and enter your system password
After entering a valid password, it displays the following messages.
To clean the terminal console, you can use
ctrl + L shortcut.
Step 5: Check the version of the MySQL
For checking the installed version of the MySQL server run
mysql --version command.
Step 6: Testing MySQL status
To check the current status of the server, we can use
systemctl status mysql.service command that will display the information along with process id.
Creating a new user in MySQL
We can create new user to the MySQL to control user accessbility. Use the below command that will take username and password as an argument.
CREATE USER 'username'@'host' IDENTIFIED WITH authentication_plugin BY 'password';
After successfully user creation, it returns a message "Query OK".
After creating a new user, we can also grant new user-appropriate privileges like CREATE, ALTER, DROP, INSERT, DELETE, SELECT, and so on.
The general syntax for granting user privileges is as follows:
GRANT PRIVILEGE ON database.table TO 'username'@'host';
Log in to MySQL Server
To access MySQL database, use the following command that will access to create and run the SQL queries.
$ mysql -u username -p
Here -u flag for the user and -p flag for password authentication.
Stop MySQL Server
To stop running MySQL, we can use the following command.
$ service msql stop
Restart MySQL Server
To restart MySQL run this following command. It will first stop the current process and then start a new instance of MySQL service.
$ service msql restart
To remove MySQL from Ubuntu, run the following command. It will remove the MySQL server from your local system.
$ sudo apt-get remove mysql-server