2019.10.17 MySQL 主主複製+Keepalived

1、MySQL主主同步mysql

須要兩臺虛擬機192.168.200.1十一、192.168.200.112sql

一、第1臺機器:vim

[root@localhost ~]# vim /etc/my.cnfbash

server-id=1
log-bin=mysql-binlog
log-slave-updates=true
max_binlog_size=1024M
auto_increment_offset = 1
auto_increment_increment = 2
replicate-ignore-db = information_schema
replicate-ignore-db = performance_schema
replicate-ignore-db = test
replicate-ignore-db = mysql
max_connections = 3000
max_connect_errors = 30
skip-character-set-client-handshake
init-connect='SET NAMES utf8'
character-set-server=utf8
wait_timeout=1800
interactive_timeout=1800
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
relay-log=relay-log-bin
relay-log-index=slave-relay-bin.indexide

[root@localhost ~]# systemctl restart mariadb測試

[root@localhost ~]# mysqlspa

MariaDB [(none)]> grant replication slave on *.* to 'repl'@'192.168.200.112' identified by '123456';rest

Query OK, 0 rows affected (0.00 sec)orm

MariaDB [(none)]> flush privileges;router

Query OK, 0 rows affected (0.00 sec)

 MariaDB [(none)]> show master status;

二、第2臺機器:

[root@localhost ~]# vim /etc/my.cnf

[mysqld]

server-id= 2

log-bin=mysql-binlog

log-slave-updates=true

max_binlog_size=1024M

auto_increment_offset = 2

auto_increment_increment = 2#偶數ID

replicate-ignore-db = information_schema

replicate-ignore-db = performance_schema

replicate-ignore-db = test

replicate-ignore-db = mysql

max_connections = 3000

max_connect_errors = 30

skip-character-set-client-handshake

init-connect='SET NAMES utf8'

character-set-server=utf8

wait_timeout=1800

interactive_timeout=1800

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

relay-log=relay-log-bin

relay-log-index=slave-relay-bin.index

[root@localhost ~]# systemctl restart mariadb

[root@localhost ~]# mysql

MariaDB [(none)]> grant replication slave on *.* to 'repl'@'192.168.200.111' identified by '123456';

Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> flush privileges;

Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> show master status;

三、測試:

(1)第1臺機器

MariaDB [(none)]> stop slave;

Query OK, 0 rows affected, 1 warning (0.00 sec)

MariaDB [(none)]> change master to master_host='192.168.200.112',master_port=3306,master_user='repl',master_password='123',master_log_file='mysql-binlog.000001',master_log_pos=486;

Query OK, 0 rows affected (0.04 sec)

MariaDB [(none)]> start slave;

Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> show slave status\G

(2)第2臺機器

MariaDB [(none)]> stop slave;

Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> change master to master_host='192.168.200.111',master_port=3306,master_user='repl',master_password='123',master_log_file='mysql-binlog.000004',master_log_pos=486;

Query OK, 0 rows affected (0.13 sec)

MariaDB [(none)]> start slave;

Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> show slave status\G

四、建立文件測試:

第1臺機器

MariaDB [(none)]> create database test01;

Query OK, 1 row affected (0.01 sec)

第2臺機器

MariaDB [(none)]> show databases;                     //裏面有在第一臺機器上建立的表

MariaDB [(none)]> create database test02;

Query OK, 1 row affected (0.00 sec)

第1臺機器

MariaDB [(none)]> show databases;                      //裏面有在第二臺機器上建立的表

 2、MySQL主主高可用方案

一、111配置:

[root@localhost ~]# yum -y install keepalived

[root@localhost ~]# vim /etc/keepalived/keepalived.conf 

! Configuration File for keepalived

global_defs {

   router_id LVS_MASTER-A

}

vrrp_script mysql {

    script "/opt/mysql.sh"

    interval 2

    weight -5                 

    fall 2                 

    rise 1

}

vrrp_instance VI_1 {

    state BACKUP

    interface ens32

    virtual_router_id 51

priority 100

nopreempt

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    track_script {

        mysql

    }

    virtual_ipaddress {

        192.168.200.254

    }

}

二、寫腳本:

[root@localhost ~]# vim /opt/mysql.sh

#!/bin/bash

counter=$(netstat -na|grep "LISTEN"|grep "3306"|wc -l)

if [ "${counter}" -eq 0 ]; then

    systemctl stop keepalived

fi

三、加權、啓動、查看

[root@localhost ~]# chmod +x /opt/mysql.sh

[root@localhost ~]# systemctl start keepalived

[root@localhost ~]# ip a | grep eno16777728

四、112配置:

在111上操做:

[root@localhost ~]# scp /etc/keepalived/keepalived.conf 192.168.200.112:/etc/keepalived/

[root@localhost ~]# scp /opt/mysql.sh 192.168.200.112:/opt/

112:

[root@localhost ~]# yum -y install keepalived

[root@localhost ~]# vim /etc/keepalived/keepalived.conf                    //稍做調整

! Configuration File for keepalived

global_defs {

   router_id LVS_MASTER-B

}

vrrp_script mysql {

    script "/opt/mysql.sh"

    interval 2

    weight -5                 

    fall 2                 

    rise 1

}

vrrp_instance VI_1 {

    state BACKUP

    interface ens32

    virtual_router_id 51

priority 99

nopreempt

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    track_script {

        mysql

    }

    virtual_ipaddress {

        192.168.200.254

    }

}

[root@localhost ~]# chmod +x /opt/mysql.sh

[root@localhost ~]# systemctl start keepalived

五、測試VIP轉移

先用ip -a查看111上有沒有254:

而後停掉服務:

[root@localhost ~]# systemctl stop mariadb

112:ip -a      //發現254轉移到了112上

六、在遠程客戶端測試

(1)1十一、112都進入mysql

[root@localhost ~]# mysql

MariaDB [(none)]> grant all on *.* to 'root'@'192.168.200.%' identified by '123456';

MariaDB [(none)]> flush privileges;

(2)經過VIP登陸測試:

[root@localhost ~]# mysql -uroot -p123 -h 192.168.200.254

Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MariaDB connection id is 5796

Server version: 5.5.56-MariaDB MariaDB Server

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;                            //裏面有以前建立的表

相關文章
相關標籤/搜索