[关闭]
@zhangyy 2021-11-10T13:27:15.000000Z 字数 10583 阅读 147

MySQL 5.7.X 二进制编译安装

MySQL系列


一:MySQL 介绍

1.1:MySQL5.7 简介:

  1. MySQL作为全球最受开发者欢迎的数据库,被广泛应用于各类场景中。数据MySQL 5.7版本已于近日正式发布,即日起,用户已可在各种云上在线创建MySQL 5.7版本的RDS
  2. 与社区版5.6版本相比,5.7版本性能最大提升3倍,并且新增了众多新特性,其中包括:支持JSONGIS地理空间数据库,更便捷的SSL安全访问配置,InnoDB 的多项功能增强等多项新特性,更简化了数据库开发流程。

1.2: mysql 5.7 版本

  1. 1. 性能优化:
  2. MySQL 5.7版本在性能提升方面做了很多改进,其中包括改进复制、重写了大部分解析器及优化器和成本模型以及临时表性能改进等。
  3. 2. 改进并行复制:
  4. MySQL 5.6已经实现了库级别的并行复制,5.7版本中新增了slave_parallel_type参数,设置为LOGICAL_CLOCK时,开启基于组提交的并行复制,即一个组提交的事务可以实现并行回放,此外仍可通过slave_parallel_workers参数设置并行复制的工作进程数,并行复制在很大程度上解决了之前MySQL主从延迟的问题。
  5. 3. 优化器重构
  6. MySQL 5.7重构了MySQL优化器,改进了解析器、优化器和成本模型,从而生成更合理的执行计划提高查询效率。
  7. 基于成本的优化
  8. mysql系统库中新增server_costengine_cost表,存储成本估算相关参数,通过配置cost_value可以改变成本估算结果。
  9. 优化器Hints
  10. MySQL 5.6通过设置optimizer_switch实现会话级的优化器策略调整,MySQL 5.7可以在语句上以类似注释的语法/*+ ... */Hints改变优化器策略,优先级高于optimizer_switch设置,例如可以增加索引和join连接的Hints
  11. l 查询重写插件 server端接收到SQL后,查询重写插件可以按照用户设置的规则对其进行改写并执行;对于某些难以通过变更业务代码来优化的SQL,通过查询重写添加Hints或改写来优化这些SQL
  12. 4. 临时表改进
  13. 新增参数innodb_temp_data_file_path,可配置独立的临时表空间,降低I/O开销;
  14. 新增参数internal_tmp_disk_storage_engine,磁盘临时表可以使用InnoDB引擎,高并发时性能更好;
  15. 临时表元数据不再存储于InnoDB系统表,存储在了IS下的innodb_temp_table_info,减少了数据字典的锁竞争;
  16. 临时表及相关对象引入新的non-redo undo logDML操作不记录redo,减少了大量的日志开销。
  17. 5. 安全性增强
  18. MySQL 5.7被称为是MySQL 发布以来最安全的版本,因此这个版本做了很多提升安全性方面的改进:
  19. 启动时默认创建SSLRSA certificatekey文件;
  20. MySQL5.7开始安装完成,用户的密码空不能为空,不再支持mysql_old_password的认证插件,推荐使用mysql_native_password
  21. 支持设置任意密码有效期,支持手动锁定/解锁账户;
  22. 支持安全模式安装数据库,逐步废弃mysql_install_db的安装方式使用 initialize代替;
  23. 支持表空间加密,可通过安装keyring_file插件来启用该功能。
  24. 灵活性更高
  25. 6. 支持JSON
  26. 随着非结构化数据存储需求的增长,MySQL 5.7.8版本开始支持JSON,支持JSON后也使得MySQL兼具关系型数据库与非关系型数据库的优点,成为灵活性更高的数据库。
  27. MySQL 5.7支持原生的JSON类型,使用binary格式进行存储,可以快速查询文档元素,且提供一系列原生函数和路径表达式高效处理JSON字符,不需要遍历全部数据,此外结合虚拟列的功能可以对JSON中的部分数据进行索引以提升查询性能。
  28. 7. GIS改进
  29. 5.7版本之前,MySQL对地理空间函数支持度有限,性能一般,所以一直没有获得广泛应用。但是目前随着LBS服务的需求越来越普遍,MySQLGIS进行了大规模重构和优化,并将这些改进整合到了5.7版本中。5.7版本对GIS的改进:
  30. 通过Boost Geometry库重构之前的地理空间数据的代码实现;
  31. 增加球面举例的计算函数ST_Distance_Sphere等多种通用地图函数;
  32. InnoDB存储引擎原生支持地理空间数据类型;
  33. InnoDB存储引擎新增R树索引支持地理空间查询;
  34. 8.更具易用性
  35. 为了增加MySQL的易用性,以达到加速网络和大数据加载等操作进程,5.7版本改进了InnoDB 的可扩展性和临时表的性能。同时,MySQL5.7新增了SYS Schema系统库,可以直接查询目标信息而无需借助外部工具。而Performance Schema新增了35张表及大量的监控项,丰富了信息源。此外,复制方面做的一些降低复制维护难度和简化步骤的改进也极大地提升了MySQL5.7的易用性。

