CentOS 7 编译安装 MySQL-5.7.9

CentOS 7 编译安装 MySQL-5.7.9

本文主要来自https://typecodes.com/web/centos7compilemysql.html

实验环境

  1. VMware 12中最小化安装的CentOS 7 x64
  2. CentOS 7 配置为 2cpu 2G内存(原来分配1G,但是在编译MySQL到54%时卡住)
  3. 下载MySQL-5.7.9版本

配置防火墙和开放端口

以下操作在root用户中操作

[root@localhost ~]#systemctl stop firewalld.service #关闭防火墙
[root@localhost ~]#systemctl disable firewalld.service #禁止防火墙开机启动
[root@localhost ~]#vi /etc/selinux/config #关闭selinux
修改为SELINUX=disabled
[root@localhost ~]#setenforce 0 #使配置生效


[root@localhost ~]#yum install -y iptables-services #安装防火墙规则
[root@localhost ~]#vi /etc/sysconfig/iptables #配置防火墙规则

-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT #添加
-A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT #添加
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited

esc键,然后输入命令 :wq! 保存修改
[root@localhost ~]#systemctl restart iptables.service #重启防火墙使配置生效
[root@localhost ~]#systemctl enable iptables.service #设置防火墙开机启动

创建mysql用户和用户组

首先创建一个名为mysql且没有登录权限的用户和一个名为mysql的用户组

[root@localhost mysql]# groupadd -r mysql && useradd -r -g mysql -s /bin/false -M mysql

下载编译MySQL5.7.9时需要的工具

######CMake编译工具
[root@localhost ~]# wget -c http://git.typecodes.com/libs/ccpp/cmake-3.2.1.tar.gz
[root@localhost ~]# tar -zxf cmake-3.2.1.tar.gz && cd cmake-3.2.1
[root@localhost cmake-3.2.1]# ./configure
[root@localhost cmake-3.2.1]# make && make install [默认安装路径是/usr/local/bin]
[root@localhost cmake-3.2.1]# cmake –version [查看cmake版本]
cmake version 3.2.1

CMake suite maintained and supported by Kitware (kitware.com/cmake).
[root@localhost cmake-3.2.1]# cd ~ && rm -rf cmake-3.2.1*

######Ncurses:提供功能键定义(快捷键),屏幕绘制以及基于文本终端的图形互动功能的动态库。
[root@localhost ~]# yum -y install ncurses-devel

#######bison:GNU分析器生成器
[root@localhost ~]# wget -c http://git.typecodes.com/libs/ccpp/bison-3.0.tar.gz
[root@localhost ~]# tar -zxf bison-3.0.tar.gz && cd bison-3.0/ && ./configure
[root@localhost bison-3.0]# make && make install
[root@localhost bison-3.0]# cd ~ && rm -rf bison-3.0*

#######Boost库:一个开源可移植的C++库,是C++标准化进程的开发引擎之一
下载 boost_1_59_0并上传到/root/目录(一定要这个版本以上,尝试多次安装1.57.0版本导致后面安装mysql5.7.9的时候会报找不到这个库的错误)
[root@typecodes ~]# tar -zxf boost_1_59_0.tar.gz && cd boost_1_59_0/
[root@localhost boost_1_59_0]# ./bootstrap.sh
[root@localhost boost_1_59_0]# ./b2 stage threading=multi link=shared
[root@localhost boost_1_59_0]# ./b2 install threading=multi link=shared
这里会报如下错误:

common.copy stage/lib/libboost_wave.so.1.59.0
ln-UNIX stage/lib/libboost_wave.so
...failed updating 29 targets...
...skipped 6 targets...
...updated 592 targets...

这个命令等编译安装完mysql后再删除
[root@localhost boost_1_59_0]# cd ~ && rm -rf boost_1_59_0*

开始MySQL-5.7.9编译安装前配置

