二. 安装操作系统
最小化安装CentOS7(64位)
三. 配置操作系统
1.关闭 selinux
cat /etc/selinux/config SELINUX=disabled 2.关闭防火墙
systemctl stop firewalld
systemctl disable firewalld3.配置NTP ,同步时间
yum -y install ntp
vi /etc/ntp.conf
增加3个时间服务器
server s2d.time.edu.cn iburst #西南地区网络中心
server s2e.time.edu.cn iburst #西北地区网络中心
server s2f.time.edu.cn iburst #东北地区网络中心
启动ntp服务 
systemctl start ntpd 
systemctl enable ntpd 
查看状态
ntpq -p四. 安装MySQL 5.6 (不是MariaDB)
1.安裝 MySQL Repository
rpm -Uvh http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm2.安裝 MySQL Server, MySQL client
yum install mysql-community-server3.开机自动启动 MySQL
systemctl enable mysqld4.启动MySQL
systemctl start mysqld5.MySQL 预设为空密码, 執行以下指令修改
mysql_secure_installation
mysql -u root -p 
#进入mysql 控制台,增加root用户远程登录的权限
GRANT ALL PRIVILEGES ON *.* TO root@"%" IDENTIFIED BY ‘GDkyUDJM6gr2Dx‘ WITH GRANT OPTION;
flush privileges;6.修改mysql的存储目录
a. 停止mysql
systemctl stop mysqlb. 迁移数据库存储目录
mkdir /opt/mysql
chown mysql:mysql /opt/mysql
mv /var/lib/mysql /opt/mysql
cd /opt/mysql
mv mysql datac. 修改配置文件
vim /etc/my.cnf
#修改datadir和socket指向
datadir=/opt/mysql/data
socket=/opt/mysql/mysql.sock
#同时增加下面的代码,这个是为了让mysql client能够连上mysql,避免Can‘t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock错误!
[mysql]
socket=/opt/mysql/mysql.sockd. 启动mysql
systemctl start mysql7.配置MySQL Relication
a. 要将添加的内容放在配置文件/etc/my.cnf的[mysqld]部分,如下:
[mysqld]
datadir=/opt/mysql/data
socket=/opt/mysql/mysql.sock
user=mysql
#下面为新添加的内容
read_only=1
default-storage-engine=innodb
replicate-ignore-db=mysql,information_schema #不同步的数据库,多个写多行
replicate-do-db=meerkat        #同步的数据库,多个写多行
binlog-ignore-db=mysql,information_schema  #不需要记录二进制日志的数据库,多个用逗号隔开
binlog-do-db=meerkat           #需要记录二进制日志的数据库,多个用逗号隔开
log_slave_updates           #当一个主故障,另一个立即接管
sync-binlog=1               #每条自动更新,安全性高,默认是0
server-id           = 1 #server-id在每台服务器上的值都是不一样,在这里依次为1、2、3、4。
#这里的日志文件命名也每台机器不一样,比如(mysql-master-001-bin.log,mysql-master-002-bin.log,mysql-slave-001-bin.log,mysql-slave-002-bin.log)
log-bin             = /opt/mysql/log/mysql-master-001-bin.log
log_bin_index       = /opt/mysql/log/mysql-master-001-bin.log.index
relay_log           = /opt/mysql/log/mysql-master-001-bin.relay
relay_log_index     = /opt/mysql/log/mysql-master-001-bin.relay.index
# 日志文件指定结束
expire_logs_days    = 10
max_binlog_size     = 100M
log_slave_updates   = 1b. 在/opt/mysql下增加log目录
cd /opt/mysql
mkdir log
chown mysql:mysql logd. 重新启动mysql
systemctl restart mysqle. 检查配置是否成功  
1)登录mysql,执行show master status,看是否有如下输出  
+——————+———-+————–+——————+——————-+  
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
              
