Installing MySQL on CentOS/Redhat 7/6 & Fedora 31/30

Posted on

Installing MySQL on CentOS/Redhat 7/6 & Fedora 31/30

Installing MySQL on CentOS/Redhat 7/6 & Fedora 31/30

MySQL is an open-source relational database management system (RDBMS) employing a client-server architecture. An RDBMS is a software application or service designed to create and manage databases structured around the relational model. This model organizes data into tables with rows (records) and columns (fields), establishing relationships between them to ensure data integrity and efficient retrieval.

This comprehensive guide provides a step-by-step walkthrough on how to install MySQL Server (Community Edition) leveraging the default package manager on CentOS/RHEL 7/6, and Fedora 31/30/29. Whether you’re setting up a development environment or deploying a production server, this guide will help you get Installing MySQL on CentOS/Redhat 7/6 & Fedora 31/30 running smoothly.

Installing MySQL on CentOS/Fedora
MySQL on CentOS/RHEL 7/6 & Fedora 31/30

Step 1: Yum Repository Configuration

The first step involves configuring the Yum repository to access the MySQL packages. This is achieved by adding the official MySQL repository to your system.

### On CentOS/RHEL 7 system ###
rpm -Uvh https://repo.mysql.com/mysql80-community-release-el7-3.noarch.rpm
-------------------------------------------
### On CentOS/RHEL 6 system ###
rpm -Uvh https://repo.mysql.com/mysql80-community-release-el6-3.noarch.rpm
-------------------------------------------
### On Fedora 32 system ###
rpm -Uvh https://repo.mysql.com/mysql80-community-release-fc32-1.noarch.rpm
-------------------------------------------
### On Fedora 31 system ###
rpm -Uvh https://repo.mysql.com/mysql80-community-release-fc31-1.noarch.rpm
-------------------------------------------
### On Fedora 30 system ###
rpm -Uvh https://repo.mysql.com/mysql80-community-release-fc30-1.noarch.rpm

Step 2: Install MySQL Community Server

The MySQL yum repository contains configurations for various MySQL versions. To ensure you install the desired version, you need to enable the specific repository for that version.

First, disable all repositories in the MySQL repo file:

$ sed -i 's/enabled=1/enabled=0/' /etc/yum.repos.d/mysql-community.repo

Then, install MySQL based on your operating system. This ensures a successful Installing MySQL on CentOS/Redhat 7/6 & Fedora 31/30.

CentOS & Red Hat
$ yum --enablerepo=mysql57-community install mysql-community-server
Fedora Systems
$ dnf --enablerepo=mysql57-community install mysql-community-server

Step 3: Start MySQL Service

After installation, start the MySQL server using either services or systemctl command.

Using SysVinit
$ service mysqld start
Using Systemd
$ systemctl start mysqld.service

Step 4: Find MySQL root Password

During the initial installation of MySQL 5.7, a temporary password for the root user is generated. This password is located in the MySQL log files.

$ grep "A temporary password" /var/log/mysqld.log
Output :
[Note] A temporary password is generated for root@server: Hsb65pdh@t1a6

Step 5: MySQL Post Install Setup

To enhance the security of your MySQL server, run the mysql_secure_installation command. It is highly recommended to answer "yes" (y) to all the questions asked during this process.

$ mysql_secure_installation
Output :
Enter password for user root:
The existing password for the user account root has expired. Please set a new password.
New password:
Re-enter new password:
The 'validate_password' plugin is installed on the server.
The subsequent steps will run with the existing configuration
of the plugin.
Using existing password for root.
Estimated strength of the password: 100
Change the password for root ? y
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? 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? 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? 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? y
Success.
All done!

Step 6: Restart and Enable MySQL Service

After configuring the basic options, restart the MySQL service.

Using SysVinit
$ service mysqld restart
Using Systemd
$ systemctl restart mysqld.service

Enable automatic service startup on system boot:

### Using SysVinit
chkconfig mysqld on
### Using Systemd
systemctl enable mysqld.service

Step 7: Working with MySQL

Connect to the MySQL database server and enter the new password when prompted. Let’s try some SQL statements to verify the Installing MySQL on CentOS/Redhat 7/6 & Fedora 31/30 was successful.

### CREATE DATABASE
mysql> CREATE DATABASE DBTest;

### CREATE USER ACCOUNT
mysql> CREATE USER 'dbtestuser'@'192.168.10.101' IDENTIFIED BY 'secretPass';

### GRANT PERMISSIONS ON DATABASE
mysql> GRANT ALL ON DBTest.* TO 'dbtestuser'@'192.168.1.100';

###  RELOAD PRIVILEGES
mysql> FLUSH PRIVILEGES;

Congratulations! MySQL server has been successfully installed on your machine.

Alternative Solutions for Installing MySQL

While the above steps detail installing MySQL using the official MySQL repositories, alternative methods exist. Here are two different approaches:

1. Using Docker

Docker provides a containerized environment, simplifying the deployment of applications like MySQL. This approach eliminates the need to configure repositories and manage dependencies directly on the host operating system. Docker ensures consistent behavior across different environments.

Explanation:

Docker images are pre-built environments that contain everything needed to run an application. The Docker Hub provides a readily available MySQL image, which can be pulled and run with minimal configuration. Docker containers are isolated from the host system, ensuring that the installation doesn’t interfere with other applications or system libraries.

Code Example:

First, install Docker on your CentOS/RHEL/Fedora system if it’s not already installed. Instructions can be found on the official Docker website. After installation, you can pull the MySQL image from Docker Hub:

docker pull mysql:5.7

Next, run the MySQL container:

docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:5.7
  • --name some-mysql: Assigns a name to the container.
  • -e MYSQL_ROOT_PASSWORD=my-secret-pw: Sets the root password. Important: Replace my-secret-pw with a strong password.
  • -d mysql:5.7: Runs the container in detached mode (background).

To connect to the MySQL server running inside the container, you can use the docker exec command:

docker exec -it some-mysql mysql -u root -p

You’ll be prompted for the password you set earlier. This is a simple way of Installing MySQL on CentOS/Redhat 7/6 & Fedora 31/30.

2. Using Software Collections (SCL)

Software Collections (SCL) allow you to install multiple versions of the same software on a single system without conflicts. This is particularly useful when you need to run applications that require specific MySQL versions.

Explanation:

SCL provides a way to package and install software in a separate directory structure, isolated from the base operating system. This prevents version conflicts and allows you to easily switch between different versions of MySQL.

Code Example:

First, enable the SCL repository:

yum install centos-release-scl

Then, install the desired MySQL version from SCL. For example, to install MySQL 5.7:

yum install rh-mysql57

To start using MySQL 5.7, you need to enable the SCL environment:

scl enable rh-mysql57 bash

Now you can start and configure the MySQL service:

systemctl start rh-mysql57-mysqld.service
systemctl enable rh-mysql57-mysqld.service

The configuration files for MySQL 5.7 in SCL are located in /opt/rh/rh-mysql57/root/etc/my.cnf.d/.

Considerations for Alternative Methods:

  • Docker: Simplifies deployment and ensures consistency but requires familiarity with containerization concepts. Good for development environments or applications that benefit from isolation.
  • SCL: Allows multiple MySQL versions to coexist on a single system but adds complexity to the installation and management process. Suitable for scenarios where different applications require different MySQL versions.

These alternative solutions provide flexibility in how you approach Installing MySQL on CentOS/Redhat 7/6 & Fedora 31/30, catering to different needs and environments.

Leave a Reply

Your email address will not be published. Required fields are marked *