孤独海岸线 发表于 2018-12-29 06:33:13

Mysql5.7.22+Keepalived双主互备高可用集群

  #安装ntpdate工具
yum install ntpdate -y
#使用ntpdate校时(后面的是ntp服务器)
ntpdate pool.ntp.org
定时任务(高可用集群时间同步很重要)
/5 * /usr/sbin/ntpdate pool.ntp.org >/dev/null 2>&1
每五分钟同步一次时间
  分别在下面两天上面部署MySQL和keepalived
DB1:192.168.254.128
DB2:192.168.254.129
  配置mysql双主备
  安装数据库链接(在主页数据库里面可以看到)
http://blog.运维网.com/10158955/1926574
  DB1修改配置文件(需重启)
vi /etc/my.cnf
#在添加
server-id=166
#开启mysql日志功能
log-bin=mysql-bin
#定义日志命名格式
relay-log=mysql-relay-bin
#以下table复制过滤
replicate-wild-ignore-table=test.%
replicate-wild-ignore-table=mysql.%
replicate-wild-ignore-table=performance_schema.%
  DB2修改配置文件(需重启)
vi /etc/my.cnf
#在添加
server-id=168
#开启mysql日志功能
log-bin=mysql-bin
#定义日志命名格式
relay-log=mysql-relay-bin
  DB1,DB2分别创建复制帐号
mysql -u root -p
#创建用户slave_up允许从192.168.254网段登录
create user 'slave_cp'@'192.168.254.%' identified by 'pass';
grant replication slave on . to 'slave_cp'@'192.168.254.%';
exit
DB1,DB2分别获取二进制日志信息
mysql -u root -p
#对数据库进行只读锁定(防止查看二进制日志同时有人对数据库修改操作)
flush tables with read lock;
#查询主机二进制文件信息
show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000005 |      154 |            |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
#解除只读锁定
unlock tables;
  数据库里面的数据一定要相同!!!如果不同就要先做同步数据!
  在DB1和DB2上分别设置对方为主服务器!
  change master to
master_host='192.168.254.128' ,
master_user='slave_cp',
master_password='pass',
master_log_file='mysql-bin.000001',
master_log_pos=154;
  change master to
master_host='192.168.254.129' ,
master_user='slave_cp',
master_password='pass',
master_log_file='mysql-bin.000001',
master_log_pos=154;
  #启动slave
start slave;
#分别查看DB1,DB2是否正常工作
DB1:192.168.254.128服务器
show slave status\G
1. row
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.254.129
Master_User: slave_cp
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 154
Relay_Log_File: mysql-relay-bin.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
  DB2:192.168.254.129服务器
mysql> show slave status\G
1. row
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.254.128
Master_User: slave_cp
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000005
Read_Master_Log_Pos: 154
Relay_Log_File: mysql-relay-bin.000011
Relay_Log_Pos: 367
Relay_Master_Log_File: mysql-bin.000005
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
  检验双主互备
①通过分别在两台服务器上使用show slave status\G,查询主库信息以及IO进程、SQL进程工作状态。若两台服务器的查询结果都为Slave_IO_Running: Yes,Slave_SQL_Running: Yes;则表示当前双主互备状态正常。
②在Mysql248数据库上建库建表,检查Mysql249上是否同步正常;然后在Mysql249上建库建表,检查Mysql248上是否同步正常。
  测试通过,可以同步库或者表 ,不上图了有点麻烦 (自己测试一下)
成功之后退出MySQL
exit

  配置keepalived实现MySQL双主高可用
128服务器上配置
! Configuration File for keepalived
global_defs {
#设置报警通知邮件地址,可以设置多个
notification_email {      admin@163.combr/>admin@163.com
页: [1]
查看完整版本: Mysql5.7.22+Keepalived双主互备高可用集群