How To Install MySQL Server on Ubuntu 22.04
MySQL is an open-source relational database management system (RDBMS).Its name is a combination of "My", the name of co-founder Michael Widenius's daughter My, and "SQL", the acronym for Structured Query Language. A relational database organizes data into one or more data tables in which data may be related to each other; these relations help structure the data. SQL is a language programmers use to create, modify and extract data from the relational database, as well as control user access to the database. In addition to relational databases and SQL, an RDBMS like MySQL works with an operating system to implement a relational database in a computer's storage system, manages users, allows for network access and facilitates testing database integrity and creation of backups.
Step 1 – Installing MySQL on Ubuntu 22.04
The default Ubuntu repositories contain MySQL 8.0. Which can be installed directly using the package manager without adding third-party PPA.
To install the available MySQL server version, execute the following command.
[root@hackthesec]# apt-get install mysql-server
Press 'y' for any confirmation asked by the installer.
Once the installation is finished, you can secure the MySQL server by executing the following command.
[root@hackthesec]# sudo mysql_secure_installation
You will go through a wizard of questions to secure the MySQL server. Follow the onscreen instructions below:
Press ‘y’ to enable validate password plugin. This will allow you to set a strict password policy for user accounts.
[root@hackthesec]# 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: y
Chose the password complexity level. Read the all 3 options and choose one:
[root@hackthesec]# 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: 1
Enter a new password and re-enter it. Make sure it matches the complexity level as described above.
New password: *************
Re-enter new password: *************Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y 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
Step 2 – Connect to MySQL Server
Remember that the above password set for the root accounts is used for remote users only. To log in from the same system, just type mysql on terminal.
[root@hackthesec]# sudo mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 8.0.28-0ubuntu4 (Ubuntu)
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
Step 3 – Creating Database and Users
Here is few example queries to create database and users in MySQL server.
Create a database named ‘hackthesec’.
CREATE DATABASE hackthesec;
Next, create a user named ‘hackthesec’ accessible from ‘localhost’ only.
CREATE USER 'hackthesec'@'localhost' IDENTIFIED BY 'Secure321';
Grant permissions on database to user.
GRANT ALL ON mydb.* to 'hackthesec'@'localhost';
Apply the permission changes at runtime.
Step 4 – Manage MySQL Service
To check the database server status.
[root@hackthesec]# sudo systemctl status mysql
Use below command to start MySQL server.
[root@hackthesec]# sudo systemctl start mysql
To stop MySQL server:
[root@hackthesec]# sudo systemctl stop mysql
Restart MySQL database server, type:
[root@hackthesec]# sudo systemctl restart mysql