標籤:高可用 叢集
IP規劃
角色 IP地址 主機名稱
Master 資料庫伺服器 192.168.4.51 master51
備用 1 master 資料庫伺服器 192.168.4.52 master52
備用 2 master 資料庫伺服器 192.168.4.53 master53
第 1 台 slave 伺服器 192.168.4.54 slave54
第 2 台 slave 伺服器 192.168.4.55 slave55
Mha_manager 伺服器 192.168.4.56 mgm56
VIP 位址 192.168.4.100
一、配置所有資料節點主機之間可以互相以ssh金鑰組方式認證登陸
二、配置manager56主機 無密碼ssh登入 所有資料節點主機
三、配置主從同步,要求如下:
51 主庫 開半同步複製
52 從庫(備用主庫) 開半同步複製
53 從庫(備用主庫) 開半同步複製
54 從庫 不做備用主庫所以不用開半同步複製
55 從庫 不做備用主庫所以不用開半同步複製
3.1、master51配置:
vim /etc/my.cnf
[mysqld]
plugin-load = "rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so"
rpl-semi-sync-master-enabled = 1
rpl-semi-sync-slave-enabled = 1
server_id=51
log-bin=master51
binlog-format="mixed"
:wq
[[email protected]~]# systemctl restart mysqld
[[email protected] ~]# ls /var/lib/mysql/master51.*
/var/lib/mysql/master51.000001 /var/lib/mysql/master51.index
[[email protected] ~]# mysql -uroot -p123456
mysql> grant replication slave on . to [email protected]"%" identified by "123456";
Query OK, 0 rows affected, 1 warning (10.04 sec)
mysql> set global relay_log_purge=off;
Query OK, 0 rows affected (0.15 sec)
mysql> show master status;
+-----------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-----------------+----------+--------------+------------------+-------------------+
| master51.000001 | 441 | | | |
+-----------------+----------+--------------+------------------+-------------------+
mysql> quit;
3.2、備用master52的配置
vim /etc/my.cnf
[mysqld]
plugin-load = "rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so"
rpl-semi-sync-master-enabled = 1
rpl-semi-sync-slave-enabled = 1
server_id=52
log-bin=master52
binlog-format="mixed"
]# systemctl restart mysqld
[[email protected]~]# ls /var/lib/mysql/master52.*
/var/lib/mysql/master52.000001 /var/lib/mysql/master52.index
[[email protected] ~]# mysql -uroot -p123456
mysql> set global relay_log_purge=off;
Query OK, 0 rows affected (0.13 sec)
mysql> change master to
-> master_host="192.168.4.51",
-> master_user="repluser",
-> master_password="123456",
-> master_log_file="master51.000001",
-> master_log_pos=441;
Query OK, 0 rows affected, 2 warnings (0.04 sec)
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
[[email protected] ~]# mysql -uroot -p123456 -e "show slave status\G" | grep -i YES
mysql: [Warning] Using a password on the command line interface can be insecure.
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
[[email protected] ~]#
3.3、備用master53的配置
]# vim /etc/my.cnf
[mysqld]
plugin-load = "rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so"
rpl-semi-sync-master-enabled = 1
rpl-semi-sync-slave-enabled = 1
server_id=53
log-bin=master53
binlog-format="mixed"
:wq
[[email protected] ~]# systemctl restart mysqld
[[email protected] ~]# ls /var/lib/mysql/master53.*
/var/lib/mysql/master53.000001 /var/lib/mysql/master53.index
[[email protected] ~]#
[[email protected] ~]# mysql -uroot -p123456
mysql> set global relay_log_purge=off;
Query OK, 0 rows affected (0.14 sec)
mysql> change master to master_host="192.168.36.51",master_user="repluser",master_password="123456",master_log_file="master51.000001",master_log_pos=441;
Query OK, 0 rows affected, 2 warnings (0.05 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql>
[[email protected] ~]# mysql -uroot -p123456 -e "show slave status\G" | grep -i yes
mysql: [Warning] Using a password on the command line interface can be insecure.
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
3.4、配置從伺服器54
[[email protected] ~]# vim /etc/my.cnf
[mysqld]
server_id=54
:wq
[[email protected]~]# systemctl restart mysqld
[[email protected] ~]# mysql -uroot -p123456
mysql> change master to master_host="192.168.36.51",master_user="repluser",master_password="123456",master_log_file="master51.000001",master_log_pos=441;
Query OK, 0 rows affected, 2 warnings (0.09 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> quit;
Bye
[[email protected] ~]#
[[email protected] ~]# mysql -uroot -p123456 -e "show slave status\G" | grep -i yes
mysql: [Warning] Using a password on the command line interface can be insecure.
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
3.5、配置從伺服器55
[[email protected] ~]# vim /etc/my.cnf
[mysqld]
server_id=55
:wq
[[email protected] ~]# systemctl restart mysqld
[[email protected] ~]# mysql -uroot -p123456
mysql> change master to master_host="192.168.36.51",master_user="repluser",master_password="123456",master_log_file="master51.000001",master_log_pos=441;
Query OK, 0 rows affected, 2 warnings (0.09 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> quit;
Bye
[[email protected] ~]#
[[email protected] ~]# mysql -uroot -p123456 -e "show slave status\G" | grep -i yes
mysql: [Warning] Using a password on the command line interface can be insecure.
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
[[email protected]~]#
3.6、在用戶端測試主從同步配置
3.6.1 在主庫51上添加訪問資料的授權使用者
[[email protected]~]# mysql -uroot -p123456
mysql> grant all on gamedb.* to [email protected]"%" identified by "123456";
3.6.2 在用戶端主機串連主庫51 建庫表記錄
mysql> create database gamedb;
Query OK, 1 row affected (0.01 sec)
mysql> create table gamedb.t1 (id int);
Query OK, 0 rows affected (0.04 sec)
mysql> insert into gamedb.t1 values(999);
Query OK, 1 row affected (0.15 sec)
mysql> insert into gamedb.t1 values(999);
Query OK, 1 row affected (0.05 sec)
mysql> select * from gamedb.t1;
+------+
| id |
+------+
| 999 |
| 999 |
+------+
2 rows in set (0.00 sec)
mysql>
3.6.3 在用戶端使用授權使用者串連從庫52-55,也能看到同樣的庫表及記錄
#mysql -h從庫IP地址 -uadmin -p123456
mysql> select * from gamedb.t1;
+------+
| id |
+------+
| 999 |
| 999 |
+------+
4.0.1下來我們配置Proxy 伺服器
配置MHA叢集
安裝軟體包:
在所有主機上安裝perl軟體包 (51~56)
在所有主機上安裝mha_node軟體包 (51~56)
只在管理主機上安裝mha_manager軟體包(56)
檢查配置環境
拷貝命令(56)
建立工作目錄 和主設定檔 (56)
建立故障切換指令碼(56)
編輯主設定檔 app1.cnf
驗證配置
驗證ssh 免密碼登入 資料節點主機
驗證 資料節點的主從同步配置
編輯管理主機主設定檔
]# cp mha4mysql-manager-0.56/bin/* /usr/local/bin/
]#mkdir /etc/mha_manager/
[[email protected] mha4mysql-manager-0.56]# cp samples/conf/app1.cnf /etc/mha_manager/
]#vim /etc/mha_manager/app1.cnf
[server default]
manager_workdir=/etc/mha_manager
manager_log=/etc/mha_manager/manager.log
master_ip_failover_script=/usr/local/bin/master_ip_failover
(
]# vim /usr/local/bin/master_ip_failover
my $vip = ‘192.168.4.100/24‘; # Virtual IP
my $key = "1";
my $ssh_start_vip = "/sbin/ifconfig eth0:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig eth0:$key down";
:wq
) 修改指令檔
ssh_user=root
ssh_port=22
repl_user=repluser
repl_password=123456
user=root
password=123456
[server1]
hostname=192.168.36.51
port=3306
candidate_master=1
[server2]
hostname=192.168.36.52
port=3306
candidate_master=1
[server3]
hostname=192.168.36.53
port=3306
candidate_master=1
4.3 啟動管理服務,並查看服務狀態
]# masterha_manager --conf=/etc/mha/app1.cnf
--remove_dead_master_conf --ignore_last_failover
[server4]
hostname=192.168.36.54
port=3306
no_master=1
[server5]
hostname=192.168.36.55
port=3306
no_master=1
:wq
測試SSH
[[email protected] ~]# masterha_check_ssh --conf /etc/mha_manager/app1.cnf
All SSH connection tests passed successfully.
測試主從同步
[[email protected] mhasoft]# masterha_check_repl --conf /etc/mha_manager/app1.cnf
啟動管理服務,並查看服務狀態
]# masterha_manager --conf=/etc/mha_manager/app1.cnf --ignore_last_failover
另開終端
在管理主機上查看服務狀態
[[email protected] ~]# masterha_check_status --conf=/etc/mha_manager/app1.cnf
把51 宕掉後 看看 是不是52 是不是主庫
但是 51 好了需手動設定成 52 的從
部署資料庫的高可用叢集和效能調優