######使用cmake命令,开始编译MySQL5.7.9
[root@localhost mysql]# tar -zxf mysql-5.7.9.tar.gz && cd mysql-5.7.9
[root@localhost mysql-5.7.9]# cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql  \              [MySQL安装的根目录]
-DMYSQL_DATADIR=/mydata/mysql/data  \                   [MySQL数据库文件存放目录]
-DSYSCONFDIR=/etc \                                     [MySQL配置文件所在目录]
-DMYSQL_USER=mysql \                                    [MySQL用户名]      
-DWITH_MYISAM_STORAGE_ENGINE=1 \                        [MySQL的数据库引擎]
-DWITH_INNOBASE_STORAGE_ENGINE=1 \                      [MySQL的数据库引擎]
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \                       [MySQL的数据库引擎]
-DWITH_MEMORY_STORAGE_ENGINE=1 \                        [MySQL的数据库引擎]
-DWITH_READLINE=1 \                                     [MySQL的readline library]
-DMYSQL_UNIX_ADDR=/var/run/mysql/mysql.sock \           [MySQL的通讯目录]
-DMYSQL_TCP_PORT=3306 \                                 [MySQL的监听端口]
-DENABLED_LOCAL_INFILE=1 \                              [启用加载本地数据]
-DENABLE_DOWNLOADS=1 \                                  [编译时允许自主下载相关文件]
-DWITH_PARTITION_STORAGE_ENGINE=1  \
-DEXTRA_CHARSETS=all \                                  [使MySQL支持所有的扩展字符]
-DDEFAULT_CHARSET=utf8 \                                [设置默认字符集为utf8]
-DDEFAULT_COLLATION=utf8_general_ci \                   [设置默认字符校对]
-DWITH_DEBUG=0 \                                        [禁用调试模式]
-DMYSQL_MAINTAINER_MODE=0 \
-DWITH_SSL:STRING=bundled \                             [通讯时支持ssl协议]
-DWITH_ZLIB:STRING=bundled                              [允许使用zlib library] \
-DDOWNLOAD_BOOST=1 \
-DWITH_BOOST=/root/mysql/boost_1_59_0

执行上面配置命令后如下图所示:
CentOS 7 编译安装 MySQL-5.7.9

开始编译安装MySQL-5.7.9

开始make命令编译时,比较耗内存和cpu,开始博主分配给虚拟机是双核1G内存,编译到54%开始卡住,无奈中断把内存改为2G就顺利编译,耗时多个小时。如果主机内存在1G并且没有设置linux交换空间,不建议编译安装MySQL5.7.9

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

执行完make命令如下:
CentOS 7 编译安装 MySQL-5.7.9
CentOS 7 编译安装 MySQL-5.7.9
CentOS 7 编译安装 MySQL-5.7.9
CentOS 7 编译安装 MySQL-5.7.9
编译完成
CentOS 7 编译安装 MySQL-5.7.9
安装完成,终于成功
CentOS 7 编译安装 MySQL-5.7.9

查看编译成功后的MySQL目录

使用命令查看MySQL的安装目录/usr/local/mysql/下面是否生成了相关目录文件(最重要的当然是bin、sbin和lib目录)。如果lib目录下面没有生成如图所示的.so动态库文件和.a静态库文件,那么说明安装不成功(即使成功了也可能会导致php进程无法找到mysql的相关库文件)。
CentOS 7 编译安装 MySQL-5.7.9

设置MySQL的配置文件my.cnf

先把编译生成的my.cnf文件备份,再添加自己配置的my.cnf

