Mysql主主同步实现


1,基本的思路是排除数据库单点故障,保障数据高可用性。

2,前期对于要求不高的话可以用主主同步来实现,后期如果不能满足要求可以在后面加从数据库。

3,实验需要的环境及数据库的版本如下表格:

序号

名称

IP地址

数据库版本

系统版本

1

MySQL-Master01

10.93.58.72

mysql-5.5.32

CentOS release 6.9

2

MySQL-Master02

10.93.58.73

mysql-5.5.32

CentOS release 6.9

3

Test

10.93.58.70

CentOS release 6.9

4

virtual IP

10.93.58.74


4,正式安装数据步骤:

1)我是用了一个脚本来安装这个Mysql-5.5.32数据库,脚本如下:

#!/bin/bash
#auto_install_mysql
#auth by tony date 2018-07-31
yum -y install gcc gcc-c++ make ncurses ncurses-devel libaio-devel cmake
groupadd mysql
useradd mysql -s/sbin/nologin -M -g mysql
mkdir /application
wget http://10.93.58.70/lamp/mysql-5.5.32.tar.gz
tar xvf mysql-5.5.32.tar.gz
cd mysql-5.5.32
cmake . -DCMAKE_INSTALL_PREFIX=/application/mysql-5.5.32 \
-DMYSQL_DATADIR=/data/mysql \
-DMYSQL_UNIX_ADDR=/application/mysql-5.5.32/tmp/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DEXTRA_CHARSETS=gbk,gb2312,utf8,ascii \
-DENABLED_LOCAL_INFILE=ON \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \
-DWITHOUT_PARTITION_STORAGE_ENGINE=1 \
-DWITH_FAST_MUTEXES=1 \
-DWITH_ZLIB=bundled \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_READLINE=1 \
-DWITH_EMBEDDED_SERVER=1 \
-DWITH_DEBUG=0
if [ $? -eq 0 ];then
    make && make install
        ln -s /application/mysql-5.5.32/ /application/mysql
        /bin/cp support-files/mysql.server /etc/init.d/mysqld
        chmod +x /etc/init.d/mysqld
        chown mysql.mysql /application/mysql
        chown mysql.mysql /data
        echo 'export PATH=/application/mysql/bin:$PATH '>>/etc/profile
        source /etc/profile
        mv /etc/my.cnf /etc/my.cnf.bak
        cat >/etc/my.cnf <<EOF
[mysqld]
port            = 3306
socket          = /application/mysql-5.5.32/tmp/mysql.sock
datadir         =/data/mysql
user            =mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-bin=mysql-bin
server-id = 1
auto_increment_offset=1
auto_increment_increment=2
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
replicate-do-db =all
EOF
/application/mysql/scripts/mysql_install_db --basedir=/application/mysql/ --datadir=/data/mysql --user=mysql
/etc/init.d/mysqld start
chkconfig mysqld on
mysqladmin -uroot password 'hwg123'
                         echo -e "\n\033[32m-----------------------------------------------\033[0m"
                echo -e "\033[32mThe $M_FILES_DIR Server Install Success !\033[0m"
        else
                echo -e "\033[32mThe $M_FILES_DIR Make or Make install ERROR,Please Check......"
                exit 0
        fi

"mysqlset.sh" 66L, 2228C written

 2)查看my.cnf配置文件

[root@Mysql-Master ~]# cat /etc/my.cnf

[mysqld]
port            = 3306
socket          = /application/mysql-5.5.32/tmp/mysql.sock
datadir         =/data/mysql
user            =mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-bin=mysql-bin
binlog_format = mixed
server-id = 1
auto_increment_offset=1
auto_increment_increment=2
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
replicate-do-db =all

3)进入数据库Master01:

[root@Mysql-Master ~]# mysql -uroot  -p

Enter password: 

输入密码:hwg123

4)给数据库授权:

mysql>  grant replication slave on *.* to 'rep'@'10.93.58.73' identified by "hwg123";

garnt-1.JPG

5)查看主库状态;mysql> show master status;

show_master_post1.JPG

6)在mater02上要做如下几步操作;

7)进入数据库;输入密码hwg123

[root@Mysql-Slave ~]# mysql -uroot -p

Enter password: 

8)根据mysql-bind和post点来授权同步库和post点位。

mysql> change master to master_host='10.93.58.73',master_user='rep',master_password='hwg123',master_log_file='mysql-bin.000014',master_log_pos=263;
Query OK, 0 rows affected (0.06 sec)

9)开启slave同步

mysql> start slave;

Query OK, 0 rows affected (0.00 sec)

10)查看同步结果如果是两个YES;证明同步成功:

mysql> show slave status\G;

show-slave-1.JPG

