微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

mysql MMM高可用的部署

这篇文章给大家分享的是mysql MMM高可用的部署,相信大部分人都还没学会这个技能,为了让大家学会,给大家总结了以下内容,话不多说,一起往下看吧。

1 环境:
1.1 OS and MysqL verson:

[root@MysqL01 ~]# uname -a
Linux MysqL01 3.10.0-327.18.2.el7.x86_64 #1 SMP Thu May 12 11:03:55 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
[root@MysqL01 ~]# /opt/MysqL7/bin/MysqL --version
/opt/MysqL7/bin/MysqL  Ver 14.14 distrib 5.7.28, for el7 (x86_64) using  EditLine wrapper

1.2 IP 规划:

192.168.1.201   MysqL01    #master1
192.168.1.202   MysqL02    #master2
192.168.1.247    slave1      #slave
192.168.1.243   monitor     #monitor

2 MysqL安装:
#在所有SERVER上安装MysqL
#准备my.cnf, 注意所有SERVER的my.cnf中,server-id要不同

# cat my.cnf 
[client]
default-character-set = utf8
port = 3309
socket = /data/57.3309/MysqL.sock

[MysqLd]
server-id = 4
collation-server = utf8_unicode_ci
init-connect = 'SET NAMES utf8'
character-set-server = utf8
port        = 3309
socket        = /data/57.3309/MysqL.sock
datadir = /data/57.3309/data
log-error = /data/57.3309/MysqL.err
pid-file = /data/57.3309/MysqL.pid
gtid_mode=on
#双主设定auto-increment-increment 和auto-increment-offset 避免主键冲突
auto-increment-increment = 2
auto-increment-offset = 1
#MysqL02
#auto-increment-offset = 2
#slave上不设置auto-increment-increment 和auto-increment-offset 

sync_binlog = 1
sync_master_info = 1
sync_relay_log = 1
sync_relay_log_info = 1
enforce-gtid-consistency=on
skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
log-bin = /data/57.3309/data/MysqL-bin
relay_log = /data/57.3309/data/relay-bin
##cascaded replication for slave to write binlog.
log_slave_updates = 1
read-only=1 #所有SERVER设定read-only
binlog_format = row
slow_query_log = 1
slow_query_log_file = /data/57.3309/log/slowquery.log
long_query_time = 1
general_log = off
general_log_file = /data/57.3309/log/general.log
#skip-grant-tables

[MysqLdump]
quick
max_allowed_packet = 16M
[MysqL]
no-auto-rehash
[myisamchk]
key_buffer_size = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M
[MysqLhotcopy]
interactive-timeout

[root@MysqL01 data]# /opt/MysqL7/bin/MysqL_install_db --basedir='/opt/MysqL7' --datadir='/data/57.3310/data' --user=MysqL
2020-01-29 16:16:50 [WARNING] MysqL_install_db is deprecated. Please consider switching to MysqLd --initialize
2020-01-29 16:16:54 [WARNING] The bootstrap log isn't empty:
2020-01-29 16:16:54 [WARNING] 2020-01-29T08:16:50.886558Z 0 [Warning] --bootstrap is deprecated. Please consider using --initialize instead
2020-01-29T08:16:50.887365Z 0 [Warning] Changed limits: max_open_files: 1024 (requested 5000)
2020-01-29T08:16:50.887370Z 0 [Warning] Changed limits: table_open_cache: 431 (requested 2000)
#第一次:skip_grant_tables方式启动
[root@MysqL01 57.3310]# /opt/MysqL7/bin/MysqLd_safe --defaults-file='/data/57.3310/my.cnf' --skip-grant-tables --user=root &
Logging to '/data/57.3310/MysqL.err'.
2020-01-29T08:39:04.537600Z MysqLd_safe Starting MysqLd daemon with databases from /data/57.3310/data
#修改root密码
 /opt/MysqL7/bin/MysqL -uroot -S /data/57.3310/MysqL.soc #免密码登录
 #update语句修改root密码
 MysqL> update MysqL.user set authentication_string=password('password123')  where user='root' and host='localhost';
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 1
#update语句设置密码不过期
MysqL> update MysqL.user set password_expired='N' where user='root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0
#刷新权限
MysqL> flush privileges;
Query OK, 0 rows affected (0.00 sec)
#正常关闭,重启MysqL登录MysqL
 /opt/MysqL7/bin/MysqLadmin -uroot -ppassword123 -S /data/57.3310/MysqL.sock shutdown
 /opt/MysqL7/bin/MysqLd_safe --defaults-file='/data/57.3310/my.cnf' --user=root &
