一、项目实验拓扑图;
二、项目重点;
DRBD 的概述:Distributed Replicated Block Device 是一个用软件实现的、无共享的、服务器 之间镜像块设备内容的存储复制解决方案。其核心功能通过 Linux 的内核实现,比文件系统 更加靠近操作系统内核及 IO 栈,drbd 共有两部分组成:内核模块和用户空间的管理工具; DRBD 是由内核模块和相关脚本而构成,用以构建高可用性的集群。其实现方式是通过网络 来镜像整个设备。可以把它看作是一种网络 RAID。它允许用户在远程机器上建立一个本地 块设备的实时镜像;
DRBD 的角色:
主:在主 DRBD 设备中可以进行不受限制的读和写的操作,他可用来创建和挂载文件系统、 初始化或者是直接 I/O 的快设备; 备:接收所有来自对等节点的更新,不能被应用也不能被读写访问。主要目的是保持缓冲及 数据一致性。人工干预和管理程序的自动聚类算法都可以改变资源的角色。资源可以由被变 换为主,以及主到备;
DRBD 原理图:
DRBD 的特点: 实时性:当某个应用程序完成对数据的修改时,复制功能立即发生;
透明性:应用程序的数据存储在镜像块设备上是独立透明的,他们的数据在两个节点上都保 存一份,因此,无论哪一台服务器宕机,都不会影响应用程序读取数据的操作,所以说是透 明的;
DRBD 的同步方式: 同步镜像:表示当应用程序提交本地的写操作后,数据后会同步写到两个节点上去; 异步镜像:表示当应用程序提交写操作后,只有当本地的节点上完成写操作后,另一个节点 才可以完成写操作;
DRBD 的工作模式:
单主模式:任何资源在任何特定的时间,集群中只存在一个主节点。 正是因为这样在集群 中,只能有一个节点可以随时操作数据,这种模式可用在任何的文件系统上( EXT3、 EXT4、 XFS 等等);
双主模式:在双主模式下(drbd8.0 后支持),任何资源在任何特定的时间,集群中都存在两 个主节点。犹豫双方数据存在并发的可能性,这种模式需要一个共享的集群文件系统,利用 分布式的锁机制进行管理,如 GFS 和 OCFS2。部署双主模式时, DRBD 是负载均衡的集群, 这就需要从两个并发的主节点中选取一个首选的访问数据。这种模式默认是禁用的,如果要 是用的话必须在配置文件中进行声明;
DRBD 的同步协议:
协议 A:本地完成写入,且数据包已在发送队列中,则认为写入完成。在一 个节点发生故障 时,可能发生数据丢失,常用与物理上分开的节点;
协议 B:本地完成写入,并收到远程主机的收到数据确认后,则认为写入完成。在两个节点 同时发生故障时,可能发生数据丢失。因为在数据传输过程中, 数据未必能提交到磁盘; 协议 C:本地完成写入,并收到远程主机的写入确认后,则认为写入完成,没有任何数据丢 失,因此这是最常用的模式;
三、项目环境;
系统类型 | IP 地址 | 主机名 | 所需软件 |
Centos 7.4 1708 64bit | 192.168.100.101 | master1 | mysql-5.6.36.tar.gz drbd84-utils kmod-drbd84 drbd84-utils-sysvinit ntp |
Centos 7.4 1708 64bit | 192.168.100.102 | master2 | mysql-5.6.36.tar.gz drbd84-utils kmod-drbd84 drbd84-utils-sysvinit ntpdate |
Centos 7.4 1708 64bit | 192.168.100.103 | slave1 | mysql-5.6.36.tar.gz ntpdate |
Centos 7.4 1708 64bit | 192.168.100.104 | slave2 | mysql-5.6.36.tar.gz ntpdate |
Centos 7.4 1708 64bit | 192.168.100.105 | amoeba | amoeba-mysql-binary-2.2.0.tar.gz jdk-6u14-linux-x64.bin |
Centos 7.4 1708 64bit | 192.168.100.106 | client | mysql |
四、项目实验步骤;
Ø 部署 master1 节点的 ntp 服务以及域名解析;
Ø 配置 master2、slave1、slave2 节点同步 ntp 时间及域名解析(在此只列举 master2 单台 主机配置);
- 分别在 master1、master2、slave1、slave2 节点上安装 mysql 服务(在此只列举 master1
单台主机配置);
Ø 分别在 master1、master2 节点上 drbd 服务(在此只列举 master1 单台主机配置);
Ø 配置优化 master1、master2 节点的 drbd 服务(在此只列举 master1 单台主机配置);
Ø 在 master1、master2 两个节点准备 drbd 的磁盘(在此只列举 master1 单台主机配置);
Ø 在 master1 主节点上进行初始化 drbd 的块设备并且进行测试挂载;
Ø 在 master2 从节点上测试挂载 drbd 块设备;
Ø 配置 master1 节点的 mysql 服务数据文件的存放位置为 drbd 块设备的挂载点;
Ø 使用 master2 节点测试查看 mysql 中数据;
- 安装 master1、master2 节点的 keepalived 服务;
- 配置 master1 节点上 master 主节点;
- 配置 master 2 节点上 backup 从节点;
- 配置 master1 节点 keepalived 服务切换 DRBD 块设备;
Ø 配置 master1、master2 节点上的主从复制;
Ø 配置 slave1 节点的主从复制;
Ø 配置 slave2 节点的主从复制;
Ø 验证 master1 节点、slave1 节点、slave2 节点的主从复制;
Ø 安装 amoeba 数据库代理程序;
Ø 配置 master1 节点授权 amoeba 节点连接数据库集群;
Ø 修改 amoeba 节点的配置文件并启动测试;
Ø 客户端访问测试主从复制;
Ø 客户端访问测试读写分离;
Ø 关闭 master1 节点,测试双主热备情况;
Ø 部署 master1 节点的 ntp 服务以及域名解析; [root@master1 ~]# cat <<END >>/etc/hosts 192.168.100.101 master1
-
-
-
- master2
- slave1
- slave2 END
-
-
[root@master1 ~]# yum -y install ntp
[root@master1 ~]# sed -i '/^server/s/^/#/g' /etc/ntp.conf [root@master1 ~]# cat <<END >>/etc/ntp.conf
server 127.127.1.0
fudge 127.127.1.0 stratum 8 END
[root@master1 ~]# systemctl start ntpd [root@master1 ~]# systemctl enable ntpd
Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to
/usr/lib/systemd/system/ntpd.service.
Ø 配置 master2、slave1、slave2 节点同步 ntp 时间及域名解析(在此只列举 master2 单台 主机配置);
[root@master2 ~]# cat <<END >>/etc/hosts 192.168.100.101 master1
-
-
-
- master2
- slave1
- slave2 END
-
-
[root@master2 ~]# yum -y install ntpdate [root@master2 ~]# /usr/sbin/ntpdate 192.168.100.101
ech 9 Aug 18:04:38 ntpdate[1106]: adjust time server 192.168.100.101 offset 0.299673 sec [root@master2 ~]# echo "/usr/sbin/ntpdate 192.168.100.101">>/etc/rc.local [root@master2 ~]# chmod +x /etc/rc.local
- 分别在 master1、master2、slave1、slave2 节点上安装 mysql 服务(在此只列举 master1
单台主机配置);
[root@master1 ~]# yum -y install ncurses cmake [root@master1 ~]# mount /dev/cdrom /mnt/ mount: /dev/sr0 写保护,将以只读方式挂载
[root@master1 ~]# rpm -ivh /mnt/Packages/ncurses-devel-5.9-13.20130511.el7.x86_64.rpm
--nodeps [root@master1 ~]# ls mysql-5.6.36.tar.gz
[root@master1 ~]# tar zxvf mysql-5.6.36.tar.gz -C /usr/src/ [root@master1 ~]# cd /usr/src/mysql-5.6.36/
[root@master2 mysql-5.6.36]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql
-DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci
-DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DENABLE_DOWNLOADS=1
[root@master1 mysql-5.6.36]# make [root@master1 mysql-5.6.36]# make install [root@master1 mysql-5.6.36]# cd
[root@master1 ~]# cp /usr/src/mysql-5.6.36/support-files/mysql.server /etc/init.d/ [root@master1 ~]# chmod +x /etc/init.d/mysql.server
[root@master1 ~]# cat <<END >>/usr/lib/systemd/system/mysqld.service [Unit]
Description=mysqldapi After=network.target
[Service] Type=forking
PIDFile=/usr/local/mysql/logs/mysqld.pid ExecStart=/etc/init.d/mysql.server start ExecReload=/etc/init.d/mysql.server restart ExecStop=/etc/init.d/mysql.server stop PrivateTmp=Flase
[Install]
WantedBy=multi-user.target END
[root@master1 ~]# echo "export PATH=$PATH:/usr/local/mysql/bin/" >>/etc/profile [root@master1 ~]# source /etc/profile
[root@master1 ~]# groupadd mysql [root@master1 ~]# useradd -g mysql mysql [root@master1 ~]# cat <<END >/etc/my.cnf [mysqld]
basedir = /usr/local/mysql datadir = /usr/local/mysql/data
port = 3306 sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
character_set_server=utf8 init_connect='SET NAMES utf8'
log-error=/usr/local/mysql/logs/mysqld.log pid-file=/usr/local/mysql/logs/mysqld.pid skip-name-resolve
END
[root@master1 ~]# mkdir /usr/local/mysql/logs [root@master1 ~]# chown mysql:mysql /usr/local/mysql/ -R
[root@master1 ~]# /usr/local/mysql/scripts/mysql_install_db --user=mysql
--basedir=/usr/local/mysql --datadir=/usr/local/mysql/data [root@master1 ~]# systemctl start mysqld
[root@master1 ~]# systemctl enable mysqld
Created symlink from /etc/systemd/system/multi-user.target.wants/mysqld.service to
/usr/lib/systemd/system/mysqld.service. [root@master1 ~]# netstat -utpln |grep 3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN
31481/mysqld
[root@master1 ~]# mysqladmin -uroot password 123123
Warning: Using a password on the command line interface can be insecure. [root@master1 ~]# mysql -uroot -p123123
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
mysql> exit
Ø 分别在 master1、master2 节点上 drbd 服务(在此只列举 master1 单台主机配置); [root@master1 ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
[root@master1 ~]# yum -y install epel-release
[root@master1 ~]# yum -y install perl-TimeDate kernel-devel kernel-headers flex resource-agents
[root@master1 ~]# rpm -ivh http://www.elrepo.org/elrepo-release-7.0-2.el7.elrepo.noarch.rpm [root@master1 ~]# yum install -y drbd84-utils kmod-drbd84 drbd84-utils-sysvinit
Ø 配置优化 master1、master2 节点的 drbd 服务(在此只列举 master1 单台主机配置);
[root@master1 ~]# cp /usr/lib/modules/3.10.0-862.el7.x86_64/extra/drbd84/drbd.ko
/lib/modules/$(uname -r)/kernel/lib [root@master1 ~]# depmod [root@master1 ~]# cat /etc/drbd.conf
# You can find an example in /usr/share/doc/drbd.../drbd.conf.example include "drbd.d/global_common.conf";
include "drbd.d/*.res";
[root@master1 ~]# cp /etc/drbd.d/global_common.conf{,-$(date +%s)} [root@master1 ~]# ls /etc/drbd.d/
global_common.conf global_common.conf-1533828130 [root@master1 ~]# vi /etc/drbd.d/global_common.conf global {
usage-count yes;
}
common {
startup {
}
options {
}
disk {
}
net {
}
}
wfc-timeout 120;
degr-wfc-timeout 120;
# cpu-mask on-no-data-accessible
on-io-error detach;
protocol C;
[root@master1 ~]# vi /etc/drbd.d/r0.res resource r0 {
on master1 {
device /dev/drbd0; disk /dev/sdb1;
address 192.168.100.101:7788;
meta-disk internal;
}
on master2 {
device /dev/drbd0; disk /dev/sdb1;
address 192.168.100.102:7788;
meta-disk internal;
}
}
Ø 在 master1、master2 两个节点准备 drbd 的磁盘(在此只列举 master1 单台主机配置);
[root@master1 ~]# fdisk /dev/sdb n--p--1--回车--回车--p--w
[root@master1 ~]# yum -y install parted [root@master1 ~]# partprobe /dev/sdb [root@master1 ~]# partprobe /dev/sdb1
[root@master1 ~]# dd if=/dev/zero of=/dev/sdb1 bs=1M count=1
记录了 1+0 的读入
记录了 1+0 的写出
1048576 字节(1.0 MB)已复制,0.00917599 秒,114 MB/秒
[root@master1 ~]# drbdadm create-md r0 ##创建 drbd 磁盘,注意 master1 和 master2
的主机名必须更改,不然导致报错
you are the 1649th user to install this version initializing activity log
initializing bitmap (640 KB) to all zero Writing meta data...
New drbd meta data block successfully created.
[root@master1 ~]# /etc/init.d/drbd start ##如若出现无法加载 drbd 模块,重启主机可 以解决
...
To abort waiting enter 'yes' [ 12]: yes
...
[root@master1 ~]# netstat -anpt |grep 7788 ##当 master1 单个节点服务启动的状态
tcp 0 0 192.168.100.101:7788 0.0.0.0:* LISTEN -
[root@master2 ~]# netstat -anpt |grep 7788 ##当 master1 和 master2 两个节点服务同时启 动的状态
tcp | 0 | 0 192.168.100.102:7788 | 192.168.100.101:56853 | ESTABLISHED - |
tcp | 0 | 0 192.168.100.102:34126 | 192.168.100.101:7788 | ESTABLISHED - |
Ø 在 master1 主节点上进行初始化 drbd 的块设备并且进行测试挂载; [root@master1 ~]# drbdadm -- --overwrite-data-of-peer primary r0 [root@master1 ~]# cat /proc/drbd ##等待其初始化完成 version: 8.4.11-1 (api:1/proto:86-101)
GIT-hash: 66145a308421e9c124ec391a7848ac20203bb03c build by mockbuild@, 2018-04-26 12:10:42
0: cs:SyncSource ro:Primary/Secondary ds:UpToDate/Inconsistent C r-----
ns:301636 nr:0 dw:0 dr:303740 al:8 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:20668184 [>....................] sync'ed: 1.5% (20180/20476)M
finish: 0:23:53 speed: 14,408 (10,772) K/sec [root@master1 ~]# mkfs -t xfs /dev/drbd0
meta-data=/dev/drbd0 isize=512 agcount=4, agsize=1310614 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=5242455, imaxpct=25
= sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0 [root@master1 ~]# mkdir /mysqldata
[root@master1 ~]# mount /dev/drbd0 /mysqldata/ [root@master1 ~]# mount |tail -1
/dev/drbd0 on /mysqldata type xfs (rw,relatime,attr2,inode64,noquota) [root@master1 ~]# echo "ceshi" >>/mysqldata/ceshi.txt [root@master1 ~]# cat /mysqldata/ceshi.txt
ceshi
[root@master1 ~]# umount /mysqldata/ [root@master1 ~]# drbdadm secondary r0
Ø 在 master2 从节点上测试挂载 drbd 块设备; [root@master2 ~]# drbdadm primary r0 [root@master2 ~]# mkdir /mysqldata [root@master2 ~]# mount /dev/drbd0 /mysqldata/ [root@master2 ~]# mount |tail -1
/dev/drbd0 on /mysqldata type xfs (rw,relatime,attr2,inode64,noquota) [root@master2 ~]# cat /mysqldata/ceshi.txt
ceshi
[root@master2 ~]# umount /mysqldata/ [root@master2 ~]# drbdadm secondary r0
Ø 配置 master1 节点的 mysql 服务数据文件的存放位置为 drbd 块设备的挂载点;
[root@master1 ~]# drbdadm primary r0 [root@master1 ~]# mount /dev/drbd0 /mysqldata/ [root@master1 ~]# ls /mysqldata/
ceshi.txt
[root@master1 ~]# sed -i 's/\/usr\/local\/mysql\/data/\/mysqldata\/mysql/g' /etc/my.cnf [root@master1 ~]# grep mysqldata /etc/my.cnf
datadir = /mysqldata/mysql
[root@master1 ~]# chown -R mysql:mysql /mysqldata/ [root@master1 ~]# systemctl stop mysqld
[root@master1 ~]# /usr/local/mysql/scripts/mysql_install_db --user=mysql
--basedir=/usr/local/mysql --datadir=/mysqldata/mysql [root@master1 ~]# systemctl start mysqld [root@master1 ~]# ls /mysqldata/mysql/
auto.cnf ibdata1 ib_logfile0 ib_logfile1 mysql performance_schema test [root@master1 ~]# mysqladmin -uroot password 123123
Warning: Using a password on the command line interface can be insecure.
[root@master1 ~]# mysql -uroot -p123123 mysql> exit
[root@master1 ~]# systemctl stop mysqld [root@master1 ~]# umount /mysqldata/ [root@master1 ~]# drbdadm secondary r0
Ø 使用 master2 节点测试查看 mysql 中数据; [root@master2 ~]# drbdadm primary r0 [root@master2 ~]# mount /dev/drbd0 /mysqldata/
[root@master2 ~]# sed -i 's/\/usr\/local\/mysql\/data/\/mysqldata\/mysql/g' /etc/my.cnf [root@master2 ~]# grep mysqldata /etc/my.cnf
datadir = /mysqldata/mysql
[root@master2 ~]# chown -R mysql:mysql /mysqldata/ [root@master2 ~]# systemctl restart mysqld [root@master2 ~]# mysql -uroot -p123123
mysql> exit
[root@master2 ~]# systemctl stop mysqld [root@master2 ~]# umount /mysqldata/ [root@master2 ~]# drbdadm secondary r0 [root@master1 ~]# drbdadm primary r0 [root@master1 ~]# mount /dev/drbd0 /mysqldata/ [root@master1 ~]# systemctl start mysqld
- 安装 master1、master2 节点的 keepalived 服务; [root@master1 ~]# yum -y install kernel-devel openssl-devel popt-devel [root@master1 ~]# tar -zxvf keepalived-1.2.13.tar.gz -C /usr/src/ [root@master1 ~]# cd /usr/src/keepalived-1.2.13/
[root@master1 keepalived-1.2.13]# ./configure --prefix=/usr/local/keepalived [root@master1 keepalived-1.2.13]# make &&make install
[root@master1 keepalived-1.2.13]# cd
[root@master1 ~]# mkdir -p /etc/keepalived ##程序的主配置目录 [root@master1 ~]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/## 复制主配置文件
[root@master1 ~]# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ ##
复制启动时需要加载的配置文件
[root@master1 ~]# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/ ## 复制 服务的控制脚本
[root@master1 ~]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/ ## 复 制
keepalived 的命令
[root@master1 ~]# chmod 755 /etc/init.d/keepalived ##为控制脚本指定权限
- 配置 master1 节点上 master 主节点; [root@master1 ~]# vi /etc/keepalived/keepalived.conf global_defs {
router_id HA_TEST_R1 ##本服务器的名称,若环境中有多个 keepalived 时,此名称 不能一致
}
vrrp_instance VI_1 { ##定义 VRRP 热备实例,每一个 keep 组都不同
state MASTER ##MASTER 表示主服务器
interface eth0 ##承载 VIP 地址的物理接口
virtual_router_id 1 ##虚拟路由器的 ID 号,每一个 keep 组都不同
priority 100 ##优先级,数值越大优先级越高
advert_int 1 ##通告检查间隔秒数(心跳频率)
authentication { ##认证信息 auth_type PASS ##认证类型 auth_pass 123456 ##密码字串
}
virtual_ipaddress {
192.168.100.95 ##指定漂移地址(VIP)
}
}
:wq
[root@master1 ~]# /etc/init.d/keepalived start
Starting keepalived (via systemctl): [ 确定 ] [root@master1 ~]# ip a |grep 192.168.100.95
inet 192.168.100.95/32 scope global eth0
- 配置 master 2 节点上 backup 从节点; [root@master2 ~]# vi /etc/keepalived/keepalived.conf global_defs {
router_id HA_TEST_R2 ##本服务器的名称
}
vrrp_instance VI_1 {
state BACKUP ##BACKUP 表示从服务器
interface eth0 virtual_router_id 1
priority 99 ##优先级,低于主服务器
advert_int 1 authentication {
auth_type PASS auth_pass 123456
}
virtual_ipaddress { 192.168.100.95
}
}
:wq
[root@master2 ~]# /etc/init.d/keepalived start
Starting keepalived (via systemctl): [ 确定 ] [root@master2~]# ip a |grep 192.168.100.95
- 配置 master1 节点 keepalived 服务切换 DRBD 块设备;
[root@master1 ~]# yum -y install expect [root@master1 ~]# vi /etc/keepalived/drbd.sh
#!/bin/bash while true;do
VIP=$(ip a |grep 192.168.100.95 |wc -l) if [ $VIP -eq 0 ];then
/etc/init.d/keepalived start sleep 2
VIP=$(ip a |grep 192.168.100.95 |wc -l) if [ $VIP -eq 0 ];then
systemctl stop mysqld sleep 2
fi
fi
MYSQLD=$(ps aux |grep mysqld |grep -v grep |wc -l) if [ $MYSQLD -eq 0 ];then
/etc/init.d/keepalived stop
echo "mysql stats is down on $(date +%F-%T)" >>/var/log/mysqld.stats sleep 2
umount /mysqldata sleep 2
drbdadm secondary r0
/etc/keepalived/expect.sh 192.168.100.102 root pwd@123 "drbdadm primary r0 && mount
/dev/drbd0 /mysqldata && systemctl start mysqld"
echo "mysql is already switched master2 on $(date +%F-%T)" >>/var/log/mysqld.stats
fi done
[root@master1 ~]# vi /etc/keepalived/expect.sh
#!/usr/bin/expect
set ip [lindex $argv 0] set user [lindex $argv 1]
set password [lindex $argv 2] set com [lindex $argv 3]
set timeout 10
spawn ssh $user@$ip $com expect {
"*yes/no" { send "yes\r"; exp_continue} "*password:" { send "$password\r" }
}
interact
[root@master1 ~]# chmod +x /etc/keepalived/drbd.sh [root@master1 ~]# chmod +x /etc/keepalived/expect.sh [root@master1 ~]# /etc/keepalived/drbd.sh & [root@master1 ~]# jobs -l
[1]+ 4349 running /etc/keepalived/drbd.sh
Ø 配置 master1、master2 节点上的主从复制;
[root@master1 ~]# cat <<END >>/etc/my.cnf server-id=1
log-bin=mysql-bin
log-slave-updates=true END
[root@master1 ~]# systemctl restart mysqld [root@master1 ~]# mysql -uroot -p123123
mysql> grant replication slave on *.* to 'myslave'@'192.168.100.%' identified by '123123'; Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec) mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 412 | | |
|
+------------------+----------+--------------+------------------+-------------------+
mysql> exit
[root@master2 ~]# cat <<END >>/etc/my.cnf server-id=1
log-bin=mysql-bin
log-slave-updates=true END
Ø 配置 slave1 节点的主从复制; [root@slave1 ~]# cat <<END >>/etc/my.cnf server-id=2
relay-log=relay-log-bin
relay-log-index=slave-relay-bin.index END
[root@slave1 ~]# systemctl restart mysqld [root@slave1 ~]# mysql -uroot -p123123
mysql> change master to
master_host='192.168.100.95',master_user='myslave',master_password='123123',master_log_fil e='mysql-bin.000001',master_log_pos=412;
Query OK, 0 rows affected, 2 warnings (0.01 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.100.95 Master_User: myslave Master_Port: 3306
Connect_Retry: 60 Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 412 Relay_Log_File: relay-log-bin.000002 Relay_Log_Pos: 283
Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes
mysql> exit
Ø 配置 slave2 节点的主从复制; [root@slave2 ~]# cat <<END >>/etc/my.cnf server-id=3
relay-log=relay-log-bin
relay-log-index=slave-relay-bin.index END
[root@slave2 ~]# systemctl restart mysqld [root@slave2 ~]# mysql -uroot -p123123
mysql> change master to master_host='192.168.100.95',master_user='myslave',master_password='123123',master_log_fil e='mysql-bin.000001',master_log_pos=412;
Query OK, 0 rows affected, 2 warnings (0.02 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.100.95 Master_User: myslave Master_Port: 3306
Connect_Retry: 60 Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 412
Relay_Log_File: relay-log-bin.000002 Relay_Log_Pos: 283
Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes
mysql> exit
Ø 验证 master1 节点、slave1 节点、slave2 节点的主从复制;
[root@master1 ~]# mysql -uroot -p123123 mysql> create database linuxfan1;
Query OK, 1 row affected (0.00 sec) mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| linuxfan |
| linuxfan1 |
| mysql |
| performance_schema |
| test |
+--------------------+
6 rows in set (0.00 sec) mysql> exit
[root@slave1 ~]# mysql -uroot -p123123 mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| linuxfan |
| linuxfan1 |
| mysql |
| performance_schema |
| test |
+--------------------+
6 rows in set (0.00 sec) mysql> exit
[root@slave2 ~]# mysql -uroot -p123123 mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| linuxfan |
| linuxfan1 |
| mysql |
| performance_schema |
| test |
+--------------------+
6 rows in set (0.00 sec) mysql> exit
Ø 安装 amoeba 数据库代理程序;
[root@amoeba ~]# ls
amoeba-mysql-binary-2.2.0.tar.gz jdk-6u14-linux-x64.bin [root@amoeba ~]# chmod +x jdk-6u14-linux-x64.bin [root@amoeba ~]# ./jdk-6u14-linux-x64.bin [root@amoeba ~]# mv jdk1.6.0_14/ /usr/local/jdk1.6 [root@amoeba ~]# vi /etc/profile
export JAVA_HOME=/usr/local/jdk1.6
export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib export PATH=$JAVA_HOME/lib:$JAVA_HOME/jre/bin/:$PATH:$HOME/bin export AMOEBA_HOME=/usr/local/amoeba
export PATH=$PATH:$AMOEBA_HOME
:wq
[root@amoeba ~]# source /etc/profile [root@amoeba ~]# java --version Unrecognized option: --version
Could not create the Java virtual machine. [root@amoeba ~]# java -version
java version "1.6.0_14"
Java(TM) SE Runtime Environment (build 1.6.0_14-b08)
Java HotSpot(TM) 64-Bit Server VM (build 14.0-b16, mixed mode)
Ø 配置 master1 节点授权 amoeba 节点连接数据库集群;
[root@master1 ~]# mysql -uroot -p123123
mysql> grant all on *.* to 'amoeba'@'192.168.100.%' identified by '123123'; Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec) mysql> exit
Ø 修改 amoeba 节点的配置文件并启动测试;
[root@amoeba ~]# mkdir /usr/local/amoeba
[root@amoeba ~]# tar zxvf amoeba-mysql-binary-2.2.0.tar.gz -C /usr/local/amoeba/
[root@amoeba ~]# chmod -R 755 /usr/local/amoeba/ [root@amoeba ~]# vi /usr/local/amoeba/conf/amoeba.xml
30 <property name="user">admin</property> 31
32 <property name="password">admin</property>
:wq
115 <property name="defaultPool">master</property> 116
117
- <property name="writePool">master</property>
- <property name="readPool">slaves</property>
[root@amoeba ~]# vi /usr/local/amoeba/conf/dbServers.xml
-
- <!-- mysql port -->
- <property name="port">3306</property> 21
- <!-- mysql schema -->
- <property name="schema">test</property> 24
- <!-- mysql user -->
- <property name="user">amoeba</property> 27
- <!-- mysql password -->
- <property name="password">123123</property>
- <dbServer name="master" parent="abstractServer">
- <factoryConfig>
- <!-- mysql ip -->
- <property name="ipAddress">192.168.100.95</property>
- </factoryConfig>
- </dbServer> 51
- <dbServer name="slave1" parent="abstractServer">
- <factoryConfig>
- <!-- mysql ip -->
- <property name="ipAddress">192.168.100.103</property>
- </factoryConfig>
- </dbServer> 58
- <dbServer name="slave2" parent="abstractServer">
- <factoryConfig>
- <!-- mysql ip -->
- <property name="ipAddress">192.168.100.104</property>
- </factoryConfig>
64 | </dbServer> |
|
66 | <dbServer name="slaves" virtual="true"> |
|
67 |
| <poolConfig |
class="com.meidusa.amoeba.server.MultipleServerPool">
- <!-- Load balancing strategy: 1=ROUNDROBIN , 2=WEIGHTBASED , 3=HA-->
- <property name="loadbalance">1</property> 70
- <!-- Separated by commas,such as:
server1,server2,server1 -->
- <property name="poolNames">slave1,slave2</property>
- </poolConfig>
- </dbServer>
:wq
[root@amoeba ~]# /usr/local/amoeba/bin/amoeba start & [1] 1237
Ø 客户端访问测试主从复制;
[root@client ~]# yum -y install mysql
[root@client ~]# mysql -uadmin -padmin -h192.168.100.105 -P 8066 MySQL [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| linuxfan |
| linuxfan1 |
| mysql |
| performance_schema |
| test |
+--------------------+
MySQL [(none)]> create database linuxfan2; Query OK, 1 row affected (0.01 sec)
MySQL [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| linuxfan |
| linuxfan1 |
| linuxfan2 |
| mysql |
| performance_schema |
| test |
+--------------------+
MySQL [(none)]> use linuxfan2; Database changed
MySQL [linuxfan]> create table t1(id int,name varchar(8)); Query OK, 0 rows affected (0.02 sec)
MySQL [linuxfan]> insert into t1 values(1,'tom'); Query OK, 1 row affected (0.01 sec)
MySQL [linuxfan]> select * from t1;
+------+------+
| id | name |
+------+------+
| 1 | tom |
+------+------+
MySQL [(none)]> exit
[root@master1 ~]# mysql -uroot -p123123 mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| linuxfan |
| linuxfan1 |
| linuxfan2 |
| mysql |
| performance_schema |
| test |
+--------------------+
mysql> exit
[root@slave1 ~]# mysql -uroot -p123123 mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| linuxfan |
| linuxfan1 |
| linuxfan2 |
| mysql |
| performance_schema |
| test |
+--------------------+
mysql> exit
[root@slave2 ~]# mysql -uroot -p123123 mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| linuxfan |
| linuxfan1 |
| linuxfan2 |
| mysql |
| performance_schema |
| test |
+--------------------+
mysql> exit
Ø 客户端访问测试读写分离; [root@slave1 ~]# mysql -uroot -p123123 mysql> stop slave;
Query OK, 0 rows affected (0.00 sec) mysql> use linuxfan2;
Database changed
mysql> insert into t1 values(2,'jack'); Query OK, 1 row affected (0.00 sec) mysql> select * from t1;
+------+------+
| id | name |
+------+------+
| 1 | tom |
| 2 | jack |
+------+------+
mysql> exit
[root@slave2 ~]# mysql -uroot -p123123 mysql> stop slave;
Query OK, 0 rows affected (0.01 sec) mysql> use linuxfan2;
Database changed
mysql> insert into t1 values(3,'marry'); Query OK, 1 row affected (0.00 sec) mysql> select * from t1;
+------+-------+
| id | name |
+------+-------+
| 1 | tom |
| 3 | marry |
+------+-------+
mysql> exit
[root@client ~]# mysql -uadmin -padmin -h192.168.100.105 -P 8066 MySQL [(none)]> use linuxfan2;
Database changed
MySQL [linuxfan2]> insert into t1 values(4,'kali'); Query OK, 1 row affected (0.01 sec)
MySQL [linuxfan2]> select * from t1;
+------+------+
| id | name |
+------+------+
| 1 | tom |
| 2 | jack |
+------+------+
MySQL [linuxfan2]> select * from t1;
+------+-------+
| id | name |
+------+-------+
| 1 | tom |
| 3 | marry |
+------+-------+
MySQL [linuxfan2]> exit
Ø 关闭 master1 节点,测试双主热备情况; [root@master1 ~]# /etc/keepalived/drbd.sh & [1] 51212
[root@master1 ~]# systemctl stop mysqld
[root@master1 ~]# Stopping keepalived (via systemctl): [ 确定 ]
spawn ssh root@192.168.100.102 drbdadm primary r0 && mount /dev/drbd0 /mysqldata && systemctl start mysqld
...
[root@master2 ~]# ip a|grep 192.168.100.95 inet 192.168.100.95/32 scope global eth0
[root@master2 ~]# ls /mysqldata/ ceshi.txt mysql
[root@master2 ~]# netstat -utpln |grep 3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN
8037/mysqld
[root@client ~]# mysql -uadmin -padmin -h192.168.100.105 -P 8066 MySQL [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| linuxfan |
| linuxfan1 |
| linuxfan2 |
| mysql |
| performance_schema |
| test |
+--------------------+
MySQL [(none)]> exit