用keepalived做mysql高可用

两台机器(centos7系统)安装mysql

[root@mysql-keep-master ~]# wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
[root@mysql-keep-master ~]# rpm -ivh mysql80-community-release-el7-3.noarch.rpm
[root@mysql-keep-master ~]# yum -y install yum-utils #安装yum工具包
[root@mysql-keep-master ~]# vim /etc/yum.repos.d/mysql-community.repo

安装mysql
[root@mysql-keep-master ~]# yum install -y mysql-community-server
[root@mysql-keep-master ~]# systemctl start mysqld 
[root@mysql-keep-master ~]# systemctl enable mysqld
修改密码
[root@mysql-keep-master ~]# grep pass /var/log/mysqld.log 
2020-02-18T13:54:25.706635Z 1 [Note] A temporary password is generated for root@localhost: %baWi=4!NsT)
[root@mysql-keep-master ~]# mysqladmin -uroot -p'%baWi=4!NsT)' password 'Admin@123!'

 制作互为主从:
[root@mysql-keep-master ~]# vim /etc/my.cnf   #编辑配置文件开启gtid
log-bin=m1.log  
server-id=1 
gtid_mode = ON    #开启gtid
enforce_gtid_consistency=1  #强制gtid移植性

[root@mysql-keep-master ~]# systemctl restart mysqld 
[root@mysql-keep-master ~]# mysql -uroot -p'Admin@123!'  #登录数据库

 mysql> GRANT ALL ON *.* TO 'root'@'%' IDENTIFIED BY 'Admin@123';  #创建远程登录用户

mysql> flush privileges;
mysql> grant replication  slave,reload,super  on *.*  to  'slave'@'%'  identified by  'Admin@123';  ---创建主从授权用户

mysql> flush privileges;  #刷新授权
mysql> \e   #指定主服务器信息
CHANGE MASTER TO
MASTER_HOST='192.168.58.136',      #指定主服务器的ip或者主机名
MASTER_USER='slave',           #授权用户
MASTER_PASSWORD='QianFeng@123',  #授权用户密码
master_auto_position=1
-> ;
启动slave
mysql> start slave;

[root@mysql-keep-backup ~]# vim /etc/my.cnf
log-bin=m2.log
server-id=2
gtid_mode = ON
enforce_gtid_consistency=1
[root@mysql-keep-backup ~]# systemctl restart mysqld 
[root@mysql-keep-backup ~]# mysql -uroot -p'QianFeng@123!'
mysql> GRANT ALL ON *.* TO 'root'@'%' IDENTIFIED BY 'QianFeng@123';
mysql> flush privileges;
mysql> grant replication  slave,reload,super  on *.*  to  'slave'@'%'  identified by  'QianFeng@123';
mysql> flush privileges;
指定主服务器信息
mysql> \e
CHANGE MASTER TO
MASTER_HOST='192.168.58.135',
MASTER_USER='slave',
MASTER_PASSWORD='QianFeng@123',
master_auto_position=1
-> ;
启动slave
mysql> start slave;

两台机器验证是否为yes

mysql> show slave status\G 

两台机器安装keepalived

[root@mysql-keep-master ~]# yum -y install keepalived
[root@mysql-keep-master ~]# cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
[root@mysql-keep-master ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalivedglobal_defs {router_id master1
}vrrp_instance VI_1 {state MASTERinterface ens33virtual_router_id 80priority 100advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.58.16/24}
}
将配置文件拷贝到backu机器
[root@mysql-keep-backup ~]# mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
[root@mysql-keep-master ~]# scp /etc/keepalived/keepalived.conf 192.168.58.136:/etc/keepalived/
[root@mysql-keep-backup ~]# vim /etc/keepalived/keepalived.conf  #修改如下
! Configuration File for keepalivedglobal_defs {router_id backup1
}vrrp_instance VI_1 {state BACKUPinterface ens33virtual_router_id 80priority 50advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.58.16/24}
}

两台机器启动keepalived

[root@mysql-keep-master ~]# systemctl start keepalived
[root@mysql-keep-backup ~]# systemctl start keepalived
[root@mysql-keep-master ~]# ip a   #vip在master上面
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host lovalid_lft forever preferred_lft foreverinet6 ::1/128 scope host valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000link/ether 00:0c:29:92:0a:64 brd ff:ff:ff:ff:ff:ffinet 192.168.58.135/24 brd 192.168.58.255 scope global dynamic ens33valid_lft 1729sec preferred_lft 1729secinet 192.168.58.16/24 scope global secondary ens33valid_lft forever preferred_lft foreverinet6 fe80::86f4:1d74:ad05:c61c/64 scope link valid_lft forever preferred_lft forever
测试:
[root@mysql-keep-master ~]# mysql -uroot -p'Admin@123' -h 192.168.58.16  #登录vip
mysql> create database test1;
Query OK, 1 row affected (0.07 sec)mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test1              |
+--------------------+
5 rows in set (0.08 sec)
验证
[root@mysql-keep-backup ~]# mysql -uroot -p'Admin@123!'
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test1              |
+--------------------+
5 rows in set (0.12 sec)
[root@mysql-keep-master ~]# systemctl stop keepalived #关闭master
[root@mysql-keep-backup ~]# ip a  #vip切换backup机器
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host lovalid_lft forever preferred_lft foreverinet6 ::1/128 scope host valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000link/ether 00:0c:29:a2:0f:5c brd ff:ff:ff:ff:ff:ffinet 192.168.58.136/24 brd 192.168.58.255 scope global dynamic ens33valid_lft 1547sec preferred_lft 1547secinet 192.168.58.16/24 scope global secondary ens33valid_lft forever preferred_lft foreverinet6 fe80::af6e:2eff:e2d1:99cb/64 scope link valid_lft forever preferred_lft forever
测试登录
[root@mysql-keep-master ~]# mysql -uroot -p'Admin@123' -h 192.168.58.16

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/256785.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【沐风老师】3dMax椅子建模教程