/opt/MysqL7/bin/MysqL -uroot -ppassword123  -S /data/57.3310/MysqL.sock
#权限,所有SERVER上。
MysqL> GRANT REPLICATION SLAVE ON *.* TO 'rep'@'%' IDENTIFIED BY 'password123';
Query OK, 0 rows affected (0.00 sec)
MysqL> flush privileges;
Query OK, 0 rows affected (0.00 sec)

3 设定MysqL主从复制
3.1 #复制架构:MysqL01 <===>MysqL02  主主复制,GTID方式,
MysqL01===>slave           主从复制,传统方式,
3.2 #MysqL01 <===>MysqL02
#MysqL02上:

MysqL> change master to  MASTER_HOST='192.168.1.201',MASTER_USER='rep',MASTER_PASSWORD='password123',MASTER_PORT=3309,master_auto_position=1;
Query OK, 0 rows affected, 2 warnings (0.02 sec)

MysqL>
MysqL>
MysqL> start slave;
Query OK, 0 rows affected (0.00 sec)
MysqL> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.201
                  Master_User: rep
                  Master_Port: 3309
                Connect_Retry: 60
              Master_Log_File: MysqL-bin.000004
          Read_Master_Log_Pos: 76618
               Relay_Log_File: relay-bin.000005
                Relay_Log_Pos: 76831
        Relay_Master_Log_File: MysqL-bin.000004
             Slave_IO_Running: Yes
            Slave_sql_Running: Yes

#MysqL01上:

MysqL>  CHANGE MASTER TO MASTER_HOST='192.168.1.202',MASTER_PORT=3309,MASTER_USER='rep',MASTER_PASSWORD='password123',MASTER_AUTO_POSITION = 1;
Query OK, 0 rows affected, 2 warnings (0.00 sec)
MysqL> start slave;
Query OK, 0 rows affected (0.00 sec)

MysqL> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.202
                  Master_User: rep
                  Master_Port: 3309
                Connect_Retry: 60
              Master_Log_File: MysqL-bin.000009
          Read_Master_Log_Pos: 391
               Relay_Log_File: relay-bin.000012
                Relay_Log_Pos: 454
        Relay_Master_Log_File: MysqL-bin.000009
             Slave_IO_Running: Yes
            Slave_sql_Running: Yes

#slave上

MysqL> change master to  MASTER_HOST='192.168.1.201',MASTER_USER='rep',MASTER_PASSWORD='password123',MASTER_PORT=3309,MASTER_LOG_FILE='MysqL-bin.000014',MASTER_LOG_POS=65754;
Query OK, 0 rows affected, 2 warnings (0.00 sec)
MysqL> start slave;
Query OK, 0 rows affected (0.00 sec)
MysqL> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.201
                  Master_User: rep
                  Master_Port: 3309
                Connect_Retry: 60
              Master_Log_File: MysqL-bin.000014
          Read_Master_Log_Pos: 65754
               Relay_Log_File: relay-bin.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: MysqL-bin.000014
             Slave_IO_Running: Yes
            Slave_sql_Running: Yes

4 安装MMM并配置:
4.1 #安装enpl源并安装MMM:

yum install epel-release.noarch
yum install -y MysqL-mmm-agent
yum install -y MysqL-mmm-monitor

4.2 #配置mmm用户,由于是全库复制,只要在MysqL01上配置,会自动同步到其他SERVER:

GRANT PROCESS, SUPER, REPLICATION CLIENT ON *.* TO 'mmm_agent'@'192.168.1.%' IDENTIFIED BY 'password123';
GRANT REPLICATION CLIENT ON *.* TO 'mmm_monitor'@'192.168.1.%' IDENTIFIED BY 'password123';

4.3 #配置mmm设定档:
#MysqL01(master1)

 cd /etc/MysqL-mmm/
cat mmm_common.conf
active_master_role      writer

<host default>
    cluster_interface       eno33554992
    pid_path                /run/MysqL-mmm-agent.pid
    bin_path                /usr/libexec/MysqL-mmm/
    replication_user        rep
    replication_password    password123
    agent_user              mmm_agent
    agent_password          password123
</host>

<host MysqL01>
    ip      192.168.1.201
    MysqL_port  3309
    mode    master
    peer    MysqL02
