CentOS 7 Compile and Install MySQL Database System

Apache HTTP Server is an excellent Web server software that provides front-end user-oriented application capabilities.In the actual production environment, in order to provide richer and more powerful web application functions, it often needs the support of background database, web page programming language and other roles.

As a qualified maintenance engineer, building a MySQL service system on Linux is also one of the essential skills.

MySQL is a truly multi-threaded, multi-user SQL database service. With its high performance, high reliability and ease of use, MySQL has become the most popular open source database system in the server field.Before 2008, MySQL project was developed, published and supported by MySQL AB Company. After Sun Company's acquisition of MySQL AB Company and Oracle Company's acquisition of Sun Company, the current MySQL project is operated and maintained by Oracle Company.

To ensure the integrity and customization of the MySQL database functionality, we decided to install the MySQL database system using source code compilation and installation.The most widely used version of MySQL 5.X system is the stable and compatible version. Take mysql-5.6.36.tar.gz for example.Its official website is: https://www.mysql.com/

Compile the source code package required to install MySQL:
cmake Source Pack Disk Download Link: https://pan.baidu.com/s/15Q78GrbKDGWV2Tp_eAp1eg
Extraction code: 85h5
mysql Source Pack Disk Download Link:Link: https://pan.baidu.com/s/1ffgOkbDctVT4T-gs3znS-A
Extraction code: i748

Compile and install MySQL steps:

I. Preparations

1. To avoid port conflicts and program conflicts, it is recommended to query the installation of MySQL software first.If so, uninstall is recommended!

[root@localhost ~]# rpm -q mysql-server mysql              
The package mysql-server is not installed 
Package mysql is not installed 

2. Install the mysql base library that provides the use of character terminals to better use commands on mysql terminals and avoid clutter.

[root@localhost ~]# rpm -ivh /mnt/Packages/ncurses-devel-5.9-13.20130511.el7.x86_64.rpm 
//Install the ncurses-devel package that comes with the system disk

3.The MySQL 5.X system version requires cmake compilation and installation, so install the cmake package first.

[root@localhost ~]# tar zxf cmake-2.8.6.tar.gz -C /usr/src
[root@localhost ~]# cd /usr/src/cmake-2.8.6/
[root@localhost cmake-2.8.6]# ./configure && gmake && gmake install

2. Source Code Compilation and Installation

1. Create a running user
To tighten permission control for database services, it is recommended that you use a dedicated running user, such as mysql.This user does not need to log on directly to the system, and may not create a host folder.

[root@localhost ~]# groupadd mysql
[root@localhost ~]# useradd -M -s /sbin/nologin mysql -g mysql

2. Unpacking
Unzip the downloaded mysql source code and switch to the unzipped directory.

[root@localhost ~]# tar zxf mysql-5.6.36.tar.gz -C /usr/src
[root@localhost ~]# cd /usr/src/mysql-5.6.36/

3. Configuration
In practice, you can use pages with multiple character sets, and the database system should support different character set encoding accordingly.During configuration, you can set the default character set to utf-8 and add support for other character sets.

[root@localhost mysql-5.6.36]# cmake \
 -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
 -DSYSCONFDIR=/etc/ -DDEFAULT_CHARSET=utf8 \
 -DDEFAULT_COLLATION=utf8_general_ci \
 -DWITH_EXTRA_CHARSETS=all

In the configuration command, the meaning of each option is:

4. Compile and install

[root@localhost mysql-5.6.36]# make && make install

3. Other adjustments after installation

1. Set permissions on database directories

[root@localhost ~]# chown -R mysql:mysql /usr/local/mysql

2. Set up a configuration file
MariaDB databases are supported by default under CentOS 7 system, so MariaDB's configuration file is in the system's default /etc/my.cnf configuration file. Before starting MySQL database, it is recommended to replace the original my.cnf file with the content of the configuration file provided by MySQL.

[root@localhost ~]# Rm-rf/etc/my.cnf //Delete my.cnf file under original etc folder
[root@localhost ~]# cp /usr/src/mysql-5.6.36/support-files/my-default.cnf /etc/my.cnf
//Generating MySQL configuration files from mysql's own profile template

3. Initialize the database
In order to use the MySQL database system properly, the initialization script mysql_install_db should be executed as running user mysql, specifying the data program directory and data storage directory.