3dMax椅子建模详解 1.将系统单位设置为&#xff1a;毫米。在前视图创建一个矩形&#xff0c;并将四个角倒圆角。 2.开启二维图形可渲染功能&#xff0c;设置线条粗细。参数如图&#xff1a; 3.应用“FFD 2x2x2”修改器&#xff0c;在前视图中&#xff0c;选中右下角所有控制点&…

IntelliJ IDEA开启git版本控制的简单教程

这篇文章想要分享一下怎么在IntelliJ IDEA开启版本控制&#xff0c;博主使用的是gitee&#xff0c;首先需要安装git&#xff0c;关于git的安装这里就不介绍了&#xff0c;很简单。 目录 创建git仓库 创建项目 开启版本控制 拉取项目 创建git仓库 首先&#xff0c;需要登录…

uni-app 微信小程序之好看的ui登录页面(三)

文章目录 1. 页面效果2. 页面样式代码 更多登录ui页面 uni-app 微信小程序之好看的ui登录页面&#xff08;一&#xff09; uni-app 微信小程序之好看的ui登录页面&#xff08;二&#xff09; uni-app 微信小程序之好看的ui登录页面&#xff08;三&#xff09; uni-app 微信小程…

Hadoop3.x完全分布式环境搭建Zookeeper和Hbase

先在主节点上进行安装和配置&#xff0c;随后分发到各个从节点上。 1. 安装zookeeper 1.1 解压zookeeper并添加环境变量 1&#xff09;解压zookeeper到/usr/local文件夹下 tar -zxvf /usr/local2&#xff09;进入/usr/local文件夹将apache-zookeeper-3.8.0-bin改名为zookeep…

MongoDB的连接数据库,创建、删除数据库,创建、删除集合命令

本文主要介绍MongoDB的连接数据库&#xff0c;创建、删除数据库&#xff0c;创建、删除集合命令。 目录 MongoDB连接数据库连接到本地 MongoDB 实例连接到远程 MongoDB 实例 MongoDB创建和删除数据库MongoDB创建和删除集合创建集合删除集合 MongoDB连接数据库 连接 MongoDB 数…

20231207给NanoPC-T4(RK3399)开发板刷Android12的挖掘机方案的LOG

20231207给NanoPC-T4(RK3399)开发板刷Android12的挖掘机方案的LOG 2023/12/7 23:50 SDK&#xff1a;rk356x_android12_220722.tgz 只修改DTS的DTC部分就【直接】可以跑NanoPC-T4 参考资料&#xff1a; http://www.friendlyelec.com.cn/agent.asp http://www.friendlyelec.com.c…

月报总结|Moonbeam 11月份大事一览

本月Moonbeam也在马不停蹄持续更新&#xff01;随着之前USDC宣布登陆波卡之后&#xff0c;本月也通过XCM来到了Moonbeam&#xff0c;连接区块链世界与传统金融&#xff1b;治理方面&#xff0c;Moonbeam基金会鼓励社区踊跃参与并投票选出新一轮的财政库委员会&#xff0c;为生态…

Java se的语言特征之封装

目录 封装的概念常见的一些包静态成员变量代码块 封装的概念 可以理解为套壳屏蔽细节,将数据和操作数据的方法进行有机的结合,隐藏对象的属性和实现细节,仅对外公开接口和对象进行交互 从语法的层面来理解就是,被private修饰的成员变或者成员方法,只能在当前类中使用,但是可以…

Matlab 用矩阵画图

文章目录 Part.I IntroductionChap.I 预备知识Chap.II 概要Chap.III 杂记 Part.II 用矩阵画图Chap.I 摸索过程Chap.II 绘制专业图Chap.III 矩阵转tiff Part.I Introduction 本文汇总了 Matlab 用矩阵画图的几种方式。 Chap.I 预备知识 关于 *.mat 文件 *.mat文件是 matlab 的…

Appium 并行测试多个设备

一、前置说明 在自动化测试中&#xff0c;经常需要验证多台设备的兼容性&#xff0c;Appium可以用同一套测试运例并行测试多个设备&#xff0c;以达到验证兼容性的目的。 解决思路&#xff1a; 查找已连接的所有设备&#xff1b;为每台设备启动相应的Appium Server&#xff1b…

【React】路由的基础使用

react-router-dom6的基础使用 1、安装依赖 npm i react-router-dom默认安装最新版本的 2、在src/router/index.js import { createBrowserRouter } from "react-router-dom"/* createBrowserRouter&#xff1a;[/home]--h5路由createHashRouter&#xff1a;[/#/ho…

axios调接口传参特殊字符丢失的问题(encodeURI 和 encodeURIComponent)

1、axios调接口特殊字符丢失的问题 项目开发过程中遇到一个接口传参&#xff0c;参数带特殊字符&#xff0c;axios调接口特殊字符丢失的问题 例如接口&#xff1a; get/user/detail/{name} name是个参数直接调接口的时候拼到接口上&#xff0c;get/user/detail/test123#$%&am…