a. 操作系统:CentOS Linux release 7.6.1810
b. MySQL:mysql-5.7.27.tar.gz,点击下载,准备MySQL的源码编译包
c. boost:boost_1_59_0.tar.gz,点击下载,安装MySQL 5.7的时候,需要依赖boost库,MySQL5.7对应的boost版本是1.59
d. cmake:cmake-2.8.12.2.tar.gz,点击下载,编译安装MySQL时,使用cmake的方式进行编译安装,因此,需要先安装cmake
#建议创建个tools目录,将所有的软件包放在一起
[root@192168066012_MySQL_5_7_27 ~]# mkdir -p /home/tools/
[root@192168066012_MySQL_5_7_27 ~]# cd /home/tools/
[root@192168066012_MySQL_5_7_27 tools]# ls -l
total 143932
-rw-r--r-- 1 root root 83709983 Jul 31 18:34 boost_1_59_0.tar.gz
-rw-r--r-- 1 root root 6068231 Jul 31 18:36 cmake-2.8.12.2.tar.gz
-rw-r--r-- 1 root root 54398696 Jul 31 18:34 mysql-5.7.27.tar.gz
#解压cmake安装包
[root@192168066012_MySQL_5_7_27 tools]# tar xf cmake-2.8.12.2.tar.gz
[root@192168066012_MySQL_5_7_27 tools]# cd cmake-2.8.12.2
[root@192168066012_MySQL_5_7_27 cmake-2.8.12.2]# ll
total 3056
-rwxr-xr-x 1 501 games 53974 Jan 17 2014 bootstrap
-rw-r--r-- 1 501 games 254736 Jan 17 2014 ChangeLog.manual
-rw-r--r-- 1 501 games 2603523 Jan 17 2014 ChangeLog.txt
-rw-r--r-- 1 501 games 5465 Jan 17 2014 CMakeCPack.cmake
-rw-r--r-- 1 501 games 3436 Jan 17 2014 CMakeCPackOptions.cmake.in
-rw-r--r-- 1 501 games 153 Jan 17 2014 CMakeGraphVizOptions.cmake
-rw-r--r-- 1 501 games 25439 Jan 17 2014 CMakeLists.txt
-rw-r--r-- 1 501 games 4481 Jan 17 2014 CMakeLogo.gif
-rw-r--r-- 1 501 games 796 Jan 17 2014 cmake_uninstall.cmake.in
-rw-r--r-- 1 501 games 2515 Jan 17 2014 CompileFlags.cmake
-rwxr-xr-x 1 501 games 99 Jan 17 2014 configure
-rw-r--r-- 1 501 games 2637 Jan 17 2014 Copyright.txt
-rw-r--r-- 1 501 games 1050 Jan 17 2014 CTestConfig.cmake
-rw-r--r-- 1 501 games 3338 Jan 17 2014 CTestCustom.cmake.in
-rw-r--r-- 1 501 games 145 Jan 17 2014 CTestCustom.ctest.in
-rw-r--r-- 1 501 games 809 Jan 17 2014 DartConfig.cmake
-rw-r--r-- 1 501 games 4144 Jan 17 2014 DartLocal.conf.in
drwxr-xr-x 3 root root 142 Aug 1 14:32 Docs
-rw-r--r-- 1 501 games 28046 Jan 17 2014 doxygen.config
drwxr-xr-x 4 root root 53 Aug 1 14:32 Example
drwxr-xr-x 9 root root 16384 Aug 1 14:32 Modules
-rw-r--r-- 1 501 games 1946 Jan 17 2014 Readme.txt
drwxr-xr-x 7 root root 24576 Aug 1 14:32 Source
drwxr-xr-x 2 root root 4096 Aug 1 14:32 Templates
drwxr-xr-x 169 root root 8192 Aug 1 14:32 Tests
drwxr-xr-x 15 root root 4096 Aug 1 14:32 Utilities
#开始编译安装
[root@192168066012_MySQL_5_7_27 cmake-2.8.12.2]# ./configure
...省略
CMake has bootstrapped. Now run gmake.
#看到“Now run gmake”说明ok,可以进行gmake && gmake install
[root@192168066012_MySQL_5_7_27 cmake-2.8.12.2]# gmake
...省略
[100%] Built target pseudonl_valgrind
[root@192168066012_MySQL_5_7_27 cmake-2.8.12.2]# gmake install
...省略
-- Installing: /usr/local/share/cmake-2.8/completions/ctest
提示:
每一步结束后,可以使用echo $?
,当返回值为0时,说明编译过程正常
#指定解压到/usr/local/目录,使用 -C 参数
[root@192168066012_MySQL_5_7_27 tools]# tar xf boost_1_59_0.tar.gz -C /usr/local/
[root@192168066012_MySQL_5_7_27 tools]# cd /usr/local/
[root@192168066012_MySQL_5_7_27 local]# mv boost_1_59_0/ boost
[root@192168066012_MySQL_5_7_27 local]# ls -l
total 0
drwxr-xr-x. 2 root root 45 Jul 30 19:49 bin
drwx------ 8 501 games 302 Aug 12 2015 boost
drwxr-xr-x 3 root root 23 Jul 30 19:49 doc
drwxr-xr-x. 2 root root 6 Apr 11 2018 etc
drwxr-xr-x. 2 root root 6 Apr 11 2018 games
drwxr-xr-x. 2 root root 6 Apr 11 2018 include
drwxr-xr-x. 2 root root 6 Apr 11 2018 lib
drwxr-xr-x. 2 root root 6 Apr 11 2018 lib64
drwxr-xr-x. 2 root root 6 Apr 11 2018 libexec
drwxr-xr-x 3 root root 18 Jul 30 19:49 man
drwxr-xr-x. 2 root root 6 Apr 11 2018 sbin
drwxr-xr-x. 7 root root 81 Jul 30 19:49 share
drwxr-xr-x. 2 root root 6 Apr 11 2018 src
[root@192168066012_MySQL_5_7_27 tools]# yum install ncurses-devel -y
...省略
Installed:
ncurses-devel.x86_64 0:5.9-14.20130511.el7_4
Complete!
提示:
MySQL 5.7依赖的软件包:gcc gcc-c++ ncurses ncurses-devel bison make等供参考,安装前可以使用rpm -qa|grep ${软件包名称}检查一下
#创建数据库相关目录
[root@192168066012_MySQL_5_7_27 tools]# mkdir -p /application/mysql
[root@192168066012_MySQL_5_7_27 tools]# mkdir -p /application/mysql/data/
[root@192168066012_MySQL_5_7_27 tools]# mkdir -p /application/mysql/tmp/
[root@192168066012_MySQL_5_7_27 tools]# mkdir -p /application/mysql/binlog/
[root@192168066012_MySQL_5_7_27 tools]# mkdir -p /application/mysql/logs/
[root@192168066012_MySQL_5_7_27 tools]# tree -L 2 /application/
/application/
└── mysql
├── binlog
├── data
├── logs
└── tmp
5 directories, 0 files
#创建mysql组和用户
[root@192168066012_MySQL_5_7_27 tools]# groupadd mysql
[root@192168066012_MySQL_5_7_27 tools]# useradd mysql -s /sbin/nologin -M -g mysql
[root@192168066012_MySQL_5_7_27 tools]# id mysql
uid=1000(mysql) gid=1000(mysql) groups=1000(mysql)
说明:
-s /sbin/nologin 用户不能登录系统
-M 不创建用户的HOME目录
-g 指定用户登录组的GID或组名
#解压源码mysql-5.7.27.tar.gz安装包
[root@192168066012_MySQL_5_7_27 tools]# cd /home/tools/
[root@192168066012_MySQL_5_7_27 tools]# tar xf mysql-5.7.27.tar.gz
#cmake配置编译参数
[root@192168066012_MySQL_5_7_27 tools]# cd mysql-5.7.27
[root@192168066012_MySQL_5_7_27 mysql-5.7.27]# cmake . -DCMAKE_INSTALL_PREFIX=/application/mysql \
-DMYSQL_DATADIR=/application/mysql/data \
-DMYSQL_UNIX_ADDR=/application/mysql/tmp/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DEXTRA_CHARSETS=gbk,gb2312,utf8,ascii \
-DENABLED_LOCAL_INFILE=ON \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \
-DWITHOUT_PARTITION_STORAGE_ENGINE=1 \
-DWITH_ZLIB=bundled \
-DWITH_EMBEDDED_SERVER=1 \
-DWITH_DEBUG=0 \
-DWITH_BOOST=/usr/local/boost
#看到如下信息,说明cmake编译完成,执行make继续安装
-- Configuring done
-- Generating done
-- Build files have been written to: /home/tools/mysql-5.7.27
#继续执行make install继续安装
...省略
Linking CXX executable my_safe_process
[100%] Built target my_safe_process
#安装完成
...省略
-- Installing: /application/mysql-5.7.27/share/aclocal/mysql.m4
-- Installing: /application/mysql-5.7.27/support-files/mysql.server
提示:
如果上述操作未出现错误(每个步骤结束后,都可以使用echo $?看返回值是否为0,为0则表示正确),查看/application/mysql/目录,若其下有内容,则表示MySQL5.7.27源代码包采用cmake方式安装成功了
编译参数 | 说明 |
---|---|
-DCMAKE_INSTALL_PREFIX=/application/mysql-5.7.27 | 设定mysql安装目录 |
-DMYSQL_DATADIR=/application/mysql-5.7.27/data | 设定mysql数据文件目录 |
-DMYSQL_UNIX_ADDR=/application/mysql-5.7.27/tmp/mysql.sock | 设定mysql.sock路径 |
-DDEFAULT_CHARSET=utf8 | 设定默认的字符集为utf8 |
-DDEFAULT_COLLATION=utf8_general_ci | 设定默认排序规则 |
-DEXTRA_CHARSETS=gbk,gb2312,utf8,ascii | 启用额外的字符集类型 |
-DENABLED_LOCAL_INFILE=ON | 启用本地数据导入支持 |
-DWITH_INNOBASE_STORAGE_ENGINE=1 | 启用存储引擎;若想启用某个引擎的支持,-DWITH_ |
-DWITH_FEDERATED_STORAGE_ENGINE=1 | 启用存储引擎 |
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 | 启用存储引擎 |
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 | 启用存储引擎 |
-DWITHOUT_PARTITION_STORAGE_ENGINE=1 | 启用存储引擎 |
-DWITH_ZLIB=bundled | 启用zlib库支持 |
-DWITH_EMBEDDED_SERVER=1 | 编译嵌入式服务器支持 |
-DWITH_DEBUG=0 | 禁用debug(默认为禁用) |
-DWITH_BOOST=/usr/local/boost | 启用boost库支持,MySQL 5.7版本编译安装需要依赖boost库 |
提示:
更多编译参数选项说明参见官方文档:https://dev.mysql.com/doc/refman/5.7/en/source-configuration-options.html#option_cmake_with_boost
[root@192168066012_MySQL_5_7_27 ~]# echo 'export PATH=/application/mysql/bin:$PATH' >>/etc/profile
[root@192168066012_MySQL_5_7_27 ~]# tail -1 /etc/profile
export PATH=/application/mysql/bin:$PATH
#使/etc/profile配置文件立即生效
[root@192168066012_MySQL_5_7_27 ~]# source /etc/profile
[root@192168066012_MySQL_5_7_27 ~]# echo $PATH
/application/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@192168066012_MySQL_5_7_27 ~]# chown -R mysql:mysql /application/mysql/
[root@192168066012_MySQL_5_7_27 ~]# chown mysql:mysql /etc/my.cnf
#修改/etc/my.cnf文件,和5.6之前的版本不一样,5.7版本support-files目录下没有.cnf文件,因此,需要自己手动编辑
[root@192168066012_MySQL_5_7_27 ~]# cp /etc/my.cnf /etc/my.cnf.bak
[root@192168066012_MySQL_5_7_27 ~]# vim /etc/my.cnf
[client]
port = 3306
socket = /application/mysql/tmp/mysql.sock
default-character-set = utf8
[mysqld]
port = 3306
user = mysql
basedir = /application/mysql
datadir = /application/mysql/data
pid-file = /application/mysql/mysqld.pid
socket = /application/mysql/tmp/mysql.sock
tmpdir = /application/mysql/tmp
character_set_server = utf8
server-id = 1
max_connections = 100
max_connect_errors = 10
sql_mode = NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES
log-bin = /application/mysql/binlog/mysql-bin
log-error = /application/mysql/logs/mysql_5_7_27.err
my.cnf文件常用选项详解
[client]
#默认连接端口
port = 3306
#用于本地连接的socket套接字
socket = /application/mysql/tmp/mysql.sock
#编码
default-character-set = utf8[mysqld]
#服务端口号,默认3306
port = 3306
#mysql启动用户
user = mysql
#mysql安装根目录
basedir = /application/mysql
#mysql数据文件所在位置
datadir = /application/mysql/data
#pid文件所在目录
pid-file = /application/mysql/mysqld.pid
#为MySQL客户端程序和服务器之间的本地通讯指定一个套接字文件
socket = /application/mysql/tmp/mysql.sock
#MySQL存放临时文件的目录
tmpdir = /application/mysql/tmp
#数据库默认字符集,主流字符集支持一些特殊表情符号(特殊表情符占用4个字节)
character_set_server = utf8
#MySQL服务的唯一编号,每个mysql服务Id需唯一
server-id = 1
#最大连接数
max_connections = 100
#最大错误连接数
max_connect_errors = 10
#sql_mode,定义了mysql应该支持的sql语法,数据校验
#NO_ENGINE_SUBSTITUTION:如果需要的存储引擎被禁用或未编译,可以防止自动替换存储引擎
#STRICT_TRANS_TABLES:存储引擎启用严格模式,非法数据值被拒绝
#NO_AUTO_CREATE_USER:禁止GRANT创建密码为空的用户
#NO_AUTO_VALUE_ON_ZERO:默认情况下自增长列是从1开始的,如果你插入值为0的数据会报错,设置这个之后就可以正常插入为0的数据了
#NO_ZERO_IN_DATE:在严格模式,不接受月或日部分为0的日期
#NO_ZERO_DATE:在严格模式,不要将 ‘0000-00-00′做为合法日期
#ERROR_FOR_DIVISION_BY_ZERO:在严格模式,在INSERT或UPDATE过程中,如果被零除(或 MOD(X,0)),则产生错误(否则为警告)。如果未给出该模式,被零除时MySQL返回NULL
#PIPES_AS_CONCAT:将||视为字符串连接操作符(+)(同CONCAT()),而不视为OR
#ANSI_QUOTES:将‘”'视为识别符引号(‘`'引号字符),不要视为字符串的引号字符
sql_mode = NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES
#开启二进制日志功能,binlog数据位置
log-bin = /application/mysql/binlog/mysql-bin
#mysql生成的错误日志存放的路径
log-error = /application/mysql/logs/mysql_5_7_27.err
[root@192168066012_MySQL_5_7_27 ~]# cd /application/mysql/
[root@192168066012_MySQL_5_7_27 mysql]# ./bin/mysqld --initialize-insecure --user=mysql --basedir=/application/mysql --datadir=/application/mysql/data
2019-07-31T13:27:06.397226Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-07-31T13:27:06.813340Z 0 [Warning] InnoDB: New log files created, LSN=45790
2019-07-31T13:27:06.871853Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2019-07-31T13:27:06.929377Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: e45d3fac-b396-11e9-9e3e-000c290943d8.
2019-07-31T13:27:06.930628Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2019-07-31T13:27:06.931630Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
提示:
#生成初始化密码参数(5.7版本才有),必须要添加,否则会初始化失败,会生成空密码
--initialize-insecure
#设定初始化用户
--user=mysql
#设定mysql工作目录
--basedir=/application/mysql
#设定数据文件目录
--datadir=/application/mysql/data
[root@192168066012_MySQL_5_7_27 mysql]# cd /application/mysql/
[root@192168066012_MySQL_5_7_27 mysql]# ./bin/mysql_ssl_rsa_setup --initialize-insecure --user=mysql --basedir=/application/mysql --datadir=/application/mysql/data
Generating a 2048 bit RSA private key
............+++
.............................................+++
writing new private key to 'ca-key.pem'
-----
Generating a 2048 bit RSA private key
......+++
...................................+++
writing new private key to 'server-key.pem'
-----
Generating a 2048 bit RSA private key
.....................................................................................................+++
........................+++
writing new private key to 'client-key.pem'
-----
[root@192168066012_MySQL_5_7_27 ~]# cd /application/mysql/data/
[root@192168066012_MySQL_5_7_27 data]# chmod +r server-key.pem
[root@192168066012_MySQL_5_7_27 data]# ls -l server-key.pem
-rw-r--r-- 1 root root 1675 Jul 31 20:01 server-key.pem
提示:
如果不给文件server-key.pem其他用户和组可读权限的话,当启动数据库的时候,会报如下错误
2019-08-07T06:39:24.111526Z 0 [ERROR] SSL error: Unable to get private key from 'server-key.pem'
2019-08-07T06:39:24.111555Z 0 [Warning] Failed to set up SSL because of the following SSL library error: Unable to get private key
#拷贝启动脚本到/etc/init.d/目录下,并改名mysqld
[root@192168066012_MySQL_5_7_27 mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[root@192168066012_MySQL_5_7_27 mysql]# ls -l /etc/init.d/mysqld
-rwxr-xr-x 1 root root 10588 Aug 1 18:33 /etc/init.d/mysqld
#重新加载系统服务,将MySQL数据库加入开机自启动
[root@192168066012_MySQL_5_7_27 ~]# /bin/systemctl daemon-reload
[root@192168066012_MySQL_5_7_27 ~]# /bin/systemctl enable mysqld.service
mysqld.service is not a native service, redirecting to /sbin/chkconfig.
Executing /sbin/chkconfig mysqld on
#启动MySQL数据库,并检查端口监听状态
[root@192168066012_MySQL_5_7_27 ~]# /etc/init.d/mysqld start
Starting MySQL. SUCCESS!
[root@192168066012_MySQL_5_7_27 ~]# netstat -lntup
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 6549/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 7023/master
tcp6 0 0 :::3306 :::* LISTEN 10947/mysqld
tcp6 0 0 :::22 :::* LISTEN 6549/sshd
tcp6 0 0 ::1:25 :::* LISTEN 7023/master
提示:
默认CentOS 7默认没有netstat命令,而是使用ss命令代替;如果想使用netstat命令的话,需要执行yum install net-tools进行安装net-tools软件包,然后就可以使用了;
#删除全部用户,添加额外管理员,重新加载mysql授予表
mysql> select user,host from mysql.user;
+---------------+-----------+
| user | host |
+---------------+-----------+
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+---------------+-----------+
3 rows in set (0.00 sec)
mysql> grant all privileges on *.* to boyu@'localhost' identified by 'boyu123' with grant option;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> delete from mysql.user where user='mysql.session';
Query OK, 1 row affected (0.01 sec)
mysql> delete from mysql.user where user='mysql.sys';
Query OK, 1 row affected (0.00 sec)
mysql> delete from mysql.user where user='root';
Query OK, 1 row affected (0.00 sec)
mysql> select user,host from mysql.user;
+------+-----------+
| user | host |
+------+-----------+
| boyu | localhost |
+------+-----------+
1 row in set (0.00 sec)
#使用MySQL自带的脚本
[root@192168066012_MySQL_5_7_27 ~]# /etc/init.d/mysqld stop
Shutting down MySQL. SUCCESS!
#使用mysqladmin的方法
[root@192168066012_MySQL_5_7_27 mysql]# mysqladmin -uboyu -pboyu123 shutdown
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
#mysqladmin:[警告]在命令行界面上使用密码可能不安全
提示:
5.7版本优化了数据库的安全配置,建议配置SSL加密连接,默认在命令行输入的密码都是明文传输的,极为不安全;
#授权192.168.88.1对MySQL所有库和所有表都赋予boyu用户权限;
mysql> grant all privileges on *.* to boyu@'192.168.66.11' identified by 'boyu123' with grant option;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> select user,host from mysql.user;
+------+---------------+
| user | host |
+------+---------------+
| boyu | 192.168.66.11 |
| boyu | localhost |
+------+---------------+
2 rows in set (0.00 sec)
#测试远程登陆
[root@192168066011 ~]# mysql -h192.168.66.12 -uboyu -pboyu123 -P3306
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.27 Source distribution
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
625某电商网站数据库宕机故障解决实录(上)
625某电商网站数据库宕机故障解决实录(下)
提示:
不要野蛮粗鲁杀死数据库,生产高并发环境会导致数据丢失
#未指定boost库,MySQL 5.7版本使用cmake配置编译参数时,强依赖boost
解决办法:
- MySQL 5.7版本对应的boost库版本为:boost_1_59_0.tar.gz,本文已提供下载连接
- 将boost_1_59_0下载到本地,放在/usr/local/目录下
- 添加cmake参数“-DWITH_BOOST=/usr/local/boost”,指定boost目录位置
#找不到CURSES_LIBRARY
解决办法:
yum install ncurses-devel -y
#make过程,报内存不足错误
解决办法:
增加内存空间,或者swap空间