[root@localhost ~]# /usr/local/mysql/scripts/mysql_install_db \
 --user=mysql --basedir=/usr/local/mysql \                       //Specify program directory
 --datadir=/usr/local/mysql/data/                                       //Specify data directory

4. Setting environment variables
To make it easy to use the mysql command in any directory, you need to set environment variables in / etc/profile.

[root@localhost ~]# echo $PATH //View environment variables
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@localhost ~]# echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
//Place the absolute path of the mysql executable command script in the default path of the environment variable

This will take effect temporarily and restart the system will be lost if you want it to take effect permanently:

[root@localhost ~]# vim /etc/profile
                          ............     /Omit some content
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:
/usr/local/mysql/bin:/usr/local/mysql/bin
//Previous is the system default path, please write the last path
[root@localhost ~]# . /etc/profile
//Take effect immediately, read the configuration file

4. Add as System Service
If you want to add the mysqld system service.To be managed through systemctl.First:

[root@localhost ~]# cp /usr/src/mysql-5.6.36/support-files/mysql.server /usr/local/mysql/bin/mysqld.sh
//Copy the script for the startup service of the mysql source package to the installation directory and rename it mysqld.sh
[root@localhost ~]# chmod +x /usr/local/mysql/bin/mysqld.sh
//Give Execution Permission

Three ways to start mysql (two of which are added as system services and one is the service script provided in the mysql source package)

The first way to add as a system service is:

[root@localhost ~]# cp /usr/local/mysql/bin/mysqld.sh /etc/init.d/mysqld
//Copy mysql service script to specified directory
[root@localhost ~]# vim /etc/init.d/mysqld                     
//Write a configuration file and add the following two lines
#!/bin/bash
# chkconfig: 2345 86 36
                    ..................                   //Omit some content
[root@localhost ~]# chkconfig --add mysqld
//Add as System Service
[root@localhost ~]# systemctl start mysqld
//Start Service Successful
[root@localhost ~]# netstat -anpt | grep mysql
tcp6       0      0 :::3306                 :::*                    LISTEN      68867/mysqld     
//The default TCP port for the mysql service is 3306
[root@localhost ~]# chkconfig  --level 2345 mysqld on
//Set to Start-Up Self-Starting

The second way to add as a system service is:

[root@localhost ~]# vim /usr/lib/systemd/system/mysqld.service
//Create a configuration file for the mysql system service and edit it
[Unit]
Description=MYSQL Server
After=network.target

[Service]
User=mysql                //User account for the specified program to run
Group=mysql               //Group Account Specifying Program Run

Type=forking
PIDFile=/usr/local/mysql/data/localhost.localdomain.pid   //localhost.localdomain is the current host name
ExecStart=/usr/local/mysql/bin/mysqld.sh start
ExecStop=/usr/local/mysql/bin/mysqld.sh stop

[Install]
WantedBy=multi-user.target
[root@localhost ~]# systemctl start mysqld
//Start service successfully and view port information
[root@localhost ~]# netstat -anpt | grep mysqld
tcp6       0      0 :::3306                 :::*                    LISTEN      68867/mysqld 
[root@localhost ~]# systemctl enable mysqld
//Set to Start-Up Self-Starting

When starting a service, if there is an error, it is recommended to use:

[root@localhost ~]# systemctl daemon-reload
//Overloaded Service Configuration Items

Let's not go into these two ways. If you don't understand them, you can refer to the blog posts. CentOS 7 uses Apache to build Web site services , which has a detailed introduction.

Third method startup method

[root@localhost ~]# /usr/local/mysql/bin/mysqld_safe &
//Start the service using the tool mysqld_safe that comes with the mysql service.
//And put it in the background, otherwise it will occupy the resources of the foreground terminal
[1] 69469
[root@localhost ~]# 190718 01:54:05 mysqld_safe Logging to '/usr/local/mysql/data/localhost.localdomain.err'.
190718 01:54:05 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data

[root@localhost ~]# netstat -anpt | grep mysqld
tcp6       0      0 :::3306                 :::*                    LISTEN      69549/mysqld 
[root@localhost ~]# mysqladmin shutdown
//Out of Service
[root@localhost ~]# netstat -anpt | grep mysqld

Keywords: Linux MySQL Database cmake RPM

Added by Dodon on Thu, 18 Jul 2019 00:21:25 +0300