1. 程式人生 > >centos mysql5.7.24 多主多從叢集搭建

centos mysql5.7.24 多主多從叢集搭建

系統

[[email protected] ~]# uname -a 
Linux i-ec0m1585 3.10.0-327.4.5.el7.x86_64 #1 SMP Mon Jan 25 22:07:14 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

主機名稱

hostname ip mysql port
mysql-master-60 10.39.47.60 3307
mysql-master-61 10.39.47.61 3308
mysql-slave-62 10.39.47.62 3309
mysql-slave-63 10.39.47.63 3310

用的是ansible安裝工具輔助安裝

host檔案如下

➜  ansible cat hosts                                                                             
[cache]
10.39.47.60
10.39.47.61
10.39.47.62
10.39.47.63

設定靜態IP例子,不需要設定固定IP的略過

vi /etc/sysconfig/network-script/ifcfg-xxx
IPADDR=10.39.47.60     #靜態IP

增加mysql yum源 以及部署mysql

ansible -i hosts cache -u root -m shell -a 'cd /opt/;wget http://repo.mysql.com/mysql57-community-release-el7-8.noarch.rpm'

ansible -i hosts cache -u root -m shell -a 'cd /opt/;rpm -ivh mysql57-community-release-el7-8.noarch.rpm' 

ansible -i hosts cache -u root -m shell -a 'yum update -y;yum -y install mysql-server'  

關閉selinux

sed -i 's/SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
setenforce 0
systemctl stop firewalld 
systemctl disable firewalld

mkdir /home/usr/local/mysql-5.7.24/data/3307/
chown -R /home/usr/local/mysql-5.7.24/data

查詢密碼方法

[[email protected] ~]# grep "password"  /usr/local/mysql-5.7.18/data/3310/error.log 
2018-10-23T15:29:30.152796Z 1 [Note] A temporary password is generated for [email protected]: 5K:j-*h&Pksu


啟動mysql

systemctl start mysqld
systemctl status mysqld

各個節點的配置

[[email protected] ~]# cat /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
[client]

port        = 3310

socket      = /usr/local/mysql-5.7.18/data/3310/mysql.sock

default-character-set=utf8



[mysqld]

port    = 3310

socket  = /usr/local/mysql-5.7.18/data/3310/mysql.sock

datadir = /usr/local/mysql-5.7.18/data/3310

log-error = /usr/local/mysql-5.7.18/data/3310/error.log

pid-file = /usr/local/mysql-5.7.18/data/3310/mysql.pid

symbolic-links=1

character-set-server=utf8

lower_case_table_names=1
log-bin=mysql-bin
relay-log       = /usr/local/mysql-5.7.18/data/3310/mysqld-relay-bin
server-id=3310
autocommit = 1

[[email protected] ~]# cat /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
[client]

port        = 3309

socket      = /usr/local/mysql-5.7.18/data/3309/mysql.sock

default-character-set=utf8



[mysqld]

port    = 3309

socket  = /usr/local/mysql-5.7.18/data/3309/mysql.sock

datadir = /usr/local/mysql-5.7.18/data/3309

log-error = /usr/local/mysql-5.7.18/data/3309/error.log

pid-file = /usr/local/mysql-5.7.18/data/3309/mysql.pid

symbolic-links=1

character-set-server=utf8

lower_case_table_names=1
relay-log       = /usr/local/mysql-5.7.18/data/3309/mysqld-relay-bin
log-bin=mysql-bin
server-id=3309
autocommit = 1

[[email protected] ~]# cat /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
[client]

port        = 3308

socket      = /usr/local/mysql-5.7.18/data/3308/mysql.sock

default-character-set=utf8



[mysqld]

port    = 3308

socket  = /usr/local/mysql-5.7.18/data/3308/mysql.sock

datadir = /usr/local/mysql-5.7.18/data/3308

log-error = /usr/local/mysql-5.7.18/data/3308/error.log

pid-file = /usr/local/mysql-5.7.18/data/3308/mysql.pid

symbolic-links=1

character-set-server=utf8

lower_case_table_names=1
log-bin=mysql-bin
server-id=3308
autocommit = 1
auto_increment_increment=2

auto_increment_offset=2

log-slave-updates

sync_binlog=1

[[email protected] ~]# cat /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
[client]

port        = 3307

socket      = /usr/local/mysql-5.7.18/data/3307/mysql.sock

default-character-set=utf8

 

[mysqld]

port    = 3307

socket  = /usr/local/mysql-5.7.18/data/3307/mysql.sock

datadir = /usr/local/mysql-5.7.18/data/3307

log-error = /usr/local/mysql-5.7.18/data/3307/error.log

pid-file = /usr/local/mysql-5.7.18/data/3307/mysql.pid

symbolic-links=1

character-set-server=utf8

lower_case_table_names=1
log-bin=mysql-bin
server-id=3307
autocommit = 1

auto_increment_increment=2

auto_increment_offset=1

log-slave-updates

sync_binlog=1

修改密碼

[email protected] ~]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 17
Server version: 5.7.24-log MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

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> 

修改密碼命令

SET PASSWORD = PASSWORD('enN$#12345');
alter user [email protected]"localhost" identified by 'xxx';
flush privileges;

在兩個master建立複製使用者

grant replication slave on *.* to 'copy'@'%' identified by 'xxx';
 flush privileges;

在master節點上重置master服務的狀態

reset master;

在slave節點上

stop slave;

reset slave;

reset master;

在master上檢視

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      154 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

如果已經執行,可以略過

