fsfss21 发表于 2017-3-6 13:28:43

部署MySQL高可用集群

部署mysql高可用集群。
mysqlMMM   +mysql主从同步
实验拓扑
监控主机pc9
                                       -------> 从pc5(pc7从)
主pc8(pc7从) -----> 主pc7(pc8从)
                                       -------> 从pc6(pc7从)

                              |
                              监控pc9
                              |
                              
            -----------------------------------
                   |                     |   
                   写pc8                写pc7
                                          |
                        -----------------------------
                               |               |   
                              读pc5            读pc6
               




一,安装MySQL
... ...

1.1,配置四台服务器的主配置文件/etc/my.cnf
主pc8配置:
# vim /etc/my.cnf

log-bin=master8   开启二进制log日志
server_id=8

主pc7配置:
# vim /etc/my.cnf

log-bin=master7
server_id=7
log-slave-updates    开启级连复制功能

从pc6配置:
# cat /etc/my.cnf

server_id=6

从pc5配置:
# cat /etc/my.cnf

server_id=5

1.2,四台服务器都要重启mysql服务:
# /etc/init.d/mysql restart

3.1,首次登入需要修改密码
# cat /root/.mysql_secret       //初始密码存放路径
# The random password set for the root user at Thu Mar2 11:15:58 2017 (local time): m9vGqj2s
# mysql -hlocalhost -uroot -pm9vGqj2s
mysql> set password for root@"localhost"=password("123");    //修改初始密码
Query OK, 0 rows affected (0.00 sec)

3.2,分别在PC8和PC7服务器创建从服务器授权:
mysql> grant   replicationslaveon*.*to slaveuser@"%"identifiedby "123456";

3.3,配置pc7为pc8的从服务器:
# mysql -hlocalhost -uroot -p123
mysql> show master status;
+----------------+----------+--------------+------------------+----------
| File         | Position | Binlog_Do_DB | Binlog_Ignore_DB
+----------------+----------+--------------+------------------+----------
| master8.000004 |      120 |            |                  |                |
+----------------+----------+--------------+------------------+----------

3.4,在PC7配置master服务器:
# mysql -uroot -p123
mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> change master to
    -> master_host="192.168.4.8",
    -> master_user="slaveuser",
    -> master_password="123",
    -> master_log_file="master8.000004",
    -> master_log_pos=120;
Query OK, 0 rows affected, 2 warnings (0.39 sec)

mysql> start slave;
Query OK, 0 rows affected (0.05 sec)

mysql> show slave status\G;
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
            
-------------------------------------------------------------------------
主从同步的工作过程
Slave_IO_Running: Yes
IO线程:负责把主数据库服务器binlog日志里的sql命令拷贝到本机的中继日志文件里。

IO线程No状态的原因?
从数据库服务器连接不上主数据库服务器:
ping   
iptables   
selinux   
连接的授权用户
binlog日志文件指定错误
binlog日志pos点位置错误

查看报错信息
Last_IO_Error:报错信息

修改错误:
stopslave;
changemaster   to    选项="值",选项="值",;
startslave;
-----------------------------------------------------------------------
Slave_SQL_Running: Yes
SQL线程:执行本机中继日志文件里的sql命令,把数据写进本机的库里。

IO线程No状态的原因?
执行本机中继日志文件里的sql命令时,本机没有命令使用到的库 表 或字段。

查看报错信息
Last_SQL_Error:   报错信息


ls/var/lib/mysql/
master.info    记录连接主数据库服务器配置信息
relay-log.info记录中继日志信息文件
mail-relay-bin.00000x   中继日志文件
mail-relay-bin.index      中继日志的索引文件
----------------------------------------------------------------------
测试:
4.1,在PC8中创建一个数据库。
mysql> create database test008;
Query OK, 1 row affected (0.01 sec)

4.2,到PC5-7看是否同步。
mysql> show databases;
+--------------------+
| Database         |
+--------------------+
| information_schema |
| db001            |
| db002            |
| db007            |
| mysql            |
| performance_schema |
| test               |
| test008            |
| webdb            |
+--------------------+
9 rows in set (0.06 sec)
=========================================================================
三,安装MySQL-MMM
软件介绍:MySQL主主复制管理器
监控、故障转移    一套脚本套件(perl)

提供2种服务:
mmm-monitor: 负责所有的监控工作, 决定故障节点的移除或恢复 。

mmm-agent   运行在MySQL服务器上,提供简单远程服务集、提供给监控节点。

-------------------------------------------------------------------------
1.1,在所以主机上安装mysql mmm 软件 (4台数据库服务器 +监控服务器)
#tar-zxvfmysql-mmm-2.2.1.tar.gz
#cd mysql-mmm-2.2.1
#make install

#cd /etc/mysql-mmm
#ls*.conf
mmm_agent.conf   mmm-agent服务的主配置文件(数据库主机)
mmm_mon.conf   mmm-monitor服务的主配置文件(监控主机)
mmm_common.conf公信息配置文件,在所有主机上都要配置
mmm_tools.conf

