mysql 主主+keepalive

####環境準備
安裝mysql(centos7 默認沒有mysql源)
yum clearup
yum update
yum install vim libaio net-tools
wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
rpm -ivh mysql-community-release-el7-5.noarch.rpm
yum install mysql-server python-mysqldb(Python-mysqldb未安裝)python

啓動修改密碼
update user set password=password(‘123456‘) where user=‘root‘;mysql

###給slave賦權
grant replication slave on *.* to '用戶名'@'192.168.1.153' identified by '密碼';
###鎖表,找到master當前的binlog點以及position位置
flush tables with read lock;
show master status;   ###記錄日誌制文件以及position點
unlock tables;nginx

###從庫上操做,先停掉salve服務,而後配置連上master的配置
stop slave 
####利用剛在主庫上面建立的帳號密碼以及記錄的二進制日誌文件和position點,鏈接到主庫的同步位置
change master to master_host='192.168.178.2',master_user='xldroot',master_password='123123',master_log_file='mysql-bin.000003', master_log_pos=790;
start slave 
show slave status\G
#######一下兩項爲yes即OK,也能夠本身去建立數據庫,看下是否有同步
Slave_IO_Running: Yes
Slave_SQL_Running: Yessql

###上面是單方向主從配置,主主的話反着來一次便可(以前的從上建立鏈接的帳號和密碼以及記錄節點位置,而後在以前的主鏈接到從),不過配置裏面仍是有區別
[client]
character-set-server = utf8  #####客戶端默認鏈接字集集,若編譯安裝時已指定則不用填寫數據庫

[mysqld]
datadir=/data/mysql/data
character-set-server = utf8
socket=/var/lib/mysql/mysql.sock
port = 3306
server_id = 1
log-bin= mysql-bin
binlog_format = mixed
bind  =  0.0.0.0
read-only=0
#binlog-do-db=test              
binlog-ignore-db=mysql          
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
auto-increment-offset=1
auto-increment-increment=2vim

#主從複製
##replicate-do-db=test              ###開啓的話只同步這些庫
replicate-ignore-db=mysql           ###忽略同步這些庫
replicate-ignore-db=information_schema
replicate-ignore-db=performance_schema
relay_log=mysql-relay-bin
log-slave-updates=oncentos

###binlog日誌
log_bin                 = /data/log/mysql/mysql-bin.log
relay_log               = /data/log/mysql/mysql-relay-bin
log_slave_updates       = 1
expire_logs_days        = 7服務器

###慢查詢
slow_query_log          = ON
slow_query_log_file     = /data/log/mysql/mysql-slow.log
#log_slow_queries       = /data/log/mysql/mysql-slow.log
long_query_time = 2socket

key_buffer              = 16M
max_allowed_packet      = 16M
thread_stack            = 192K
thread_cache_size       = 50ide

[mysqld_safe]
log-error=/data/log/mysql/mariadb.log
pid-file=/data/log/mysql/mariadb.pid


[client]
character-set-server = utf8  #####客戶端默認鏈接字集集,若編譯安裝時已指定則不用填寫

[mysqld]
socket=/var/lib/mysql/mysql.sock
character-set-server = utf8
datadir =/data/mysql/data
port = 3306
server_id = 2
log-bin= mysql-bin
binlog_format = mixed

read-only=0
#replicate-do-db=test
replicate-ignore-db=mysql
replicate-ignore-db=information_schema
replicate-ignore-db=performance_schema
relay_log=mysql-relay-bin
log-slave-updates=on
#
##
##binlog-do-db=test
binlog-ignore-db=mysql
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
auto-increment-offset=2
auto-increment-increment=2


log_bin                 = /data/log/mysql/mysql-bin.log
relay_log               = /data/log/mysql/mysql-relay-bin
log_slave_updates       = 1
expire_logs_days        = 7

slow_query_log          = ON
slow_query_log_file     = /data/log/mysql/mysql-slow.log
#log_slow_queries       = /data/log/mysql/mysql-slow.log
long_query_time = 2

