1、上周尾巴(一套烂的
1.1、检查MHA的运行状态
masterha_check_status --conf=/etc/mha/app1.cnf
1.2、判断主节点
直接的方法,看哪台MySQL没有从库状态,结合从库的mysql指向位置
1.3、恢复1主2从的状态
db03: grep -i 'change master to ' /var/log/mha/app1/manager
db02:
CHANGE MASTER TO
MASTER_HOST='10.0.0.51',
MASTER_PORT=3306,
MASTER_AUTO_POSITION=1,
MASTER_USER='repl',
MASTER_PASSWORD='123';
start slave ;
1.4、检查vip
db01:
ifconfig eth0:1 10.0.0.55/24
1.5、检查db03配置文件
vim /etc/mha/app1.cnf
[server1]
hostname=10.0.0.51
port=3306
[server2]
hostname=10.0.0.52
port=3306
[server3]
hostname=10.0.0.53
port=3306
1.6 启动前预检查
[root@db03 ~]# masterha_check_ssh --conf=/etc/mha/app1.cnf
[root@db03 ~]# masterha_check_repl --conf=/etc/mha/app1.cnf
1.7 启动MHA
nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null> /var/log/mha/app1/manager.log 2>&1 &
masterha_check_status --conf=/etc/mha/app1.cnf
2、Binlog Server
选择一个专门保存 binlog 的服务器,必须要有mysqlbinlog命令,我妈们选择db03
vim /etc/mha/qpp1.cnf
[binlog1]
no_master=1
hostname=10.0.0.53
master_binlog_dir=/data/mysql/binlog
创建必要目录
mkdir -p /data/mysql/binlog
chown -R mysql.mysql /data/*
cd /data/mysql/binlog -----》必须进入到自己创建好的目录
mysqlbinlog -R --host=10.0.0.51 --user=mha --password=mha --raw --stop-never mysql-bin.000007 &
3、Altas
rpm -ivh Atlas-2.2.1.el6.x86_64.rpm
cd /usr/local/mysql-proxy/conf
mv test.cnf test.cnf.bak
cat > test.cnf <select @@server_id;
测写:
db03 [(none)]>begin;select @@server_id; commit;
4. 生产用户要求 (Atlas+MHA+VIP+SENDREPORT+BINLOG)
开发人员申请一个应用用户 app( select update insert) 密码123456,要通过10网段登录
1. 在主库中,创建用户
grant select ,update,insert on *.* to app@'10.0.0.%' identified by '123456';
2. 在atlas中添加生产用户
/usr/local/mysql-proxy/bin/encrypt 123456 ---->制作加密密码
3. 改配置文件
vim test.cnf
pwds = repl:3yb5jEku5h4=,mha:O2jBXONX098=,app:/iZxz+0GRoA=
/usr/local/mysql-proxy/bin/mysql-proxyd test restart
[root@db03 conf]# mysql -uapp -p123456 -h 10.0.0.53 -P 33060
5、Altas基本管理
5.1 连接管理接口
mysql -uuser -ppwd -h10.0.0.53 -P2345
5.2 打印帮助:
mysql> select * from help;
5.3 查询后端所有节点信息:
mysql> SELECT * FROM backends
5.4、动态删除节点
db03 [(none)]>REMOVE BACKEND 3;
Empty set (0.00 sec)
db03 [(none)]> SELECT * FROM backends;
+-------------+----------------+-------+------+
| backend_ndx | address | state | type |
+-------------+----------------+-------+------+
| 1 | 10.0.0.55:3306 | up | rw |
| 2 | 10.0.0.52:3306 | up | ro |
+-------------+----------------+-------+------+
5.5、动态添加节点
db03 [(none)]>ADD SLAVE 10.0.0.53:3306;
Empty set (0.00 sec)
db03 [(none)]> SELECT * FROM backends;
+-------------+----------------+-------+------+
| backend_ndx | address | state | type |
+-------------+----------------+-------+------+
| 1 | 10.0.0.55:3306 | up | rw |
| 2 | 10.0.0.52:3306 | up | ro |
| 3 | 10.0.0.53:3306 | up | ro |
+-------------+----------------+-------+------+
5.6、保存配置到配置文件
db03 [(none)]>save config;
6、关于读写分离建议(自己扩展)
MyCAT分布式架构
2、MyCAT分布式架构搭建
db01 db02
2.1、 删除历史环境:
pkill mysqld
rm -rf /data/330*
mv /etc/my.cnf /etc/my.cnf.bak
2.2、 初始化
mkdir /data/33{07..10}/data -p
mysqld --initialize-insecure --user=mysql --datadir=/data/3307/data --basedir=/application/mysql
mysqld --initialize-insecure --user=mysql --datadir=/data/3308/data --basedir=/application/mysql
mysqld --initialize-insecure --user=mysql --datadir=/data/3309/data --basedir=/application/mysql
mysqld --initialize-insecure --user=mysql --datadir=/data/3310/data --basedir=/application/mysql
2.3、 准备db01配置文件和启动脚本
========db01==============
cat >/data/3307/my.cnf</data/3308/my.cnf</data/3309/my.cnf</data/3310/my.cnf</etc/systemd/system/mysqld3307.service</etc/systemd/system/mysqld3308.service</etc/systemd/system/mysqld3309.service</etc/systemd/system/mysqld3310.service<
2.4、准备db02配置文件和启动脚本
cat >/data/3307/my.cnf</data/3308/my.cnf</data/3309/my.cnf</data/3310/my.cnf</etc/systemd/system/mysqld3307.service</etc/systemd/system/mysqld3308.service</etc/systemd/system/mysqld3309.service</etc/systemd/system/mysqld3310.service<
2.5修改权限,启动多实例
chown -R mysql.mysql /data/*
systemctl start mysqld3307
systemctl start mysqld3308
systemctl start mysqld3309
systemctl start mysqld3310
mysql -S /data/3307/mysql.sock -e "show variables like 'server_id'"
mysql -S /data/3308/mysql.sock -e "show variables like 'server_id'"
mysql -S /data/3309/mysql.sock -e "show variables like 'server_id'"
mysql -S /data/3310/mysql.sock -e "show variables like 'server_id'"
2.6、规划
2.7、构建主从
shard1
10.0.0.51:3307 <-----> 10.0.0.52:3307
db02
mysql -S /data/3307/mysql.sock -e "grant replication slave on *.* to repl@'10.0.0.%' identified by '123';"
mysql -S /data/3307/mysql.sock -e "grant all on *.* to root@'10.0.0.%' identified by '123' with grant option;"
db01
mysql -S /data/3307/mysql.sock -e "CHANGE MASTER TO MASTER_HOST='10.0.0.52', MASTER_PORT=3307, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='123';"
mysql -S /data/3307/mysql.sock -e "start slave;"
mysql -S /data/3307/mysql.sock -e "show slave status\G"
db02
mysql -S /data/3307/mysql.sock -e "CHANGE MASTER TO MASTER_HOST='10.0.0.51', MASTER_PORT=3307, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='123';"
mysql -S /data/3307/mysql.sock -e "start slave;"
mysql -S /data/3307/mysql.sock -e "show slave status\G"
10.0.0.51:3309 ------> 10.0.0.51:3307
db01
mysql -S /data/3309/mysql.sock -e "CHANGE MASTER TO MASTER_HOST='10.0.0.51', MASTER_PORT=3307, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='123';"
mysql -S /data/3309/mysql.sock -e "start slave;"
mysql -S /data/3309/mysql.sock -e "show slave status\G"
10.0.0.52:3309 ------> 10.0.0.52:3307
db02
mysql -S /data/3309/mysql.sock -e "CHANGE MASTER TO MASTER_HOST='10.0.0.52', MASTER_PORT=3307, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='123';"
mysql -S /data/3309/mysql.sock -e "start slave;"
mysql -S /data/3309/mysql.sock -e "show slave status\G"
shard2
10.0.0.52:3308 <-----> 10.0.0.51:3308
db01
mysql -S /data/3308/mysql.sock -e "grant replication slave on *.* to repl@'10.0.0.%' identified by '123';"
mysql -S /data/3308/mysql.sock -e "grant all on *.* to root@'10.0.0.%' identified by '123' with grant option;"
db02
mysql -S /data/3308/mysql.sock -e "CHANGE MASTER TO MASTER_HOST='10.0.0.51', MASTER_PORT=3308, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='123';"
mysql -S /data/3308/mysql.sock -e "start slave;"
mysql -S /data/3308/mysql.sock -e "show slave status\G"
db01
mysql -S /data/3308/mysql.sock -e "CHANGE MASTER TO MASTER_HOST='10.0.0.52', MASTER_PORT=3308, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='123';"
mysql -S /data/3308/mysql.sock -e "start slave;"
mysql -S /data/3308/mysql.sock -e "show slave status\G"
10.0.0.52:3310 -----> 10.0.0.52:3308
db02
mysql -S /data/3310/mysql.sock -e "CHANGE MASTER TO MASTER_HOST='10.0.0.52', MASTER_PORT=3308, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='123';"
mysql -S /data/3310/mysql.sock -e "start slave;"
mysql -S /data/3310/mysql.sock -e "show slave status\G"
10.0.0.51:3310 -----> 10.0.0.51:3308
db01
mysql -S /data/3310/mysql.sock -e "CHANGE MASTER TO MASTER_HOST='10.0.0.51', MASTER_PORT=3308, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='123';"
mysql -S /data/3310/mysql.sock -e "start slave;"
mysql -S /data/3310/mysql.sock -e "show slave status\G"
3. MyCAT安装
3.1 预先安装Java运行环境
yum install -y java
3.2下载
Mycat-server-xxxxx.linux.tar.gz
http://dl.mycat.io/
3.3 解压文件
tar xf Mycat-server-1.6.5-release-20180122220033-linux.tar.gz
3.4 软件目录结构
ls
bin catlet conf lib logs version.txt
3.5 启动和连接
配置环境变量
vim /etc/profile
export PATH=/data/mycat/bin:$PATH
source /etc/profile
启动
mycat start
连接mycat:
mysql -uroot -p123456 -h 127.0.0.1 -P8066
4. 配置文件介绍
logs目录:
wrapper.log ---->mycat启动日志
mycat.log ---->mycat详细工作日志
conf目录:
schema.xml
主配置文件(读写分离、高可用、分布式策略定制、节点控制)
server.xml
mycat软件本身相关的配置
rule.xml
分片规则配置文件,记录分片规则列表、使用方法等
5.应用前环境准备
5.1 用户创建及数据库导入
db01:
mysql -S /data/3307/mysql.sock
grant all on *.* to root@'10.0.0.%' identified by '123';
source /root/world.sql
mysql -S /data/3308/mysql.sock
grant all on *.* to root@'10.0.0.%' identified by '123';
source /root/world.sql
5.2 配置文件处理
cd /application/mycat/conf
mv schema.xml schema.xml.bak
vim schema.xml
select user()
重启mycat
mycat restart
读写分离测试
mysql -uroot -p -h 127.0.0.1 -P8066
show variables like 'server_id';
begin;
show variables like 'server_id';
总结:
以上案例实现了1主1从的读写分离功能,写操作落到主库,读操作落到从库.如果主库宕机,从库不能在继续提供服务了。
6、MyCAT 核心特性---分片
分片:对一个"bigtable",比如说t3表
(1)行数非常多,800w
(2)访问非常频繁
分片的目的:
(1)将大数据量进行分布存储
(2)提供均衡的访问路由
分片策略:
范围 range 800w 1-400w 400w01-800w
取模 mod 取余数
枚举
哈希 hash
时间 流水
优化关联查询
全局表
ER分片
range范围分片
比如说t3表
(1)行数非常多,2000w(1-1000w:sh1 1000w-2000w:sh2)
(2)访问非常频繁,用户访问较离散
1)准备配置文件
[root@db01 /application/mycat/conf]# vim schema.xml
select user()
select user()
2)修改
[root@db01 /application/mycat/conf]# cat autopartition-long.txt
1-5=0
6-10=1
3)重启
mycat restart
4)
创建测试表:
mysql -S /data/3307/mysql.sock -e "use taobao;create table t3 (id int not null primary key auto_increment,name varchar(20) not null);"
mysql -S /data/3308/mysql.sock -e "use taobao;create table t3 (id int not null primary key auto_increment,name varchar(20) not null);"
5)插入数据,检查
insert into t3(id,name) values(1,'a');
insert into t3(id,name) values(2,'b');
insert into t3(id,name) values(3,'c');
insert into t3(id,name) values(7,'x');
insert into t3(id,name) values(8,'y');
insert into t3(id,name) values(9,'z');
select * from t3;
取模分片
取余分片方式:分片键(一个列)与节点数量进行取余,得到余数,将数据写入对应节点
1)修改配置文件
vim schema.xml
vim rule.xml
2
2)
准备测试环境
创建测试表:
mysql -S /data/3307/mysql.sock -e "use taobao;create table t4 (id int not null primary key auto_increment,name varchar(20) not null);"
mysql -S /data/3308/mysql.sock -e "use taobao;create table t4 (id int not null primary key auto_increment,name varchar(20) not null);"
3)重启mycat
mycat restart
4)
测试:
mysql -uroot -p123456 -h127.0.0.1 -P8066
use TESTDB
insert into t4(id,name) values(1,'a');
insert into t4(id,name) values(2,'b');
insert into t4(id,name) values(3,'c');
insert into t4(id,name) values(4,'d');
5)
分别登录后端节点查询数据
mysql -S /data/3307/mysql.sock -e "select * from taobao.t4;"
mysql -S /data/3308/mysql.sock -e "select * from taobao.t4;"
枚举分片
1)
vim schema.xml
2)
vim rule.xml
1
3)
partition-hash-int.txt 配置:
bj=0
sh=1
DEFAULT_NODE=1
4)
mysql -S /data/3307/mysql.sock -e "use taobao;create table t5 (id int not null primary key auto_increment,name varchar(20) not null);"
mysql -S /data/3308/mysql.sock -e "use taobao;create table t5 (id int not null primary key auto_increment,name varchar(20) not null);"
5)重启
mycat restart
6)登录测试
mysql -uroot -p123456 -h10.0.0.51 -P8066
use TESTDB
insert into t5(id,name) values(1,'bj');
insert into t5(id,name) values(2,'sh');
insert into t5(id,name) values(3,'bj');
insert into t5(id,name) values(4,'sh');
insert into t5(id,name) values(5,'tj');
7)分别登录后端节点查询数据
mysql -S /data/3307/mysql.sock -e "select * from taobao.t5;"
mysql -S /data/3308/mysql.sock -e "select * from taobao.t5;"
maycat 全局表
a b c d
join
t
select t1.name ,t.x from t1
join t
select t2.name ,t.x from t2
join t
select t3.name ,t.x from t3
join t
使用场景:
如果你的业务中有些数据类似于数据字典,比如配置文件的配置,
常用业务的配置或者数据量不大很少变动的表,这些表往往不是特别大,
而且大部分的业务场景都会用到,那么这种表适合于Mycat全局表,无须对数据进行切分,
要在所有的分片上保存一份数据即可,Mycat 在Join操作中,业务表与全局表进行Join聚合会优先选择相同分片内的全局表join,
避免跨库Join,在进行数据插入操作时,mycat将把数据分发到全局表对应的所有分片执行,在进行数据读取时候将会随机获取一个节点读取数据。
vim schema.xml
后端数据准备
mysql -S /data/3307/mysql.sock
use taobao
create table t_area (id int not null primary key auto_increment,name varchar(20) not null);
mysql -S /data/3308/mysql.sock
use taobao
create table t_area (id int not null primary key auto_increment,name varchar(20) not null);
重启mycat
mycat restart
测试:
mysql -uroot -p123456 -h10.0.0.51 -P8066
use TESTDB
insert into t_area(id,name) values(1,'a');
insert into t_area(id,name) values(2,'b');
insert into t_area(id,name) values(3,'c');
insert into t_area(id,name) values(4,'d');
mysql -S /data/3307/mysql.sock -e "select * from taobao.t_area;"
mysql -S /data/3308/mysql.sock -e "select * from taobao.t_area;"
E-R分片
A
join
B
为了防止跨分片join,可以使用E-R模式
A join B
on a.xx=b.yy
join C
on A.id=C.id