MySQLマスターコピー+Keepalived 2.0 mysql高可用性の実現


サーバ1:192.168.1.1100(server 1)サーバ2:192.168.1.1101(server 2)サーバオペレーティングシステム:Firefly-Ubuntu 16.04
MySQL5.7プライマリ・レプリケーション
mysql-serverのインストール
#       
apt-get update
#  mysql-server
apt-get install mysql-server


サーバ1の構成
sudo vi /etc/mysql/my.cnf
bind-address=192.168.1.100
server-id=1
log_bin=/var/local/mysql/mysql-bin.log
expire_logs_days					=	10
max_binlog_size 					=	100M
binlog_ignore_db					=	mysql
auto_increment_increment			=	2
auto_increment_offset				=	1
sudo systemctl start mysql
mysql -uroot -p
mysql>grant replication slave,repilcation client,super,reload on *.* to 'replicate'@'192.168.1.101' identified by 'password';
mysql>exit;

サーバ2の構成
sudo vi /etc/mysql/my.cnf
bind-address=192.168.1.101
server-id=3
log_bin=/var/local/mysql/mysql-bin.log
expire_logs_days					=	10
max_binlog_size 					=	100M
binlog_ignore_db					=	mysql
auto_increment_increment			=	2
auto_increment_offset				=	2
sudo systemctl start mysql
mysql -uroot -p
mysql>grant replication slave,repilcation client,super,reload on *.* to 'replicate'@'192.168.1.100' identified by 'password';
mysql>exit;

サーバ1の構成を続行
mysql -uroot -p
mysql>change master to 
->master_host='192.168.1.101',
->master_port=3306,
->master_user='replicate',
->master_password='replicate',
->master_log_file='mysql-bin.000001',#         show master status
->master_log_pos=154;#         show master status
mysql>start slave;
mysql>exit;

サーバ2の構成を続行
mysql -uroot -p
mysql>change master to 
->master_host='192.168.1.100',
->master_port=3306,
->master_user='replicate',
->master_password='replicate',
->master_log_file='mysql-bin.000001',#         show master status
->master_log_pos=164;#         show master status
mysql>start slave;
mysql>exit;

Keepalived-2.0インストール
keepalived公式サイトに行ってtar.をダウンロードしますgzパッケージ、/tmpディレクトリに解凍:
tar -zxvf keepalived-2.0.18.tar.gz /tmp/
cd /tmp/keepalived-2.0.18
apt-get install openssl libssl-dev
sudo ./configure
sudo make
sudo make install

keepalived起動の設定
sudo systemctl enable keepalived

プロファイルサーバ1
#/etc/keepalived/keepalived.conf
! Configuraion file  for Keepalived
global_defs {
router_id 1.100
notification_email {
[email protected]
}
notification_email_from [email protected]
smtp_server smtp.163.com
smtp_connect_timeout 30
}
vrrp_instance V_mysql_1 {
state BACKUP
interface eth0
virtual_router_id 233
priority 100
advert_int 1
nopreemept
authentication {
auth_type PASS
auth_pass password
}
virtual_ipaddress {
192.168.1.150/24
}
}


プロファイルサーバ2
#/etc/keepalived/keepalived.conf
! Configuraion file  for Keepalived
global_defs {
router_id 1.101 #    ID
notification_email {
[email protected]
}
notification_email_from [email protected]
smtp_server smtp.163.com
smtp_connect_timeout 30
}
vrrp_instance V_mysql_1 {
state BACKUP
interface eth0
virtual_router_id 233
priority 90 #     
advert_int 1
nopreemept
authentication {
auth_type PASS
auth_pass password
}
virtual_ipaddress {
192.168.1.150/24
}
}

2台のサーバのkeepalivedサービスを起動すればテストできます!
systemctl start keepalived

DONE.
P.S:mysqlサービスモニタリングについては、スクリプトを作成してkeepalivedプロファイルに入れてモニタリングしたり、スクリプトを書いて同期mysqlとkeepalivedの起動停止状態を判断しcrontabでタイミングタスクを追加してモニタリングしたりすることができます.