SET PASSWORD = PASSWORD('xxx');
alter user [email protected]"localhost" identified by 'xxx';
flush privileges;
grant replication slave on *.* to 'copy'@'%' identified by 'xxx';
flush privileges;

在Slave上設定Master(相當於是4臺都需要設定)

設定從伺服器3308、3309,他們的主均為3307,即在3308和3309上執行如下操作

change master to master_host='10.39.47.60',

master_user='copy',

master_password='xxx',

master_port=3307,

master_log_file='mysql-bin.000001',

master_log_pos=154;

 

設定從伺服器3310、3307,他們的主均為3308,即在3310和3307上執行如下操作

change master to master_host='10.39.47.61',

master_user='copy',

master_password='enN$#12345',

master_port=3308,

master_log_file='mysql-bin.000001',

master_log_pos=154;

在所有的節點執行,確保這兩個引數處於yes狀態Slave_IO_Running: Yes,Slave_SQL_Running: Yes 即表示複製功能配置正常
例如:

start slave;
mysql> show slave status \G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.39.47.61
                  Master_User: copy
                  Master_Port: 3308
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 318
               Relay_Log_File: couchbase-60-relay-bin.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000001
             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: 318
              Relay_Log_Space: 534
              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: 3308
                  Master_UUID: 92c1778a-d6d6-11e8-90a1-525489ddae8b
             Master_Info_File: /usr/local/mysql-5.7.18/data/3307/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)

經常用的命令

stop slave;

reset slave;

reset master;
show slave status \G

遇到的問題

日誌

2018-10-24T02:59:03.680461Z 25 [ERROR] Slave I/O for channel '': Got fatal error 1236 from master when reading data from binary log: 'unknown error reading log event on the master; the first event 'mysql-bin.000001' at 154, the last event read from './mysql-bin.000001' at 318, the last byte read from './mysql-bin.000001' at 318.', Error_code: 1236

解決辦法
http://blog.51cto.com/461205160/1737721
https://www.cnblogs.com/cobbliu/p/4311926.html

mysqlbinlog --no-defaults /usr/local/mysql-5.7.18/data/3307/mysql-bin.000001 > test.txt

mysql> stop slave;
Query OK, 0 rows affected (0.16 sec)

mysql> change master to master_log_file='mysql-bin.000001',master_log_pos=123;
Query OK, 0 rows affected (0.03 sec)

mysql> start slave;

經過測試成功

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| demo               |
| demo01             |
| hello              |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
7 rows in set (0.00 sec)

高可用搭建

安裝mysql-mmm
新增一臺機器10.39.47.64

需要在這60,61,62,63,64臺 上都安裝 mysql-mmm

通過ansible安裝mysql-mmm工具,先裝epel-release源

ansible -i hosts cache -u root -m shell -a 'yum install epel-release -y'
 ansible -i hosts cache -u root -m shell -a 'yum update -y' 
  ansible -i hosts cache -u root -m shell -a 'yum install -y mysql-mmm* '

配置mmm代理和監控賬號的許可權

現在環境已經配置好,我沒有配置忽略 mysql庫和 user表,所以只要在任意一臺主庫上執行下面的操作,其他的庫就都有這倆賬號了。

mysql> GRANT REPLICATION CLIENT ON *.* TO 'mmm_monitor'@'10.39.47.%' IDENTIFIED  BY 'xxx';

mysql> GRANT SUPER, REPLICATION CLIENT, PROCESS ON *.* TO 'mmm_agent'@'10.39.47.%' IDENTIFIED BY 'xxx';  
    
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges; 

Query OK, 0 rows affected (0.06 sec)

注:60,61,62,63 是一樣的
檢視

mysql> select user,host from mysql.user; 
+---------------+------------+
| user          | host       |
+---------------+------------+
| mmm_agent     | 10.39.47.% |
| mmm_monitor   | 10.39.47.% |
| mysql.session | localhost  |
| mysql.sys     | localhost  |
| root          | localhost  |
+---------------+------------+
5 rows in set (0.00 sec)

新建mmm_common.conf檔案

vi mmm_common.conf

active_master_role      writer
cluster_interface            eth0
pid_path                      /var/run/mysql-mmm/mmm_agentd.pid
bin_path                      /usr/libexec/mysql-mmm/
replication_user          copy
replication_password     enN$#12345
agent_user       mmm_agent      
agent_password   enN$#12345
    ip      10.39.47.60
    mode    master
    peer    mysql-master-60



    ip      10.39.47.61
    mode    master
    peer    mysql-master-61




    ip      10.39.47.62
    mode    slave
    
    ip      10.39.47.63
    mode    slave

    hosts   mysql-master-60 mysql-master-61
    ips     10.39.47.65
    mode    exclusive




    hosts   db1, db2, db3
    ips      10.0.0.15, 10.0.0.16
    mode    balanced

由於issa不支援VIP高可用未完待續,
高可用部署可參考
http://www.cnblogs.com/gomysql/p/3671896.html
http://www.cnblogs.com/chenmh/p/5563778.html
http://blog.51cto.com/lizhenliang/1354576
http://mysql-mmm.org/doku.php

參考:
CentOS 7 下使用yum安裝MySQL5.7.20 最簡單 圖文詳解
linux-installation-yum-repo 5.7
Mysql叢集講解(五) 多主多從環境搭建
mysql 多主多從
Mysql叢集講解(四) 一主多從環境搭建
de

Linux下的MySQL主主複製和Mysql-MMM實現(Mysql雙主多從高可用)