MySQL高可用群集

來源:互聯網
上載者:User

標籤:date   pid   exec   配置   copyright   主主   file   acl   0.00   

MySQL高可用群集技能目標
  • 熟悉MMM高可用架構
  • 熟悉MHA高可用架構
  • 學會配置MMM高可用
  • 學會配置MHA高可用
關於MMM高可用架構說明
  • mmm_mon:監控進程負責所有監控工作、決定和處理節點角色活動
  • mmm_agent:運行在每個MySQL伺服器上的代理進程完成監控的探針工作和執行簡單的遠端服務設定
  • mmm_control:一個簡單的指令碼,提供管理mmm_mond的進程命令
  • mysql-mmm的監管會提供多個虛擬IP(VIP),包括一個可寫VIP,多個可讀VIP,通過監管的管理綁定在可用MySQL之上,當某一台MySQL關閉時,監管會將VIP遷移至其他MySQL
實驗環境mysql-monitor 是監控伺服器
主機 作業系統 IP地址 只要軟體
mysql-master1 Centos 7 x86_64 192.168.32.222 MySQL-MMM
mysql-master2 Centos 7 x86_64 192.168.32.223 MySQL-MMM
mysql-slave1 Centos 7 x86_64 192.168.32.224 MySQL-MMM
mysql-slave2 Centos 7 x86_64 192.168.32.225 MySQL-MMM
mysql-monitor Centos 7 x86_64 192.168.32.226 MySQL-MMM
master1,2 Centos 7 x86_64 VIP:192.168.32.221 MySQL-MMM
slave1 Centos 7 x86_64 VIP1:192.168.32.220 MySQL-MMM
slave2 Centos 7 x86_64 VIP1:192.168.32.199 MySQL-MMM
搭建主伺服器master1修改master1主設定檔
[[email protected] ~] vim /etc/my.cnf              #刪除從第一行開始到第九行刪除添加下列語句[client]port = 3306default-character-set=utf8socket = /usr/local/mysql/mysql.sock[mysql]port = 3306default-character-set=utf8socket = /usr/local/mysql/mysql.sock[mysqld]user = mysqlbasedir = /usr/local/mysqldatadir = /usr/local/mysql/dataport = 3306character_set_server=utf8pid-file = /usr/local/mysql/mysqld.pidsocket = /usr/local/mysql/mysql.sockserver-id = 1                                               #server-id每台資料庫server值都是唯一的log-error=/usr/local/mysql/data/mysql_error.log         #開啟錯誤記錄檔general_log=ON                                          #開啟訪問日誌general_log_file=/usr/local/mysql/data/mysql_general.loglog_bin=mysql-bin                                       #開啟二進位日誌slow_query_log=ON                                       #開啟慢日誌slow_query_log_file=mysql_slow_query.loglong_query_time=5               log_slave_updates=truesync_binlog=1                                               #同步auto_increment_increment=2                                  #增量值auto_increment_offset=1                                     #起點值binlog-ignore-db=mysql,information_schema                   #關閉mysql,information_schema 這兩個二進位日誌[[email protected] ~]# systemctl restart mysqld.service #重啟資料庫[[email protected] data]# netstat -anpt | grep 3306tcp6       0      0 :::3306                 :::*                    LISTEN      3074/mysqld   #查看連接埠有沒開啟
master2、slave1、slave2複製master1的設定檔把server-id改掉改成不一樣的做主從同步 主主複製,主從同步我在這是一台一台配的有一條命令可以節省改設定檔的時間scp /etc/my.cnf [email protected]:/etc/配置主主複製-兩台主伺服器相互複製在m1上為m2授予從的許可權,在m2上也要為m1授予從的許可權
#先用show master status 查看兩台主伺服器的二進位記錄檔和位移量#master1mysql> show master status;+------------------+----------+--------------+--------------------------+-------------------+| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB         | Executed_Gtid_Set |+------------------+----------+--------------+--------------------------+-------------------+| mysql-bin.000004 |      154 |              | mysql,information_schema |                   |+------------------+----------+--------------+--------------------------+-------------------+1 row in set (0.00 sec)#master2mysql> show master status;+------------------+----------+--------------+--------------------------+-------------------+| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB         | Executed_Gtid_Set |+------------------+----------+--------------+--------------------------+-------------------+| mysql-bin.000004 |      154 |              | mysql,information_schema |                   |+------------------+----------+--------------+--------------------------+-------------------+1 row in set (0.00 sec)
先把master2授權給master1
mysql> grant replication slave on *.* to ‘replication‘@‘192.168.32.%‘ identified by ‘123456‘;  Query OK, 0 rows affected, 1 warning (0.03 sec)mysql> change master to master_host=‘192.168.32.184‘,master_user=‘replication‘,master_passwordd=‘123456‘,master_log_file=‘mysql_bin.000002‘,master_log_pos=154;Query OK, 0 rows affected, 2 warnings (0.04 sec)### 開啟同步功能 #slave代表開啟同步不是代表從伺服器,我上面設定的只是個名稱與這邊的意思不同mysql> start slave;Query OK, 0 rows affected (0.03 sec)
先把master1授權給master2
mysql> grant replication slave on *.* to ‘replication‘@‘192.168.32.%‘ identified by ‘123456‘;Query OK, 0 rows affected, 1 warning (0.00 sec)mysql> change master to master_host=‘192.168.32.151‘,master_user=‘replication‘,master_password=‘123456‘,master_log__file=‘mysql_bin.000004‘,master_log_pos=154;Query OK, 0 rows affected, 2 warnings (0.02 sec)#開啟同步功能mysql> start slave;Query OK, 0 rows affected (0.03 sec)#查看IO線程和SQL線程是否都已經開啟是否顯示為yesmysql>show slave status\G*************************** 1. row ***************************               Slave_IO_State: Waiting for master to send event                  Master_Host: 192.168.32.151                  Master_User: replication                  Master_Port: 3306                Connect_Retry: 60              Master_Log_File: mysql-bin.000005          Read_Master_Log_Pos: 154               Relay_Log_File: localhost-relay-bin.000011                Relay_Log_Pos: 367        Relay_Master_Log_File: mysql-bin.000005             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: 154              Relay_Log_Space: 625              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: 0Master_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: 111eb6e9-78dd-11e8-9337-000c2940c252             Master_Info_File: /usr/local/mysql/data/master.info                    SQL_Delay: 0          SQL_Remaining_Delay: NULL      Slave_SQL_Running_State: Slave 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: 1 row in set (0.00 sec)如果只開啟了SQL線程而IO線程沒有開啟那麼檢查防火牆有無關閉 增強性安全模組有沒有關閉再執行下列語句mysql>stop slave; #先停止slavemysql>reset slave; #重新整理slave同步功能mysql>start slave; #最後再開啟然後查看
建立一個資料庫看是否能同步
  • 主1