1.2,在4台数据库服务器上做如下授权
mysql>grantreplication client,process,super on *.*to   agent@"%"identified by   "123";

mysql>grantreplication clienton *.*to   monitor@"%"identified by   "123";   
-------------------------------------------------------------------------
# ls
mmm_agent.confmmm_common.confmmm_mon.confmmm_tools.conf


mmm_common.conf公信息配置文件,在所有主机上都要配置
-------------------
active_master_role      writer
<host default>
      cluster_interface       eth0                         //本机当前使用的网卡
      pid_path                /var/run/mmm_agentd.pid
      bin_path                /usr/lib/mysql-mmm/
    replication_user            slaveuser            
    replication_password      123
      agent_user            agent
      agent_password          123
</host>
<host master8>
      ip                192.168.4.8
      mode            master
      peer            master7
</host>      
<host master7>
      ip                192.168.4.7
      mode            master
      peer            master8
</host>
<host slave6>
      ip                192.168.4.6
      mode            slave
</host>

<host slave5>
      ip                192.168.4.5
      mode            slave
</host>
<role writer>
      hosts            master8, master7
      ips                192.168.4.101
      mode               exclusive
</role>
<role reader>
      hosts            slave6, slave5
      ips            192.168.4.102, 192.168.4.103
      mode             balanced
</role>
-----------------------------------------------------------------------   
mmm_agent.conf   mmm-agent服务的主配置文件(数据库主机)
-----------------
# catmmm_agent.conf
include mmm_common.conf
this slave5
-------------------------------------------------------------------------
mmm_mon.conf   mmm-monitor服务的主配置文件(监控主机)
--------------
# cat mmm_mon.conf
include mmm_common.conf

<monitor>
    ip            192.168.4.9                        //本机IP
    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.4.5, 192.168.4.6, 192.168.4.8, 192.168.4.7      //要监控的数据库服务器
</monitor>

<host default>
    monitor_user            monitor               //需数据库授权
    monitor_password      123
</host>

debug 0         0|1报错时开机可以设置为1,方便查看报错信息
=========================================================================
1.3,在所以主机上 安装服务运行时依赖的软件包。
tar -zxvf Algorithm-Diff-1.1902.tar.gz
cd Algorithm-Diff-1.1902
perl Makefile.PL
make
make install

rpm -ivh--nodeps perl-Log-Log4perl-1.26-1.el6.rf.noarch.rpm

tar -zxvf Proc-Daemon-0.03.tar.gz
cd Proc-Daemon-0.03
perl Makefile.PL
make
make install

1.4,在4台数据库服务器上安装获取虚拟Ip地址程序。
#yum-yinstallgcc   gcc-c++
#gunzipNet-ARP-1.0.8.tgz
#tar -xvf Net-ARP-1.0.8.tar
#cdNet-ARP-1.0.8
#perl   Makefile.PL
#make
#makeinstall

1.5,启动服务
1 启动数据库服务器上mmm-agent 服务
# /etc/init.d/mysql-mmm-agent start
Daemon bin: '/usr/sbin/mmm_agentd'
Daemon pid: '/var/run/mmm_agentd.pid'
Starting MMM Agent daemon... Ok
# netstat -utnalp| grep agent
tcp      0      0 172.40.50.171:9989          0.0.0.0:*                   LISTEN      24009/mmm_agentd   
# netstat -utnalp| grep :9989
tcp      0      0 172.40.50.171:9989          0.0.0.0:*       LISTEN      24009/mmm_agentd   
#

日志文件/var/log/mysql-mmm/mmm_agentd.log

-------------------------------------------------------------------------
2启动监控服务器上mmm-monitor 服务
# /etc/init.d/mysql-mmm-monitor start
Daemon bin: '/usr/sbin/mmm_mond'
Daemon pid: '/var/run/mmm_mond.pid'
Starting MMM Monitor daemon: Ok

# netstat -utnalp| grep :9988
tcp      0      0 172.40.50.177:9988          0.0.0.0:*      LISTEN      23544/mmm_mond      

3.查看数据库服务器的状态:
# mmm_control show
# mmm_control set_online master8
# mmm_control set_online master7
# mmm_control set_online slave5
# mmm_control set_online slave6
# mmm_control show
master7(192.168.4.7) master/ONLINE. Roles:
master8(192.168.4.8) master/ONLINE. Roles: writer(192.168.4.101)
slave5(192.168.4.5) slave/ONLINE. Roles: reader(192.168.4.103)
slave6(192.168.4.6) slave/ONLINE. Roles: reader(192.168.102)

-------------------------------------------------------------------------
4.查看虚拟ip地址
# ip addr show | grep eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    inet 192.168.4.8/24 brd 192.168.4.255 scope global eth0
    inet 192.168.4.101/32 scope global eth0
=========================================================================
页: [1]
查看完整版本: 部署MySQL高可用集群