一、实验环境介绍
cpu:16核心
内存:64G
操作系统:centos7.8
mysql版本:mysql8.0
ip地址规划:10.99.19.74; 10.99.19.75; 10.99.19.76
配置主机名及解析
二、安装mysql
https://blog.csdn.net/zyj81092211/article/details/107489793
更改root用户主机名为%
mysql> update mysql.user set host = '%' where user = 'root';
创建集群管理用户
mysql> CREATE USER 'icadmin'@'%' IDENTIFIED WITH mysql_native_password BY '1qazXSW@';
mysql> GRANT all on *.* to 'icadmin'@'%' with grant option;
mysql> GRANT REPLICATION SLAVE ON *.* to 'icadmin'@'%';
mysql> FLUSH PRIVILEGES;
三、安装mysql-shell(74上进行)
yum install mysql-shell
四、配置集群
1、检查mysql实例 是否符合集群要求
mysqlsh ##进入mysql-shell
MySQL JS > dba.checkInstanceConfiguration('icadmin@ic1:3306')
MySQL JS > dba.checkInstanceConfiguration('icadmin@ic2:3306')
MySQL JS > dba.checkInstanceConfiguration('icadmin@ic3:3306')
2、登录到具体实例修改相应配置(三个实例都要操作)
mysqlsh --uri [email protected]:3306
执行自动修复(MySQL 8.0.11版本之后可以自动持久化)
MySQL 10.99.19.74:3306 ssl JS > dba.configureLocalInstance()
mysqlsh --uri [email protected]:3306
创建集群第一个节点
MySQL 10.99.19.74:3306 ssl JS > var cluster = dba.createCluster('testCluster')
从新获取集群
mysql-js> var cluster=dba.getCluster()
添加节点
MySQL ic1:3306 ssl JS > cluster.addInstance('icadmin@ic2:3306')
MySQL ic1:3306 ssl JS > cluster.addInstance('icadmin@ic3:3306')
MySQL ic1:3306 ssl JS > cluster.status()
重启集群
mysqlsh
\connect icadmin@ic1
var cluster = dba.rebootClusterFromCompleteOutage();
查看集群状态
var cluster = dba.getCluster()
cluster.status()
宕机测试记录
所有节点全部关闭 执行systemctl stop mysqld
开始恢复
a、启动数据库
systemctl start mysqld
b、连接至ic1
mysqlsh icadmin@ic1
c、尝试恢复集群
var cluster = dba.rebootClusterFromCompleteOutage();
SELECT clusters.cluster_id,clusters.cluster_name from mysql_innodb_cluster_metadata.clusters;
var cluster = dba.rebootClusterFromCompleteOutage(‘testCluster’);
还是这个错误 直接恢复不行
e、产看组复制情况
SELECT * FROM performance_schema.replication_group_members;
发现全部节点 都不在线
f、准备找到主节点 先恢复主节点
找到 super_read_only=off 的节点就是最后的主节点
mysql> show global variables like "%read_only%";
发现super_read_only都是on
g、选择其中一节点升级为主节点(这里可能丢失数据,暂时无法验证)
mysql> SET GLOBAL group_replication_bootstrap_group=ON;
mysql> START GROUP_REPLICATION;
mysql> SET GLOBAL group_replication_bootstrap_group=OFF;
h、mysqlsh连接至刚才启动的主节点并查看集群状态
mysqlsh icadmin@ic2
MySQL ic2:3306 ssl JS > var cluster=dba.getCluster()
MySQL ic2:3306 ssl JS > cluster.status()
MySQL ic2:3306 ssl JS > cluster.rejoinInstance("icadmin@ic1:3306")
重新加入不成功,提示有些事务日志不属于集群,让把节点剔除集群,再从新加入,并选择clone
MySQL ic2:3306 ssl JS > cluster.removeInstance("icadmin@ic1:3306")
MySQL ic2:3306 ssl JS > cluster.addInstance("icadmin@ic1")
ic1和ic3全部重复这个部动作后,集群恢复正常
常用维护命令参考https://www.jianshu.com/p/e947f0969e9f
五、配置使用mysql-router
1、安装mysql-router
yum install mysql-router -y
2、初始化mysql-router
mysqlrouter --bootstrap icadmin@ic1:3306 --user=mysqlrouter
systemctl start mysqlrouter
systemctl enable mysqlrouter
4、通过mysql-router连接数据库
mysql -h ic1 -uroot -p1qazXSW@ -P 6446
六、集群模式切换
更改为单主
SELECT group_replication_switch_to_single_primary_mode();
更改为多主
SELECT group_replication_switch_to_multi_primary_mode();
查看切换期间任务执行情况
SELECT event_name, work_completed, work_estimated FROM performance_schema.events_stages_current WHERE event_name LIKE "%stage/group_rpl%";
七、测试mysql性能
脚本如下
#!/bin/bash
function log_info(){
local msg=$1
echo -e "\033[1;32;40m[INFO]\033[0m [`date +'%Y-%m-%d %H:%M:%S'`] $msg "
return 0
}
function log_warn(){
local msg=$1
echo -e "\033[1;31;40m[WARN]\033[0m [`date +'%Y-%m-%d %H:%M:%S'`] $msg "
return 1
}
function print_help(){
cat <<EOF
=========================================================================
Run SysBench Test
[email protected]
OPTS:
./sysbench.sh help : show help
./sysbench.sh install : install fio sysbench
./sysbench.sh fio_4k : Test fio 4k files
./sysbench.sh fio_16k : Test fio 16k files
./sysbench.sh create_sbtest : Create sbtest
./sysbench.sh sysbench_prepare : Prepare test data
./sysbench.sh sysbench_run : Run MySQL Test
./sysbench.sh sysbench_cleanup : Cleanup test data
./sysbench.sh sysbench_io : Run System files IO R/W Test
./sysbench.sh sysbench_locks : Test System locks
./sysbench.sh sysbench_thread : Test System threads
Note:
Please Install MySQL and ./sysbench.sh install
=========================================================================
EOF
}
IP=`ip a | grep -w "inet" | grep eth |sed 's/^.*inet //g'|sed 's/\/[0-9][0-9].*$//g'|awk 'NR==1{print $1}'`
if [ -z "$IP" ];then
IP=`ip a | grep -w "inet" | grep ens |sed 's/^.*inet //g'|sed 's/\/[0-9][0-9].*$//g'|awk 'NR==1{print $1}'`
fi
DIR=`pwd`
function install(){
yum -y install epel-release
yum -y install fio sysbench
}
function fio_4k(){
#fio
fio -filename=/tmp/test_rw -direct=1 -iodepth 1 -thread -rw=randrw -ioengine=psync -bs=4k -size=2G -numjobs=10 -runtime=600 -group_reporting -name=mytest >> ${DIR}/mysysbench-${IP}.log
rm -rf /tmp/test_rw
if [ $? -eq 127 ];then
log_warn "Please install ./sysbench.sh install"
print_help
fi
}
function fio_16k(){
#fio
fio -filename=/tmp/test_rw -direct=1 -iodepth 1 -thread -rw=randrw -ioengine=psync -bs=16k -size=2G -numjobs=10 -runtime=600 -group_reporting -name=mytest >> ${DIR}/mysysbench-${IP}.log
rm -rf /tmp/test_rw
if [ $? -eq 127 ];then
log_warn "Please install ./sysbench.sh install"
print_help
fi
}
function create_sbtest(){
#登录数据库创建账号密码
mysql -uroot -p1qazXSW@ -e"CREATE USER 'sbtest'@'%' IDENTIFIED WITH mysql_native_password BY '1qazXSW@';GRANT all on *.* to 'sbtest'@'%' with grant option;flush privileges;create database sbtest;"
if [ $? -eq 127 ];then
log_warn "Please install MySQL"
print_help
else
log_info "sysbench账号、数据库创建成功"
fi
}
function sysbench_prepare(){
#数据准备
log_info "正在创建压测数据,数据大小约160G。"
sysbench --num-threads=32 --max-time=600 --max-requests=999999999 --test= /usr/share/sysbench/tests/include/oltp_legacy/oltp.lua --oltp-table-size=10000000 --oltp-tables-count=64 --db-driver=mysql --mysql-table-engine=innodb --mysql-host="127.0.0.1" --mysql-port=6446 --mysql-user="sbtest" --mysql-password="1qazXSW@" prepare
if [ $? -eq 127 ];then
log_warn "Please install ./sysbench.sh install or ./sysbench.sh create_sbtest"
print_help
fi
}
function sysbench_run(){
#压测性能
log_info "正在执行测试,结果将保存在${DIR}/mysysbench-${IP}.log"
sysbench --num-threads=32 --max-time=600 --max-requests=999999999 --test= /usr/share/sysbench/tests/include/oltp_legacy/oltp.lua --oltp-table-size=10000000 --oltp-tables-count=64 --db-driver=mysql --mysql-table-engine=innodb --mysql-host="127.0.0.1" --mysql-port=6446 --mysql-user="sbtest" --mysql-password="1qazXSW@" run >> ${DIR}/mysysbench-${IP}.log
}
function sysbench_cleanup(){
log_info "正在清理文件"
#清理环境
sysbench --num-threads=32 --max-time=600 --max-requests=999999999 --test= /usr/share/sysbench/tests/include/oltp_legacy/oltp.lua --oltp-table-size=10000000 --oltp-tables-count=64 --db-driver=mysql --mysql-table-engine=innodb --mysql-host="127.0.0.1" --mysql-port=6446 --mysql-user="sbtest" --mysql-password="1qazXSW@" cleanup
}
function sysbench_io(){
log_info "正在执行测试,结果将保存在${DIR}/mysysbench-${IP}.log"
#文件io测试
sysbench --threads=12 --events=10000 fileio --file-total-size=2G --file-test-mode=rndrw prepare
if [ $? -eq 127 ];then
log_warn "Please install ./sysbench.sh install"
print_help
exit 0
fi
sysbench --threads=12 --events=10000 fileio --file-total-size=2G --file-test-mode=rndrw run >> ${DIR}/mysysbench-${IP}.log
sysbench --threads=12 --events=10000 fileio --file-total-size=2G --file-test-mode=rndrw cleanup
}
function sysbench_locks(){
log_info "正在执行测试,结果将保存在${DIR}/mysysbench-${IP}.log"
#锁测试互斥锁测试模拟所有线程在同一时刻并发运行。
sysbench --threads=12 mutex --mutex-num=1024 --mutex-locks=10000 --mutex-loops=10000 run >> ${DIR}/mysysbench-${IP}.log
if [ $? -eq 127 ];then
log_warn "Please install ./sysbench.sh install"
print_help
exit 0
fi
}
function sysbench_thread(){
log_info "正在执行测试,结果将保存在${DIR}/mysysbench-${IP}.log"
#线程测试
sysbench threads --num-threads=64 --thread-yields=100 --thread-locks=2 run >> ${DIR}/mysysbench-${IP}.log
if [ $? -eq 127 ];then
log_warn "Please install ./sysbench.sh install"
print_help
exit 0
fi
}
$1
#help support
if [ "$1"x == ""x ]; then
print_help
fi;
测试结果
tmp_table_size=256M
max_heap_table_size=256M
innodb_buffer_pool_size=51200M
innodb_log_file_size=12800M
innodb_flush_log_at_trx_commit=2
sync_binlog=1000
多主模式加入innodb优化参数
多主加入mgr优化参数
slave_parallel_type=LOGICAL_CLOCK
slave_parallel_workers=16
slave_preserve_commit_order=ON
group_replication_compression_threshold=2000000
binlog_row_image=MINIMAL
group_replication_flow_control_mode=DISABLED
SET GLOBAL group_replication_flow_control_certifier_threshold=250000;
SET GLOBAL group_replication_flow_control_applier_threshold=250000;
总结:单机和半同步复制速度相差不多,但是MGR组复制明显变慢,性能相差较大,MGR增加日志同步优化参数后性能提升明显,但是会牺牲掉一部分同步性,生产环境应按需求自行更改参数