Mysql主主

    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 { 
      [email protected] 
   } 
   notification_email_from [email protected]
   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 {
      [email protected]
   }
   notification_email_from [email protected]
   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已經漂移過來了,實驗成功了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章