赞
踩
目录
搭建一个基于keepalived的高可用Mysql主从复制读写分离集群
项目步骤:(主从复制、读写分离、单vip高可用、双vip高可用)
4、修改keepalived的配置文件(从36行以下的全部都不需要)(配置单vip的高可用服务)
5、模拟测试:如果master挂掉了,那么vip是否会转移到backup上去呢?
8、配置双vip实现keepalived配置(跟MySQL的主主复制十分相似)
高可用性(High Availability,HA)是指系统或功能提供者在经过设计后,该系统或功能能够长时间的保持在正常状态运行的能力。在现代信息化的环境中,高可用性已经成为了一个必不可少的概念。
防止出现单点故障,主要是因为现代IT系统对于稳定性具有较高的要求,一旦系统出现故障(出现单点故障),将会影响到业务的正常运转,甚至会导致数据丢失等重大问题,从而造成严重的损失。所以,必须确保系统能够尽可能地持续稳定地运行,保证了业务的连续性与数据的可靠性。
高可用的主要作用包括:
提高系统的稳定性:高可用架构通过冗余、备份、灾备等措施,可以避免单点故障,提高系统的稳定性和可用性,保证服务的连续性。
提高业务的可靠性:高可用架构可以保证业务的可靠性,避免因为系统故障导致的数据丢失等问题,从而保证业务的正常运转。
改善用户体验:高可用架构可以提高系统的性能和响应速度,改善用户的体验,同时还可以提高系统的扩展性和可靠性。
降低维护成本:通过高可用架构降低应用程序的停机时间和维护成本,避免出现服务中断而导致的维护费用增加。
总之,高可用性是一种确保业务连续性的重要手段,它可以避免因为单点故障而带来的损失,并且可以保证业务的持续稳定运行。
keepalived的官方网站:Keepalived for Linux
keepalived是一种开源软件,它提供了一个简单而高效的方式来实现负载均衡和故障转移。keepalived能够监视可用服务器的状态,并确保将请求路由到可用服务器上,同时在服务器故障时能够及时切换到备用服务器,以保证服务的高可用性。
keepalived主要用于以下几个方面:
实现负载均衡:keepalived能够使用不同的算法,例如轮询(Round Robin)、加权轮询、最少连接数等,将客户端请求平均地分配到多个后端服务器上,以达到负载均衡的目的。
提高系统的可用性:keepalived可以对多台服务器进行监视,并自动切换到另一个可用的节点来避免单点故障。通过自动检测和切换,keepalived可以确保应用程序始终处于可用状态,从而提高系统的可用性。
构建高可用性架构:通过配合其他技术,例如虚拟IP地址(Virtual IP address)、虚拟路由器冗余协议(VRRP)等,keepalived可以帮助构建高可用性架构,从而保证服务的连续性和稳定性。
总之,keepalived是一款功能强大的开源软件,它可以帮助构建高可用性、可伸缩性和安全性的应用程序。它在各种Web应用程序、负载均衡设备、邮件服务器等系统中得到了广泛的应用。
VRRP(Virtual Router Redundancy Protocol,虚拟路由器冗余协议)是一种网络协议,用于提高路由器系统的可用性和可靠性。它通过将多个路由器组合成一个虚拟路由器,只有一个路由器处于活动状态,其他路由器都是备份状态,从而达到提供冗余和故障转移的目的。
vip:virtual ip 虚拟IP地址,这个地址是对外提供服务的
参考文档:VRRP原理及配置 - 榕霖 - 博客园 (cnblogs.com)
具体来说,VRRP的作用包括:
提供冗余:通过将多个路由器组合成一个虚拟路由器,只有一个路由器处于活动状态,其他路由器都是备份状态,当活动路由器出现故障时,备用路由器可以自动接管其工作,确保网络服务不中断。
提高可用性:由于VRRP使用了路由器的冗余,所以即使在单个设备或链路出现故障时,也能够保持网络的连通性,从而提高了网络的可用性和可靠性。
支持负载均衡:当多个路由器同时处于活动状态时,VRRP可以自动将流量分布到各个路由器之间,实现负载均衡。
总之,VRRP协议是一种非常有效的网络协议,主要用于提供网络冗余和故障转移,提高网络的可用性和可靠性。它被广泛应用于企业内部网络、互联网服务提供商(ISP)等各种网络环境中。
vid:虚拟ip地址,这个地址是对外提供服务的
中间件:安装mysqlrouter 和 keepalived
master:192.168.2.181
backup:192.168.2.182
vip:192.168.2.221
Linux客户机:192.168.2.43
Mysql服务器:安装Mysql数据库
master:192.168.2.150
slave-1:192.168.2.151
slave-2:192.168.2.152
参考:Mysql - 主从复制介绍_Claylpf的博客-CSDN博客
1、修改主机名
- [root@web-3 ~]# hostnamectl set-hostname mysql-3
- [root@web-3 ~]# su -
- 上一次登录:五 8月 11 13:28:25 CST 2023从 192.168.2.7pts/0 上
- [root@mysql-3 ~]#
2、安装Mysql
参考:Mysql的介绍和软件环境的部署_mysql有软件吗_Claylpf的博客-CSDN博客
编辑一键成功安装mysql脚本 onekey_install_mysql_binary.sh
-
- [root@web ~]# cat onekey_install_mysql_binary.sh
- #!/bin/bash
-
- #步骤:
- #解决软件依赖关系
- yum install cmake ncurses-devel gcc gcc-c++ vim lsof bzip2 openssl-devel -y
-
- #解压二进制安装包
- tar xf mysql-5.7.41-linux-glibc2.12-x86_64.tar.gz
-
- #移动mysql解压文件到/usr/local下,改名为mysql
- mv mysql-5.7.41-linux-glibc2.12-x86_64 /usr/local/mysql
-
- #新建组和用户 mysql
- groupadd mysql
-
- #mysql这个用户的shell 是/bin/false 无法被调用 表示系统用户 属于mysql这个组
- useradd -r -g mysql -s /bin/false mysql
-
- #进入/usr/local/mysql
- cd /usr/local/mysql
-
- #关闭firewalld防火墙服务,并且设置开机不启动
- service firewalld stop
- systemctl disable firewalld
-
- #临时关闭selinux,永久关闭selinux
- setenforce 0
- sed -i '/^SELINUX=/ s/enforcing/disabled/' /etc/selinux/config
-
- #mysql初始化操作
- #创建/data/mysql文档用于存放数据使用
- mkdir /data/mysql -p
- #修改文件权限和组别,这样mysql用户可以对这个文件夹进行读写操作了
- chown mysql:mysql /data/mysql/
- chmod 750 /data/mysql/
-
- #进入/usr/local/mysql/bin目录下进行初始化操作
- cd /usr/local/mysql/bin
- #设置启动用户为mysql base目录为/usr/local/mysql/ data目录为/data/mysql &>passwd.txt目的是将生成的临时密码存入passwd.txt文件中
- ./mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/data/mysql &>passwd.txt
-
- #让mysql支持ssl方式登录的设置
- ./mysql_ssl_rsa_setup --datadir=/data/mysql/
-
- #获得临时密码
- tem_passwd=$(cat passwd.txt| grep "temporary" | awk '{print $NF}')
- #$NF表示最后一个字段
-
- #修改环境变量,添加我们编译安装的mysql的可执行命令的路径
- #临时修改PATH变量的值
- export PATH=/usr/local/mysql/bin/:$PATH
- #永久修改
- echo "PATH=/usr/local/mysql/bin:$PATH" >>/root/.bashrc
- #刷新/root/.bashrc文档
- source ~/.bashrc
-
- #复制support-files里的mysql.server文件到/etc/init.d目录下叫mysqld
- cp ../support-files/mysql.server /etc/init.d/mysqld
-
- #修改/etc/init.d/mysqld内的第70行的内容(datadir目录的值)
- sed -i '70c datadir=/data/mysql' /etc/init.d/mysqld
-
- #生成/etc/my.cnf配置文件
- cat >/etc/my.cnf <<EOF
- [mysqld_safe]
- [client]
- socket=/data/mysql/mysql.sock
- [mysqld]
- socket=/data/mysql/mysql.sock
- port = 3306
- open_files_limit = 8192
- innodb_buffer_pool_size = 512M
- character-set-server=utf8
- [mysql]
- auto-rehash
- prompt=\\u@\\d \\R:\\m mysql>
- EOF
-
- #启动mysqld服务
- service mysqld start
-
- #将mysqld添加到linux系统里的服务管理名单里
- chkconfig --add mysqld
- #设置mysqld服务开机启动
- /sbin/chkconfig mysqld on
-
- #登录重新设置初始密码为123456
- #初次修改密码需要使用 --connect-expired-password 选项
- #-e 后面接的命令是表示我们需要在mysql里执行的命令
- #set password='123456'; 表示修改root用户的密码为:123456
- mysql -uroot -p$tem_passwd --connect-expired-password -e "set password='123456';"
-
- #建议修改密码是否修改成功
- mysql -uroot -p123456 -e 'show databases;' && echo "database is installed success"
-
- [root@web ~]#
3、在master服务器上开启二进制日志配置server_id=1,并且在从服务器上配置server_id=2
master上
- [root@mysql-1 ~]# cat /etc/my.cnf
- [mysqld_safe]
-
- [client]
- socket=/data/mysql/mysql.sock
-
- [mysqld]
- socket=/data/mysql/mysql.sock
- log-error=/data/mysql/mysql_error.log #表示设置Mysql的error日志的路径
- slow_query_log = 1 #表示开启Mysql慢日志
- long_query_time = 0.001 #表示设置慢日志的阈值为0.001毫秒
- general_log #我们不需要添加路径了,因为Mysql会自动帮助我们添加通用日志的路径和日志文件,表示打开了通用日志
- log_bin #表示我们开启了二进制日志
- server_id = 1 #给我们的Mysql服务器进行编号
- expire_logs_days = 7 #表示二进制日志文件过 7天 自动清除
-
- port = 3306
- open_files_limit = 8192
- innodb_buffer_pool_size = 512M
- character-set-server=utf8
-
- [mysql]
- auto-rehash
- prompt=\u@\d \R:\m mysql>
- [root@mysql-1 ~]#
slave上:
- [root@mysql-2 ~]# cat /etc/my.cnf
- [mysqld_safe]
-
- [client]
- socket=/data/mysql/mysql.sock
-
- [mysqld]
- socket=/data/mysql/mysql.sock
- log-error=/data/mysql/mysql_error.log #表示设置Mysql的error日志的路径
- server_id = 2 #给我们的Mysql服务器进行编号
-
- port = 3306
- open_files_limit = 8192
- innodb_buffer_pool_size = 512M
- character-set-server=utf8
-
- [mysql]
- auto-rehash
- prompt=\u@\d \R:\m mysql>
- [root@mysql-2 ~]#
重启master和slave的Mysql数据库服务
[root@mysql-1 ~]# systemctl restart mysqld
4、在master上创建可以给slave服务器过来复制二进制日志文件的用户
- [root@mysql-1 ~]# mysql -u root -p
- Enter password:
- Welcome to the MySQL monitor. Commands end with ; or \g.
- Your MySQL connection id is 4
- Server version: 5.7.41 MySQL Community Server (GPL)
-
- Copyright (c) 2000, 2023, Oracle and/or its affiliates.
-
- Oracle is a registered trademark of Oracle Corporation and/or its
- affiliates. Other names may be trademarks of their respective
- owners.
-
- Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
-
- #创建sc_slave用户,允许任意网段访问
- root@(none) 15:40 mysql>create user 'sc_slave'@'%' identified by '123456';
- Query OK, 0 rows affected (0.01 sec)
-
- #赋予该用户允许复制二进制日志文件的权限
- root@(none) 15:40 mysql>grant replication slave on *.* to 'sc_slave'@'%';
- Query OK, 0 rows affected (0.00 sec)
-
- #重新加载用户权限表
- root@(none) 15:40 mysql>FLUSH PRIVILEGES;
5、在master上做一个全备,导出数据,导入到slave上,保持master和slave上的数据是一致的。
- #为master上的数据做全备,并导出
- [root@localhost /]# mkdir /backup
- [root@localhost /]# mysqldump -uroot -p'123456' --all-databases > /backup/all_db.sql
- mysqldump: [Warning] Using a password on the command line interface can be insecure.
-
6、再将导出的数据传出到slave服务器上去,并将该sql文件导入Mysql数据库中
- [root@mysql-1 ~]# scp /backup/all_db.sql root@192.168.2.151:/root
- The authenticity of host '192.168.2.151 (192.168.2.151)' can't be established.
- ECDSA key fingerprint is SHA256:5T9UOchKhhnoIu0w3essfdXjciGmtmWYTSS/XBnZasM.
- ECDSA key fingerprint is MD5:d4:bb:9a:9e:00:17:06:f7:6d:bd:e5:04:71:f5:b3:29.
- Are you sure you want to continue connecting (yes/no)? yes
- Warning: Permanently added '192.168.2.151' (ECDSA) to the list of known hosts.
- root@192.168.2.151's password:
- all_db.sql 100% 866KB 18.9MB/s 00:00
- [root@mysql-1 ~]# scp /backup/all_db.sql root@192.168.2.152:/root
- The authenticity of host '192.168.2.152 (192.168.2.152)' can't be established.
- ECDSA key fingerprint is SHA256:k1RjBZPXEHcDY+5UaqCp7ZdyW0e1daRrc1m+GVoZ5fc.
- ECDSA key fingerprint is MD5:e6:1f:62:65:8e:66:9c:b2:8e:a1:40:c3:57:ad:e5:ab.
- Are you sure you want to continue connecting (yes/no)? yes
- Warning: Permanently added '192.168.2.152' (ECDSA) to the list of known hosts.
- root@192.168.2.152's password:
- all_db.sql 100% 866KB 58.2MB/s 00:00
- [root@mysql-1 ~]#
将该sql文件导入Mysql数据库中
- [root@mysql-2 ~]# mysql -uroot -p'123456' < all_db.sql
- mysql: [Warning] Using a password on the command line interface can be insecure.
- [root@mysql-2 ~]#
检查我们的slave的Mysql数据是否与master的Mysql数据一致
7、在slave上配置master上拉取的二进制日志用户名和密码和日志文件名称和位置号和端口等信息
在master上查看二进制日志文件和位置号:
- [root@mysql-1 ~]# mysql -uroot -p123456
- mysql: [Warning] Using a password on the command line interface can be insecure.
- Welcome to the MySQL monitor. Commands end with ; or \g.
- Your MySQL connection id is 2
- Server version: 5.7.41-log MySQL Community Server (GPL)
-
- Copyright (c) 2000, 2023, Oracle and/or its affiliates.
-
- Oracle is a registered trademark of Oracle Corporation and/or its
- affiliates. Other names may be trademarks of their respective
- owners.
-
- Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
-
- # 刷新二进制文件
- root@(none) 15:51 mysql>flush logs;
- Query OK, 0 rows affected (0.01 sec)
-
- # 查看位置号
- root@(none) 15:51 mysql>show master status;
- +--------------------+----------+--------------+------------------+-------------------+
- | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
- +--------------------+----------+--------------+------------------+-------------------+
- | mysql-1-bin.000002 | 154 | | | |
- +--------------------+----------+--------------+------------------+-------------------+
- 1 row in set (0.00 sec)
-
- root@(none) 15:51 mysql>
上图所示,我通过flush logs命令刷新了二进制日志,产生了一个新的二进制日志文件,位置号为:154。
在slave上配置关于master的配置并上拉取二进制日志的用户信息和日志文件的名称和位置号
- root@(none) 15:31 mysql>CHANGE MASTER TO MASTER_HOST='192.168.2.150',
- -> MASTER_USER='sc_slave',
- -> MASTER_PASSWORD='123456',
- -> MASTER_PORT=3306,
- -> MASTER_LOG_FILE='mysql-1-bin.000002',
- -> MASTER_LOG_POS=154;
- Query OK, 0 rows affected, 2 warnings (0.01 sec)
当我们的信息配置完成后,查看slave服务器状态
- root@(none) 15:21 mysql>show slave status\G;
- *************************** 1. row ***************************
- Slave_IO_State:
- Master_Host: 192.168.2.149
- Master_User: sc_slave
- Master_Port: 3306
- Connect_Retry: 60
- Master_Log_File: mysql-bin.000006
- Read_Master_Log_Pos: 154
- Relay_Log_File: mysql-2-relay-bin.000001
- Relay_Log_Pos: 4
- Relay_Master_Log_File: mysql-bin.000006
- Slave_IO_Running: No
- Slave_SQL_Running: No
- Replicate_Do_DB:
- Replicate_Ignore_DB:
- Replicate_Do_Table:
- Replicate_Ignore_Table:
- Replicate_Wild_Do_Table:
- Replicate_Wild_Ignore_Table:
- Last_Errno: 0
- Last_Error:
- Skip_Counter: 0
- Exec_Master_Log_Pos: 154
- Relay_Log_Space: 154
- Until_Condition: None
- Until_Log_File:
- Until_Log_Pos: 0
- Master_SSL_Allowed: No
- Master_SSL_CA_File:
- Master_SSL_CA_Path:
- Master_SSL_Cert:
- Master_SSL_Cipher:
- Master_SSL_Key:
- Seconds_Behind_Master: NULL
- Master_SSL_Verify_Server_Cert: No
- Last_IO_Errno: 0
- Last_IO_Error:
- Last_SQL_Errno: 0
- Last_SQL_Error:
- Replicate_Ignore_Server_Ids:
- Master_Server_Id: 0
- Master_UUID:
- Master_Info_File: /data/mysql/master.info
- SQL_Delay: 0
- SQL_Remaining_Delay: NULL
- Slave_SQL_Running_State:
- Master_Retry_Count: 86400
- Master_Bind:
- Last_IO_Error_Timestamp:
- Last_SQL_Error_Timestamp:
- Master_SSL_Crl:
- Master_SSL_Crlpath:
- Retrieved_Gtid_Set:
- Executed_Gtid_Set:
- Auto_Position: 0
- Replicate_Rewrite_DB:
- Channel_Name:
- Master_TLS_Version:
9、在master和slave上都关闭防火墙和selinux(可以防止我们的端口信息备防火墙拦截了)
- [root@mysql-2 ~]# service firewalld stop
- Redirecting to /bin/systemctl stop firewalld.service
- [root@mysql-2 ~]# systemctl disable firewalld
- [root@mysql-2 ~]#
10、启动slave服务器,查看IO线程和SQL线程是否正常启动
直接使用start slave命令
如果启动了,可以查看如下:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
如果出现了上述内容,说明Mysql的主从复制功能成功的搭建了
- root@(none) 15:32 mysql>start slave;
- Query OK, 0 rows affected (0.01 sec)
-
- root@(none) 15:32 mysql>show slave status\G;
- *************************** 1. row ***************************
- Slave_IO_State: Waiting for master to send event
- Master_Host: 192.168.2.150
- Master_User: sc_slave
- Master_Port: 3306
- Connect_Retry: 60
- Master_Log_File: mysql-1-bin.000002
- Read_Master_Log_Pos: 154
- Relay_Log_File: mysql-2-relay-bin.000002
- Relay_Log_Pos: 322
- Relay_Master_Log_File: mysql-1-bin.000002
- Slave_IO_Running: Yes
- Slave_SQL_Running: Yes
- Replicate_Do_DB:
- Replicate_Ignore_DB:
- Replicate_Do_Table:
- Replicate_Ignore_Table:
- Replicate_Wild_Do_Table:
- Replicate_Wild_Ignore_Table:
- Last_Errno: 0
- Last_Error:
- Skip_Counter: 0
- Exec_Master_Log_Pos: 154
- Relay_Log_Space: 531
- Until_Condition: None
- Until_Log_File:
- Until_Log_Pos: 0
- Master_SSL_Allowed: No
- Master_SSL_CA_File:
- Master_SSL_CA_Path:
- Master_SSL_Cert:
- Master_SSL_Cipher:
- Master_SSL_Key:
- Seconds_Behind_Master: 0
- Master_SSL_Verify_Server_Cert: No
- Last_IO_Errno: 0
- Last_IO_Error:
- Last_SQL_Errno: 0
- Last_SQL_Error:
- Replicate_Ignore_Server_Ids:
- Master_Server_Id: 1
- Master_UUID: 692c05f7-3819-11ee-b41f-000c292d4c72
- Master_Info_File: /data/mysql/master.info
- SQL_Delay: 0
- SQL_Remaining_Delay: NULL
- Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
- Master_Retry_Count: 86400
- Master_Bind:
- Last_IO_Error_Timestamp:
- Last_SQL_Error_Timestamp:
- Master_SSL_Crl:
- Master_SSL_Crlpath:
- Retrieved_Gtid_Set:
- Executed_Gtid_Set:
- Auto_Position: 0
- Replicate_Rewrite_DB:
- Channel_Name:
- Master_TLS_Version:
- 1 row in set (0.00 sec)
-
- ERROR:
- No query specified
-
- root@(none) 15:32 mysql>
参考:Mysql - 读写分离_mysql读写分离的工具_Claylpf的博客-CSDN博客
mysqlrouter.conf 配置文件配置:(mysqlrouter必须绑定到vip上或者使用任意地址0.0.0.0)
(切记不能添加注释,我添加是为了方便解释,否则会报错)
- [root@mysqlrouter-1 mysqlrouter]# cat mysqlrouter.conf
- # Copyright (c) 2015, 2023, Oracle and/or its affiliates.
- #
- # This program is free software; you can redistribute it and/or modify
- # it under the terms of the GNU General Public License, version 2.0,
- # as published by the Free Software Foundation.
- #
- # This program is also distributed with certain software (including
- # but not limited to OpenSSL) that is licensed under separate terms,
- # as designated in a particular file or component or in included license
- # documentation. The authors of MySQL hereby grant you an additional
- # permission to link the program and your derivative works with the
- # separately licensed software that they have included with MySQL.
- #
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License, version 2.0, for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with this program; if not, write to the Free Software
- # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
-
- #
- # MySQL Router configuration file
- #
- # Documentation is available at
- # http://dev.mysql.com/doc/mysql-router/en/
-
- [DEFAULT]
- logging_folder = /var/log/mysqlrouter
- runtime_folder = /run/mysqlrouter
- config_folder = /etc/mysqlrouter
-
- [logger]
- level = INFO
-
- # If no plugin is configured which starts a service, keepalive
- # will make sure MySQL Router will not immediately exit. It is
- # safe to remove once Router is configured.
- [keepalive]
- interval = 60
-
- [routing:read_write]
- bind_address = 192.168.2.221 #vip地址 虚拟IP地址,可以在keepalived配置之前确定 当然我们可以使用0.0.0.0(任意IP地址),那么不管是vip还是本机的ip地址所对应的7001端口都能访问到
- bind_port= 7001 #自己的端口号
- destinations = 192.168.2.150:3306 #设置目的机器IP地址,可以填master的IP,端口为mysqld运行的端口
- mode = read-write #设置目的机器IP地址,可以填master的IP,端口为mysqld运行的端口
- max_connections = 65535
- max_connect_errors = 100
- client_connect_timeout = 9
-
- [routing:read_only_1]
- bind_address = 192.168.2.221
- bind_port= 7002
- destinations = 192.168.2.151:3306 #设置目的机器IP地址,可以填master的IP,端口为mysqld运行的端口
- mode = read-only
- max_connections = 65535
- max_connect_errors = 100
- client_connect_timeout = 9
-
- [routing:read_only_2]
- bind_address = 192.168.2.221
- bind_port= 7003
- destinations = 192.168.2.152:3306 #设置目的机器IP地址,可以填master的IP,端口为mysqld运行的端口
- mode = read-only
- max_connections = 65535
- max_connect_errors = 100
- client_connect_timeout = 9
- [root@mysqlrouter-1 mysqlrouter]#
成功刷新mysqlrouter服务
- [root@mysqlrouter-1 mysqlrouter]# service mysqlrouter restart
- Redirecting to /bin/systemctl restart mysqlrouter.service
- [root@mysqlrouter-1 mysqlrouter]#
查看端口:
- [root@mysqlrouter-1 mysqlrouter]# netstat -anpult |grep mysqlrouter
- tcp 0 0 192.168.2.221:7001 0.0.0.0:* LISTEN 42847/mysqlrouter
- tcp 0 0 192.168.2.221:7002 0.0.0.0:* LISTEN 42847/mysqlrouter
- tcp 0 0 192.168.2.221:7003 0.0.0.0:* LISTEN 42847/mysqlrouter
- [root@mysqlrouter-1 mysqlrouter]#
下载keepalived服务:
- [root@mysqlrouter-1 mysqlrouter]# yum install keepalived -y
- 已安装:
- keepalived.x86_64 0:1.3.5-19.el7
-
- 作为依赖被安装:
- lm_sensors-libs.x86_64 0:3.4.0-8.20160601gitf9185e5.el7 net-snmp-agent-libs.x86_64 1:5.7.2-49.el7_9.2 net-snmp-libs.x86_64 1:5.7.2-49.el7_9.2
-
- 完毕!
master上的配置
- global_defs {
- notification_email {
- acassen@firewall.loc
- failover@firewall.loc
- sysadmin@firewall.loc
- }
- notification_email_from Alexandre.Cassen@firewall.loc
- smtp_server 192.168.200.1
- smtp_connect_timeout 30
- router_id LVS_DEVEL
- vrrp_skip_check_adv_addr
- # vrrp_strict # 这一行需要注释:原因是因为会在iptables里添加一条规则,会阻止网络通信,导致出现脑裂现象
- vrrp_garp_interval 0
- vrrp_gna_interval 0
- }
-
- vrrp_instance VI_1 { # 定义一个vrrp协议的实例 名字是VI_1 表示第一个vrrp实例
- state MASTER # 做master角色
- interface ens33 # 指定监听网络的接口,其实就是vip绑定到那个网络接口上
- virtual_router_id 81 # 虚拟路由器ID 81表示的是编号,另外一台机器上的配置也必须是81
- priority 160 # 优先级 数字越大就越容易成为master
- advert_int 1 # 宣告消息的时间间隔 为1s
- authentication {
- auth_type PASS # 密码认证 password
- auth_pass 1111 # 具体密码 可以不用修改
- }
- virtual_ipaddress { # vip 虚拟ip地址 可以配置多个
- 192.168.2.221 # 配置的vip
- #192.168.200.17
- #192.168.200.18
- }
- }
backup上的配置
- [root@mysqlrouter-2 keepalived]# cat keepalived.conf
- global_defs {
- notification_email {
- acassen@firewall.loc
- failover@firewall.loc
- sysadmin@firewall.loc
- }
- notification_email_from Alexandre.Cassen@firewall.loc
- smtp_server 192.168.200.1
- smtp_connect_timeout 30
- router_id LVS_DEVEL
- vrrp_skip_check_adv_addr
- # vrrp_strict # 这一行需要注释:原因是因为会在iptables里添加一条规则,会阻止网络通信,导致出现脑裂现象
- vrrp_garp_interval 0
- vrrp_gna_interval 0
- }
-
- vrrp_instance VI_1 { # 定义一个vrrp协议的实例 名字是VI_1 表示第一个vrrp实例
- state BACKUP # 做backup角色
- interface ens33 # 指定监听网络的接口,其实就是vip绑定到那个网络接口上
- virtual_router_id 81 # 虚拟路由器ID 81表示的是编号,另外一台机器上的配置也必须是81
- priority 100 # 优先级 数字越大就越容易成为master
- advert_int 1 # 宣告消息的时间间隔 为1s
- authentication {
- auth_type PASS # 密码认证 password
- auth_pass 1111 # 具体密码 可以不用修改
- }
- virtual_ipaddress { # vip 虚拟ip地址 可以配置多个
- 192.168.2.221 # 配置的vip 必须与master配置的vip是一样的
- #192.168.200.17
- #192.168.200.18
- }
- }
- [root@mysqlrouter-2 keepalived]#
重新启动keepalived服务
- [root@mysqlrouter-1 keepalived]# service keepalived restart
- Redirecting to /bin/systemctl restart keepalived.service
- [root@mysqlrouter-1 keepalived]#
查看效果:
- [root@mysqlrouter-1 keepalived]# ip add
- 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
- link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
- inet 127.0.0.1/8 scope host lo
- valid_lft forever preferred_lft forever
- inet6 ::1/128 scope host
- valid_lft forever preferred_lft forever
- 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
- link/ether 00:0c:29:90:87:dc brd ff:ff:ff:ff:ff:ff
- inet 192.168.2.181/24 brd 192.168.2.255 scope global noprefixroute ens33
- valid_lft forever preferred_lft forever
- inet 192.168.2.221/32 scope global ens33
- valid_lft forever preferred_lft forever
- inet6 fe80::20c:29ff:fe90:87dc/64 scope link
- valid_lft forever preferred_lft forever
- [root@mysqlrouter-1 keepalived]#
我们将master上的keepalived关闭,查看backup上的ip地址
- [root@mysqlrouter-1 keepalived]# service keepalived stop
- Redirecting to /bin/systemctl stop keepalived.service
- [root@mysqlrouter-1 keepalived]#
backup上的IP地址
- [root@mysqlrouter-2 keepalived]# ip add
- 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
- link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
- inet 127.0.0.1/8 scope host lo
- valid_lft forever preferred_lft forever
- inet6 ::1/128 scope host
- valid_lft forever preferred_lft forever
- 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
- link/ether 00:0c:29:a9:7f:87 brd ff:ff:ff:ff:ff:ff
- inet 192.168.2.182/24 brd 192.168.2.255 scope global noprefixroute ens33
- valid_lft forever preferred_lft forever
- inet 192.168.2.221/32 scope global ens33
- valid_lft forever preferred_lft forever
- inet6 fe80::20c:29ff:fea9:7f87/64 scope link
- valid_lft forever preferred_lft forever
- [root@mysqlrouter-2 keepalived]#
成功观察到我们想要的效果,vip真的漂移到了backup上了
当我们重启master,vip又会从backup上漂移到master上去的
- [root@mysqlrouter-1 keepalived]# service keepalived start
- Redirecting to /bin/systemctl start keepalived.service
- [root@mysqlrouter-1 keepalived]# ip add
- 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
- link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
- inet 127.0.0.1/8 scope host lo
- valid_lft forever preferred_lft forever
- inet6 ::1/128 scope host
- valid_lft forever preferred_lft forever
- 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
- link/ether 00:0c:29:90:87:dc brd ff:ff:ff:ff:ff:ff
- inet 192.168.2.181/24 brd 192.168.2.255 scope global noprefixroute ens33
- valid_lft forever preferred_lft forever
- inet 192.168.2.221/32 scope global ens33
- valid_lft forever preferred_lft forever
- inet6 fe80::20c:29ff:fe90:87dc/64 scope link
- valid_lft forever preferred_lft forever
- [root@mysqlrouter-1 keepalived]#
当我通过Linux上访问192.168.2.221:7001端口的时候,他会访问到我们的master机器的3306端口上去(Mysql数据库的默认端口是3306)
- [root@mysql-2 ~]# mysql -h 192.168.2.221 -P 7001 -u claylpf -p
- Enter password:
- Welcome to the MySQL monitor. Commands end with ; or \g.
- Your MySQL connection id is 21
- Server version: 5.7.41-log MySQL Community Server (GPL)
-
- Copyright (c) 2000, 2023, Oracle and/or its affiliates.
-
- Oracle is a registered trademark of Oracle Corporation and/or its
- affiliates. Other names may be trademarks of their respective
- owners.
-
- Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
-
- claylpf@(none) 18:54 mysql>exit
- Bye
- [root@test ~]#
-
- [root@mysql-2 ~]# mysql -h 192.168.2.221 -P 7002 -u claylpf -p
- Enter password:
- Welcome to the MySQL monitor. Commands end with ; or \g.
- Your MySQL connection id is 20
- Server version: 5.7.41 MySQL Community Server (GPL)
-
- Copyright (c) 2000, 2023, Oracle and/or its affiliates.
-
- Oracle is a registered trademark of Oracle Corporation and/or its
- affiliates. Other names may be trademarks of their respective
- owners.
-
- Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
-
- claylpf@(none) 19:15 mysql>
双 VIP 可以用于实现高可用性架构。在一个服务器出现故障时,流量可以被自动切换到另一个服务器,从而保持服务的连续性。这在负载均衡、高可用的网络应用中特别有用。
master配置文件:
- [root@mysqlrouter-1 keepalived]# cat keepalived.conf
- global_defs {
- notification_email {
- acassen@firewall.loc
- failover@firewall.loc
- sysadmin@firewall.loc
- }
- notification_email_from Alexandre.Cassen@firewall.loc
- smtp_server 192.168.200.1
- smtp_connect_timeout 30
- router_id LVS_DEVEL
- vrrp_skip_check_adv_addr
- # vrrp_strict
- vrrp_garp_interval 0
- vrrp_gna_interval 0
- }
-
- vrrp_instance VI_1 {
- state MASTER
- interface ens33
- virtual_router_id 81
- priority 160
- advert_int 1
- authentication {
- auth_type PASS
- auth_pass 1111
- }
- virtual_ipaddress {
- 192.168.2.221
- }
- }
-
- vrrp_instance VI_2 {
- state BACKUP
- interface ens33
- virtual_router_id 101
- priority 100
- advert_int 1
- authentication {
- auth_type PASS
- auth_pass 1111
- }
- virtual_ipaddress {
- 192.168.2.201
- }
- }
- [root@mysqlrouter-1 keepalived]#
backup配置文件:
- [root@mysqlrouter-2 keepalived]# cat keepalived.conf
- global_defs {
- notification_email {
- acassen@firewall.loc
- failover@firewall.loc
- sysadmin@firewall.loc
- }
- notification_email_from Alexandre.Cassen@firewall.loc
- smtp_server 192.168.200.1
- smtp_connect_timeout 30
- router_id LVS_DEVEL
- vrrp_skip_check_adv_addr
- # vrrp_strict # 这一行需要注释:原因是因为会在iptables里添加一条规则,会阻止网络通信,导致出现脑裂现象
- vrrp_garp_interval 0
- vrrp_gna_interval 0
- }
-
- vrrp_instance VI_1 { # 定义一个vrrp协议的实例 名字是VI_1 表示第一个vrrp实例
- state BACKUP # 做backup角色
- interface ens33 # 指定监听网络的接口,其实就是vip绑定到那个网络接口上
- virtual_router_id 81 # 虚拟路由器ID 81表示的是编号,另外一台机器上的配置也必须是51
- priority 100 # 优先级 数字越大就越容易成为master
- advert_int 1 # 宣告消息的时间间隔 为1s
- authentication {
- auth_type PASS # 密码认证 password
- auth_pass 1111 # 具体密码 可以不用修改
- }
- virtual_ipaddress { # vip 虚拟ip地址 可以配置多个
- 192.168.2.221 # 配置的vip 必须与master配置的vip是一样的
- }
- }
-
- vrrp_instance VI_2 { # 定义一个vrrp协议的实例 名字是VI_1 表示第一个vrrp实例
- state MASTER # 做backup角色
- interface ens33 # 指定监听网络的接口,其实就是vip绑定到那个网络接口上
- virtual_router_id 101 # 虚拟路由器ID 101表示的是编号,另外一台机器上的配置也必须是101
- priority 120 # 优先级 数字越大就越容易成为master
- advert_int 1 # 宣告消息的时间间隔 为1s
- authentication {
- auth_type PASS # 密码认证 password
- auth_pass 1111 # 具体密码 可以不用修改
- }
- virtual_ipaddress { # vip 虚拟ip地址 可以配置多个
- 192.168.2.201 # 配置的vip 必须与master配置的vip是一样的
- }
- }
- [root@mysqlrouter-2 keepalived]#
查看master和backup的IP地址
配置负载均衡器来实现流量的合理分配:
我们可以通过DNS域名解析来配置负载均衡器或者是通过nginx来配置负载均衡器
Mysql压力测试工具:Mysqlslap、Sysbench(数据库专用测试工具)
可以模拟每秒钟处理完的请求数,每秒处理事务处,一次请求所需的平均数,系统内处理的并发请求数。
"脑裂"(Brain Split)通常用于指代分布式计算系统中的一个问题,特别是在集群环境下,其中集群中的节点在某种情况下无法与其他节点进行通信,导致节点之间的数据不一致。脑裂问题可能会导致严重的系统故障和数据损坏。
脑裂问题的危害在于:
数据不一致性:脑裂可能导致节点之间的数据不一致。如果在脑裂发生时,某些节点认为其他节点不可用,从而导致不同节点之间的数据更新没有被正确传播,最终导致数据不一致。
故障和系统崩溃:脑裂可能导致系统中的节点以不同的状态运行,从而影响整个系统的稳定性。这可能导致故障和系统崩溃,因为无法在节点之间协调操作。
无法达成共识:分布式系统通常需要在节点之间达成共识以进行操作,例如领导选举、数据复制等。脑裂可能会破坏共识机制,导致系统无法正常运行。
脑裂现象:
1、vrid不一样的时候会产生
2、中间存在防火墙,阻止了网络之间的keepalived的master和backup的选举过程,vrrp报文无法通信(网络通信出现错误)
3、认证密码不一样也会出现脑裂现象
当我们的master的vrid是82,而backup上的vrid是81,那么会产生脑裂现象
master上和backup上都出现了vip,这说明出现了脑裂现象
添加iptables防火墙规则
除了22号端口(ssh协议)能够通信,其他端口都不能通信
查看 master上和backup上是否都出现了vip,发现都出现了,说明产生了脑裂现象
最后去除防火墙规则
查看 master上和backup上是否都出现了vip,发现都出现了,说明产生了脑裂现象
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。