[root@localhost ~]# mv /etc/my.cnf /etc/my.cnf.bak
[root@localhost ~]# vi /etc/my.cnf
添加如下配置:(配置来自:https://github.com/vfhky/mylnmp/blob/master/MySql/my.cnf

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.

[client]
port=3306
socket=/var/run/mysql/mysql.sock

[mysqld]

# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M

# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin

# These are commonly set, remove the # and set as required.
user = mysql
basedir = /usr/local/mysql
datadir = /mydata/mysql/data
port=3306
server-id = 1
socket=/var/run/mysql/mysql.sock

character-set-server = utf8
log-error = /var/log/mysql/error.log
pid-file = /var/log/mysql/mysql.pid
general_log = 1
skip-name-resolve
#skip-networking
back_log = 300

max_connections = 1000
max_connect_errors = 6000
open_files_limit = 65535
table_open_cache = 128 
max_allowed_packet = 4M
binlog_cache_size = 1M
max_heap_table_size = 8M
tmp_table_size = 16M

read_buffer_size = 2M
read_rnd_buffer_size = 8M
sort_buffer_size = 8M
join_buffer_size = 28M
key_buffer_size = 4M

thread_cache_size = 8

query_cache_type = 1
query_cache_size = 8M
query_cache_limit = 2M

ft_min_word_len = 4

log_bin = mysql-bin
binlog_format = mixed
expire_logs_days = 30


performance_schema = 0
explicit_defaults_for_timestamp

#lower_case_table_names = 1



myisam_sort_buffer_size = 8M
myisam_repair_threads = 1

interactive_timeout = 28800
wait_timeout = 28800


# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M 

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

# Recommended in standard MySQL setup
sql_mode=NO_ENGINE_SUBSTITUTION,NO_AUTO_CREATE_USER,STRICT_TRANS_TABLES 

[mysqldump]
quick
max_allowed_packet = 16M

[myisamchk]
key_buffer_size = 8M
sort_buffer_size = 8M
read_buffer = 4M
write_buffer = 4M

保存即可

添加mysql的环境变量

将MySQL编译生成的bin目录添加到当前Linux系统的环境变量中

[root@localhost ~]# echo -e ‘\n\nexport PATH=/usr/local/mysql/bin:$PATH\n’ >> /etc/profile && source /etc/profile

创建MySQL数据库文件存放路径及相关安全配置

在Linux主机上创建一个目录/mydata/mysql/data,用于存放MySQL的数据库文件。同时设置其用户和用户组为之前创建的mysql,权限为700。这样其它用户是无法进行读写的,尽量保证数据库的安全。

[root@localhost ~]# mkdir -p /mydata/mysql/data && chown -R root:mysql /usr/local/mysql
[root@localhost ~]# chown -R mysql:mysql /mydata/mysql/data
[root@localhost ~]# chmod -R go-rwx /mydata/mysql/data

创建MySQL日志和数据存放目录

下面配置的MySQL日志存放目录以及权限都是根据前面my.cnf文件写的,也就是两者需要保持一致。

[root@localhost ~]# mkdir -p /var/run/mysql && mkdir -p /var/log/mysql
[root@localhost ~]# chown -R mysql:mysql /var/log/mysql && chown -R mysql:mysql /var/run/mysql

初始化MySQL自身的数据库

在MySQL安装目录的/usr/local/mysql/bin路径下,执行mysqld命令,初始化MySQL自身的数据库。

######参数user表示用户,basedir表示mysql的安装路径,datadir表示数据库文件存放路径
[root@localhost bin]# mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/mydata/mysql/data

执行完后,可以通过[root@localhost bin]# ls -lrt /mydata/mysql/data/命令查看是否生成了MySQL自身的数据库文件。

设置开机启动

######配置开机自启动
[root@localhost ~]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@localhost ~]# chmod +x /etc/init.d/mysqld #增加可执行权限
[root@localhost ~]# chkconfig --add mysqld #添加到sysV服务
[root@localhost ~]# chkconfig mysqld on

启动MySQL服务

在完成上面的操作后,就可以正式使用MySQL服务了。启动MySQL进程服务的命令如下:

[root@localhost ~]# mysqld_safe --user=mysql --datadir=/mydata/mysql/data --log-error=/var/log/mysql/error.log [直接回车]
[1] 10274
150513 21:28:16 mysqld_safe Logging to ‘/var/log/mysql/error.log’.
150513 21:28:16 mysqld_safe Starting mysqld daemon with databases from /mydata/mysql/data

