1.1 设计图

1.2 集群搭建

1.修改计算机名
	hostnamectl set-hostname bigdata01
2.修改ip
	vi /etc/sysconfig/network-scripts/ifcfg-ens33
	删除网卡中的唯一标志符UUID,下次自动生成
3.增加映射
	Centos7: vi /etc/hosts
	windows: C:\Windows\System32\drivers\etc
    
4.重启网卡
	service network restart

5.免密钥登陆
	1.生成密钥
		ssh-keygen -t rsa
	2.发送私钥(本机) 
		ssh-copy-id localhost
	3.发送公钥(其他计算机)
		ssh-copy-id bigdata02
	4.测试免密钥登陆 
		ssh bigdata02

6.同步时间
	1.终端安装时间同步插件
		yum -y install ntp ntpdate
	2.设置时间同步
		ntpdate cn.pool.ntp.org
	3.将系统时间写入硬盘时间
		hwclock --systohc
		
192.168.2.128 bigdata01
192.168.2.129 bigdata02
192.168.2.130 bigdata03
192.168.2.131 bigdata04
192.168.2.132 bigdata05
192.168.2.2 bigdata

1.3 主从同步

1.3.1 rpm版MySQL

cp /usr/share/mysql/my-huge.cnf /etc/my.cnf.d/my.cnf
1.清理Centos7自带mariadb
	rpm -qa |grep -i mysql
	rpm -qa |grep -i mariadb
	yum remove mariadb-libs-5.5.56-2.el7.x86_64
	
2.rpm安装MySQL
	rpm -ivh MySQL-server-5.5.58-1.el6.x86_64.rpm --force --nodeps
	rpm -ivh MySQL-client-5.5.58-1.el6.x86_64.rpm --force --nodeps
	验证:#mysqladmin --version
	
3.配置MySQL
	a.启动MySQL服务
		service mysql start
	b.查看MySQL状态
		service mysql status
	c.设置密码
		/usr/bin/mysqladmin -u root password root
	d.登录MySQL
		mysql -uroot -p
	e.开启MySQL远程访问
		mysql> grant all privileges on *.* to 'root'@'%' identified by 'root' with grant option;
		mysql> flush privileges; 		//刷新设置立即生效
		mysql> exit						//退出
	f.Windows远程访问
		mysql -uroot -proot -h192.168.2.128 -P3306
	
4.常见命令
	a.启动命令
    	service mysql start
	b.关闭命令			
		service mysql stop
	c.重启命令			
		service mysql restart
	d.查看服务状态	       
		service mysql status
	e.查看MySQL系统配置    
		cat /etc/my.cnf
		
5.MySQL服务:常见报错/var/lib/mysql/mysql.sock不存在
	a.手动启动服务
		/etc/init.d/mysql start
	b.开机自启
		chkconfig mysql on
	c.关闭开机自启
		chkconfig mysql off
	d.检查开机自启
    	ntsysv

1.3.2 配置bigdata01

1.配置master(windows)  my.ini
    [mysqld]
    #master的id
    server-id=1
    #binary log
    log-bin="D:/software/sql/MySQL/data/mysql-bin"
    #异常信息的日志
    log-error="D:/software/sql/MySQL/data/mysql-error"
    #指定需要同步哪些数据库(默认全部数据库)
    #binlog-do-bo=mydb01
    #指定需要 不同步哪些数据库(排除)
    binlog-ignore-db=mysql
    # The TCP/IP Port the MySQL Server will listen on
    port=3306
    #Path to installation directory. All paths are usually resolved relative to this.
    basedir="D:/software/sql/MySQL/"
    #Path to the database root
    datadir="C:/ProgramData/MySQL/MySQL Server 5.5/Data/"
    
2.配置slave(bigdata01)   /etc/my.cnf
	[mysqld]
	server-id=2
	log-bin=mysql-bin
	replicate-do-db=mydb01
	replicate-do-db=mydb02
	
3.windwos/centos需要在配置完毕后,重启电脑

4.设置主从关系
	a.windows中查看master的信息
		sql> show master status;
	b.bigdata01指定master
		sql> change master to 
			 master_host='192.168.2.2',
			 master_user='root',
			 master_password='root',
			 master_port=3306,
			 master_log_file='mysql-bin.000004',
			 master_log_pos=829;
	
