CentOS 7 compile and install MySQL database system

Apache HTTP Server as an excellent Web server software that provides a user-oriented front-end applications. In the actual production environment, in order to provide a richer, more powerful Web applications, often also need to have a back-end database supports a variety of roles, web programming language.

As a qualified operation and maintenance engineers, building services system is one of the skills necessary MySQL on Linux systems.

MySQL is a true multi-threaded, multi-user SQL database service, with its high performance, high reliability and ease of use features, servers become the most popular open source database field system. Before 2008, the project conducted by MySQL MySQL AB develops, launch and support, then after Sun's acquisition of MySQL AB, Oracle's acquisition of Sun's process, the project is currently responsible for the operation and maintenance of MySQL by Oracle Corporation.

In order to ensure the integrity of MySQL database functionality, customizability, we decided to adopt source code compiler installation to install MySQL database system. MySQL 5.X most widely used system version, the version of the stability and compatibility are good, we have a mysql-5.6.36.tar.gz example. Its official website: https://www.mysql.com/

Required to compile and install MySQL source code package:
cmake source package dish network download link: https://pan.baidu.com/s/15Q78GrbKDGWV2Tp_eAp1eg
extraction code: 85h5
MySQL source package dish network download link: link: HTTPS: // PAN. baidu.com/s/1ffgOkbDctVT4T-gs3znS-A
extraction code: i748

Compile and install MySQL steps:

First, the preparatory work

1. In order to avoid the phenomenon of port conflicts, program conflict occurs, it is recommended to query MySQL installation software. If there is, it is recommended to uninstall!

[root@localhost ~]# rpm -q mysql-server mysql              
未安装软件包 mysql-server 
未安装软件包 mysql 

2. Install the provided library mysql basic character on the terminal to use better use mysql command terminal, to avoid distortion.

[root@localhost ~]# rpm -ivh /mnt/Packages/ncurses-devel-5.9-13.20130511.el7.x86_64.rpm 
//安装系统盘自带的ncurses-devel包

3.MySQL 5.X system version requires cmake to compile and install, so install cmake package.

[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

Second, the source code to compile and install

1. Create a user operation
in order to enhance access control database services, it is recommended to use a dedicated user to run, such as mysql. The user does not need to log in directly to the system, you can not create a host folder.

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

2. unpack
the downloaded mysql extract the source, and switch to the unpacked directory.

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

3. Configure
in the actual environment, can be used in web page multiple character sets, and accordingly the database system should also support different character set encoding. In the configuration process, you can use the default character set is 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

Configuration commands, the meaning of each option:
CentOS 7 compile and install MySQL database system

4. Compile and install

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

Third, other adjustments after installation

1. database directory permissions

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

2. Profiling
default under CentOS 7 support MariaDB database system, so the system default configuration file is MariaDB /etc/my.cnf configuration file before starting the MySQL database, it is recommended to replace the original file my.cnf content provided by MySQL configuration file.

[root@localhost ~]# rm -rf /etc/my.cnf                       //将原来etc文件夹下的my.cnf文件删除
[root@localhost ~]# cp /usr/src/mysql-5.6.36/support-files/my-default.cnf /etc/my.cnf
//通过mysql本身的配置文件模板生成mysql配置文件

3. Initialize the database
to be able to properly use the MySQL database system, perform the initialization script mysql_install_db to run the mysql user, specifying that data program directory, data storage directory.

[root@localhost ~]# /usr/local/mysql/scripts/mysql_install_db \
 --user=mysql --basedir=/usr/local/mysql \                       //指定程序目录
 --datadir=/usr/local/mysql/data/                                       //指定数据目录

4. Set environment variables
for convenience mysql command in any directory, set the environment variable in the / etc / profile.

[root@localhost ~]# echo $PATH                        //查看环境变量
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@localhost ~]# echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
//将mysql可执行命令脚本绝对路径放在环境变量默认的路径中

In this way temporarily to take effect, reboot the system will be lost, want to permanently take effect:

[root@localhost ~]# vim /etc/profile
                          …………     /省略部分内容
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:
/usr/local/mysql/bin:/usr/local/mysql/bin
//前面是系统默认路径,请编写最后一条路径
[root@localhost ~]# . /etc/profile
//立即生效,读取配置文件

IV. Adding a system service
if you want to add mysqld system services. In order to be managed by systemctl. First of all:

[root@localhost ~]# cp /usr/src/mysql-5.6.36/support-files/mysql.server /usr/local/mysql/bin/mysqld.sh
//将mysql源码包的启动服务的脚本复制到安装目录,并改名为mysqld.sh
[root@localhost ~]# chmod +x /usr/local/mysql/bin/mysqld.sh
//赋予执行权限

Three ways to start the mysql (two of which are added as a system service, one is serving mysql script source package provided)

The first method is to add system services:

[root@localhost ~]# cp /usr/local/mysql/bin/mysqld.sh /etc/init.d/mysqld
//复制mysql服务脚本到指定目录
[root@localhost ~]# vim /etc/init.d/mysqld                     
//编写配置文件,添加以下两行
#!/bin/bash
# chkconfig: 2345 86 36
                    ………………                   //省略部分内容
[root@localhost ~]# chkconfig --add mysqld
//添加为系统服务
[root@localhost ~]# systemctl start mysqld
//启动服务成功
[root@localhost ~]# netstat -anpt | grep mysql
tcp6       0      0 :::3306                 :::*                    LISTEN      68867/mysqld     
//mysql服务默认的TCP端口是3306
[root@localhost ~]# chkconfig  --level 2345 mysqld on
//设置为开机自启动

The second method is to add system services:

[root@localhost ~]# vim /usr/lib/systemd/system/mysqld.service
//创建mysql系统服务的配置文件并编辑
[Unit]
Description=MYSQL Server
After=network.target

[Service]
User=mysql                //指定程序运行的用户账号
Group=mysql               //指定程序运行的组账号

Type=forking
PIDFile=/usr/local/mysql/data/localhost.localdomain.pid   //localhost.localdomain为当前的主机名
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
//启动服务成功,并查看端口信息
[root@localhost ~]# netstat -anpt | grep mysqld
tcp6       0      0 :::3306                 :::*                    LISTEN      68867/mysqld 
[root@localhost ~]# systemctl enable mysqld
//设置为开机自启动

When you start the service, if an error occurs, it is recommended to use:

[root@localhost ~]# systemctl daemon-reload
//重载服务配置项

These two ways here is not to say, if there do not understand, you can refer Bowen CentOS 7 build Web sites using the Apache service , there are detailed introduction.

The third method starting method

[root@localhost ~]# /usr/local/mysql/bin/mysqld_safe &
//使用mysql服务自带的工具mysqld_safe来启动服务,
//并且放入后台执行,否则会占用前台终端的资源
[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
//停止服务
[root@localhost ~]# netstat -anpt | grep mysqld

Guess you like

Origin blog.51cto.com/14157628/2421146