Linux企业化运维
一、延迟复制
server2;
> stop slave;
> change master to master_delay = 30;
> start slave;
> show slave status\G;
server1;
> insert into linux values ('user9','999');
server2;
> show slave status\G;
> show slave status\G;
二、慢查询
server2;
vim /etc/my.cnf
///
slave_parallel_workers=16
master_info_repository=TABLE
relay_log_info_repository=TABLE
relay_log_recovery=ON
slave-parallel-type=LOGICAL_CLOCK
///
/etc/init.d/mysqld restart
mysql -pwestos
> show variables like 'slow%';
> set global slow_query_log=1;
> show variables like 'long%';
> select sleep(10);
测试:执行后等待10秒
三、组复制
server1;
/etc/init.d/mysqld stop
cd /data/mysql/
rm -fr *
vim /etc/my.cny ##删除以前的参数添加新的参数,不删除会有冲突
添加新的参数
///
disabled_storage_engines="MyISAM,BLACKHOLE,FEDERATED,ARCHIVE,MEMORY"
server_id=1
gtid_mode=ON
enforce_gtid_consistency=ON
master_info_repository=TABLE
relay_log_info_repository=TABLE
binlog_checksum=NONE
log_slave_updates=ON
log_bin=binlog
binlog_format=ROW
plugin_load_add='group_replication.so'
transaction_write_set_extraction=XXHASH64
group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
group_replication_start_on_boot=off
group_replication_local_address= "server1:33061"
group_replication_group_seeds= "server1:33061,server2:33061,server3:33061"
group_replication_bootstrap_group=off
group_replication_ip_whitelist="172.25.77.0/24,127.0.0.1/8"
group_replication_single_primary_mode=OFF
group_replication_enforce_update_everywhere_checks=ON
///
mysqld --initialize-insecure --user=mysql ##不需要生成初始密码初始化
/etc/init.d/mysqld start
server1
mysql -pwestos
SET SQL_LOG_BIN=0;
CREATE USER rpl_user@'%' IDENTIFIED BY 'westos';
GRANT REPLICATION SLAVE ON *.* TO rpl_user@'%';
FLUSH PRIVILEGES;
SET SQL_LOG_BIN=1;
CHANGE MASTER TO MASTER_USER='rpl_user', MASTER_PASSWORD='westos' FOR CHANNEL 'group_replication_recovery';
SET GLOBAL group_replication_bootstrap_group=ON;
START GROUP_REPLICATION;
SET GLOBAL group_replication_bootstrap_group=OFF;
START GROUP_REPLICATION;
server2和server3;
/etc/init.d/mysqld stop
cd /data/mysql/
rm -fr *
vim /etc/my.cny ##删除以前的参数添加新的参数,不删除会有冲突
添加新的参数
///
disabled_storage_engines="MyISAM,BLACKHOLE,FEDERATED,ARCHIVE,MEMORY"
server_id=2 和 3 ################
gtid_mode=ON
enforce_gtid_consistency=ON
master_info_repository=TABLE
relay_log_info_repository=TABLE
binlog_checksum=NONE
log_slave_updates=ON
log_bin=binlog
binlog_format=ROW
plugin_load_add='group_replication.so'
transaction_write_set_extraction=XXHASH64
group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
group_replication_start_on_boot=off
group_replication_local_address= "server2和3:33061" ########
group_replication_group_seeds= "server1:33061,server2:33061,server3:33061"
group_replication_bootstrap_group=off
group_replication_ip_whitelist="172.25.77.0/24,127.0.0.1/8"
group_replication_single_primary_mode=OFF
group_replication_enforce_update_everywhere_checks=ON
///
mysqld --initialize-insecure --user=mysql ##不需要生成初始密码初始化
/etc/init.d/mysqld start
server2和server3
mysql -pwestos
SET SQL_LOG_BIN=0;
CREATE USER rpl_user@'%' IDENTIFIED BY 'westos';
GRANT REPLICATION SLAVE ON *.* TO rpl_user@'%';
FLUSH PRIVILEGES;
SET SQL_LOG_BIN=1;
CHANGE MASTER TO MASTER_USER='rpl_user', MASTER_PASSWORD='westos' FOR CHANNEL 'group_replication_recovery';
START GROUP_REPLICATION;
检验:
server1:
mysql> select * from performance_schema.replication_group_members;
四、 mysql路由器
需要在主从的基础上进行
访问mysql路由器,然后由mysql路由器访问后端服务器server主机,确保当某一台主机宕掉后,可以迅速启用其他主机。相当于负载均衡。
1、建立一个新的虚拟机
erver4
hostnamectl set-hostname server4
vim /etc/sysconfig/network-scripts/ifcfg-eth0
///
TYPE=Ethernet
BOOTPROTO=static
NAME=eth0
DEVICE=eth0
ONBOOT=yes
IPADDR=172.25.24.4
PREFIX=24
GATEWAY=172.25.24.250
DNS1=114.114.114.114
///
systemctl restart network ##注意重启网络
ip addr ##检查ip
ssh -l root 172.25.24.4
lftp 172.25.254.250
> ls
> cd pub/docs/mysql
> get mysql-router-community-8.0.21-1.el7.x86_64.rpm
> exit
rpm -ivh mysql-router-community-8.0.21-1.el7.x86_64.rpm
cd /etc/mysqlrouter/
ls
vim mysqlrouter.conf
///
[routing:ro] ##只读
bind_address = 0.0.0.0
bind_port = 7001
destinations = 172.25.24.1:3306,172.25.24.2:3306,172.25.24.3:3306
routing_strategy = round-robin
[routing:rw] ##读写
bind_address = 0.0.0.0
bind_port = 7002
destinations = 172.25.24.1:3306,172.25.24.2:3306,172.25.24.3:3306
routing_strategy = first-available ##第一可用
///
systemctl start mysqlrouter.service ##开启服务
netstat -antlp ##7001,7002
2、server1中授权
systemctl start mysql
mysql -pwestos
> select * from user; ##查看是否有全局用户;
> grant all on test.* to yun@'%' identified by 'westos'; ##授权用户
> flush privileges; ##刷新授权表
> exit
3、路由器配置好之后在真机上以指定用户远程连接mysql指定路由端口,7001或7002
[root@westos_student73 ~]# mysql -h 172.25.73.4 -P 7001 -u user1 -pwestos 连接7001端口为只读
[root@westos_student73 ~]# mysql -h 172.25.73.4 -P 7002 -u user2 -pwestos 连接7002端口为可读写
yum -y install mariadb
测试user2是否可以写入数据
4、使用lsof在mysql其它节点上测试连接情况
当某个节点mysql退出之后(不是关闭mysql服务),远程主机可通过路由端口连接到其他mysql主机上继续进行相关操作,因为配置文件中只读策略中定义的算法为round-robin
(轮询调度,一个一个轮着调度)
[root@server1 data]# yum install -y lsof
[root@server1 ~]# lsof -i :3306
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mysqld 4172 mysql 30u IPv6 32018 0t0 TCP *:mysql (LISTEN)
mysqld 4172 mysql 57u IPv6 31449 0t0 TCP server1:mysql->server4:56634 (ESTABLISHED)
mysql路由具有后端健康检查功能 ,配置文件中设置rw端口连接调度算法为first-available,一但有节点挂掉就会调度到其它节点上;远程主机在登录数据库时实际上连接的是server4 mysql路由器,之后server4通过proxy来对后端server2、server3进行调度
远程主机再次登入数据库发现已经调度到server2上
五、mysql MHA高可用主从切换
server1为管理端,可以管理多个一主多从的复制组。
server1、2、3
/etc/init.d/mysqld stop
###server1
cd /data/mysql
rm -rf *
cd
vim /etc/my.cnf
///
server-id=1
gtid_mode=ON
enforce-gtid-consistency=ON
master_info_repository=TABLE
relay_log_info_repository=TABLE
log_bin=binlog
log_slave_updates=ON
binlog_format=ROW
///
1、在server1上作slave授权、其它两个节点作change master设置,构建一个一主两从的设置
[root@server1 data]# mysql
mysql> grant replication slave on *.* to repl@'%' identified by 'westos';
[root@server2 data]# mysql
mysql> change master to MASTER_HOST = '172.25.73.1', MASTER_USER = 'repl', MASTER_PASSWORD = 'westos', MASTER_AUTO_POSITION = 1;
mysql> start slave;
[root@server3 data]# mysql
mysql> change master to MASTER_HOST = '172.25.73.1', MASTER_USER = 'repl', MASTER_PASSWORD = 'westos', MASTER_AUTO_POSITION = 1;
mysql> start slave;
mysql> show slave status\G;
2、在sever4上安装需要安装的资源,并将node节点上所需要安装的资源复制到各个node上安装
[root@server4 ~]# ls
MHA-7 mysql-router-community-8.0.21-1.el7.x86_64.rpm
[root@server4 ~]# cd MHA-7/
[root@server4 MHA-7]# ls
mha4mysql-manager-0.58-0.el7.centos.noarch.rpm perl-Mail-Sender-0.8.23-1.el7.noarch.rpm
mha4mysql-manager-0.58.tar.gz perl-Mail-Sendmail-0.79-21.el7.noarch.rpm
mha4mysql-node-0.58-0.el7.centos.noarch.rpm perl-MIME-Lite-3.030-1.el7.noarch.rpm
perl-Config-Tiny-2.14-7.el7.noarch.rpm perl-MIME-Types-1.38-2.el7.noarch.rpm
perl-Email-Date-Format-1.002-15.el7.noarch.rpm perl-Net-Telnet-3.03-19.el7.noarch.rpm
perl-Log-Dispatch-2.41-1.el7.1.noarch.rpm perl-Parallel-ForkManager-1.18-2.el7.noarch.rpm
[root@server4 MHA-7]# yum install *.rpm -y
[root@server4 MHA-7]# scp mha4mysql-node-0.58-0.el7.centos.noarch.rpm server1:
[root@server4 MHA-7]# scp mha4mysql-node-0.58-0.el7.centos.noarch.rpm server2:
[root@server4 MHA-7]# scp mha4mysql-node-0.58-0.el7.centos.noarch.rpm server3:
在三个节点上server1、2、3:
yum install -y mha4mysql-node-0.58-0.el7.centos.noarch.rpm
3、将MHA全局部署文件与另外一个单独文件整合到一起并编辑
[root@server4 ~]# cd MHA-7/
[root@server4 MHA-7]# ls
mha4mysql-manager-0.58-0.el7.centos.noarch.rpm perl-Mail-Sender-0.8.23-1.el7.noarch.rpm
mha4mysql-manager-0.58.tar.gz perl-Mail-Sendmail-0.79-21.el7.noarch.rpm
mha4mysql-node-0.58-0.el7.centos.noarch.rpm perl-MIME-Lite-3.030-1.el7.noarch.rpm
perl-Config-Tiny-2.14-7.el7.noarch.rpm perl-MIME-Types-1.38-2.el7.noarch.rpm
perl-Email-Date-Format-1.002-15.el7.noarch.rpm perl-Net-Telnet-3.03-19.el7.noarch.rpm
perl-Log-Dispatch-2.41-1.el7.1.noarch.rpm perl-Parallel-ForkManager-1.18-2.el7.noarch.rpm
[root@server4 MHA-7]# tar zxf mha4mysql-manager-0.58.tar.gz
[root@server4 MHA-7]# cd mha4mysql-manager-0.58/
[root@server4 mha4mysql-manager-0.58]# ls
AUTHORS bin COPYING debian lib Makefile.PL MANIFEST MANIFEST.SKIP README rpm samples t tests
[root@server4 mha4mysql-manager-0.58]# cd samples/
[root@server4 samples]# ls
conf scripts
[root@server4 samples]# cd conf/
[root@server4 conf]# ls
app1.cnf masterha_default.cnf
[root@server4 conf]# cat masterha_default.cnf app1.cnf > /etc/masterha/app.cnf
[root@server4 masterha]# vim app.cnf
[server default]
user=root mysql主从节点的管理员用户密码,确保可以从远程登陆
password=westos
ssh_user=root
repl_user=repl 主从复制用户密码
repl_password=westos
master_binlog_dir= /usr/local/mysql/data mysql主服务器的binlog目录
remote_workdir=/tmp
secondary_check_script= masterha_secondary_check -s 172.25.73.1 -s 172.25.73.2 修改
ping_interval=3
# master_ip_failover_script= /script/masterha/master_ip_failover
# shutdown_script= /script/masterha/power_manager
# report_script= /script/masterha/send_report
# master_ip_online_change_script= /script/masterha/master_ip_online_change
manager_workdir=/etc/masterha/app1 manager工作目录
manager_log=/etc/masterha/app1/manager.log manager日志文件
[server1]
hostname=172.25.73.1 修改
[server2]
hostname=172.25.73.2 修改
candidate_master=1 指定failover时此slave会接管master,即使数据不是最新的
[server3]
hostname=172.25.73.3 修改
no_master=1 始终是slave
4、在server4与进行与其它三个节点的免密,并使用脚本进行检查
[root@server4 masterha]# ssh-keygen
[root@server4 ~]# ssh-copy-id server1 只做这一步使用脚本检测时会提示失败
[root@server4 ~]# ssh-copy-id server2
[root@server4 ~]# ssh-copy-id server3
[root@server4 ~]# scp -r .ssh/ server1: 复制私钥和公钥到三个节点上
[root@server4 ~]# scp -r .ssh/ server2:
[root@server4 ~]# scp -r .ssh/ server3:
[root@server4 ~]# masterha_check_ssh --conf=/etc/masterha/app.cnf
当脚本检测返回内容如下所示时说明免密做成功
Tue Sep 14 20:15:43 2021 - [debug] Connecting via SSH from root@172.25.73.1(172.25.73.1:22) to root@172.25.73.2(172.25.73.2:22)..
Tue Sep 14 20:15:44 2021 - [debug] ok.
Tue Sep 14 20:15:44 2021 - [debug] Connecting via SSH from root@172.25.73.1(172.25.73.1:22) to root@172.25.73.3(172.25.73.3:22)..
Tue Sep 14 20:15:44 2021 - [debug] ok.
Tue Sep 14 20:15:44 2021 - [debug]
Tue Sep 14 20:15:44 2021 - [debug] Connecting via SSH from root@172.25.73.2(172.25.73.2:22) to root@172.25.73.1(172.25.73.1:22)..
Tue Sep 14 20:15:44 2021 - [debug] ok.
Tue Sep 14 20:15:44 2021 - [debug] Connecting via SSH from root@172.25.73.2(172.25.73.2:22) to root@172.25.73.3(172.25.73.3:22)..
Tue Sep 14 20:15:44 2021 - [debug] ok.
Tue Sep 14 20:15:45 2021 - [debug]
Tue Sep 14 20:15:44 2021 - [debug] Connecting via SSH from root@172.25.73.3(172.25.73.3:22) to root@172.25.73.1(172.25.73.1:22)..
Tue Sep 14 20:15:45 2021 - [debug] ok.
Tue Sep 14 20:15:45 2021 - [debug] Connecting via SSH from root@172.25.73.3(172.25.73.3:22) to root@172.25.73.2(172.25.73.2:22)..
Tue Sep 14 20:15:45 2021 - [debug] ok.
5、在server1的mysql对远程管理员登陆数据库授权
[root@server1 ~]# mysql
mysql> grant all on *.* to root@'%' identified by 'westos';
mysql> flush privileges;
6、在server4中用脚本进行检查
[root@server4 ~]# masterha_check_repl --conf=/etc/masterha/app.cnf 检查MySQL复制状况
六、MHA的故障切换
1、手动切换
server1和server2均为存活状态
[root@server4 ~]#
masterha_master_switch --conf=/etc/masterha/app.cnf --master_state=alive --new_master_host=172.25.73.2 --new_master_port=3306 --orig_master_is_new_slave --running_updates_limit=10000 切换master为server2
在server1、server3查看,master切换为server2
mysql> show slave status\G;
server1
mysql
>change master to MASTER_HOST = '172.25.77.2', MASTER_USER = 'repl', MASTER_PASSWORD = 'westos', MASTER_AUTO_POSITION = 1; ##指向master
>start slave;
2、自动切换
server4
cd /etc/masterha/
lftp 172.25.254.250
cd /pub/docs/mysql
get master_ip_failover master_ip_online_change
ls
chmod +x master_ip_failover master_ip_online_change
vim master_ip_failover ##更改ip
vim master_ip_online_change ##更改ip
server2
添加vip
ip addr add 172.25.77.100/24 dev eth0
ip addr ##查看vip是否添加
server4
masterha_manager --conf=/etc/masterha/app.conf &
ls ##app1.master_status.health ##更改后文件发生更改
server2
/etc/init.d/mysqld stop ##master 在那个server上就在那个上面stop
server4
ls ##app1.failover.complete ##成功的文件
###server2
> show slave status\G; ##可以看到状态,Master_Host: 172.25.24.2
###server3
> show slave status\G; ##可以看到状态,Master_Host: 172.25.24.2