</host>

<host MysqL02>
    ip      192.168.1.202
    MysqL_port  3309
    mode    master
    peer    MysqL01
</host>

<host slave1>
    ip      192.168.1.247
    MysqL_port  3309
    mode    slave
</host>

#<host slave2>
#    ip      192.168.1.242
#    MysqL_port  3310
 #   mode    slave
#</host>

<role writer>
    hosts   MysqL01, MysqL02
    ips     192.168.1.200
    mode    exclusive
</role>

<role reader>
    hosts   MysqL01, MysqL02,slave1
    ips     192.168.1.251, 192.168.1.252, 192.168.1.253
    mode    balanced
</role>

[root@MysqL01 MysqL-mmm]# cat mmm_agent.conf 
include mmm_common.conf

# The 'this' variable refers to this server.  Proper operation requires
# that 'this' server (db1 by default), as well as all other servers, have the
# proper IP addresses set in mmm_common.conf.
this MysqL01 

#MysqL02(master2)上:

[root@MysqL02 MysqL-mmm]# ll
total 8
-rw-r-----. 1 root root 235 Jan 29 20:54 mmm_agent.conf
-rw-r-----. 1 root root 991 Jan 30 14:02 mmm_common.conf
[root@MysqL02 MysqL-mmm]# cat mmm_common.conf
active_master_role      writer

<host default>
    cluster_interface       eno33554992
    pid_path                /run/MysqL-mmm-agent.pid
    bin_path                /usr/libexec/MysqL-mmm/
    replication_user        rep
    replication_password    password123
    agent_user              mmm_agent
    agent_password          password123
</host>

<host MysqL01>
    ip      192.168.1.201
    MysqL_port  3309
    mode    master
    peer    MysqL02
</host>

<host MysqL02>
    ip      192.168.1.202
    MysqL_port  3309
    mode    master
    peer    MysqL01
</host>

<host slave1>
    ip      192.168.1.247
    MysqL_port  3309    
    mode    slave
</host>

#<host slave2>
#    ip      192.168.1.242
#    MysqL_port  3310
#    mode    slave
#</host>

<role writer>
    hosts   MysqL01, MysqL02
    ips     192.168.1.200
    mode    exclusive
</role>

<role reader>
    hosts   MysqL01, MysqL02,slave1
    ips     192.168.1.251, 192.168.1.252, 192.168.1.253
    mode    balanced
</role>
[root@MysqL02 MysqL-mmm]# cat mmm_agent.conf 
include mmm_common.conf

# The 'this' variable refers to this server.  Proper operation requires
# that 'this' server (db1 by default), as well as all other servers, have the
# proper IP addresses set in mmm_common.conf.
this MysqL02 
[root@MysqL02 MysqL-mmm]# 

#slave上:

[root@salve1 MysqL-mmm]# pwd
/etc/MysqL-mmm
[root@salve1 MysqL-mmm]# ll
total 8
-rw-r-----. 1 root root 234 Jan 30 14:11 mmm_agent.conf
-rw-r-----. 1 root root 945 Jan 30 14:56 mmm_common.conf
[root@salve1 MysqL-mmm]# cat mmm_common.conf
active_master_role      writer

<host default>
    cluster_interface       eno16777736
    pid_path                /run/MysqL-mmm-agent.pid
    bin_path                /usr/libexec/MysqL-mmm/
    replication_user        rep
    replication_password    password123
    agent_user              mmm_agent
    agent_password          password123
    MysqL_port  3309
</host>

<host MysqL01>
    ip      192.168.1.201
    mode    master
    peer    MysqL02
</host>

<host MysqL02>
    ip      192.168.1.202
    mode    master
    peer    MysqL01
</host>

<host slave1>
    ip      192.168.1.247
    mode    slave
</host>

#<host slave2>
#    ip      192.168.1.242
#    MysqL_port  3310
#    mode    slave
#</host>

<role writer>
    hosts   MysqL01, MysqL02
    ips     192.168.1.200
    mode    exclusive
</role>

<role reader>
    hosts   MysqL01, MysqL02,slave1
    ips     192.168.1.251, 192.168.1.252, 192.168.1.253
    mode    balanced
</role>
[root@salve1 MysqL-mmm]# cat mmm_agent.conf 
include mmm_common.conf