二: MySQL 5.7.x 编译安装

2.1 系统环境介绍

  1. 系统: CentOS7.9x64
  2. 主机名:
  3. cat /etc/hosts
  4. ------
  5. 192.168.100.11 node01.flyfish.com
  6. 192.168.100.12 node02.flyfish.com
  7. 192.168.100.13 node03.flyfish.com
  8. 192.168.100.14 node04.flyfish.com
  9. 192.168.100.15 node05.flyfish.com
  10. 192.168.100.16 node06.flyfish.com
  11. 192.168.100.17 node07.flyfish.com
  12. 192.168.100.18 node08.flyfish.com
  13. 192.168.100.19 node09.flyfish.com
  14. -------
  15. 本次只安装第一台 单机版本编译安装MySQL 5.7.x

2.2 系统环境初始化

  1. 1.系统磁盘:
  2. 测试环境系统:
  3. ----
  4. OS FS: root 分区 50g
  5. 分两个区:
  6. /boot 512M
  7. / 剩下全部
  8. swap 16G
  9. MySQL 分区:
  10. softvg30 G
  11. appvg51 G
  12. datavg52 G
  13. logvg53 G
  14. bakvg54 G
  15. -----
  16. --OS
  17. 采用逻辑分区方式方便扩容
  18. /dev/sdb
  19. /dev/sdc
  20. /dev/sdd
  21. /dev/sde
  22. /dev/sdf
  23. pvcreate /dev/sdb /dev/sdc /dev/sdd /dev/sde /dev/sdf pvs
  24. vgcreate appvg /dev/sdb
  25. vgcreate datavg /dev/sdc
  26. vgcreate logvg /dev/sdd
  27. vgcreate bakvg /dev/sde
  28. vgcreate softvg /dev/sdf
  29. vgdisplay
  30. vgs
  31. lvcreate -n softlv -L 30000M softvg
  32. lvcreate -n mysqllv -L 50000M appvg
  33. lvcreate -n datalv -L 50000M datavg
  34. lvcreate -n loglv -L 50000M logvg
  35. lvcreate -n baklv -L 49998M bakvg
  36. lvdisplay
  37. lvs
  38. mkfs.xfs /dev/softvg/softlv
  39. mkfs.xfs /dev/appvg/mysqllv
  40. mkfs.xfs /dev/datavg/datalv
  41. mkfs.xfs /dev/logvg/loglv
  42. mkfs.xfs /dev/bakvg/baklv
  43. mkdir -p /mysql/app
  44. mkdir -p /mysql/data
  45. mkdir -p /mysql/log
  46. mkdir -p /mysql/backup
  47. mkdir -p /soft
  48. echo "/dev/appvg/mysqllv /mysql/app xfs defaults 0 0" >> /etc/fstab
  49. echo "/dev/datavg/datalv /mysql/data xfs defaults 0 0" >> /etc/fstab
  50. echo "/dev/logvg/loglv /mysql/log xfs defaults 0 0" >> /etc/fstab
  51. echo "/dev/bakvg/baklv /mysql/backup xfs defaults 0 0" >> /etc/fstab
  52. echo "/dev/softvg/softlv /soft xfs defaults 0 0" >> /etc/fstab
  53. cat /etc/fstab
  54. mount /mysql/app
  55. mount /mysql/data
  56. mount /mysql/log
  57. mount /mysql/backup
  58. mount /soft
  59. cat /etc/hosts
  60. echo "export LANG=en_US.UTF8" >> ~/.bash_profile
  61. echo "export LANG=en_US.UTF8" >> /etc/profile
  62. cat ~/.bash_profile
  63. source ~/.bash_profile
  64. mount /dev/cdrom /mnt
  65. cd /etc/yum.repos.d
  66. mkdir bk
  67. mv public-yum-ol7.repo bk/
  68. echo "[EL7]" >> itpux.repo
  69. echo "name = linux 7.5 dvd" >> itpux.repo
  70. echo "baseurl=file:///mnt" >> itpux.repo
  71. echo "gpgcheck=0" >> itpux.repo
  72. echo "enabled=1" >> itpux.repo
  73. systemctl set-default multi-user.target
  74. systemctl get-default
  75. echo "* soft memlock 300000" >> /etc/security/limits.conf
  76. echo "* hard memlock 300000" >> /etc/security/limits.conf
  77. echo "vm.nr_hugepages=150" >> /etc/sysctl.conf
  78. echo "vm.swappiness=10" >> /etc/sysctl.conf
  79. echo "vm.min_free_kbytes=51200" >> /etc/sysctl.conf
  80. echo "#vm.min_free_kbytes=512000" >> /etc/sysctl.conf
  81. sysctl -p
  82. grep "net.ipv4.tcp_keepalive_time = 30" /etc/sysctl.conf
  83. if [ $? != 0 ]
  84. then
  85. cat <<EOF>> /etc/sysctl.conf
  86. net.ipv4.tcp_max_tw_buckets = 6000
  87. net.ipv4.ip_local_port_range = 1024 65000
  88. net.ipv4.tcp_tw_recycle = 0
  89. net.ipv4.tcp_tw_reuse = 1
  90. net.core.somaxconn = 262144
  91. net.core.netdev_max_backlog = 262144
  92. net.ipv4.tcp_max_orphans = 262144
  93. net.ipv4.tcp_max_syn_backlog = 262144
  94. net.ipv4.tcp_synack_retries = 2
  95. net.ipv4.tcp_syn_retries = 1
  96. net.ipv4.tcp_fin_timeout = 1
  97. net.ipv4.tcp_keepalive_time = 30
  98. net.ipv4.tcp_keepalive_probes = 6
  99. net.ipv4.tcp_keepalive_intvl = 5
  100. net.ipv4.tcp_timestamps = 0
  101. EOF
  102. sed -i 's/net.bridge.bridge-nf-call-ip6tables =0/#net.bridge.bridge-nf-call-ip6tables = 0/g' /etc/sysctl.conf
  103. sed -i 's/net.bridge.bridge-nf-call-iptables = 0/#net.bridge.bridge-nf-call-iptables = 0/g' /etc/sysctl.conf
  104. sed -i 's/net.bridge.bridge-nf-call-arptables = 0/#net.bridge.bridge-nf-call-arptables = 0/g' /etc/sysctl.conf
  105. fi
  106. sysctl -p
  107. echo "* - nproc 65535" > /etc/security/limits.d/90-nproc.conf
  108. echo "SELINUX=disable" >/etc/selinux/config
  109. echo "#SELINUXTYPE=trageted" >> /etc/selinux/config
  110. cat /etc/selinux/config
  111. setenforce 0
  112. systemctl status firewalld.service
  113. systemctl stop firewalld.service
  114. systemctl disable firewalld.service
  115. echo "if test -f /sys/kernel/mm/transparent_hugepage/enabled" then >> /etc/rc.d/rc.local
  116. echo "echo never > /sys/kernel/mm/transparent_hugepage/enabled" >> /etc/rc.d/rc.local
  117. echo "fi">> /etc/rc.d/rc.local
  118. echo "if test -f /sys/kernel/mm/transparent_hugepage/defrag; then" >> /etc/rc.d/rc.local
  119. echo "echo never > /sys/kernel/mm/transparent_hugepage/defrag">> /etc/rc.d/rc.local
  120. echo "fi">> /etc/rc.d/rc.local
  121. chmod +x /etc/rc.d/rc.local
  122. echo "session required pam_limits.so" >> /etc/pam.d/login
  123. cat /etc/pam.d/login
  124. echo "* soft nproc 65535" >> /etc/security/limits.conf
  125. echo "* hard nproc 65535" >> /etc/security/limits.conf
  126. echo "* soft nofile 65535" >> /etc/security/limits.conf
  127. echo "* hard nofile 65535" >> /etc/security/limits.conf
  128. echo "* soft stack 65535" >> /etc/security/limits.conf
  129. echo "* hard stack 65535" >> /etc/security/limits.conf
  130. cat /etc/security/limits.conf
  131. systemctl stop avahi-daemon.socket avahi-daemon.service
  132. systemctl disable avahi-daemon.socket avahi-daemon.service
  133. cat /sys/block/sd*/queue/scheduler
  134. #--sas/stat
  135. echo 'deadline' > /sys/block/sdb/queue/scheduler
  136. echo 'deadline' > /sys/block/sdc/queue/scheduler
  137. echo 'deadline' > /sys/block/sdd/queue/scheduler
  138. echo 'deadline' > /sys/block/sde/queue/scheduler
  139. echo 'deadline' > /sys/block/sdf/queue/scheduler
  140. #--ssd
  141. echo 'NOOP' > /sys/block/sd*/queue/scheduler
  142. --开启后生效(下面两部分)
  143. vi /etc/rc.local
  144. #减少预读
  145. echo '16' > /sys/block/sdb/queue/read_ahead_kb
  146. echo '16' > /sys/block/sdc/queue/read_ahead_kb
  147. echo '16' > /sys/block/sdd/queue/read_ahead_kb
  148. echo '16' > /sys/block/sde/queue/read_ahead_kb
  149. echo '16' > /sys/block/sdf/queue/read_ahead_kb
  150. #增大队列
  151. echo '512' > /sys/block/sdb/queue/nr_requests
  152. echo '512' > /sys/block/sdc/queue/nr_requests
  153. echo '512' > /sys/block/sdd/queue/nr_requests
  154. echo '512' > /sys/block/sde/queue/nr_requests
  155. echo '512' > /sys/block/sdf/queue/nr_requests
  156. cp -f /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
  157. hwclock
  158. 禁用 NUMA:
  159. vi /etc/default/grub
  160. GRUB_CMDLINE_LINUX="crashkernel=auto rhgb quiet numa=off"
  161. 然后执行:
  162. grub2-mkconfig -o /etc/grub2.cfg
  163. numastat
  164. numactl --show
  165. numactl --hardware
  166. shutdown -r now
  167. 克隆虚拟机:
  168. rm -f /etc/udev/rules.d/70-persistent-net.rules
  169. hostname itpuxdb01
  170. echo "itpuxdb01" > /etc/hostname
  171. vi /etc/sysconfig/network-scripts/ifcfg-ens34 IP、删 UUID
  172. reboot