5.开启主从同步
	a.查看server_id
		sql> show variables like 'server_id';
	b.永久设置server_id
		sql> set global server_id=2;
	c.开启slave
    	sql> start slave;
    d.确保Slave_IO_Running / Slave_SQL_Running开启
		sql> show slave status \G

1.3.3 测试bigdata01

6.尝试在master中mydb01/mydb02中创建表、插入数据,然后测试slave中是否有相应的表和数据
	a.window
		sql> create database mydb01;
		sql> create database mydb02;
		sql> use mydb01;
		sql> create table student(id int(4), name varchar(10));
		sql> use mydb02;
		sql> create table student(id int(4), name varchar(10));
		sql> insert into student(id, name) values(1, 'zs'); --验证是否主从同步
	b.bigdata01
		sql> create database mydb01;
		sql> create database mydb02;
		sql> use mydb01;
		sql> create table student(id int(4), name varchar(10));
		sql> use mydb02;
		sql> create table student(id int(4), name varchar(10));
		sql> select * from student;         	 		   --验证是否主从同步
		
7.注意:每次要获取最新的File、Position
	a.windows中查看master的信息
		sql> show master status;
	b.关闭slave
		sql> stop slave;
	c.重置slave
		sql> reset slave;
	d.bigdata01指定master
		sql> change master to
			 master_host='192.168.2.2',
			 master_user='root',
			 master_password='root',
			 master_port=3306,
			 master_log_file='mysql-bin.000004',
			 master_log_pos=2940;		
	e.启动slave
		sql> start slave;
	f.确保Slave_IO_Running / Slave_SQL_Running开启
		sql> show slave status \G
		
8.常见报错:数据库之前已经设置过了主从同步
	1.ERROR 1201 :could not initlze master info structure ...
	2.解决:关闭并重置slave
		sql> slave stop;
		sql> reset slave;
		sql> change master to (检查最新的File、Position)
		
9.常见报错:数据库之前已经设置过了主从同步,并且也已经开启了主从同步
	1.ERROR 1198 :  ..... RUN STOP SLAVE ..
	2.解决:关闭并重置slave
		sql> slave stop;
		sql> reset slave;
		sql> change master to (检查最新的File、Position)

1.4 MyCat

1.4.1 配置bigdata02

基于主从同步的MyCat分库分表
	a.水平拆分:订单数据(订单数据1、订单数据2、订单数据3)
	b.垂直拆分:系统(订单数据库、用户数据库)

基于主从同步的MyCat读写分离
	a.读 -> 服务器节点(数据库)
	b.写 -> 服务器节点(数据库)

1.mycat安装目录:cd /usr/local
	tar -zxvf Mycat-server-1.6.6.1-release-20181031195535-linux.tar.gz 
	
2.server.xml(配置访问mycat的用户,schemas代表逻辑库)
    <user name="root" defaultAccount="true">
        <property name="password">root</property>
        <!-- 逻辑库-->
        <property name="schemas">mydb</property>
    </user>
    
3.schema.xml(读写分离、分库分表)
	a.balance:读请求的负载均衡
        0:不开启读写分离,所有的读操作都发送到writehost中
        1:全部的readhost和stand by writehost都参与读操作的负责均衡        
        2:读请求随机发送给readhost、writehost
        3:读请求随机发送给writehost中的readhost  (writehost不参与读请求) --推荐
	b.writeType:写请求的负载均衡
		0:写请求先发送给schema.xml中的第一个writehost,当第一个writehost挂掉时,再自动切换到writehost中,切换的记录会被记录在 conf/dnindex.properties  --推荐
		1:写请求随机发送到所有的wirtehost中
	c.switchType:是否允许“读操作”在readhost和writehost上自动切换(解决延迟问题:当从readhost中读取数据中出现网络延迟等问题时,自动从writehost中读数据)
		-1:不许
		1:默认,允许
		2:根据“主从同步的状态” 自动选择是否切换。主从之间会持续发送心跳。当心跳检测机制发送了IO延迟,则readhost自动切换到writehost;否则不切换。此情况下必须将心跳设置 show slave status  --推荐    

4.rule.xml:分库分表的拆分规则(平均拆分)
	修改规则名为:myrule

1.4.2 测试bigdata02