# The 'this' variable refers to this server.  Proper operation requires
# that 'this' server (db1 by default), as well as all other servers, have the
# proper IP addresses set in mmm_common.conf.
this slave1 
[root@salve1 MysqL-mmm]# 

#monitor上:

[root@MysqL01 MysqL-mmm]# cat mmm_mon.conf
include mmm_common.conf

<monitor>
    ip                  127.0.0.1
    pid_path            /run/MysqL-mmm-monitor.pid
    bin_path            /usr/libexec/MysqL-mmm
    status_path         /var/lib/MysqL-mmm/mmm_mond.status
    ping_ips            192.168.1.201,192.168.1.202,192.168.1.247
    auto_set_online     60

    # The kill_host_bin does not exist by default, though the monitor will
    # throw a wning about it missing.  See the section 5.10 "Kill Host
    # Functionality" in the PDF documentation.
    #
    # kill_host_bin     /usr/libexec/MysqL-mmm/monitor/kill_host
    #
</monitor>

<host default>
    monitor_user        mmm_monitor
    monitor_password    password123
</host>

debug 0

4.4 启动monitor和agent
#monitor:

systemctl enable MysqL-mmm-monitor.service   #加入启动项
systemctl start MysqL-mmm-monitor.service  #启动monitor
#状态:
[root@MysqL01 MysqL-mmm]# systemctl status MysqL-mmm-monitor.service 
* MysqL-mmm-monitor.service - MysqL MMM Monitor
   Loaded: loaded (/usr/lib/systemd/system/MysqL-mmm-monitor.service; enabled; vendor preset: disabled)
   Active: active (running) since 四 2020-01-30 19:52:04 CST; 21h ago
  Process: 1464 ExecStart=/usr/sbin/mmm_mond (code=exited, status=0/SUCCESS)
 Main PID: 2703 (mmm_mond)
   CGroup: /system.slice/MysqL-mmm-monitor.service
           |-2703 mmm_mond
           |-2704 mmm_mond
           |-2848 perl /usr/libexec/MysqL-mmm/monitor/checker ping_ip
           |-2851 perl /usr/libexec/MysqL-mmm/monitor/checker MysqL
           |-2853 perl /usr/libexec/MysqL-mmm/monitor/checker ping
           |-2855 perl /usr/libexec/MysqL-mmm/monitor/checker rep_backlog
           `-2858 perl /usr/libexec/MysqL-mmm/monitor/checker rep_threads

1月 30 19:51:53 MysqL01 systemd[1]: Starting MysqL MMM Monitor...
1月 30 19:52:04 MysqL01 systemd[1]: Started MysqL MMM Monitor.

#MysqL01(master1):

systemctl enable MysqL-mmm-agent.service  #加入启动项
systemctl start MysqL-mmm-agent.service #启动mmm agent
#状态:
[root@MysqL01 MysqL-mmm]# systemctl status MysqL-mmm-agent.service        
* MysqL-mmm-agent.service - MysqL MMM agent
   Loaded: loaded (/usr/lib/systemd/system/MysqL-mmm-agent.service; enabled; vendor preset: disabled)
   Active: active (running) since 四 2020-01-30 19:52:03 CST; 21h ago
  Process: 1459 ExecStart=/usr/sbin/mmm_agentd (code=exited, status=0/SUCCESS)
 Main PID: 2695 (mmm_agentd)
   CGroup: /system.slice/MysqL-mmm-agent.service
           |-2695 mmm_agentd
           `-2909 mmm_agentd

1月 30 19:51:53 MysqL01 systemd[1]: Starting MysqL MMM agent...
1月 30 19:52:03 MysqL01 systemd[1]: Started MysqL MMM agent.
1月 30 21:08:07 MysqL01 systemd[1]: Started MysqL MMM agent.
[root@MysqL01 MysqL-mmm]# 

#MysqL02(master2):