key_buffer              = 16M
max_allowed_packet      = 16M
thread_stack            = 192K
thread_cache_size       = 50

[mysqld_safe]
log-error=/data/log/mysql/mariadb.log
pid-file=/data/log/mysql/mariadb.pid

#####由於這個主從的跑配置沒有在my.cnf裏面,從新啓動會加載數據目錄下面的配置

/var/lib/mysql/master.info   ###根據你本身的數據目錄而定,記錄了服務器地址,同步節點位置

####keepalived實現mysql主主自動切換
yum -y install keepalived
###加入開啓啓動
chkconfig keepalived on 2345
chkconfig mysqld on 2345
###查驗
centos7
systemctl list-unit-files |egrep 'mysql|keepalived'

####keepalived配置
global_defs {
   notification_email {
     #failover@firewall.loc
     root@localhost    ###告警通知,如今做用不大,通常均可以經過其餘監控軟件來作
   }
   notification_email_from root@localhost     ###發件人
   smtp_server localhost                      ###SMTP服務器
   smtp_connect_timeout 30
   router_id LVS_DEVEL                        ###可一致,也可不一致
}

vrrp_instance VI_1 {                          ###至關於Cisco的vrrp協議,虛擬一個組,若是有多個時,定義多個組便可
    state MASTER                              ###目前的主,只是一種稱呼,實際由優先級決定,越高的是老大
    interface ens4                            ###綁定的接口
    virtual_router_id 51                      ###組id,必須一致,表示都加入同一個組
    priority 100                              ###優先級
    advert_int 1                              ###通告時間,也就是加入這個組的全部機器以前的通告
    authentication {                          ###定義認證,內容必須一致
        auth_type PASS
        auth_pass baowang
    }
    virtual_ipaddress {                       ###這就是虛擬出來對外訪問的地址
        10.68.3.160
        #192.168.200.17
    }
}

virtual_server 10.68.3.160 3306 {             ###細化,虛擬到哪些端口
    delay_loop 6
    lb_algo wrr                               ###帶有權重的輪詢
    lb_kind NAT
    nat_mask 255.255.255.0
    persistence_timeout 50
    protocol TCP

    real_server 10.68.3.145 3306 {            ###綁定到的真實地址和端口
        weight 5                              ###權重降低值
        notify_up   /etc/keepalived/keepalived_start.sh              ###檢測總體服務器環境正常以後的操做
        notify_down /etc/keepalived/keepalived_shutdown.sh           ###定義腳本檢測,down以後的操做
        TCP_CHECK {                              ###定義一些基本的校驗項
            connect_timeout 10                ###超時時間
            nb_get_retry 5                    ###重連次數
            delay_before_retry 3              ###重連間隔
            connect_port 3306
        }
    }
  }
}

###額外的,能夠定義全腳本
vrrp_script chk_nginx {
script "/home/check_nginx.sh"
interval 2
weight 2
}

###局部模塊,位置虛擬組vrrp_instance VI_1裏面,用於調用全局的腳本
track_script {
        chk_nginx  #檢測腳本
}

####定義日誌輸出:默認是在/var/log/message,這個日誌裏面混合太多,查看不太方便
1.修改etc/sysconfig/keepalived文件
把KEEPALIVED_OPTIONS="-D" 
修改成KEEPALIVED_OPTIONS="-D -d -S 0"

2.設置syslog,修改/etc/syslog.conf,添加內容以下(centos7是/etc/rsyslog.conf) 
# keepalived -S 0 
local0.*                    /data/log/keepalive/keepalived.log

3.###重啓日誌服務和keepalive服務,在定義的路徑下就能夠看到須要的日誌
systemctl restart rsyslog or systemctl restart syslog
service keepalived restart       

4.####mysql得建立容許keepalive虛擬IP鏈接的帳號

相關文章
相關標籤/搜索