+——————+———-+————–+——————+——————-+  
| mysql-master-001-bin.000001 | 120 | | mysql | |  
+——————+———-+————–+——————+——————-+  
2)到/var/log/mysql目录下,看是否产生了类似mysql-master-001-bin.000001和mysql-master-001-bin.log.index的文件。
f. 在4个mysql的服务器上都修改一下。注意日志命名每台机器都不一样
五. 新建同步数据库需要的用户  
使用mysql-mmm时一共需要三个用户: replication、mmm_agent和mmm_monitor(管理服务器上用来监控cluster状态的用户,所以可以限定只能从管理服务器登录)。使用下面三条命令新建这三个用户并分配相应的权限  
所有的mysql 服务器都运行一遍
GRANT REPLICATION CLIENT ON *.* TO ‘mmm_monitor‘@‘192.168.1.%‘ IDENTIFIED BY ‘monitor‘;
GRANT SUPER, REPLICATION CLIENT, PROCESS ON *.* TO ‘mmm_agent‘@‘192.168.1.%‘ IDENTIFIED BY ‘agent‘;
GRANT REPLICATION SLAVE ON *.* TO ‘replication‘@‘192.168.1.%‘ IDENTIFIED BY ‘replication‘;
flush privileges;六.设置复制机制(从master-001复制到master-002,从master-002复制到slave-001,slave-002)
1.配置master-001作为主,复制到master-002
#ssh到 master-002 服务器
mysql -u root -p 
#在mysql控制台录入
CHANGE MASTER TO MASTER_HOST=‘192.168.1.211‘,MASTER_USER=‘replication‘,MASTER_PASSWORD=‘replication‘,MASTER_PORT=3306,MASTER_LOG_FILE=‘mysql-master-002-bin.000001‘, MASTER_LOG_POS=260,MASTER_CONNECT_RETRY=10;
#重置reset
reset slave;
#启动slave
start slave;
#查看slave状态
show slave status\G
#结果如下
mysql> show slave status\G
*************************** 1. row ***************************
       Slave_IO_State: Waiting for master to send event
          Master_Host: 192.168.1.212
          Master_User: replication
          Master_Port: 3306
        Connect_Retry: 10
      Master_Log_File: mysql-master-002-bin.000001
  Read_Master_Log_Pos: 120
       Relay_Log_File: mysql-master-001-bin.000004
        Relay_Log_Pos: 294
Relay_Master_Log_File: mysql-master-002-bin.000001
     Slave_IO_Running: Yes
    Slave_SQL_Running: Yes
      Replicate_Do_DB:
  Replicate_Ignore_DB: mysql
   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: 120
      Relay_Log_Space: 472
      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: 2
          Master_UUID: 3d3b9f4f-f74f-11e5-9a30-005056b324c4
     Master_Info_File: /opt/mysql/data/master.info
            SQL_Delay: 0
  SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
   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
