MySQL-雙主高可用


主服務器
開啓binlog日誌
[mysqld]
log-bin=master
log-bin-index=master
server-id=1
1.全備:
[root@localhost data]# mysqldump -u root -p123 --all-databases > /tmp/all.sqlmysql

2.將全備拷貝給從機,並在從機徹底恢復,保證主從數據的一致性算法

3.受權從機能夠從主機複製數據
mysql> grant replication slave on *.* to slave@'192.168.10.201' identified by '123';sql

4.查看主服務器日誌狀態:
mysql> show master status\G
*************************** 1. row ***************************
File: master.000001
Position: 120
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)數據庫

從服務器
1.徹底恢復
[root@localhost mysql]# mysql -u root < /root/all.sql服務器

2.測試登錄主服務器
[root@localhost mysql]# mysql -u slave -p123 -h 192.168.10.200less

3.配置文件
[root@localhost data]# cat /etc/my.cnf
[mysqld]
server-id=2ide

4.配置鏈接服務器
mysql> change master to
-> master_host='192.168.10.200',
-> master_user='slave',
-> master_password='123',
-> master_log_file='master.000001',
-> master_log_pos=120;
mysql> start slave;
mysql> show slave status\G
Slave_IO_Running: Yes
Slave_SQL_Running: Yesoop

調換兩臺主機的身份再作一次
從服務器:
開啓binlog日誌:
[root@localhost data]# cat /etc/my.cnf
[mysqld]
server-id=2
log-bin=slave
log-bin-index=slave測試

給主服務器受權
mysql> grant replication slave on *.* to master@'192.168.10.200' identified by '123' ;ui


主服務器:
mysql> change master to
-> master_host='192.168.10.201',
-> master_user='master',
-> master_password='123',
-> master_log_file='slave.000001',
-> master_log_pos=333;

mysql> start slave;
mysql> show slave status\G

=====================================================
在mysql互主的基礎上配置keepalived(兩臺mysql都要安裝)
root@localhost smb]# tar -xvf keepalived-1.2.24.tar.gz -C /usr/local/src/
[root@localhost ~]# cd /usr/local/src/keepalived-1.2.24/
[root@localhost keepalived-1.2.24]#./configure --prefix=/ --mandir=/usr/local/share/man/
[root@localhost keepalived-1.2.24]# make
[root@localhost keepalived-1.2.24]# make install
[root@localhost keepalived-1.2.24]# cd /etc/keepalived/
! Configuration File for keepalived

global_defs { #全局定義主要設置 keepalived 的通知機制和標識
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id test
}

vrrp_instance VI_1 { #VRRP(虛擬路由冗餘協議)實例配置
state MASTER #另外一個 Director 標記爲 BACKUP!!!
interface eth0 #實例綁定的網卡
virtual_router_id 51 #VRID 虛擬路由標識 00-00-5e-00-01-{VRID}
priority 150 #優先級高爲master,master 至少要高於 backup 50 !!!
advert_int 1 #檢查間隔
authentication {
auth_type PASS 驗證:主備之間作身份驗證 主備之間必定一致
auth_pass 1111
}
virtual_ipaddress { 浮動ip
192.168.10.222/24
}
}

==================================================

virtual_server 192.168.0.200 3306 {
delay_loop 2 #每一個2秒檢查一次real_server狀態
lb_algo wrr #LVS算法
lb_kind DR #LVS模式
persistence_timeout 60 #會話保持時間
protocol TCP
real_server 192.168.0.219 3306 {
weight 3
notify_down /usr/local/MySQL/bin/MySQL.sh #檢測到服務down後執行的腳本
TCP_CHECK {
connect_timeout 10 #鏈接超時時間
nb_get_retry 3 #重連次數
delay_before_retry 3 #重連間隔時間
connect_port 3306 #健康檢查端口
}
}
}
====================================================

 

[root@localhost keepalived]# scp keepalived.conf 192.168.10.201:/etc/keepalived/
拷貝後,修改配置文件
state BACKUP
priority 100

2臺mysql上,啓動Keepalived服務
[root@localhost keepalived]# service keepalived start


測試2臺mysql的故障轉移

==================================
MYSQL ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.10.210' (111) 解決方法

今天在測試MySQL的鏈接時候,發現鏈接不經過,並報錯ERROR 2003 (HY000): Can't connect to mysql server on '192.168.10.210' (111)
測試代碼:

require 'mysql2'
client = Mysql2::Client.new(:host=>"192.168.10.210",:username=>'root',:password=>"root")
puts results = client.query("show databases;")

谷歌了一下以後,原來是在mysql的my.cnf中有下面一段代碼:

# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address = 127.0.0.1 #這裏默認監聽本地localhost

若是要讓mysql監聽到其餘的地址,能夠將bind-address = 127.0.0.1註釋掉。
或者將bind-address = 0.0.0.0監聽全部的地址

屏蔽掉以後再次運行代碼又出現:Host '192.168.10.83' is not allowed to connect to this MySQL server
這裏寫圖片描述
解決方法:
若是想讓192.168.10.83可以鏈接到本地的這個數據庫,要讓數據庫給其分配權限,登陸mysql,執行:(username 和 password是登陸mysql的用戶名和密碼)

GRANT ALL PRIVILEGES ON *.* TO 'username'@'192.168.10.83' IDENTIFIED BY 'password' WITH GRANT OPTION;

若是要想全部的外部ip地址都可以訪問使用mysql,能夠執行下面:

GRANT ALL PRIVILEGES ON *.* TO 'username'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;

以後執行刷新數據庫:

flush privileges;

若是要查看用戶的權限,能夠執行:

> show grants for 'root'@192.168.10.83

這裏寫圖片描述

相關文章
相關標籤/搜索