5,切换到Master02上正式安装mysql-5.5.32。

1)使用脚本安装这里略过

2)查看配置文件,这里注意标红的地方:

[root@Mysql-Slave ~]# cat /etc/my.cnf

[mysqld]
port            = 3306
socket          = /application/mysql-5.5.32/tmp/mysql.sock
datadir         =/data/mysql
user            =mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-bin=mysql-bin
binlog_format = mixed
server-id = 2
auto_increment_offset=2
auto_increment_increment=2
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
replicate-do-db =all

3)进入数据库:输入密码:hwg123

[root@Mysql-Slave ~]# mysql -uroot -p

Enter password:

4)给数据库授权:

mysql> grant replication slave on *.* to 'rep'@'10.93.58.72' identified by "hwg123";

mysql-slave-2.JPG

5)查看数据库状态:mysql> show master status;

show-master-status.JPG

6)切换到master01上授权;

mysql> change master to master_host='10.93.58.73',master_user='rep',master_password='hwg123',master_log_file='mysql-bin.000006',master_log_pos=263;

7)开启slave同步

mysql> slave start;

8)查询同步状态:mysql> show slave status\G;

show-slave-2.JPG

7)这样主主同步就好了


6,安装keepalived来做高可用。我用的是keepalived-1.2.20.tar.gz

在Master01上操作如下:

yum –y install openssl openssl-devel
tar xf keepalived-1.2.20.tar.gz
cd keepalived-1.2.20
./configure --with-kernel-dir=/usr/src/kernels/2.6.32-696.el6.x86_64
make && make install
cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
cp /usr/local/sbin/keepalived /usr/sbin/
mkdir /etc/keepalived
cd /etc/keepalived/

添加keepalived配置文件

[root@Mysql-Master ~]# vim /etc/keepalived/keepalived.conf

#####MASTER keepalived配置文件######
! Configuration File for keepalived 
global_defs { 
   notification_email { 
      hwg1227@163.com 
   } 
   notification_email_from hwg1227@163.com
   smtp_server 127.0.0.1 
   smtp_connect_timeout 30 
   router_id LVS_DEVEL 
} 
# VIP1 
vrrp_instance VI_1 { 
    state MASTER   
    interface eth0 
    lvs_sync_daemon_inteface eth0 
    virtual_router_id 50 
    priority 50
    advert_int 1 
    nopreempt 
    authentication { 
        auth_type PASS 
        auth_pass 1111 
    } 
    virtual_ipaddress { 
        10.93.58.74
    } 
} 
virtual_server 10.93.58.74 3306 { 
    delay_loop 6    
    lb_algo wrr    
    lb_kind DR   
    persistence_timeout 60    
    protocol TCP         
    real_server 10.93.58.72 3306 { 
        weight 1   
        notify_down /root/mysql.sh 
        TCP_CHECK { 
        connect_timeout 10 
        nb_get_retry 3 
        delay_before_retry 3 
        connect_port 3306 
        } 
    }

[root@Mysql-Master ~]#  vim  mysql.sh

#!/bin/bash
/etc/init.d/keepalived stop

[root@Mysql-Master ~]# chmod +x mysql.sh

在Master02上操作基本和Master01类似,只需要改keepalived.conf,配置如下,注意SLAVE 、priority、IP几个地方:

#####SLAVE keepalived配置文件#####
! Configuration File for keepalived
global_defs {
   notification_email {
      hwg1227@163.com
   }
   notification_email_from hwg1227@163.com
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
# VIP1
vrrp_instance VI_1 {
    state SLAVE
    interface eth0
    lvs_sync_daemon_inteface eth0
    virtual_router_id 50
    priority 30
    advert_int 5
#    nopreempt
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        10.93.58.74
    }
}
virtual_server 10.93.58.74 3306 {
    delay_loop 6
    lb_algo wrr
    lb_kind DR
    persistence_timeout 60
    protocol TCP
    real_server 10.93.58.73 3306 {
        weight 1
        notify_down /root/mysql.sh
        TCP_CHECK {
        connect_timeout 10
        nb_get_retry 3
        delay_before_retry 3
        connect_port 3306
        }
    }

7,最后测试一下,停掉Master01主库;没有停主库前的ip。

ip_a1.JPG

停了数据库后,看看日志显示;

[root@Mysql-Master ~]# /etc/init.d/mysqld stop 

Shutting down MySQL. SUCCESS! 

[root@Mysql-Slave ~]# tail -fn20 /var/log/messages

ip_add.JPG

随后查看Master02上面的ip看看VIP有没有漂移过来。

[root@Mysql-Slave ~]# ip a

ip_remove.JPG

最后可以看出来IP已经漂移过来了,实验成功了。