1 row in set (0.00 sec)注意:  
Slave_IO_Running: Yes  
Slave_SQL_Running: Yes
b.配置slave-001
#ssh到 slave-001 服务器 
mysql -u root -p 
#在mysql控制台录入
CHANGE MASTER TO MASTER_HOST=‘192.168.1.212‘,MASTER_USER=‘replication‘,MASTER_PASSWORD=‘replication‘,MASTER_PORT=3306,MASTER_LOG_FILE=‘mysql-slave-001-bin.000001‘, MASTER_LOG_POS=260,MASTER_CONNECT_RETRY=10;
#重置reset
reset slave;
#启动slave
start slave;
#查看slave状态
show slave status\G
#结果如下
mysql> show slave status\Gc.配置slave-002
#ssh到 slave-002 服务器 
mysql -u root -p 
#在mysql控制台录入
CHANGE MASTER TO MASTER_HOST=‘192.168.1.212‘,MASTER_USER=‘replication‘,MASTER_PASSWORD=‘replication‘,MASTER_PORT=3306,MASTER_LOG_FILE=‘mysql-slave-002-bin.000001‘, MASTER_LOG_POS=260,MASTER_CONNECT_RETRY=10;
#重置reset
reset slave;
#启动slave
start slave;
#查看slave状态
show slave status\G
#结果如下
mysql> show slave status\G      2.配置master-002作为主,复制到master-001
#ssh到 master-001 服务器
mysql -u root -p 
#在mysql控制台录入
CHANGE MASTER TO MASTER_HOST=‘192.168.1.212‘,MASTER_USER=‘replication‘,MASTER_PASSWORD=‘replication‘,MASTER_PORT=3306,MASTER_LOG_FILE=‘mysql-master-001-bin.000001‘, MASTER_LOG_POS=260,MASTER_CONNECT_RETRY=10;
#重置reset
reset slave;
#启动slave
start slave;
#查看slave状态
show slave status\G
#结果如下
mysql> show slave status\G这样就完成了master-001 和 master-002的相互复制,并且从master-002复制到slave-001 和 slave-002.
七.安装MMM(http://mysql-mmm.org/)
1.安装epel扩展包  
CentOS软件仓库默认是不含这些软件的,必须要有epel这个包的支持。故我们必须先安装epel
wget http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-5.noarch.rpm
rpm -ivh epel-release-7-5.noarch.rpm
yum install mysql-mmm-monitor
yum install mysql-mmm-agent
#用yum安装的 mysql-mmm组件启动的时候会出错。
所以用下面的tar.gz文件来安装
yum erase mysql-mmm-monitor 
yum erase mysql-mmm-agent2.在monitor(192.168.1.210) 上安装监控程序
cd /tmp
wget http://pkgs.fedoraproject.org/repo/pkgs/mysql-mmm/mysql-mmm-2.2.1.tar.gz/f5f8b48bdf89251d3183328f0249461e/mysql-mmm-2.2.1.tar.gz
tar -xzvf mysql-mmm-2.2.1.tar.gz
cd mysql-mmm-2.2.1.tar.gz
make install3.在数据库服务器(192.168.1.211-214)上安装代理
cd /tmp
wget http://pkgs.fedoraproject.org/repo/pkgs/mysql-mmm/mysql-mmm-2.2.1.tar.gz/f5f8b48bdf89251d3183328f0249461e/mysql-mmm-2.2.1.tar.gz
tar -xzvf mysql-mmm-2.2.1.tar.gz
cd mysql-mmm-2.2.1.tar.gz
make install4.配置MMM
a.编辑/etc/mysql-mmm/mmm_common.conf  
完成安装后,所有的配置文件都放到了/etc/mysql-mmm/下面。管理服务器和数据库服务器上都要包含一个共同的文件mmm_common.conf,内容如下:
active_master_role writer
<host default>
    cluster_interface  eno16777984  #网卡设备
    pid_path     /var/run/mmm_agentd.pid
    bin_path     /usr/lib/mysql-mmm/ #这里要确认是否下面有agent,monitor,tools的目录。否则无法生成vip
    replication_user        replication
    replication_password    replication
    agent_user              mmm_agent
    agent_password          agent
</host>
<host db1>
    ip      192.168.1.211
    mode    master
    peer    db2
</host>
<host db2>
    ip      192.168.1.212
    mode    master
    peer    db1
</host>
<host db3>
    ip      192.168.1.213
    mode    slave
</host>
<host db4>
    ip      192.168.1.214
    mode    slave
</host>
<role writer>
    hosts   db1, db2
    ips     192.168.1.220
    mode    exclusive
</role>
<role reader>
    hosts   db2, db3, db4
    ips     192.168.1.221, 192.168.1.222, 192.168.1.223
    mode    balanced
</role>通过scp命令分别复制到monitor-001、master-001、master-002、slave-001和slave-002共五台机器上。
b. 编辑 4台mysql节点机上的/etc/mysql-mmm/mmm_agent.conf  
在数据库服务器上,还有一个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 db1最后一行的db1,在不同的数据库服务器上要分别改为db2、db3和db4,否则代理就会无法启动。
c. 编辑 monitor主机上的/etc/mysql-mmm/mmm_mon.conf  
在monitor-001上配置mmm_mon.conf
include mmm_common.conf
<monitor>
    ip                  127.0.0.1   #为了安全性,设置只在本机监听,mmm_mond 默认监听9988 
    pid_path            /var/run/mmm_mond.pid
    bin_path            /usr/lib/mysql-mmm/
    status_path         /var/lib/misc/mmm_mond.status
    ping_ips            192.168.1.211, 192.168.1.212, 192.168.1.213, 192.168.1.214       #用于测试网络可用性 IP 地址列表,只要其中有一个地址 ping 通,就代表网络正常,这里不要写入本机地址 
    #flap_duration 3600 #抖动的时间范围,单位秒,这两个参数考虑情况添加 
    # flap_count 3 #在抖动的时间范围内,最大的抖动次数 
    auto_set_online      60#是否设置自动上线,如果该值大于0,抖动的主机在抖动的时间范围过后,则设置自动上线
    # The kill_host_bin does not exist by default, though the monitor will
    # throw a warning about it missing. See the section 5.10 "Kill Host
</monitor>
<host default>
    monitor_user        mmm_monitor
    monitor_password    monitor
</host>5.自动启动agent 和 monitor
a. 在monitor上启动monitor
systemctl enable mysql-mmm-monitor
systemctl start mysql-mmm-monitorb. 在4个数据库服务器上启动agent
 systemctl enable mysql-mmm-agent
 systemctl starat mysql-mmm-agent如果出现Configuration file /etc/mysql-mmm/mmm_common.conf is world readable!这种错误,需要查看/etc/mysql-mmm/mmm_common.conf文件的权限,应该是 chmod 640 /etc/mysql-mmm/mmm_common.conf  
集群中所有配置文件的权限最好都设置为640,否则启动 MMM 服务的时候可能出错
八. MMM启动顺序:先启动monitor,再启动 agent
九. 问题和解决办法
2.vip漂移后,无法ping通192.168.1.200
a.解决办法1
vim /usr/share/perl5/vendor_perl/MMM/Agent/Helpers/Actions.pm
在sub configure_ip($$)代码段里面的_exit_ok();前面加入以下代码
#这里是解决vip偏移后,无法ping通。原因是arp 老化时间过长,这里强制刷新arp
my $getway = `/sbin/route | awk ‘default/ (print $2)‘` `/sbin/arping -I $if -c 3 -s $ip $getway `; #配置结束
b.解决办法2  
在vip漂移到的主机上手工重置arping
arping -I eno16777984 -c 3 -s 192.168.1.220 192.168.1.1
c.解决办法3  
在master-001 和 master-002 上使用shell脚本来刷新(这个方法正常运行)
vim /root/refresh_vip.sh
#增加如下内容:
#!/bin/sh 
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin:/usr/local/sbin
MMM_COMM_CONF="/etc/mysql-mmm/mmm_common.conf"
ETH_NAME=`awk ‘/cluster_interface/{print $2}‘ ${MMM_COMM_CONF}`
VIP_ADDR=`grep -A 2 ‘<role writer>‘ ${MMM_COMM_CONF} | awk ‘/ips/{print $2}‘`
GETWAY_ADDR=`/sbin/route | awk ‘/default/ {print $2}‘`
if [[ -n `/sbin/ip addr show ${ETH_NAME} | grep ${VIP_ADDR}` ]]; then
    /sbin/arping -I ${ETH_NAME} -c 3 -s ${VIP_ADDR} ${GETWAY_ADDR} >/dev/null 2>&1
fi放入crontab中运行
crontab -e
* * * * * sleep 10; /root/refresh_vip.sh >/dev/null 2>&1
* * * * * sleep 20; /root/refresh_vip.sh >/dev/null 2>&1
* * * * * sleep 30; /root/refresh_vip.sh >/dev/null 2>&1
* * * * * sleep 40; /root/refresh_vip.sh >/dev/null 2>&1
* * * * * sleep 50; /root/refresh_vip.sh >/dev/null 2>&1原文:http://newthink.blog.51cto.com/872263/1775488