@zhangyy
2021-11-10T13:27:15.000000Z
字数 10583
阅读 147
MySQL系列
MySQL作为全球最受开发者欢迎的数据库,被广泛应用于各类场景中。数据MySQL 5.7版本已于近日正式发布,即日起,用户已可在各种云上在线创建MySQL 5.7版本的RDS。
与社区版5.6版本相比,5.7版本性能最大提升3倍,并且新增了众多新特性,其中包括:支持JSON、GIS地理空间数据库,更便捷的SSL安全访问配置,InnoDB 的多项功能增强等多项新特性,更简化了数据库开发流程。
1. 性能优化:
MySQL 5.7版本在性能提升方面做了很多改进,其中包括改进复制、重写了大部分解析器及优化器和成本模型以及临时表性能改进等。
2. 改进并行复制:
MySQL 5.6已经实现了库级别的并行复制,5.7版本中新增了slave_parallel_type参数,设置为LOGICAL_CLOCK时,开启基于组提交的并行复制,即一个组提交的事务可以实现并行回放,此外仍可通过slave_parallel_workers参数设置并行复制的工作进程数,并行复制在很大程度上解决了之前MySQL主从延迟的问题。
3. 优化器重构
MySQL 5.7重构了MySQL优化器,改进了解析器、优化器和成本模型,从而生成更合理的执行计划提高查询效率。
基于成本的优化
mysql系统库中新增server_cost和engine_cost表,存储成本估算相关参数,通过配置cost_value可以改变成本估算结果。
优化器Hints
MySQL 5.6通过设置optimizer_switch实现会话级的优化器策略调整,MySQL 5.7可以在语句上以类似注释的语法/*+ ... */加Hints改变优化器策略,优先级高于optimizer_switch设置,例如可以增加索引和join连接的Hints。
l 查询重写插件 在server端接收到SQL后,查询重写插件可以按照用户设置的规则对其进行改写并执行;对于某些难以通过变更业务代码来优化的SQL,通过查询重写添加Hints或改写来优化这些SQL。
4. 临时表改进
新增参数innodb_temp_data_file_path,可配置独立的临时表空间,降低I/O开销;
新增参数internal_tmp_disk_storage_engine,磁盘临时表可以使用InnoDB引擎,高并发时性能更好;
临时表元数据不再存储于InnoDB系统表,存储在了IS下的innodb_temp_table_info,减少了数据字典的锁竞争;
临时表及相关对象引入新的non-redo undo log,DML操作不记录redo,减少了大量的日志开销。
5. 安全性增强
MySQL 5.7被称为是MySQL 发布以来最安全的版本,因此这个版本做了很多提升安全性方面的改进:
启动时默认创建SSL,RSA certificate和key文件;
MySQL5.7开始安装完成,用户的密码空不能为空,不再支持mysql_old_password的认证插件,推荐使用mysql_native_password;
支持设置任意密码有效期,支持手动锁定/解锁账户;
支持安全模式安装数据库,逐步废弃mysql_install_db的安装方式使用 initialize代替;
支持表空间加密,可通过安装keyring_file插件来启用该功能。
灵活性更高
6. 支持JSON
随着非结构化数据存储需求的增长,MySQL 自5.7.8版本开始支持JSON,支持JSON后也使得MySQL兼具关系型数据库与非关系型数据库的优点,成为灵活性更高的数据库。
MySQL 5.7支持原生的JSON类型,使用binary格式进行存储,可以快速查询文档元素,且提供一系列原生函数和路径表达式高效处理JSON字符,不需要遍历全部数据,此外结合虚拟列的功能可以对JSON中的部分数据进行索引以提升查询性能。
7. GIS改进
在5.7版本之前,MySQL对地理空间函数支持度有限,性能一般,所以一直没有获得广泛应用。但是目前随着LBS服务的需求越来越普遍,MySQL对GIS进行了大规模重构和优化,并将这些改进整合到了5.7版本中。5.7版本对GIS的改进:
通过Boost Geometry库重构之前的地理空间数据的代码实现;
增加球面举例的计算函数ST_Distance_Sphere等多种通用地图函数;
InnoDB存储引擎原生支持地理空间数据类型;
InnoDB存储引擎新增R树索引支持地理空间查询;
8.更具易用性
为了增加MySQL的易用性,以达到加速网络和大数据加载等操作进程,5.7版本改进了InnoDB 的可扩展性和临时表的性能。同时,MySQL5.7新增了SYS Schema系统库,可以直接查询目标信息而无需借助外部工具。而Performance Schema新增了35张表及大量的监控项,丰富了信息源。此外,复制方面做的一些降低复制维护难度和简化步骤的改进也极大地提升了MySQL5.7的易用性。
系统: CentOS7.9x64
主机名:
cat /etc/hosts
------
192.168.100.11 node01.flyfish.com
192.168.100.12 node02.flyfish.com
192.168.100.13 node03.flyfish.com
192.168.100.14 node04.flyfish.com
192.168.100.15 node05.flyfish.com
192.168.100.16 node06.flyfish.com
192.168.100.17 node07.flyfish.com
192.168.100.18 node08.flyfish.com
192.168.100.19 node09.flyfish.com
-------
本次只安装第一台 单机版本编译安装MySQL 5.7.x
1.系统磁盘:
测试环境系统:
----
OS FS: root 分区 50g
分两个区:
/boot 512M
/ 剩下全部
swap 16G
MySQL 分区:
softvg:30 G
appvg:51 G
datavg:52 G
logvg:53 G
bakvg:54 G
-----
--OS:
采用逻辑分区方式方便扩容
/dev/sdb
/dev/sdc
/dev/sdd
/dev/sde
/dev/sdf
pvcreate /dev/sdb /dev/sdc /dev/sdd /dev/sde /dev/sdf pvs
vgcreate appvg /dev/sdb
vgcreate datavg /dev/sdc
vgcreate logvg /dev/sdd
vgcreate bakvg /dev/sde
vgcreate softvg /dev/sdf
vgdisplay
vgs
lvcreate -n softlv -L 30000M softvg
lvcreate -n mysqllv -L 50000M appvg
lvcreate -n datalv -L 50000M datavg
lvcreate -n loglv -L 50000M logvg
lvcreate -n baklv -L 49998M bakvg
lvdisplay
lvs
mkfs.xfs /dev/softvg/softlv
mkfs.xfs /dev/appvg/mysqllv
mkfs.xfs /dev/datavg/datalv
mkfs.xfs /dev/logvg/loglv
mkfs.xfs /dev/bakvg/baklv
mkdir -p /mysql/app
mkdir -p /mysql/data
mkdir -p /mysql/log
mkdir -p /mysql/backup
mkdir -p /soft
echo "/dev/appvg/mysqllv /mysql/app xfs defaults 0 0" >> /etc/fstab
echo "/dev/datavg/datalv /mysql/data xfs defaults 0 0" >> /etc/fstab
echo "/dev/logvg/loglv /mysql/log xfs defaults 0 0" >> /etc/fstab
echo "/dev/bakvg/baklv /mysql/backup xfs defaults 0 0" >> /etc/fstab
echo "/dev/softvg/softlv /soft xfs defaults 0 0" >> /etc/fstab
cat /etc/fstab
mount /mysql/app
mount /mysql/data
mount /mysql/log
mount /mysql/backup
mount /soft
cat /etc/hosts
echo "export LANG=en_US.UTF8" >> ~/.bash_profile
echo "export LANG=en_US.UTF8" >> /etc/profile
cat ~/.bash_profile
source ~/.bash_profile
mount /dev/cdrom /mnt
cd /etc/yum.repos.d
mkdir bk
mv public-yum-ol7.repo bk/
echo "[EL7]" >> itpux.repo
echo "name = linux 7.5 dvd" >> itpux.repo
echo "baseurl=file:///mnt" >> itpux.repo
echo "gpgcheck=0" >> itpux.repo
echo "enabled=1" >> itpux.repo
systemctl set-default multi-user.target
systemctl get-default
echo "* soft memlock 300000" >> /etc/security/limits.conf
echo "* hard memlock 300000" >> /etc/security/limits.conf
echo "vm.nr_hugepages=150" >> /etc/sysctl.conf
echo "vm.swappiness=10" >> /etc/sysctl.conf
echo "vm.min_free_kbytes=51200" >> /etc/sysctl.conf
echo "#vm.min_free_kbytes=512000" >> /etc/sysctl.conf
sysctl -p
grep "net.ipv4.tcp_keepalive_time = 30" /etc/sysctl.conf
if [ $? != 0 ]
then
cat <<EOF>> /etc/sysctl.conf
net.ipv4.tcp_max_tw_buckets = 6000
net.ipv4.ip_local_port_range = 1024 65000
net.ipv4.tcp_tw_recycle = 0
net.ipv4.tcp_tw_reuse = 1
net.core.somaxconn = 262144
net.core.netdev_max_backlog = 262144
net.ipv4.tcp_max_orphans = 262144
net.ipv4.tcp_max_syn_backlog = 262144
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_fin_timeout = 1
net.ipv4.tcp_keepalive_time = 30
net.ipv4.tcp_keepalive_probes = 6
net.ipv4.tcp_keepalive_intvl = 5
net.ipv4.tcp_timestamps = 0
EOF
sed -i 's/net.bridge.bridge-nf-call-ip6tables =0/#net.bridge.bridge-nf-call-ip6tables = 0/g' /etc/sysctl.conf
sed -i 's/net.bridge.bridge-nf-call-iptables = 0/#net.bridge.bridge-nf-call-iptables = 0/g' /etc/sysctl.conf
sed -i 's/net.bridge.bridge-nf-call-arptables = 0/#net.bridge.bridge-nf-call-arptables = 0/g' /etc/sysctl.conf
fi
sysctl -p
echo "* - nproc 65535" > /etc/security/limits.d/90-nproc.conf
echo "SELINUX=disable" >/etc/selinux/config
echo "#SELINUXTYPE=trageted" >> /etc/selinux/config
cat /etc/selinux/config
setenforce 0
systemctl status firewalld.service
systemctl stop firewalld.service
systemctl disable firewalld.service
echo "if test -f /sys/kernel/mm/transparent_hugepage/enabled" then >> /etc/rc.d/rc.local
echo "echo never > /sys/kernel/mm/transparent_hugepage/enabled" >> /etc/rc.d/rc.local
echo "fi">> /etc/rc.d/rc.local
echo "if test -f /sys/kernel/mm/transparent_hugepage/defrag; then" >> /etc/rc.d/rc.local
echo "echo never > /sys/kernel/mm/transparent_hugepage/defrag">> /etc/rc.d/rc.local
echo "fi">> /etc/rc.d/rc.local
chmod +x /etc/rc.d/rc.local
echo "session required pam_limits.so" >> /etc/pam.d/login
cat /etc/pam.d/login
echo "* soft nproc 65535" >> /etc/security/limits.conf
echo "* hard nproc 65535" >> /etc/security/limits.conf
echo "* soft nofile 65535" >> /etc/security/limits.conf
echo "* hard nofile 65535" >> /etc/security/limits.conf
echo "* soft stack 65535" >> /etc/security/limits.conf
echo "* hard stack 65535" >> /etc/security/limits.conf
cat /etc/security/limits.conf
systemctl stop avahi-daemon.socket avahi-daemon.service
systemctl disable avahi-daemon.socket avahi-daemon.service
cat /sys/block/sd*/queue/scheduler
#--sas/stat
echo 'deadline' > /sys/block/sdb/queue/scheduler
echo 'deadline' > /sys/block/sdc/queue/scheduler
echo 'deadline' > /sys/block/sdd/queue/scheduler
echo 'deadline' > /sys/block/sde/queue/scheduler
echo 'deadline' > /sys/block/sdf/queue/scheduler
#--ssd
echo 'NOOP' > /sys/block/sd*/queue/scheduler
--开启后生效(下面两部分)
vi /etc/rc.local
#减少预读
echo '16' > /sys/block/sdb/queue/read_ahead_kb
echo '16' > /sys/block/sdc/queue/read_ahead_kb
echo '16' > /sys/block/sdd/queue/read_ahead_kb
echo '16' > /sys/block/sde/queue/read_ahead_kb
echo '16' > /sys/block/sdf/queue/read_ahead_kb
#增大队列
echo '512' > /sys/block/sdb/queue/nr_requests
echo '512' > /sys/block/sdc/queue/nr_requests
echo '512' > /sys/block/sdd/queue/nr_requests
echo '512' > /sys/block/sde/queue/nr_requests
echo '512' > /sys/block/sdf/queue/nr_requests
cp -f /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
hwclock
禁用 NUMA:
vi /etc/default/grub
GRUB_CMDLINE_LINUX="crashkernel=auto rhgb quiet numa=off"
然后执行:
grub2-mkconfig -o /etc/grub2.cfg
numastat
numactl --show
numactl --hardware
shutdown -r now
克隆虚拟机:
rm -f /etc/udev/rules.d/70-persistent-net.rules
hostname itpuxdb01
echo "itpuxdb01" > /etc/hostname
vi /etc/sysconfig/network-scripts/ifcfg-ens34 改 IP、删 UUID
reboot
上传软件到的机器
# install the basie lib
mount /dev/cdrom /mnt
yum remove mariadb* -y
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
groupadd mysql
useradd -r -g mysql -s /bin/false mysql
cd /soft
tar xvf /soft/mysql-5.7.36-linux-glibc2.12-x86_64.tar -C /mysql/app/
cd /mysql/app/
tar zxvf /soft/mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz
mv /mysql/app/mysql-5.7.36-linux-glibc2.12-x86_64 mysql
echo "export PATH=\$PATH:$HOME/bin:/mysql/app/mysql/bin">> /etc/profile
source /etc/profile
# add mysql account,create the basic directory
mkdir -p /mysql/data/3306/data
mkdir -p /mysql/log/3306/binlog
mkdir -p /mysql/log/3306/relaylog
mkdir -p /mysql/backup/backup-db
mkdir -p /mysql/backup/backup-tmp
mkdir -p /mysql/backup/backup-binlog
chown -R mysql:mysql /mysql/
# init databases -- my.cnf 每台机都不同
rm -rf /mysql/data/3306/data/
cp /soft/my.cnf /mysql/data/3306/
/mysql/app/mysql/bin/mysqld --defaults-file=/mysql/data/3306/my.cnf --initialize --user=mysql --basedir=/mysql/app/mysql --datadir=/mysql/data/3306/data
ln -sf /mysql/data/3306/mysql.sock /tmp/mysql.sock
# remove default my.cnf
rm -f /etc/my.cnf
rm -rf /etc/my.cnf.d
# set the default password
/mysql/app/mysql/bin/mysqld_safe --defaults-file=/mysql/data/3306/my.cnf --skip-grant-tables --skip-networking &
/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;"
/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';"
/mysql/app/mysql/bin/mysql -uroot --password='root' --socket='/mysql/data/3306/mysql.sock' -e "create USER 'root'@'%' IDENTIFIED BY 'root';"
/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;"
/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;"
echo "/mysql/app/mysql/bin/mysqld_safe --defaults-file=/mysql/data/3306/my.cnf --user=mysql &" > /mysql/data/3306/mysql.start
chown mysql:mysql /mysql/data/3306/mysql.start
chmod +x /mysql/data/3306/mysql.start
修改 mysql.server 文件
cd /mysql/app/mysql/support-files/
mv mysql.server mysql.server.bak
cp /soft/mysql.server /mysql/app/mysql/support-files/
chown mysql:mysql /mysql/app/mysql/support-files/mysql.server
chmod +x /mysql/app/mysql/support-files/mysql.server
cp /mysql/app/mysql/support-files/mysql.server /mysql/app/mysql/bin/mysqlservice
mysqlservice status
将脚本和安装软件 scp 到 N 台服务器,然后 ssh ip 地址 " sh /soft/init_linux.sh; sh /soft/install_mysql.sh;",
用后台批量进程来安装。
---配置启动脚本-
第 1 步: # set the auto start on linux server started
cp /soft/mysqld.service /usr/lib/systemd/system/
---配置启动脚本-第 2 步(开机自动启动):
systemctl daemon-reload
systemctl stop mysqld
systemctl start mysqld
systemctl enable mysqld
systemctl status mysqld
-- 登录测试
mysql -uroot -proot -e "show databases"