In this tutorial we will learn How How to Install MySQL on Rocky Linux and Alma Linux. You will eventually need to configure a Rocky Linux database server. When you do, you may need to set it up so that other server/client may use it as a database server. This is an excellent technique to construct a single database server that can be utilized by several applications running on separate computers, or it may be used to offload database services to other servers.
One system might be used to host WordPress, while another machine could be used to direct that platform to a database. This has the advantage that you won’t lose your data in the event that the WordPress server goes down.
Pre-requisite
- Rocky Linux 8 or 9 Host or Alma Linux 8 or 9. If you need to install OS refer How to Install Rocky Linux
- User with sudo privileges
Installing MySQL packages
Run the below command to install the MySQL packages
sudo dnf install mysql-server -y
Enable and restart the service
sudo systemctl enable --now mysqld
sudo systemctl restart mysqld
Allow the firewall
You must use the following command to open the appropriate port in the firewall in order to permit remote connections:
sudo firewall-cmd --permanent --zone=public --add-port=3306/tcp
sudo firewall-cmd --reload
Securing the Database
Run the command
sudo mysql_secure_installation
The VALIDATE PASSWORD COMPONENT will be shown to you first. When you see this, hit Y to confirm that only strong passwords are permitted.
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
After turning this on, you will need to choose one of the following password validation policy levels:
LOW: The password length needs to be at least eight characters long.
MEDIUM: The password must contain a mixture of mixed case, special characters, and numeric characters, and its length must equal or exceed eight characters.
STRONG: The password must be at least eight characters long, contain a mix of numeric and mixed case characters as well as special characters, and not be contained in a dictionary file.
Next, you’ll be prompted to enter 2 for STRONG, 1 for MEDIUM, and 0 for LOW
Type N if you want not to enable the policy. After that, you’ll need to enter and confirm the new password for the MySQL admin account. After completing that, select Y for the remaining inquiries.
Configuring the database
Open the configuration file
sudo vim /etc/my.cnf.d/mysql-server.cnf
Add the following two lines at the bottom of that file:
bind-address = 0.0.0.0
default_authentication_plugin=mysql_native_password
Save and exit the file followed by restart of the service
sudo systemctl restart mysqld
Creating User in the database
Login to the MySQL
mysql -u root -p
Create the appdb database
CREATE DATABASE appdb;
Let us now create users. We will create two users – one for localhost and other for remote access
CREATE USER 'appuser'@'localhost' IDENTIFIED BY 'mypass';
CREATE USER 'apppuser'@'%' IDENTIFIED BY 'mypass';
Here, ‘mypass‘ is a unique and strong password
Let us give the users access to the appdb database
GRANT ALL ON appdb.* TO 'appuser'@'localhost';
GRANT ALL ON appdb.* TO 'appuser'@'%';
Flush the privileges and exit the MySQL console with:\
FLUSH PRIVILEGES;
exit
Testing the Access
You can log in to any machine within the same network (LAN) and run the below command to test if you are able to access the DB
mysql -u appuser -h <SERVER_IP> -p
Here, <SERVER_IP> is the IP of the Rocky Linux Host and hence replace it with your IP address. You will be prompted for the password for the ‘appuser‘ and you should be able to get the MYSQL console prompt.
Conclusion
With Rocky Linux, your MySQL database server is now up and running and accepting connections from IP addresses other than localhost. For more information refer the Official MySQL Documentation