Mysql部署MHA高可用

部署前准备:

  mysql-8.0.27下载地址:https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.27-1.el7.x86_64.rpm-bundle.tar

  mha-manager下载地址:https://github.com/yoshinorim/mha4mysql-manager/releases/download/v0.58/mha4mysql-manager-0.58-0.el7.centos.noarch.rpm

  mha-node下载地址:https://github.com/yoshinorim/mha4mysql-node/releases/download/v0.58/mha4mysql-node-0.58-0.el7.centos.noarch.rpm

部署架构:

基本配置

4个节点逐台配置hostname和/etc/hosts文件

安装mysql8

4个节点均创建/data目录,将mysql-8.0.27-1.el7.x86_64.rpm-bundle.tar上传到/data目录下,进行解压安装

我这里使用操作系统镜像制作了本地yum源,用来解决依赖问题,命令见下图

看到如下内容,代表安装完成

 配置Mysql异步复制集群

启动hdp2、hdp3、hdp4上的mysql服务,并配置开机自启

1

systemctl enable mysqld --now

 连接3个节点的mysql数据库,数据库默认root密码通过查看日志获取

1

2

cat /var/log/mysqld.log |grep password

...... [Server] A temporary password is generated for root@localhost: euBcRQk+3tpa

3个节点数据库修改root用户密码,命令及sql如下

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

[root@hdp2 data]# mysql -uroot -p

Enter password:

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 20

Server version: 8.0.27

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

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

mysql> alter user 'root'@'localhost' identified by '1qaz@WSX123';

Query OK, 0 rows affected (0.01 sec)

mysql> flush privileges;

Query OK, 0 rows affected (0.01 sec)

mysql> exit;

Bye

3个节点配置数据库server_id,以root用户登录mysql进行临时配置,然后将server_id配置在/etc/my.cnf中,防止重启后失效

1

2

3

4

5

6

7

8

9

10

11

12

hdp2

mysql> set global server_id=1;

hdp3

mysql> set global server_id=2;

hdp4

mysql> set global server_id=3;

hdp2

]# echo "server_id=1" >> /etc/my.cnf

hdp3

]# echo "server_id=2" >> /etc/my.cnf

hdp4

]# echo "server_id=3" >> /etc/my.cnf

查看主库hdp2的二进制日志信息

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

[root@hdp2 data]# mysql -uroot -p

Enter password:

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 23

Server version: 8.0.27 MySQL Community Server - GPL

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

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

mysql> show master status;

+---------------+----------+--------------+------------------+-------------------+

| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

+---------------+----------+--------------+------------------+-------------------+

| binlog.000003 |      642 |              |                  |                   |

+---------------+----------+--------------+------------------+-------------------+

1 row in set (0.00 sec)

mysql>  

在主库hdp2上建立复制专用用户,并授权

1

2

3

4

5

6

7

mysql> create user 'repl'@'%' identified with mysql_native_password by '1qaz@WSX123';

Query OK, 0 rows affected (0.01 sec)

mysql> grant replication client,replication slave on *.* to 'repl'@'%';

Query OK, 0 rows affected (0.01 sec)

mysql> 

在从库hdp3和hdp4上创建主库信息

1

2

3

4

5

6

7

8

9

10

mysql> change master to

    -> master_host='192.168.244.151',

    ->  master_port=3306,

    -> master_user='repl',

    -> master_password='1qaz@WSX123',

    -> master_log_file='binlog.000003',               #主库状态显示的File

    -> master_log_pos=642;                            #主库状态显示的Position

Query OK, 0 rows affected, 9 warnings (0.02 sec)

mysql>   

在从库启动复制并查看从库状态

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

mysql> start slave;

Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> show slave status\G

*************************** 1. row ***************************

               Slave_IO_State: Waiting for source to send event

                  Master_Host: 192.168.244.151

                  Master_User: repl

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: binlog.000003

          Read_Master_Log_Pos: 1165

               Relay_Log_File: hdp3-relay-bin.000002

                Relay_Log_Pos: 844

        Relay_Master_Log_File: binlog.000003

             Slave_IO_Running: Yes

            Slave_SQL_Running: Yes

              Replicate_Do_DB:

          Replicate_Ignore_DB:

           Replicate_Do_Table:

       Replicate_Ignore_Table:

      Replicate_Wild_Do_Table:

  Replicate_Wild_Ignore_Table:

                   Last_Errno: 0

                   Last_Error:

                 Skip_Counter: 0

          Exec_Master_Log_Pos: 1165

              Relay_Log_Space: 1052

              Until_Condition: None

               Until_Log_File:

                Until_Log_Pos: 0

           Master_SSL_Allowed: No

           Master_SSL_CA_File:

           Master_SSL_CA_Path:

              Master_SSL_Cert:

            Master_SSL_Cipher:

               Master_SSL_Key:

        Seconds_Behind_Master: 0

Master_SSL_Verify_Server_Cert: No

                Last_IO_Errno: 0

                Last_IO_Error:

               Last_SQL_Errno: 0

               Last_SQL_Error:

  Replicate_Ignore_Server_Ids:

             Master_Server_Id: 1

                  Master_UUID: 7096d4eb-7378-11ec-8ec4-000c29f7d066

             Master_Info_File: mysql.slave_master_info

                    SQL_Delay: 0

          SQL_Remaining_Delay: NULL

      Slave_SQL_Running_State: Replica has read all relay log; waiting for more updates

           Master_Retry_Count: 86400

                  Master_Bind:

      Last_IO_Error_Timestamp:

     Last_SQL_Error_Timestamp:

               Master_SSL_Crl:

           Master_SSL_Crlpath:

           Retrieved_Gtid_Set:

            Executed_Gtid_Set:

                Auto_Position: 0

         Replicate_Rewrite_DB:

                 Channel_Name:

           Master_TLS_Version:

       Master_public_key_path:

        Get_master_public_key: 0

            Network_Namespace:

1 row in set, 1 warning (0.00 sec)

mysql>

配置Mysql半同步复制

在主库加载插件semisync_master.so,从库加载插件semisync_slave.so

1

2

3

4

5

6

7

8

9

hda2

mysql> install plugin rpl_semi_sync_master soname 'semisync_master.so';

Query OK, 0 rows affected, 1 warning (0.01 sec)

hda3

mysql> install plugin rpl_semi_sync_slave soname 'semisync_slave.so';

Query OK, 0 rows affected, 1 warning (0.01 sec)

hda4

mysql> install plugin rpl_semi_sync_slave soname 'semisync_slave.so';

Query OK, 0 rows affected, 1 warning (0.01 sec)

启用半同步复制

1

2

3

4

5

6

7

8

9

hdp2

mysql> set global rpl_semi_sync_master_enabled=1;

Query OK, 0 rows affected (0.00 sec)

hdp3

mysql> set global rpl_semi_sync_slave_enabled=1;

Query OK, 0 rows affected (0.00 sec)

hdp4

mysql> set global rpl_semi_sync_slave_enabled=1;

Query OK, 0 rows affected (0.00 sec)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值