######上面这条命令会在后台继续执行,所以直接回车并执行下面这条命令
[root@localhost ~]# service mysqld start
Starting MySQL.[ OK ] [启动成功]
[root@localhost ~]#

然后使用下面这命令ps -ef | grep mysql和netstat -tunpl | grep 3306查看MySQL服务进程和端口监听情况

初始化MySQL数据库的root用户密码

和Oracle数据库一样,MySQL数据库也默认自带了一个root用户(这个和当前Linux主机上的root用户是完全不搭边的),我们在设置好MySQL数据库的安全配置后初始化root用户的密码。配置过程中,一路输入y就行了。这里只说明下MySQL-5.7.9版本中,用户密码策略分成低级LOW、中等MEDIUM和超强STRONG三种,推荐使用中等MEDIUM级别!

[root@localhost ~]# mysql_secure_installation

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD PLUGIN 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 plugin?

Press y|Y for Yes, any other key for No: y

There are three levels of password validation policy:

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
Please set the password for root here.

New password: 

将MySQL数据库的动态链接库共享至系统链接库

一般MySQL数据库还会被类似于PHP等服务调用,所以我们需要将MySQL编译后的lib库文件添加至当前Linux主机链接库/etc/ld.so.conf.d/下,这样MySQL服务就可以被其它服务调用了。

[root@localhost ~]# echo “/usr/local/mysql/lib” > /etc/ld.so.conf.d/mysql.conf
[root@localhost ~]# ldconfig
[root@localhost ~]# ldconfig -v | grep mysql
ldconfig: 无法对 /libx32 进行 stat 操作: 没有那个文件或目录
ldconfig: 多次给出路径“/usr/lib”
ldconfig: 多次给出路径“/usr/lib64”
ldconfig: 无法对 /usr/libx32 进行 stat 操作: 没有那个文件或目录
/usr/lib64/mysql:
libmysqlclient.so.18 -> libmysqlclient.so.18.0.0
/usr/local/mysql/lib:
libmysqlclient.so.20 -> libmysqlclient.so.20.0.9

创建其它MySQL数据库用户

使用MySQL数据库root管理员用户登录MySQL数据库后,可以管理数据库和其他用户了。这里演示创建一个名为mysql的MySQL用户(密码为Mysql12.)和一个名为test的数据库。

[root@localhost ~]# mysql -uroot -p密码

######登录成功后,创建test数据库,并设置字符集和字符校
mysql> CREATE DATABASE test DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
Query OK, 1 row affected (0.00 sec)

######创建名为typecodes用户,并让它拥有typecodes数据库所有的权限
mysql> grant all privileges on test.* to ‘mysql’@’%’ identified by ‘Mysql12.’;
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> exit; [创建完毕,root用户退出]

查看数据库编码方式

[root@localhost data]# mysql -uroot -p
Enter password:

mysql> show variables like 'character%';
+--------------------------+----------------------------------+
| Variable_name            | Value                            |
+--------------------------+----------------------------------+
| character_set_client     | utf8                             |
| character_set_connection | utf8                             |
| character_set_database   | utf8                             |
| character_set_filesystem | binary                           |
| character_set_results    | utf8                             |
| character_set_server     | utf8                             |
| character_set_system     | utf8                             |
| character_sets_dir       | /usr/local/mysql/share/charsets/ |
+--------------------------+----------------------------------+
8 rows in set (0.00 sec)
mysql> show variables like 'collation%';
+----------------------+-----------------+
| Variable_name        | Value           |
+----------------------+-----------------+
| collation_connection | utf8_general_ci |
| collation_database   | utf8_general_ci |
| collation_server     | utf8_general_ci |
+----------------------+-----------------+
3 rows in set (0.00 sec)

参考网址

CentOS 7.1编译安装MySQL5.7.7rc 主要
CentOS 7下源码安装MySQL 5.6
CentOS下查看修改mysql编码方式
CentOS 7.0系统安装配置LAMP服务器(Apache+PHP+MariaDB)

上一篇:hadoop2.6.4 安装和编译


下一篇:CentOS 7 编译安装PHP7