--开放mysql的一切权限(root)
sql> use mysql ;
sql> update user set
`Select_priv` = 'Y', `Insert_priv` = 'Y',`Update_priv` = 'Y',`Delete_priv` = 'Y',
`Create_priv` = 'Y',`Drop_priv` = 'Y',`Reload_priv` = 'Y',`Shutdown_priv` = 'Y',
`Process_priv` = 'Y',`File_priv` = 'Y',`Grant_priv` = 'Y',`References_priv` = 'Y',
`Index_priv` = 'Y',`Alter_priv` = 'Y',`Show_db_priv` = 'Y',`Super_priv` = 'Y',
`Create_tmp_table_priv` = 'Y',`Lock_tables_priv` = 'Y',`Execute_priv` = 'Y',
`Repl_slave_priv` = 'Y',`Repl_client_priv` = 'Y',`Create_view_priv` = 'Y',
`Show_view_priv` = 'Y',`Create_routine_priv` = 'Y',`Alter_routine_priv` = 'Y',
`Create_user_priv` = 'Y',`Event_priv` = 'Y',`Trigger_priv` = 'Y',`Create_tablespace_priv` = 'Y'
where user="root" ;
sql> flush privileges;
1.使用MyCat的方法和mysql基本一致
	SQL92:语法严格SQL,操作端口8066,管理端口9066
		insert into student(id,name) values(1,zs) ;
	SQL99:语法较为宽松,端口3306
		insert into student values(1,zs);
		
2.MyCat相关命令
	cd /usr/local/mycat
	systemctl stop firewalld  -- 关闭当前防火墙
	systemctl disable firewalld	-- 禁止开机自启
	bin/mycat start		--开启mycat
	bin/mycat stop		--关闭mycat
	bin/mycat status	--状态mycat
	
3.登陆MyCat,
	注意:不能直接在mycat所在节点bigdata02登录,需要借助已经安装MySQL的bigdata远程操作MyCat
	mysql -uroot -proot -h192.168.2.130 -P8066

4.验证写数据(写节点bigdata,读节点bigdata01,mycat为bigdata02)
	向bigdata02中插入数据 -> 查看bigdata是否写入成功?
	
5.验证读数据(写节点bigdata,读节点bigdata01,mycat为bigdata02)	
	向bigdata中插入数据 -> 利用主从同步,bigdata01中也插入数据 -> 查看bigdata02是否读入成功?
	
6.常见报错
	a.如果无法正常实现mycat功能,调试日志:
		/mycat/logs/mycat.log 		--执行错误
		/mycat/logs/wrapper.log		--启动错误
	b.如果出现报错Invalid DataSource:0 
		解决:防火墙、IP、端口、权限问题:临时开放全部的权限

1.4.3 配置bigdata03

1.5 haproxy

1.5.1 配置bigdata04

1.使用yum在线安装
	a.查看可用的haproxy版本
		yum list | grep haproxy
	b.安装
		yum -y install haproxy.x86_64
	
2.配置haproxy
	a.必须设置bigdata04的操作用户为haproxy
		chown -R haproxy:haproxy /etc/haproxy/
	b.配置日志
		vi /etc/rsyslog.conf
		i.将以下2个指令的注释释放
			$ModLoad imudp
			$UDPServerRun 514
		ii.设置日志文件的路径
			local2.*  /var/log/haproxy.log
		iii.重启日志服务
			systemctl restart rsyslog.service
	c.配置haproxy
		vi /etc/haproxy/haproxy.cfg   --设置为local2,与日志文件中的local2保持一致
	d.开放权限
		setenforce 0  --关闭0,开启1,适用于时间不同步
		
3.启动haproxy
	systemctl start haproxy.service
	systemctl status haproxy.service
	systemctl stop haproxy.service
	systemctl restart haproxy.service

4.登陆haproxy
	systemctl stop firewalld  -- 关闭当前防火墙
	systemctl disable firewalld	-- 禁止开机自启
	192.168.2.131:5000/haproxy

1.5.2 配置bigdata05

1.6 keepalived

1.6.1 配置bigdata04

1.使用yum在线安装
	a.查看可用的keepalived版本
		yum list | grep keepalived
	b.安装
		yum -y install  keepalived.x86_64
		
2.配置keepalived
	a.vi /etc/keepalived/keepalived.conf
		cd /etc/keepalived
	b.vi /etc/check_haproxy.sh
		cd /etc
	c.开放权限
		setenforce 0  --关闭0,开启1,适用于时间不同步
	
