1.主从复制
在MySQL中建立表
在MySQL2中查看
2.MySQL主从集群的slave添加及延迟回放
建立新的MySQL3虚拟机
2.1拉平数据(要锁数据,实验环节没锁,生产环境需要锁)
sql
[root@mysql ~]# mysqldump -uroot -p timinglee > timinglee.sql
[root@mysql ~]# scp timinglee.sql root@172.25.254.30:/mnt/
[root@mysql3 ~]# cd /mnt/
[root@mysql3 mnt]# ls
timinglee.sql
恢复数据
建立库
[root@mysql3 mnt]# mysql -uroot -p123 -e "create database timinglee;"
[root@mysql3 mnt]# mysql -uroot -p123 timinglee < timinglee.sql
在MySQL3中配置slave
sql
mysql> change master to master_host='172.25.254.100',master_user='repl',master_password='123',master_log_file='mysql-bin.000002',master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.03 sec)
mysql> slave start;
检查 : Slave_IO_State: Waiting for master to send event
Master_Host: 172.25.254.100
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 154
Relay_Log_File: mysql3-relay-bin.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
2.2测试一主两从,在主中加入数据
mysql> insert into timinglee.userlist values('user2','123');
在两从中查看数据
MySQL2中:mysql> select * from timinglee.userlist;
+----------+----------+
| username | password |
+----------+----------+
| lee1 | 123 |
| user2 | 123 |
+----------+----------+
2 rows in set (0.00 sec)
MySQL3中:
mysql> select * from timinglee.userlist;
+----------+----------+
| username | password |
+----------+----------+
| lee1 | 123 |
| user2 | 123 |
+----------+----------+
2 rows in set (0.00 sec)
2.3延迟回放
在slave端中将延迟改为120
sql
mysql> stop slave sql_thread;
mysql> change master to master_delay=120;
mysql> start slave sql_thread;
mysql> show slave status\G;
Master_Info_File: /data/mysql/master.info
SQL_Delay: 120
SQL_Remaining_Delay: NULL
3.慢查询
在主机中做,从机也会有
mysql> set global slow_query_log=on;
mysql> show variables like "slow%";
+---------------------+----------------------------+
| Variable_name | Value |
+---------------------+----------------------------+
| slow_launch_time | 2 |
| slow_query_log | ON |
| slow_query_log_file | /data/mysql/mysql-slow.log |
+---------------------+----------------------------+
3 rows in set (0.00 sec)
查看慢查询时长mysql> show variables like "long%";
+-----------------+-----------+
| Variable_name | Value |
+-----------------+-----------+
| long_query_time | 10.000000 |
+-----------------+-----------+
1 row in set (0.00 sec)
测试慢查询
mysql> select sleep (10);
+------------+
| sleep (10) |
+------------+
| 0 |
+------------+
1 row in set (10.01 sec)
4.MySQL主从日志的多线程回放
作用:尽量缩小主从数据差异
编辑主配置文件
编辑之后重启MySQL:/etc/init.d/mysqld restart
查看进程
5.MySQL的gitd日志模式
配置文件
[root@mysql ~]# vim /etc/my.cnf
[root@mysql ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
[root@mysql ~]# cat /etc/my.cnf
[mysqld]
datadir=/data/mysql
socket=/data/mysql/mysql.sock
symbolic-links=0
server-id=10
slow_query_log=on
log-bin=mysql-bin
gtid_mode=ON
enforce-gtid-consistency=ON
[root@mysql3 mysql]# vim /etc/my.cnf[root@mysql3 mysql]# /etc/init.d/mysqld restart
Shutting down MySQL. SUCCESS!
Starting MySQL. SUCCESS!
[root@mysql3 mysql]# cat /etc/my.cnf
[mysqld]
datadir=/data/mysql
socket=/data/mysql/mysql.sock
symbolic-links=0
server-id=30
gtid_mode=ON
enforce-gtid-consistency=ON
查看gtid的状态
在MySQL2和MySQL3中先关闭slave
mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)
在MySQL2和MySQL3中设置
mysql> change master to
-> master_host='172.25.254.100',
-> master_user='repl',
-> master_password='123',
-> master_auto_position=1;
Query OK, 0 rows affected, 2 warnings (0.02 sec)
mysql> start slave;
Query OK, 0 rows affected (0.02 sec)
测试检查:
MySQL1中
MySQL2
MySQL3
6.MySQL的半同步模式
在/etc/my.cnf中开启半同步
[root@mysql ~]# vim /etc/my.cnf
gtid_mode=ON
enforce-gtid-consistency=ON
rpl_semi_sync_master_enabled=1
6.1在MySQL中安装插件
mysql> INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';
查看插件状态
查看半同步状态功能
6.2在slave中开启半同步(MySQL2和MySQL3)
[root@mysql3 ~]# cat /etc/my.cnf
[mysqld]
datadir=/data/mysql
socket=/data/mysql/mysql.sock
symbolic-links=0
server-id=30
gtid_mode=ON
enforce-gtid-consistency=ON
rpl_semi_sync_master_enabled=1
在MySQL中启动
mysql> SET GLOBAL rpl_semi_sync_slave_enabled =1;
重启io线程
mysql> STOP SLAVE IO_THREAD;
Query OK, 0 rows affected (0.00 sec)
mysql> START SLAVE IO_THREAD;
Query OK, 0 rows affected (0.00 sec)
mysql> SHOW VARIABLES LIKE 'rpl_semi_sync%';
+---------------------------------+-------+
| Variable_name | Value |
+---------------------------------+-------+
| rpl_semi_sync_slave_enabled | ON |
| rpl_semi_sync_slave_trace_level | 32 |
+---------------------------------+-------+
2 rows in set (0.00 sec)
测试:直写正常 模拟故障
在MySQL2和MySQL3关闭IO线程
mysql> STOP SLAVE IO_THREAD;
Query OK, 0 rows affected (0.00 sec)
在MySQL1中写入数据卡住了
之后写入成功
7.MySQL组复制功能的实现(MGR)
因为基于之前实验的主机,为了保证一致性需要清除数据
[root@mysql ~]# rm -fr /data/mysql/*
修改配置文件
之后初始化MySQL
进行MySQL内的修改
mysql> alter user root@localhost identified by 'lee';
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE USER rpl_user@'%' IDENTIFIED BY 'lee';
Query OK, 0 rows affected (0.00 sec)
mysql> SET SQL_LOG_BIN=0;
Query OK, 0 rows affected (0.00 sec)
mysql> GRANT REPLICATION SLAVE ON *.* TO rpl_user@'%';
Query OK, 0 rows affected (0.00 sec)
mysql> SET SQL_LOG_BIN=1;
Query OK, 0 rows affected (0.00 sec)
mysql> CHANGE MASTER TO MASTER_USER='rpl_user', MASTER_PASSWORD='lee' FOR CHANNEL
-> 'group_replication_recovery';
Query OK, 0 rows affected, 2 warnings (0.02 sec)
修改三台机子的解析(三台都要加)
将/etc/my.cnf复制到其他两台主机
初始化重启之后进行操作(MySQL2和MySQL3操作相同)
在MySQL主机中查看成员
测试:在MySQL中建立库
mysql> create database timinglee;
Query OK, 1 row affected (0.01 sec)
在MySQL2中查看
在MySQL3中建立表
在MySQL2中查看
8.MySQL路由
安装MySQL-router软件
首先停止MySQL的引用
[root@mysql ~]# /etc/init.d/mysqld stop
Shutting down MySQL. SUCCESS!
修改mysqlrouter的主配置文件
[root@mysql ~]# systemctl start mysqlrouter.service
[root@mysql ~]# netstat -antlupe | grep 7001
tcp 0 0 0.0.0.0:7001 0.0.0.0:* LISTEN 988 47417 4477/mysqlrouter
增加MySQL2和MySQL3的用户(操作相同)
用MySQL登录(200和30轮询)
[root@mysql ~]# mysql -uroot -plee -h172.25.254.100 -P 7001
9.MySQL高可用之MHA
在mha主机中做免密认证
[root@mysql-mha ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@172.25.254.100
[root@mysql-mha ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@172.25.254.200
[root@mysql-mha ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@172.25.254.30
因为基于之前实验的实验机,所以要删掉数据恢复一主两从的状态
下载压缩包文件
[root@mysql-mha MHA-7]# yum install *.rpm -y
将node包发送至节点端
[root@mysql-mha MHA-7]# scp mha4mysql-node-0.58-0.el7.centos.noarch.rpm root@172.25.254.100:/root
[root@mysql-mha MHA-7]# scp mha4mysql-node-0.58-0.el7.centos.noarch.rpm root@172.25.254.200:/root
[root@mysql-mha MHA-7]# scp mysql-node-0.58-0.el7.centos.noarch.rpm root@172.25.254.30:/root
在节点下载node包
yum install mha4mysql-node-0.58-0.el7.centos.noarch.rpm -y
在mha端进行操作
[root@mysql-mha MHA-7]# cd mha4mysql-manager-0.58/
[root@mysql-mha mha4mysql-manager-0.58]# cd samples/
[root@mysql-mha samples]# cd conf/
[root@mysql-mha conf]# cat masterha_default.cnf app1.cnf
[root@mysql-mha conf]# cat masterha_default.cnf app1.cnf > /etc/masterha/app1.cnf
[root@mysql-mha conf]# cd /etc/masterha/
[root@mysql-mha masterha]# vim app1.cnf
在mha端修改主配置文件(第二个-s后跟的必须是server里没有的id)
检测网络及ssh
检测主从复制
[root@mysql-mha .ssh]# masterha_check_repl --conf=/etc/masterha/app1.cnf
10.MySQL高可用集群故障迁移
手动切换master
在172.25.254.100上查看master
将100中的mysql停掉的切换
[root@mysql ~]# /etc/init.d/mysqld stop
Shutting down MySQL............ SUCCESS!
在mha主机上切换
查看200的master状态和30的slave
200:
30:
恢复故障节点
先启用100中的MySQL并且开启slave
如果生成锁文件要进行删除锁文件否则会执行失败
删除锁文件再次执行
[root@mysql-mha masterha]# rm -fr app1.failover.complete
查看200中的slave
11.MySQL高可用集群的VIP管理
修改配置文件
[root@mysql-mha ~]# vim /usr/local/bin/master_ip_failover
[root@mysql-mha ~]# vim /usr/local/bin/master_ip_online_change
在100上把vip加上
[root@mysql ~]# ip a a 172.25.254.10/24 dev eth0
检查一主两从的架构
停掉100的mysql服务查看vip
100上的vip消失
200上出现vip
手动恢复后查看vip