第十五週

一、編寫腳本,支持讓用戶自主選擇,使用mysqldump仍是xtraback全量備份。

#!/bin/bashnode

PS3="Please input a choice:"
select menu in mysqldump xtrabackup; do
case $REPLY in
1)
mkdir /backup/dump/ &> /dev/null
mysqldump -F -A --single-transaction --master-data=1 > /backup/dump/all.sql
break
;;
2)
mkdir /backup/test/ &> /dev/null
xtrabackup --backup --target-dir=/backup/test/ &> /dev/null
break
;;
*)
echo "please input again!"
esac
donemysql

第十五週

二、配置Mysql主從同步

環境:192.168.43.127 master
192.168.43.137 slavesql

一、master配置文件:
[root@centos7 backup]#cat /etc/my.cnf
[mysqld]
**server-id=1**
max_connections=2000
binlog_format=ROW
**log-bin=/data/mysql/logbin/mysql-bin**
transaction-isolation=SERIALIZABLE
datadir=/data/mysql/data
socket=/var/lib/mysql/mysql.sock
[mysqld_safe]
log-error=/data/mysql/log/mariadb/mariadb.log
pid-file=/data/mysql/mariadb/mariadb.pid

!includedir /etc/my.cnf.d

二、在master上添加複製用戶數據庫

MariaDB [(none)]> grant replication slave on . to 'repluser'@'192.168.43.%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)centos

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)bash

三、備份master數據庫,並拷貝到slave上:服務器

[root@centos7 mysql]#mysqldump -F -A --single-transaction --master-data=1 > /backup/all.sql
[root@centos7 backup]#scp -r /backup/all.sql 192.168.43.137:/data/app

四、slave節點配置:ssh

[mysqld]
socket = /data/mysql/mysql.sock
datadir = /data/mysql
server-id=2
read_only
socket

(1)slave導入數據庫以前數據
[root@ka1 mysql]#mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.2.25-MariaDB Source distribution

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+

3 rows in set (0.00 sec)

(2)導入數據庫,與主服務器保持數據一致:

[root@ka1 mysql]#mysql < /data/all.sql
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| hellodb |
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
第十五週

(3)配置主從同步(slave上操做)

查看數據庫備份文件主服務二進制日誌位置:
第十五週

設置以前,slave狀態是空的
MariaDB [(none)]> show slave status\G
Empty set (0.00 sec)

MariaDB [(none)]> CHANGE MASTER TO
-> MASTER_HOST='192.168.43.127',
-> MASTER_USER='repluser',
-> MASTER_PASSWORD='123456',
-> MASTER_PORT=3306,
-> MASTER_LOG_FILE='mysql-bin.000005',
-> MASTER_LOG_POS=245;

Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.00 sec)

設置以後能夠看到slave狀態以下:

MariaDB [(none)]> show slave status\G
1. row
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.43.127
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000005
Read_Master_Log_Pos: 245
Relay_Log_File: ka1-relay-bin.000002
Relay_Log_Pos: 544
Relay_Master_Log_File: mysql-bin.000005
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
………

第十五週
第十五週

五、主從同步測試:

(1) master刪除數據:

MariaDB [(none)]> use hellodb;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed

MariaDB [hellodb]> select * from teachers
-> ;
+-----+---------------+-----+--------+
| TID | Name | Age | Gender |
+-----+---------------+-----+--------+
| 1 | Song Jiang | 45 | M |
| 2 | Zhang Sanfeng | 94 | M |
| 3 | Miejue Shiitai | 77 | F |
+-----+---------------+-----+--------+
3 rows in set (0.00 sec)

MariaDB [hellodb]> delete from teachers where Age=77;
Query OK, 1 row affected (0.00 sec)

MariaDB [hellodb]> select * from teachers;
+-----+---------------+-----+--------+
| TID | Name | Age | Gender |
+-----+---------------+-----+--------+
| 1 | Song Jiang | 45 | M |
| 2 | Zhang Sanfeng | 94 | M |
+-----+---------------+-----+--------+
2 rows in set (0.00 sec)

(2)從服務器實時同步數據:

MariaDB [hellodb]> select * from teachers;
+-----+---------------+-----+--------+
| TID | Name | Age | Gender |
+-----+---------------+-----+--------+
| 1 | Song Jiang | 45 | M |
| 2 | Zhang Sanfeng | 94 | M |
+-----+---------------+-----+--------+
2 rows in set (0.00 sec)