2.3 安装编译MySQL

2.3.1 系统依赖包:

  1. 上传软件到的机器
  2. # install the basie lib
  3. mount /dev/cdrom /mnt
  4. yum remove mariadb* -y
  5. yum install gcc gcc-c++ perl-DBD-MySQL ncurses-devel.x86_64 cmake.x86_64 libaio.x86_64 bison.x86_64 gcc-c++.x86_64 bind-utils wget curl curl-devel perl openssh-clients setuptool sysstat make redhat-lsb* lrzsz

image_1fio3t8kf1lupm6r14f91hnt7a89.png-215.8kB

2.3.2 安装MySQL

  1. groupadd mysql
  2. useradd -r -g mysql -s /bin/false mysql
  3. cd /soft
  4. tar xvf /soft/mysql-5.7.36-linux-glibc2.12-x86_64.tar -C /mysql/app/
  5. cd /mysql/app/
  6. tar zxvf /soft/mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz
  7. mv /mysql/app/mysql-5.7.36-linux-glibc2.12-x86_64 mysql
  8. echo "export PATH=\$PATH:$HOME/bin:/mysql/app/mysql/bin">> /etc/profile
  9. source /etc/profile

image_1fio53ga2114411pb1unekf11udm2l.png-64.6kB

  1. # add mysql account,create the basic directory
  2. mkdir -p /mysql/data/3306/data
  3. mkdir -p /mysql/log/3306/binlog
  4. mkdir -p /mysql/log/3306/relaylog
  5. mkdir -p /mysql/backup/backup-db
  6. mkdir -p /mysql/backup/backup-tmp
  7. mkdir -p /mysql/backup/backup-binlog
  8. chown -R mysql:mysql /mysql/