MariaDB [(none)]> create database info;Query OK, 1 row affected (0.00 sec)MariaDB [(none)]> show databases;+--------------------+| Database           |+--------------------+| information_schema || info               || mysql              || performance_schema || test               |+--------------------+
  • 主2
MariaDB [(none)]> show databases;+--------------------+| Database           |+--------------------+| information_schema || info               || mysql              || performance_schema || test               |+--------------------+5 rows in set (0.00 sec)
  • 從1
MariaDB [(none)]> show databases;+--------------------+| Database           |+--------------------+| information_schema || info               || mysql              || performance_schema || test               |+--------------------+5 rows in set (0.00 sec)
  • 從2
MariaDB [(none)]> show databases;+--------------------+| Database           |+--------------------+| information_schema || info               || info1              || mysql              || performance_schema || test               |+--------------------+6 rows in set (0.00 sec)
在從2 上建立資料庫看主1 主2 從1 會不會同步不會同步就說明從伺服器只會同步主伺服器、主伺服器不會同步從伺服器配置從伺服器兩個從伺服器要能同時能同步兩個主伺服器安裝MMM在所有伺服器上安裝注意,epel源要配置好先配置epel源
配置ALI雲源,然後安裝epel-release源。wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repoyum clean all && yum makecacheyum -y install epel-releaseyum -y install mysql-mmm*
修改設定檔 //所有主機上都要配置,直接複製多份
[[email protected] etc]# cd /etc/mysql-mmm/[[email protected] mysql-mmm]# vim mmm_common.confactive_master_role      writer<host default>    cluster_interface       ens33 #修改網卡預設是eth0    pid_path                /run/mysql-mmm-agent.pid    bin_path                /usr/libexec/mysql-mmm/    replication_user        replicant    #登陸使用者是replicat    replication_password    123456  #複製同步登陸密碼    agent_user              mmm_agent    agent_password          123456 #登陸代理密碼</host><host db1>    ip      192.168.32.222  ——> 主伺服器1的IP地址    mode    master ——>主伺服器    peer    db2</host><host db2>    ip      192.168.32.223  ——>主伺服器2的IP地址    mode    master ——>主伺服器    peer    db1</host><host db3>    ip      192.168.32.224  ——>從伺服器1的IP地址    mode    slave ——>從伺服器</host><host db4>    ip      192.168.32.225 ——>從伺服器2的IP地址    mode    slave ——>從伺服器</host><role writer> #許可權可寫    hosts   db1, db2    ips     192.168.32.221 ——>主伺服器1、2的虛擬IP    mode    exclusive</role><role reader> #許可權可讀    hosts   db3, db4 #主機名稱    ips     192.168.32.220 192.168.32.199 #從伺服器1、2的虛擬IP    mode    balanced</role>
上傳設定檔
scp mmm_common.conf [email protected]:/etc/mysql-mmm/scp mmm_common.conf [email protected]:/etc/mysql-mmm/scp mmm_common.conf [email protected]:/etc/mysql-mmm/
------------------在monitor(監聽)伺服器上配置----
#修改IP兩主兩從的IP地址在ping_ips後面,auto_set_online 自動化佈建線上時間長度為10秒<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.32.222,192.168.32.223,192.168.32.224,192.168.32.225    auto_set_online     10cd /etc/mysql-mmm/ //改密碼vi mmm_mon.conf<host default>    monitor_user        mmm_monitor    monitor_password    123456</host>
--------------在所有資料庫上為mmm_moniter和mmm_agent授權-----
#為mmm_agent授權MariaDB [(none)]> grant super, replication client, process on *.* to ‘mmm_agent‘@‘192.168.32.%‘ identified by ‘123456‘;Query OK, 0 rows affected (0.01 sec)#為mmm_moniter授權MariaDB [(none)]> grant replication client on *.* to ‘mmm_monitor‘@‘192.168.32.%‘ identified by ‘123456‘; Query OK, 0 rows affected (0.01 sec)# 重新整理MariaDB [(none)]> flush privileges;Query OK, 0 rows affected (0.01 sec)
---------------修改所有資料庫的mmm_agent.conf----
vi /etc/mysql-mmm/mmm_agent.confthis db1 //根據規划進行逐一調整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 db4
--------------在所有資料庫伺服器上啟動mysql-mmm-agent---
#開啟服務systemctl start mysql-mmm-agent.service#設定開機自啟動systemctl enable mysql-mmm-agent.service#用 mmm_control show 查看各個節點[[email protected] mysql-mmm]# mmm_control show   db1(192.168.32.222) master/ONLINE. Roles: writer(192.168.32.221)  db2(192.168.32.223) master/ONLINE. Roles:   db3(192.168.32.224) slave/ONLINE. Roles: reader(192.168.32.199)  db4(192.168.32.225) slave/ONLINE. Roles: reader(192.168.32.220)
故障測試停止db1 確認 虛擬位址 221 是否移動到 db2 上。注意:主不會搶佔
[[email protected] mysql-mmm]# systemctl stop mariadb.service [[email protected] mysql-mmm]# mmm_control show   db1(192.168.32.222) master/HARD_OFFLINE. Roles:   db2(192.168.32.223) master/ONLINE. Roles: writer(192.168.32.221)  db3(192.168.32.224) slave/ONLINE. Roles: reader(192.168.32.199)  db4(192.168.32.225) slave/ONLINE. Roles: reader(192.168.32.220)
再開一台mysql伺服器作為用戶端在db1伺服器上為最後一台用戶端伺服器位址授權登入
MariaDB [(none)]> grant all on *.* to ‘testdba‘@‘192.168.32.222‘ identified by ‘123456‘;MariaDB [(none)]> flush privileges;#用戶端登陸虛擬IP[[email protected] ~]# mysql -utestdba -p -h 192.168.32.221 Enter password:  #設定代理授權是的登陸密碼Welcome to the MariaDB monitor.  Commands end with ; or \g.Your MariaDB connection id is 1358Server version: 5.5.56-MariaDB MariaDB ServerCopyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.#在用戶端建立資料庫,測試同步MariaDB [(none)]> create database info; #在用戶端建立的Query OK, 1 row affected (0.01 sec)MariaDB [(none)]> show databases; +--------------------+| Database           |+--------------------+| information_schema || info               || mysql              || performance_schema || test               |+--------------------+5 rows in set (0.00 sec)#在服務端查詢MariaDB [(none)]> show databases; #db1+--------------------+| Database           |+--------------------+| information_schema || info               || mysql              || performance_schema || test               |+--------------------+5 rows in set (0.00 sec)MariaDB [(none)]> show databases; #db2+--------------------+| Database           |+--------------------+| information_schema || info               || mysql              || performance_schema || test               |+--------------------+5 rows in set (0.00 sec)MariaDB [(none)]> show databases; #db3+--------------------+| Database           |+--------------------+| information_schema || mysql              || info               || performance_schema || test               |+--------------------+4 rows in set (0.00 sec)MariaDB [(none)]> show databases; #db4+--------------------+| Database           |+--------------------+| information_schema || mysql              || info               || performance_schema || test               |+--------------------+4 rows in set (0.00 sec)#在用戶端建立就是在主伺服器上建立從伺服器同步
---驗證將從伺服器停止一台,另一台將接管兩個虛擬IP,以保證業務不停止-----
[[email protected] mysql-mmm]# mmm_control show   db1(192.168.32.222) master/ONLINE. Roles:   db2(192.168.32.223) master/ONLINE. Roles: writer(192.168.32.221)  db3(192.168.32.224) slave/ONLINE. Roles: reader(192.168.32.199), reader(192.168.32.220)  db4(192.168.32.225) slave/HARD_OFFLINE. Roles: 
==上述同步做完了存在一個問題在做主從同步的時候同步的是主伺服器上面的真實IP但是在down掉同步的那個主伺服器的時候從伺服器將不會在同步所以我們再設定主從同步的時候IP地址用他們設定的VIP(虛擬IP)==

MySQL高可用群集

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.