第十五週

三、使用MHA實現Mysql高可用。

在主從複製基礎之上,實現MHA高可用;
節點要開啓二進制日誌及中繼日誌;
從節點爲 read_only;
關閉從節點的 relay_log_purge 中繼日誌自動清理功能
環境準備:
master 192.168.43.127
slave 192.168.43.137
slave/manager 192.168.43.147

一、master的配置文件:
[mysqld]
server-id=1
skip_name_resolve=1
relay-log=relay-log
log-bin=bin-log
innodb-file-per-table=ON
datadir=/data/mysql/data
slave1配置文件:
[mysqld]
datadir=/data/mysql/data
server-id=2
skip_name_resolve=1
read_only
relay_log_purge=0
relay-log=relay-log
log-bin=bin-log
innodb-file-per-table=ON
slave2配置文件:
[mysqld]
datadir=/data/mysql/data
server-id=3
skip_name_resolve=1
read_only
relay_log_purge=0
relay-log=relay-log
log-bin=bin-log
innodb-file-per-table=ON

二、各個節點上安裝mha4mysql-node,manager節點上安裝mha4mysql-manager

[root@]#yum install mha4mysql-node-0.56-0.el6.noarch.rpm
[root@]#yum install mha4mysql-manager-0.56-0.el6.noarch.rpm

三、master上建立MHA管理用戶覈覆制用戶,在主從同步正常狀況下會同步給slave和slave2:

MariaDB [(none)]> grant replication slave on . to repluser@'192.168.43.%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> grant all on . to mhauser@'192.168.43.%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

四、各節點之間免密登陸:

(1)各節點上生成密鑰:br/>[root@]#ssh-keygen
第十五週

(2)後用命令將公鑰拷貝到其餘節點
[root@]#ssh-copy-id 192.168.43.147
[root@]#ssh-copy-id 192.168.43.137

[root@]#ssh-copy-id 192.168.43.127
[root@]#ssh-copy-id 192.168.43.137

[root@]#ssh-copy-id 192.168.43.127
[root@]#ssh-copy-id 192.168.43.147

五、在manager節點生成mha配置文件:

[root@]#cat /etc/mastermha/app1.cnf
[server default]
user=mhauser
password=123456
manager_workdir=/data/mastermha/mysql/
manager_log=/data/mastermha/mysql/manager.log
remote_workdir=/data/mastermha/mysql/
ssh_user=root
repl_user=repluser
repl_password=123456
ping_interval=1
[server1]
hostname=192.168.43.127
candidate_master=1
master_binlog_dir=/data/mysql/data/
[server2]
hostname=192.168.43.137
candidate_master=1
master_binlog_dir=/data/mysql/data/
[server3]
hostname=192.168.43.147
master_binlog_dir=/data/mysql/data/

註釋:

master_binlog_dir #mysql的binlog日誌目錄沒有在默認的/var/log/mysql下,須要配置此項,寫明具體路徑
candidate_master=1 #是否 能夠成爲master,1是

六、檢查

(1) 檢查ssh登陸是否正常:

[root@]#masterha_check_ssh --conf=/etc/mastermha/app1.cnf
第十五週

(2)檢查節點之間複製是否正常:
[root@]# masterha_check_repl --conf=/etc/mastermha/app1.cnf

第十五週

七、啓動MHA:

[root@]# nohup masterha_manager --conf=/etc/mastermha/app1.cnf &
第十五週

八、檢查MHA狀態:

[root@]#masterha_check_status --conf=/etc/mastermha/app1.cnf
app1 (pid:75633) is running(0:PING_OK), master:192.168.43.127

第十五週

九、故障轉移測試:

(1)在master上中止mariadb服務
[root@]#systemctl stop mariadb

(2)在manager上查看日誌:
[root@]#tail -f /data/mastermha/mysql/manager.log
第十五週

第十五週

(3)在備用master上狀態,已經由slave變成了master:
MariaDB [hellodb]> show slave status\G

第十五週

(4)在slave2上查看slave狀態,此時的master已經由192.168.43.127變成了192.168.43.137
MariaDB [(none)]> show slave status\G

第十五週

相關文章
相關標籤/搜索