image_1fio5b7cf2h4po11kd515da15p59.png-185.7kB

  1. # init databases -- my.cnf 每台机都不同
  2. rm -rf /mysql/data/3306/data/
  3. cp /soft/my.cnf /mysql/data/3306/
  4. /mysql/app/mysql/bin/mysqld --defaults-file=/mysql/data/3306/my.cnf --initialize --user=mysql --basedir=/mysql/app/mysql --datadir=/mysql/data/3306/data
  5. ln -sf /mysql/data/3306/mysql.sock /tmp/mysql.sock
  6. # remove default my.cnf
  7. rm -f /etc/my.cnf
  8. rm -rf /etc/my.cnf.d
  9. # set the default password
  10. /mysql/app/mysql/bin/mysqld_safe --defaults-file=/mysql/data/3306/my.cnf --skip-grant-tables --skip-networking &
  11. /mysql/app/mysql/bin/mysql -uroot --password='' --socket='/mysql/data/3306/mysql.sock' -e " update mysql.user set authentication_string=password('root') where user='root' and Host = 'localhost'; flush privileges;"
  12. /mysql/app/mysql/bin/mysql -uroot --password='root' --connect-expired-password --socket='/mysql/data/3306/mysql.sock' -e " alter user 'root'@'localhost' identified by 'root';"
  13. /mysql/app/mysql/bin/mysql -uroot --password='root' --socket='/mysql/data/3306/mysql.sock' -e "create USER 'root'@'%' IDENTIFIED BY 'root';"
  14. /mysql/app/mysql/bin/mysql -uroot --password='root' --socket='/mysql/data/3306/mysql.sock' -e "grant all privileges on *.* to 'root'@'%' identified by 'root' with grant option;flush privileges;"
  15. /mysql/app/mysql/bin/mysql -uroot --password='root' --socket='/mysql/data/3306/mysql.sock' -e " create database t; use t;create table t1 select 1 as a; select * from t1;drop database t;"
  16. echo "/mysql/app/mysql/bin/mysqld_safe --defaults-file=/mysql/data/3306/my.cnf --user=mysql &" > /mysql/data/3306/mysql.start
  17. chown mysql:mysql /mysql/data/3306/mysql.start
  18. chmod +x /mysql/data/3306/mysql.start

  1. 修改 mysql.server 文件
  2. cd /mysql/app/mysql/support-files/
  3. mv mysql.server mysql.server.bak
  4. cp /soft/mysql.server /mysql/app/mysql/support-files/
  5. chown mysql:mysql /mysql/app/mysql/support-files/mysql.server
  6. chmod +x /mysql/app/mysql/support-files/mysql.server
  7. cp /mysql/app/mysql/support-files/mysql.server /mysql/app/mysql/bin/mysqlservice
  8. mysqlservice status

image_1fioc5jj9152sh3l148m1d5m1m4b5m.png-56.5kB


  1. 将脚本和安装软件 scp N 台服务器,然后 ssh ip 地址 " sh /soft/init_linux.sh; sh /soft/install_mysql.sh;"
  2. 用后台批量进程来安装。
  3. ---配置启动脚本-
  4. 1 步: # set the auto start on linux server started
  5. cp /soft/mysqld.service /usr/lib/systemd/system/
  6. ---配置启动脚本-第 2 步(开机自动启动):
  7. systemctl daemon-reload
  8. systemctl stop mysqld
  9. systemctl start mysqld
  10. systemctl enable mysqld
  11. systemctl status mysqld
  12. -- 登录测试
  13. mysql -uroot -proot -e "show databases"

image_1fiolj6vofpfi4qnse1tnsq7u63.png-70.9kB

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注