3.启动keepalived
	systemctl start keepalived.service
	systemctl status keepalived.service
	systemctl stop keepalived.service
	systemctl restart keepalived.service
	systemctl enable keepalived.service

1.6.2 配置bigdata05

仅修改一处配置,vi /etc/keepalived/keepalived.conf
	router_id:NodeB
	priority:90

1.6.3 测试单点故障

分别在配置了keepalived的计算机上(bigdata04和bigdata05) 通过ip a查看ip情况

1.7 架构测试

0.准备架构测试中需要的mydb01/mydb02,以及student表
	a.window
		sql> create database mydb01;
		sql> create database mydb02;
		sql> use mydb01;
		sql> create table student(id int(4), name varchar(10));
		sql> use mydb02;
		sql> create table student(id int(4), name varchar(10));
	b.bigdata01
		sql> create database mydb01;
		sql> create database mydb02;
		sql> use mydb01;
		sql> create table student(id int(4), name varchar(10));
		sql> use mydb02;
		sql> create table student(id int(4), name varchar(10));

1.bigdata
	sql> mysql -uroot -proot
	sql> show master status\G   -- 测试

2.bigdata01
	systemctl stop firewalld    -- 关闭当前防火墙
	systemctl disable firewalld	-- 禁止开机自启
	sql> mysql -uroot -proot
	sql> stop slave;
	sql> reset slave;
	sql> change master to
		 master_host='192.168.2.2',
		 master_user='root',
		 master_password='root',
		 master_port=3306,
		 master_log_file='mysql-bin.000004',
		 master_log_pos=5638;			
	sql> start slave;
	sql> show slave status \G   -- 测试

3.bigdata02
	systemctl stop firewalld    -- 关闭当前防火墙
	systemctl disable firewalld	-- 禁止开机自启
	cd /usr/local/mycat
	bin/mycat start		--开启mycat
	bin/mycat stop		--关闭mycat
	bin/mycat status	--状态mycat
	mysql -uroot -proot -h192.168.2.129 -P8066   -- win10中登陆测试
	
4.bigdata03
	systemctl stop firewalld    -- 关闭当前防火墙
	systemctl disable firewalld	-- 禁止开机自启
	cd /usr/local/mycat
	bin/mycat start		--开启mycat
	bin/mycat stop		--关闭mycat
	bin/mycat status	--状态mycat
	mysql -uroot -proot -h192.168.2.130 -P8066   -- win10中登陆测试
	
5.bigdata04
	setenforce 0
	systemctl stop firewalld    -- 关闭当前防火墙
	systemctl disable firewalld	-- 禁止开机自启
	systemctl start haproxy.service
	systemctl status haproxy.service
	systemctl stop haproxy.service
	systemctl restart haproxy.service
	192.168.2.131:5000/haproxy  -- 测试网址

6.bigdata05
	setenforce 0
	systemctl stop firewalld    -- 关闭当前防火墙
	systemctl disable firewalld	-- 禁止开机自启
	systemctl start haproxy.service
	systemctl status haproxy.service
	systemctl stop haproxy.service
	systemctl restart haproxy.service
	192.168.2.132:5000/haproxy  -- 测试网址

7.bigdata04
	setenforce 0
	systemctl stop firewalld    -- 关闭当前防火墙
	systemctl disable firewalld	-- 禁止开机自启
	systemctl start keepalived.service
	systemctl status keepalived.service
	systemctl stop keepalived.service
	systemctl restart keepalived.service
	systemctl enable keepalived.service
	ip a  	-- 查看ip
	
8.bigdata05
	setenforce 0
	systemctl stop firewalld    -- 关闭当前防火墙
	systemctl disable firewalld	-- 禁止开机自启
	systemctl start keepalived.service
	systemctl status keepalived.service
	systemctl stop keepalived.service
	systemctl restart keepalived.service
	systemctl enable keepalived.service
	ip a	-- 查看ip
	
9.架构测试
	a.windows中利用虚拟ip来登陆访问mycat
		mysql -uroot -proot -h192.168.2.222  -P8066
	b.bigdata04模拟故障,使用halt关机,但是用户无法感知
		[root@bigdata04 ~]# halt
	c.向该虚拟ip中发送一条SQL语句,如果成功,则架构测试通过!
		sql> insert into student(id, name) values(1, 'zs');