1. 程式人生 > >MySQL-雙主高可用

MySQL-雙主高可用


主伺服器
開啟binlog日誌
[mysqld]
log-bin=master
log-bin-index=master
server-id=1
1.全備:
[[email protected] data]# mysqldump -u root -p123 --all-databases > /tmp/all.sql

2.將全備拷貝給從機,並在從機完全恢復,保證主從資料的一致性

3.授權從機可以從主機複製資料
mysql> grant replication slave on *.* to [email protected]'192.168.10.201' identified by '123';

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.完全恢復
[[email protected] mysql]# mysql -u root < /root/all.sql

2.測試登陸主伺服器
[[email protected]

mysql]# mysql -u slave -p123 -h 192.168.10.200

3.配置檔案
[[email protected] data]# cat /etc/my.cnf
[mysqld]
server-id=2

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: Yes

調換兩臺主機的身份再做一次
從伺服器:
開啟binlog日誌:
[[email protected] data]# cat /etc/my.cnf
[mysqld]
server-id=2
log-bin=slave
log-bin-index=slave

給主伺服器授權
mysql> grant replication slave on *.* to [email protected]'192.168.10.200' identified by '123' ;


主伺服器:
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都要安裝)
[email protected] smb]# tar -xvf keepalived-1.2.24.tar.gz -C /usr/local/src/
[[email protected] ~]# cd /usr/local/src/keepalived-1.2.24/
[[email protected] keepalived-1.2.24]#./configure --prefix=/ --mandir=/usr/local/share/man/
[[email protected] keepalived-1.2.24]# make
[[email protected] keepalived-1.2.24]# make install
[[email protected] keepalived-1.2.24]# cd /etc/keepalived/
! Configuration File for keepalived

global_defs { #全域性定義主要設定 keepalived 的通知機制和標識
notification_email {
[email protected]
}
notification_email_from [email protected]
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 #健康檢查埠
}
}
}
====================================================

 

[[email protected] keepalived]# scp keepalived.conf 192.168.10.201:/etc/keepalived/
拷貝後,修改配置檔案
state BACKUP
priority 100

2臺mysql上,啟動Keepalived服務
[[email protected] 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

這裡寫圖片描述