systemctl enable MysqL-mmm-agent.service  #加入启动项
systemctl start MysqL-mmm-agent.service #启动mmm agent
#状态:
[root@MysqL02 MysqL-mmm]# systemctl status MysqL-mmm-agent.service        
* MysqL-mmm-agent.service - MysqL MMM agent
   Loaded: loaded (/usr/lib/systemd/system/MysqL-mmm-agent.service; enabled; vendor preset: disabled)
   Active: active (running) since 四 2020-01-30 19:52:03 CST; 21h ago
  Process: 1459 ExecStart=/usr/sbin/mmm_agentd (code=exited, status=0/SUCCESS)
 Main PID: 2695 (mmm_agentd)
   CGroup: /system.slice/MysqL-mmm-agent.service
           |-2695 mmm_agentd
           `-2909 mmm_agentd

1月 30 19:51:53 MysqL02 systemd[1]: Starting MysqL MMM agent...
1月 30 19:52:03 MysqL02 systemd[1]: Started MysqL MMM agent.
1月 30 21:08:07 MysqL02 systemd[1]: Started MysqL MMM agent.
[root@MysqL02 MysqL-mmm]# 

#slave:

systemctl enable MysqL-mmm-agent.service  #加入启动项
systemctl start MysqL-mmm-agent.service #启动mmm agent
#状态
[root@salve1 MysqL-mmm]# systemctl status MysqL-mmm-agent.service 
[0m MysqL-mmm-agent.service - MysqL MMM agent
   Loaded: loaded (/usr/lib/systemd/system/MysqL-mmm-agent.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2020-01-30 21:07:52 CST; 20h ago
  Process: 5323 ExecStart=/usr/sbin/mmm_agentd (code=exited, status=0/SUCCESS)
 Main PID: 5325 (mmm_agentd)
   CGroup: /system.slice/MysqL-mmm-agent.service
           25 mmm_agentd
           26 mmm_agentd

Jan 30 21:07:51 MysqL01 systemd[1]: Starting MysqL MMM agent...
Jan 30 21:07:52 MysqL01 systemd[1]: Started MysqL MMM agent.
[root@salve1 MysqL-mmm]# 

5 #查看MMM状态:(monitor上)

[root@MysqL01 MysqL-mmm]# mmm_control --help
Invalid command '--help'

Valid commands are:
    help                              - show this message
    ping                              - ping monitor
    show                              - show status
    checks [<host>|all [<check>|all]] - show checks status
    set_online <host>                 - set host <host> online
    set_offline <host>                - set host <host> offline
    mode                              - print current mode.
    set_active                        - switch into active mode.
    set_manual                        - switch into manual mode.
    set_passive                       - switch into passive mode.
    move_role [--force] <role> <host> - move exclusive role <role> to host <host>
                                        (Only use --force if you kNow what you are doing!)
    set_ip <ip> <host>                - set role with ip <ip> to host <host>

[root@MysqL01 MysqL-mmm]# 

#show 状态:

[root@MysqL01 MysqL-mmm]# mmm_control show
  MysqL01(192.168.1.201) master/ONLINE. Roles: reader(192.168.1.251), writer(192.168.1.200)
  MysqL02(192.168.1.202) master/ONLINE. Roles: reader(192.168.1.253)
  slave1(192.168.1.247) slave/ONLINE. Roles: reader(192.168.1.252)

#尝试切换
#切换前检查slave复制状态:可以看到这个时候的主库是MysqL01

MysqL> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.201
                  Master_User: rep
                  Master_Port: 3309
                Connect_Retry: 60
              Master_Log_File: MysqL-bin.000014
          Read_Master_Log_Pos: 65754
               Relay_Log_File: relay-bin.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: MysqL-bin.000014
             Slave_IO_Running: Yes
            Slave_sql_Running: Yes

#切换:

[root@MysqL01 MysqL-mmm]# mmm_control move_role writer MysqL02
OK: Role 'writer' has been moved from 'MysqL01' to 'MysqL02'. Now you can wait some time and check new roles info!
[root@MysqL01 MysqL-mmm]# 
[root@MysqL01 MysqL-mmm]#  mmm_control show
  MysqL01(192.168.1.201) master/ONLINE. Roles: reader(192.168.1.251)
  MysqL02(192.168.1.202) master/ONLINE. Roles: reader(192.168.1.253), writer(192.168.1.200)
  slave1(192.168.1.247) slave/ONLINE. Roles: reader(192.168.1.252)

#可以看到已经把writer角色切换到MysqL02上
#检查slave复制状态,自动切换到MysqL02上

MysqL> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.202
                  Master_User: rep
                  Master_Port: 3309
                Connect_Retry: 60
              Master_Log_File: MysqL-bin.000010
          Read_Master_Log_Pos: 64494
               Relay_Log_File: relay-bin.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: MysqL-bin.000010
             Slave_IO_Running: Yes
            Slave_sql_Running: Yes

关于MysqL MMM高可用的部署就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果喜欢这篇文章,不如把它分享出去让更多的人看到。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