内容就不展示了,里面已经收录上百套项目资料,自行获取。
无套路分享:
http://106.75.108.26:8001/index?empid=15
建议收藏:会一直持续更新。。。
centos7 下载:
http://mirrors.aliyun.com/centos/7/isos/x86_64/
环境&软件 | 版本 |
---|---|
虚拟机&VMware Fusion | VMware Fusion |
服务器¢os | 7.8(我用的是mini版) |
数据库&MySQL | 5.7.28 |
远程连接& | ZenTermLite |
远程文件传输 | rz sz |
架构如图所示:4台机器的IP和角色如下表:
机器名称 | IP | 角色 | 权限 |
---|---|---|---|
MySQL_Master | 172.16.255.2 | 数据库master | 可读写、主库 |
MySQL_Slave1 | 172.16.255.6 | 数据库Slave | 只读、从库 |
MySQL_Slave2 | 172.16.255.7 | 数据库Slave | 只读、从库 |
MySQL_MHA | 172.16.255.8 | MHA Manager | 高可用监控 |
下载
wget https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.28-1.el7.x86_64.rpm-bundle.tar
解压
tar xvf mysql-5.7.28-1.el7.x86_64.rpm-bundle.tar
安装
要移除CentOS自带的mariadb-libs,不然会提示冲突
rpm -qa | grep mariadb
rpm -e mariadb-libs-5.5.41-2.el7_0.x86_64 --nodeps
由于MySQL的server服务依赖了common、libs、client,所以需要按照以下顺序依次安装。
RPM是Red Hat公司随Redhat Linux推出的一个软件包管理器,通过它能够更加方便地实现软件的安
装。rpm常用的命令有以下几个:
-i, --install 安装软件包
-v, --verbose 可视化,提供更多的详细信息的输出
-h, --hash 显示安装进度
-U, --upgrade=+ 升级软件包
-e, --erase=+ 卸载软件包
--nodeps 不验证软件包的依赖
组合可得到几个常用命令:
安装软件:rpm -ivh rpm包名
升级软件:rpm -Uvh rpm包名
卸载软件:rpm -e rpm包名
查看某个包是否被安装 rpm -qa | grep 软件名称
下面就利用安装命令来安装mysql:
rpm -ivh mysql-community-common-5.7.28-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-5.7.28-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-compat-5.7.28-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-5.7.28-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-5.7.28-1.el7.x86_64.rpm
rpm -ivh mysql-community-devel-5.7.28-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-5.7.28-1.el7.x86_64.rpm 执行报错
不要慌~
问题原因:
很简单,如错误信息显示,缺少perl 、 net-tools 两个依赖,使用yum安装即可
问题解决:
yum安装对应的包
yum -y install perl.x86_64
yum install net-tools.x86_64
继续执行:
rpm -ivh mysql-community-server-5.7.28-1.el7.x86_64.rpm
rpm -ivh mysql-community-devel-5.7.28-1.el7.x86_64.rpm
mysqld --initialize --user=mysql
cat /var/log/mysqld.log | grep password
systemctl start mysqld
systemctl enable mysqld
mysql -uroot -p'初始密码'
mysql> SET PASSWORD = PASSWORD('你想要设置的密码');
Query OK, 0 rows affected, 1 warning (0.00 sec)
不同的MySQL直接要互相访问,需要关闭Linux的防火墙,否则就要在配置/etc/sysconfig/iptables中增加规则。配置防火墙不是本次作业的重点,所以四台服务器均关闭防火墙。
systemctl stop firewalld
永久关闭防火墙:(可选)
systemctl disable firewald
同样的步骤,克隆:Mysql_Slave2、MysqlMHA即可。
结果如图:
使用vim /etc/my.cnf命令修改Master配置文件
如果vim命令执行出错,则执行 yum install vim 安装即可
#bin_log配置
log_bin=mysql-bin
server-id=1
sync-binlog=1
binlog-ignore-db=information_schema
binlog-ignore-db=mysql
binlog-ignore-db=performance_schema
binlog-ignore-db=sys
#relay_log配置
relay_log=mysql-relay-bin
log_slave_updates=1
relay_log_purge=0
重启服务
systemctl restart mysqld
登录MySQL,在MySQL命令行执行如下命令:
mysql> grant replication slave on *.* to root@'%' identified by '密码';
mysql> grant all privileges on *.* to root@'%' identified by '密码';
mysql> flush privileges;
//查看主库状态信息,例如master_log_file='mysql-bin.000007',master_log_pos=154
mysql> show master status;
修改Slave的MySQL配置文件my.cnf,两台Slave的server-id分别设置为2和3
#bin_log配置
log_bin=mysql-bin
#服务器ID,从库1是2,从库2是3
server-id=2
sync-binlog=1
binlog-ignore-db=information_schema
binlog-ignore-db=mysql
binlog-ignore-db=performance_schema
binlog-ignore-db=sys
#relay_log配置
relay_log=mysql-relay-bin
log_slave_updates=1
relay_log_purge=0
read_only=1
重启服务
systemctl restart mysqld
开启同步
登录MySQL,在Slave节点的MySQL命令行执行同步操作,例如下面命令(注意参数与上面show master status操作显示的参数一致):
change master to master_host='172.16.255.2',master_port=3306,master_user='root',master_password ='root',master_log_file='mysql-bin.000001',master_log_pos=869;
start slave; // 开启同步
执行与Slave1相同的节点,唯一的区别是:server-id不同
登录MySQL,在MySQL命令行执行下面命令安装插件
install plugin rpl_semi_sync_master soname 'semisync_master.so';
show variables like '%semi%';
# 自动开启半同步复制
rpl_semi_sync_master_enabled=ON
rpl_semi_sync_master_timeout=1000
systemctl restart mysqld
两台Slave节点都执行以下步骤。
登录MySQL,在MySQL命令行执行下面命令安装插件
install plugin rpl_semi_sync_slave soname 'semisync_slave.so';
使用vim/etc/my.cnf,修改MySQL配置文件
# 自动开启半同步复制
rpl_semi_sync_slave_enabled=ON
重启服务
systemctl restart mysqld
测试半同步状态
首先通过MySQL命令行检查参数的方式,查看半同步是否开启。
show variables like '%semi%';
cat /var/log/mysqld.log
可以看到日志中已经启动半同步信息,例如:
Start semi-sync binlog_dump to slave (server_id: 2), pos(mysql-bin.000005, 154)
结果发现(尴尬了吧,报错了):
master节点日志显示:
[Warning] IP address '172.16.255.6' could not be resolved: Name or service not known
[Warning] IP address '172.16.255.7' could not be resolved: Name or service not known
slave节点日志显示:
[ERROR] Slave I/O for channel '': Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work. Error_code: 1593
没关系,不要慌,淡定~
原因:
我的slave节点是克隆的master节点,于是克隆的虚拟机与原虚拟机的UUID也是一样了,冲突
解决方案:
删除slave1、slave2、mha机器上的这个文件,然后重启数据库即可解决
这个文件删除,重启MySQL之后,会自动生成,无须担心
再次查看master节点的日志,如下图:证明半同步开启成功~
cat /var/log/mysqld.log
在四台服务器上分别执行下面命令,生成公钥和私钥(注意:连续按换行回车采用默认值)
ssh-keygen -t rsa
在三台MySQL服务器分别执行下面命令,密码输入系统密码,将公钥拷到MHA Manager服务器上
ssh-copy-id 172.16.255.8
之后可以在MHA Manager服务器上检查下,看看.ssh/authorized_keys文件是否包含3个公钥
cat /root/.ssh/authorized_keys
执行下面命令,将MHA Manager的公钥添加到authorized_keys文件中(此时应该包含4个公钥)
cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys
从MHA Manager服务器执行下面命令,向其他三台MySQL服务器分发公钥信息。
scp /root/.ssh/authorized_keys [email protected]:/root/.ssh/authorized_keys
scp /root/.ssh/authorized_keys [email protected]:/root/.ssh/authorized_keys
scp /root/.ssh/authorized_keys [email protected]:/root/.ssh/authorized_keys
可以MHA Manager执行下面命令,检测下与三台MySQL是否实现ssh互通。
ssh 172.16.255.2
exit
ssh 172.16.255.6
exit
ssh 172.16.255.7
exit
MySQL5.7对应的MHA版本是0.5.8,所以在GitHub上找到对应的rpm包进行下载,MHA manager和
node的安装包需要分别下载:
https://github.com/yoshinorim/mha4mysql-manager/releases/tag/v0.58
https://github.com/yoshinorim/mha4mysql-node/releases/tag/v0.58
下载后,将Manager和Node的安装包分别上传到对应的服务器。
提示:也可以使用wget命令在linux系统直接下载获取,例如
wget https://github.com/yoshinorim/mha4mysql- manager/releases/download/v0.58/mha4mysql-manager-0.58-0.el7.centos.noarch.rpm
在四台服务器上安装mha4mysql-node。
MHA的Node依赖于perl-DBD-MySQL,所以要先安装perl-DBD-MySQL。
yum install perl-DBD-MySQL -y
wget https://github.com/yoshinorim/mha4mysql-node/releases/download/v0.58/mha4mysql-node-0.58-0.el7.centos.noarch.rpm
rpm -ivh mha4mysql-node-0.58-0.el7.centos.noarch.rpm
在MHA Manager服务器安装mha4mysql-node和mha4mysql-manager。
MHA的manager又依赖了perl-Config-Tiny、perl-Log-Dispatch、perl-Parallel-ForkManager,也分别进行安装。
wget http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -ivh epel-release-latest-7.noarch.rpm
yum install perl-DBD-MySQL perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager -y
wget https://github.com/yoshinorim/mha4mysql-node/releases/download/v0.58/mha4mysql-node-0.58-0.el7.centos.noarch.rpm
rpm -ivh mha4mysql-node-0.58-0.el7.centos.noarch.rpm
wget https://github.com/yoshinorim/mha4mysql-manager/releases/download/v0.58/mha4mysql-manager-0.58-0.el7.centos.noarch.rpm
rpm -ivh mha4mysql-manager-0.58-0.el7.centos.noarch.rpm
提示:由于perl-Log-Dispatch和perl-Parallel-ForkManager这两个被依赖包在yum仓库找不到,
因此安装epel-release-latest-7.noarch.rpm。在使用时,可能会出现下面异常:Cannot
retrieve metalink for repository: epel/x86_64。可以尝试使
用/etc/yum.repos.d/epel.repo,然后注释掉metalink,取消注释baseurl。
MHA Manager服务器需要为每个监控的 Master/Slave 集群提供一个专用的配置文件,而所有的
Master/Slave 集群也可共享全局配置。
#目录说明
#/var/log (CentOS目录)
# /mha (MHA监控根目录)
# /app1 (MHA监控实例根目录)
# /manager.log (MHA监控实例日志文件)
mkdir -p /var/log/mha/app1
touch /var/log/mha/app1/manager.log
配置监控全局配置文件
vim /etc/masterha_default.cnf
需要先在master的MySQL主库执行命令,创建一个新用户mha
[server default]
#主库用户名,在master mysql的主库执行下列命令建一个新用户
#create user 'mha'@'%' identified by 'root';
#grant all on *.* to mha@'%' identified by 'root';
#flush privileges;
user=mha
password=root
port=3306
#ssh登录账号
ssh_user=root
#从库复制账号和密码
repl_user=root
repl_password=root
port=3306
#ping次数
ping_interval=1
#二次检查的主机
secondary_check_script=masterha_secondary_check -s 172.16.255.2 -s 172.16.255.6 -s 172.16.255.7
先使用 mkdir -p /etc/mha 命令创建目录,然后使用 vim /etc/mha/app1.cnf 命令编辑文件
[server default]
#MHA监控实例根目录
manager_workdir=/var/log/mha/app1
#MHA监控实例日志文件
manager_log=/var/log/mha/app1/manager.log
#[serverx] 服务器编号
#hostname 主机名
#candidate_master 可以做主库
#master_binlog_dir binlog日志文件目录
[server1]
hostname=172.16.255.2
candidate_master=1
master_binlog_dir="/var/lib/mysql"
[server2]
hostname=172.16.255.6
candidate_master=1
master_binlog_dir="/var/lib/mysql"
[server3]
hostname=172.16.255.7
candidate_master=1
master_binlog_dir="/var/lib/mysql"
在MHA Manager服务器上执行:
masterha_check_ssh --conf=/etc/mha/app1.cnf
在MHA Manager服务器上执行:
masterha_check_repl --conf=/etc/mha/app1.cnf
[root@bogon ~]# masterha_check_repl --conf=/etc/mha/app1.cnf
Tue Jul 13 17:04:15 2021 - [info] Reading default configuration from /etc/masterha_default.cnf..
Tue Jul 13 17:04:15 2021 - [info] Reading application default configuration from /etc/mha/app1.cnf..
Tue Jul 13 17:04:15 2021 - [info] Reading server configuration from /etc/mha/app1.cnf..
Tue Jul 13 17:04:15 2021 - [info] MHA::MasterMonitor version 0.58.
Tue Jul 13 17:04:16 2021 - [info] GTID failover mode = 0
Tue Jul 13 17:04:16 2021 - [info] Dead Servers:
Tue Jul 13 17:04:16 2021 - [info] Alive Servers:
Tue Jul 13 17:04:16 2021 - [info] 172.16.255.2(172.16.255.2:3306)
Tue Jul 13 17:04:16 2021 - [info] 172.16.255.6(172.16.255.6:3306)
Tue Jul 13 17:04:16 2021 - [info] 172.16.255.7(172.16.255.7:3306)
Tue Jul 13 17:04:16 2021 - [info] Alive Slaves:
Tue Jul 13 17:04:16 2021 - [info] 172.16.255.6(172.16.255.6:3306) Version=5.7.28-log (oldest major version between slaves) log-bin:enabled
Tue Jul 13 17:04:16 2021 - [info] Replicating from 172.16.255.2(172.16.255.2:3306)
Tue Jul 13 17:04:16 2021 - [info] Primary candidate for the new Master (candidate_master is set)
Tue Jul 13 17:04:16 2021 - [info] 172.16.255.7(172.16.255.7:3306) Version=5.7.28-log (oldest major version between slaves) log-bin:enabled
Tue Jul 13 17:04:16 2021 - [info] Replicating from 172.16.255.2(172.16.255.2:3306)
Tue Jul 13 17:04:16 2021 - [info] Primary candidate for the new Master (candidate_master is set)
Tue Jul 13 17:04:16 2021 - [info] Current Alive Master: 172.16.255.2(172.16.255.2:3306)
Tue Jul 13 17:04:16 2021 - [info] Checking slave configurations..
Tue Jul 13 17:04:16 2021 - [info] Checking replication filtering settings..
Tue Jul 13 17:04:16 2021 - [info] binlog_do_db= , binlog_ignore_db= information_schema,mysql,performance_schema,sys
Tue Jul 13 17:04:16 2021 - [info] Replication filtering check ok.
Tue Jul 13 17:04:16 2021 - [info] GTID (with auto-pos) is not supported
Tue Jul 13 17:04:16 2021 - [info] Starting SSH connection tests..
Tue Jul 13 17:04:20 2021 - [info] All SSH connection tests passed successfully.
Tue Jul 13 17:04:20 2021 - [info] Checking MHA Node version..
Tue Jul 13 17:04:21 2021 - [info] Version check ok.
Tue Jul 13 17:04:21 2021 - [info] Checking SSH publickey authentication settings on the current master..
Tue Jul 13 17:04:21 2021 - [info] HealthCheck: SSH to 172.16.255.2 is reachable.
Tue Jul 13 17:04:22 2021 - [info] Master MHA Node version is 0.58.
Tue Jul 13 17:04:22 2021 - [info] Checking recovery script configurations on 172.16.255.2(172.16.255.2:3306)..
Tue Jul 13 17:04:22 2021 - [info] Executing command: save_binary_logs --command=test --start_pos=4 --binlog_dir=/var/lib/mysql --output_file=/var/tmp/save_binary_logs_test --manager_version=0.58 --start_file=mysql-bin.000002
Tue Jul 13 17:04:22 2021 - [info] Connecting to [email protected](172.16.255.2:22)..
Creating /var/tmp if not exists.. ok.
Checking output directory is accessible or not..
ok.
Binlog found at /var/lib/mysql, up to mysql-bin.000002
Tue Jul 13 17:04:22 2021 - [info] Binlog setting check done.
Tue Jul 13 17:04:22 2021 - [info] Checking SSH publickey authentication and checking recovery script configurations on all alive slave servers..
Tue Jul 13 17:04:22 2021 - [info] Executing command : apply_diff_relay_logs --command=test --slave_user='mha' --slave_host=172.16.255.6 --slave_ip=172.16.255.6 --slave_port=3306 --workdir=/var/tmp --target_version=5.7.28-log --manager_version=0.58 --relay_log_info=/var/lib/mysql/relay-log.info --relay_dir=/var/lib/mysql/ --slave_pass=xxx
Tue Jul 13 17:04:22 2021 - [info] Connecting to [email protected](172.16.255.6:22)..
Checking slave recovery environment settings..
Opening /var/lib/mysql/relay-log.info ... ok.
Relay log found at /var/lib/mysql, up to mysql-relay-bin.000006
Temporary relay log file is /var/lib/mysql/mysql-relay-bin.000006
Checking if super_read_only is defined and turned on.. not present or turned off, ignoring.
Testing mysql connection and privileges..
mysql: [Warning] Using a password on the command line interface can be insecure.
done.
Testing mysqlbinlog output.. done.
Cleaning up test file(s).. done.
Tue Jul 13 17:04:23 2021 - [info] Executing command : apply_diff_relay_logs --command=test --slave_user='mha' --slave_host=172.16.255.7 --slave_ip=172.16.255.7 --slave_port=3306 --workdir=/var/tmp --target_version=5.7.28-log --manager_version=0.58 --relay_log_info=/var/lib/mysql/relay-log.info --relay_dir=/var/lib/mysql/ --slave_pass=xxx
Tue Jul 13 17:04:23 2021 - [info] Connecting to [email protected](172.16.255.7:22)..
Checking slave recovery environment settings..
Opening /var/lib/mysql/relay-log.info ... ok.
Relay log found at /var/lib/mysql, up to mysql-relay-bin.000005
Temporary relay log file is /var/lib/mysql/mysql-relay-bin.000005
Checking if super_read_only is defined and turned on.. not present or turned off, ignoring.
Testing mysql connection and privileges..
mysql: [Warning] Using a password on the command line interface can be insecure.
done.
Testing mysqlbinlog output.. done.
Cleaning up test file(s).. done.
Tue Jul 13 17:04:24 2021 - [info] Slaves settings check done.
Tue Jul 13 17:04:24 2021 - [info]
172.16.255.2(172.16.255.2:3306) (current master)
+--172.16.255.6(172.16.255.6:3306)
+--172.16.255.7(172.16.255.7:3306)
Tue Jul 13 17:04:24 2021 - [info] Checking replication health on 172.16.255.6..
Tue Jul 13 17:04:24 2021 - [info] ok.
Tue Jul 13 17:04:24 2021 - [info] Checking replication health on 172.16.255.7..
Tue Jul 13 17:04:24 2021 - [info] ok.
Tue Jul 13 17:04:24 2021 - [warning] master_ip_failover_script is not defined.
Tue Jul 13 17:04:24 2021 - [warning] shutdown_script is not defined.
Tue Jul 13 17:04:24 2021 - [info] Got exit code 0 (Not master dead).
MySQL Replication Health is OK.
出现“MySQL Replication Health is OK.”证明MySQL复制集群没有问题。
在MHA Manager服务器上执行:
nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mha/app1/manager.log 2>&1 &
查看监控状态命令如下:
masterha_check_status --conf=/etc/mha/app1.cnf
查看监控日志命令如下:
tail -f /var/log/mha/app1/manager.log
在MHA Manager服务器执行打开日志命令:
tail -200f /var/log/mha/app1/manager.log
显示manager.log文件的末尾200行
关闭Master MySQL服务器服务,模拟主节点崩溃
systemctl stop mysqld
show master status;
create TABLE position (
id int(20), name varchar(50),
salary varchar(20),
city varchar(50) ) ENGINE=innodb charset=utf8;
insert into position values(1, 'Java', 13000, 'shanghai');
insert into position values(2, 'DBA', 20000, 'beijing');
create TABLE position_detail (
id int(20),
pid int(20),
description text ) ENGINE=innodb charset=utf8;
insert into position_detail values(1, 1, 'Java Developer');
insert into position_detail values(2, 2, 'Database Administrator');