mycat分库分表
mycat分库分表
实验环境:
三台centos7
192.168.100.134 mysql读
192.168.100.138 mysql写
192.168.100.135 mycat
ps:我这里的两台mysql做的双主工作模式
1,安装mysql
wget http://repo.mysql.com/mysql57-community-release-el7-9.noarch.rpm
yum localinstall mysql57-community-release-el7-9.noarch.rpm -y
yum install mysql-community-server -y
mysql -V #查看版本号`
2,关闭防火墙和selinux
systemctl stop firewalld.service
systemctl disable firewalld.service
firewall-cmd --state
sed -i ‘/^SELINUX=./c SELINUX=disabled’ /etc/selinux/config
sed -i 's/^SELINUXTYPE=./SELINUXTYPE=disabled/g’ /etc/selinux/config
grep --color=auto ‘^SELINUX’ /etc/selinux/config
setenforce 0
3,启动mysql,使用默认密码登陆并修改密码
systemctl start mysqld #启动数据库
systemctl enable mysqld #下次开机自启
grep ‘temporary password’ /var/log/mysqld.log #查看默认的mysql密码
mysql -uroot -p #回车后跟上面的密码(粘贴复制即可)
alter user ‘root’@‘localhost’ identified by ‘Aa123…’; #修改root密码
flush privileges; #刷新权限
4,修改mysql的配置文件搭建双主复制模式,主主模式两个配置文件只有server-id和auto-increment-offset的值不能一样
vim /etc/my.cnf
binlog_do_db=database_name #指定binlog日志是记录的是哪个库
replicate-do-db=database_name #指定复制哪一个库
auto-increment-offset = 1 //设置自动增长的字段的偏移量,即初始值为1
log_bin_trust_function_creators=1 #详解如下方
expire_logs_days = 10 #保留10天的bin_log日志,防止日志太多占用磁盘空间
max_binlog_size = 100M #限制每个bin_log日志大小最大为100M。
max_connections=600 #指定最大连接数
wait_timeout=5 #等待超时
log-bin=mysql-bin #指定bin-log文件前缀名称,开启binlog日志
server-id=1 #指定master主机的id,不可为0,否则拒绝所有slave连接。
auto-increment-increment = 2 //每次增长2
5,在master主库上创建一个用于主从复制的用户
mysql -uroot -pAa123…
grant replication client,replication slave on . to aaa@qq.com‘从库ip’ identified by ‘密码’;
show master status\G #检查数据库状态
6,更改slave库的配置
systemctl start mysqld
grep ‘temporary password’ /var/log/mysqld.log #读取默认的root密码,
mysql -uroot -p #后跟读取到的默认密码,
alter user ‘root’@‘localhost’ identified by ‘Aa123…’; #给新的密码
flush privileges; #刷新
修改my.cnf文件
7,两个库进行连接:(server1&server2)
grant REPLICATION SLAVE on . to ‘bai’@‘从库ip’ identified by ‘Aa123…’; #使用主库授权的账号密码
change master to master_host=‘主库ip’,master_user=‘bai’,master_password=‘Aa123…’,master_log_file=‘mysql-bin.0000016’,master_log_pos=154;
start slave; 开启备份
检查数据库状态:
show master status
8,检查:在任意数据库写入新东西,看是否同步
9,配置半同步模式(server2)
为了避免遗忘,在两台机器中将主备半同步模块都安装并且启动
mysql> install plugin rpl_semi_sync_master soname ‘semisync_master.so’;
mysql> install plugin rpl_semi_sync_slave soname ‘semisync_slave.so’;
mysql> set global rpl_semi_sync_master_enabled = 1;
mysql> set global rpl_semi_sync_master_timeout = 2000;
查看半同步的状态信息
show global variables like ‘%semi%’;
备库开启半同步:(server1)
install plugin rpl_semi_sync_master soname ‘semisync_master.so’;
install plugin rpl_semi_sync_slave soname ‘semisync_slave.so’;
set global rpl_semi_sync_slave_enabled = 1;
show global variables like ‘%semi_sync_slave%’;
stop slave io_thread;
start slave io_thread;
查看主库的半同步模式:
show global status like ‘%semi%’;
10,搭建多线程并行复制(server1)
mysql> show full processlist; #搭建前,先查看当前线程状态,显示为等待
mysql> show variables like ‘slave_parallel_type’; #查看当前复制模式和进程数
mysql> show variables like ‘slave_parallel_workers’;
stop slave; #停止从节点复制
set global slave_parallel_type=‘logical_clock’; #更改复制类型为基于组的复制
mysql> set global slave_parallel_workers=4; #设置并行数量为 4
创建表:
11,配置mycat
安装Java环境
yum install java-1.8.0-openjdk -y (因为mycat是基于java启动的)
wget http://dl.mycat.io/1.6-RELEASE/Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz 下载mycat的压缩包
tar -zxvf Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz -C /usr/local/src/ 解压
-C #指定解压位置
查看端口状态:mycat默认两个端口8066和9066
ss -ntlp
12,修改配置文件,实现写入由主库1完成,读取由主库2完成
修改配置文件之前先备份,以免出错
cd /usr/local/src/mycat/conf/(你的安装路径)
cp server.xml server.xml.bak
cp schema.xml schema.xml.bak
vim server.cml
<user name="root">
<property name="password">Aa123...</property>
<property name="schemas">aa</property>
<!-- 表级 DML 权限设置 -->
<!--
<privileges check="false">
<schema name="TESTDB" dml="0110" >
<table name="tb01" dml="0000"></table>
<table name="tb02" dml="1111"></table>
</schema>
</privileges>
-->
</user>
<!--
<user name="user">
<property name="password">user</property>
<property name="schemas">TESTDB</property>
<property name="readOnly">true</property>
</user>
-->
user模块那里的name是登陆mycat的用户名,下面是密码,和你想让用户看到的库名(非物理库名)。
vim schema.cml
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
<schema name="aa" checkSQLschema="false" sqlMaxLimit="100" dataNode="bww"> 可见数据库名关联可见
</schema>
<dataNode name="bww" dataHost="192.168.100.138" database="bai1" /> <dataHost name="192.168.100.138" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>show slave status</heartbeat>
<writeHost host="192.168.100.138" url="192.168.100.138:3306" user="root" password="Aa123...">
<readHost host="192.168.100.134" url="192.168.100.134:3306" user="root" password="Aa123...">
</readHost>
</writeHost>
</dataHost>
</mycat:schema>
把log4j2.xml中的级别改为debug。
把两个数据库都给mycat权限。
GRANT ALL ON . TO ‘root’@‘192.168.100.135’ identified by ‘Aa123…’ WITH GRANT OPTION;
flush privileges;
13,客户端连接mycat
mysql -h192.168.100.135 -P8066 -uroot -pAa123…
show databases;
use aa;
show tables;
create database baibai;
14,查看日志
tail -f mycat.log |grep create
…[node=sjq{create table baibai}, packetId=1], host=192.168.100.138, port=3306, 写入到了138服务器…
…[node=baibai{show database}, packetId=1], host=192.168.100.134, port=3306, saaa@qq.come96d7a6, writeQueue=0, modifiedSQLExecuted=false] frontend host:192.168.100.134/35558/root 读命令到134服务器上…