1, 分库分表的优缺点、以及为什么分表方式无法成为主流?

分表:在台server上,长处是易维护,相似表分区。缺点是在一台dbserver上。无法分担IO、负载集中。

分库:在多台server上,长处是分担IO、负载均衡,缺点是较不易维护、数据统计以及jion操作有些难度。

数据库切分的目的是为了分担IO、负载均衡,分表无法达到最佳的要求。所以无法成为主流。


2, 准备主库

tar -xvf mysql-5.6.12.tar.gz
cd mysql-5.6.12 time cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql56m1 -DMYSQL_DATADIR=/home/data/mysql56m1/data -DWITH_INNOBASE_STORAGE_ENGINE=1 -DMYSQL_UNIX_ADDR==/usr/local/mysql56m1/mysql.sock -DMYSQL_USER=mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci time make
time make install chown -R mysql /home/data/
chgrp -R mysql /home/data/
chown -R mysql /usr/local/mysql chown -R mysql /usr/local/mysql56m1
chgrp -R mysql /usr/local/mysql chgrp -R mysql /usr/local/mysql56m1/
mkdir -p /home/data/mysql56m1/binlog/
chown -R mysql.mysql /home/data/mysql56m1/binlog/
mkdir -p /home/data/mysql5610/binlog/
chown -R mysql.mysql /home/data/ cd /usr/local/mysql56m1
time scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql56m1 --datadir=/home/data/mysql56m1/data --defaults-file=/usr/local/mysql56m1/my.cnf cp support-files/mysql.server /etc/init.d/mysql56m1
chmod 700 /etc/init.d/mysql56m1
echo "export PATH=$PATH:/usr/local/mysql56m1/bin">>/etc/profile
source /etc/profile
chkconfig --add mysql56m1 service mysql56m1 start

3。 准备备库

tar -xvf mysql-5.6.12.tar.gz
cd mysql-5.6.12 time cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql56s1 -DMYSQL_DATADIR=/home/data/mysql56s1/data -DWITH_INNOBASE_STORAGE_ENGINE=1 -DMYSQL_UNIX_ADDR==/usr/local/mysql56s1/mysql.sock -DMYSQL_USER=mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci time make
time make install chown -R mysql /home/data/
chgrp -R mysql /home/data/
chown -R mysql /usr/local/mysql chown -R mysql /usr/local/mysql56s1
chgrp -R mysql /usr/local/mysql chgrp -R mysql /usr/local/mysql56s1/
mkdir -p /home/data/mysql56s1/binlog/
chown -R mysql.mysql /home/data/mysql56s1/binlog/
mkdir -p /home/data/mysql5610/binlog/
chown -R mysql.mysql /home/data/ cd /usr/local/mysql56s1
time scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql56s1 --datadir=/home/data/mysql56s1/data --defaults-file=/usr/local/mysql56s1/my.cnf cp support-files/mysql.server /etc/init.d/mysql56s1
chmod 700 /etc/init.d/mysql56s1
echo "export PATH=$PATH:/usr/local/mysql56s1/bin">>/etc/profile
source /etc/profile
chkconfig --add mysql56s1
service mysql56s1 start

4, 搭建主从环境

在一台server。主3317端口,从3327端口。

主库备库上,加入复制帐号:

GRANT REPLICATION SLAVE ON . TO ‘repl’@’10.254.%’ IDENTIFIED BY ‘mycatms’;

在备库上3327端口上设置复制:

stop slave;
reset slave;
change master to master_user='repl', master_password='mycatms', master_host='127.0.0.1',master_port=3317, master_log_file='mysql-bin.000003',master_log_pos=840;
start slave;
show slave status\G;

主库:3317端口

从库:3327端口

数据同步測试:


5。 数据同步測试:

配置schema.xml:

然后改动mycat的schema.xml:

balance为1:让所有的readHost及备用的writeHost參与select的负载均衡。

switchType为2:基于MySQL主从同步的状态决定是否切换。

heartbeat:主从切换的心跳语句必须为show slave status。

5.0 数据录入:

mysql> explain create table company(id int not null primary key,name varchar(100));
+-----------+---------------------------------------------------------------------+
| DATA_NODE | SQL |
+-----------+---------------------------------------------------------------------+
| dn1 | create table company(id int not null primary key,name varchar(100)) |
| dn2 | create table company(id int not null primary key,name varchar(100)) |
| dn3 | create table company(id int not null primary key,name varchar(100)) |
+-----------+---------------------------------------------------------------------+
3 rows in set (0.00 sec) mysql> create table company(id int not null primary key,name varchar(100));
Query OK, 0 rows affected (0.01 sec) mysql> explain insert into company(id,name) values(1,'alibaba');
+-----------+--------------------------------------------------+
| DATA_NODE | SQL |
+-----------+--------------------------------------------------+
| dn1 | insert into company(id,name) values(1,'alibaba') |
| dn2 | insert into company(id,name) values(1,'alibaba') |
| dn3 | insert into company(id,name) values(1,'alibaba') |
+-----------+--------------------------------------------------+
3 rows in set (0.10 sec) mysql> insert into company(id,name) values(1,'alibaba');
Query OK, 1 row affected (0.00 sec) mysql>

5.1主库验证:

[root@wgq_idc_squid_1_11 logs]# /usr/local/mysql56m1/bin/mysql -uroot -p -P3317 --socket=/usr/local/mysql56m1/mysql.sock -e "select @@port;select * from db1.company";
Enter password:
+--------+
| @@port |
+--------+
| 3317 |
+--------+
+----+---------+
| id | name |
+----+---------+
| 1 | alibaba |
+----+---------+
[root@wgq_idc_squid_1_11 logs]#

5.2从库验证:

[root@wgq_idc_squid_1_11 logs]# /usr/local/mysql56s1/bin/mysql -uroot -p -P3327 --socket=/usr/local/mysql56s1/mysql.sock -e "select @@port;select * from db1.company";
Enter password:
+--------+
| @@port |
+--------+
| 3327 |
+--------+
+----+---------+
| id | name |
+----+---------+
| 1 | alibaba |
+----+---------+
[root@wgq_idc_squid_1_11 logs]#


6,读写分离模式

6.1然后改动mycat的schema.xml:

balance为1:让所有的readHost及备用的writeHost參与select的负载均衡。

switchType为2:基于MySQL主从同步的状态决定是否切换。

heartbeat:主从切换的心跳语句必须为show slave status。



有配置读节点:

6.2 打开debug模式

[root@wgq_idc_squid_1_11 conf]# vim log4j.xml
<level value="info" /> 变成 <level value="debug" />

6.3 运行查询读操作

在mycat窗体上运行select操作:

mysql> explain select * from company where id=1;
+-----------+----------------------------------------------+
| DATA_NODE | SQL |
+-----------+----------------------------------------------+
| dn3 | SELECT * FROM company WHERE id = 1 LIMIT 100 |
+-----------+----------------------------------------------+
1 row in set (0.14 sec) mysql> select * from company where id=1;
+----+---------+
| id | name |
+----+---------+
| 1 | alibaba |
+----+---------+
1 row in set (0.01 sec) mysql>

后台日志显示出仅仅在3327上运行:

01/18 01:35:01.536  DEBUG [$_NIOREACTOR-3-RW] (PhysicalDBPool.java:452) -select read source hostS2 for dataHost:wgq_idc_mon_1_11
01/18 01:35:01.537 DEBUG [$_NIOREACTOR-3-RW] (MySQLConnection.java:445) -con need syn ,total syn cmd 2 commands SET names latin1;SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;schema change:false con:MySQLConnection [id=14, lastTime=1453052101537, user=root, schema=db1, old shema=db1, borrowed=true, fromSlaveDB=false, threadId=3326, charset=latin1, txIsolation=0, autocommit=true, attachment=dn1{SELECT *
FROM company
WHERE id = 1
LIMIT 100}, respHandler=SingleNodeHandler [node=dn1{SELECT *
FROM company
WHERE id = 1
LIMIT 100}, packetId=0], host=127.0.0.1, port=3327, statusSync=null, writeQueue=0, modifiedSQLExecuted=false]
01/18 01:35:01.546 DEBUG [$_NIOREACTOR-2-RW] (NonBlockingSession.java:229) -release connection MySQLConnection [id=14, lastTime=1453052101529, user=root, schema=db1, old shema=db1, borrowed=true, fromSlaveDB=false, threadId=3326, charset=latin1, txIsolation=3, autocommit=true, attachment=dn1{SELECT *
FROM company
WHERE id = 1
LIMIT 100}, respHandler=SingleNodeHandler [node=dn1{SELECT *
FROM company
WHERE id = 1
LIMIT 100}, packetId=5], host=127.0.0.1, port=3327, statusSync=org.opencloudb.mysql.nio.MySQLConnection$StatusSync@7fb43f0f, writeQueue=0, modifiedSQLExecuted=false]
01/18 01:35:01.547 DEBUG [$_NIOREACTOR-2-RW] (PhysicalDatasource.java:403) -release channel MySQLConnection [id=14, lastTime=1453052101529, user=root, schema=db1, old shema=db1, borrowed=true, fromSlaveDB=false, threadId=3326, charset=latin1, txIsolation=3, autocommit=true, attachment=null, respHandler=null, host=127.0.0.1, port=3327, statusSync=null, writeQueue=0, modifiedSQLExecuted=false]
^C

6.4 运行写操作

Mycat窗体写操作:

后台log显示写操作在3317上:

01/18 01:39:54.550  DEBUG [$_NIOREACTOR-3-RW] (NonBlockingSession.java:229) -release connection MySQLConnection [id=7, lastTime=1453052394535, user=root, schema=db3, old shema=db3, borrowed=true, fromSlaveDB=false, threadId=163, charset=latin1, txIsolation=3, autocommit=true, attachment=dn3{insert into company values(3,'baidu')}, respHandler=org.opencloudb.mysql.nio.handler.MultiNodeQueryHandler@42bed1e7, host=127.0.0.1, port=3317, statusSync=null, writeQueue=0, modifiedSQLExecuted=true]
01/18 01:39:54.550 DEBUG [$_NIOREACTOR-3-RW] (PhysicalDatasource.java:403) -release channel MySQLConnection [id=7, lastTime=1453052394535, user=root, schema=db3, old shema=db3, borrowed=true, fromSlaveDB=false, threadId=163, charset=latin1, txIsolation=3, autocommit=true, attachment=null, respHandler=null, host=127.0.0.1, port=3317, statusSync=null, writeQueue=0, modifiedSQLExecuted=false]
01/18 01:39:54.550 DEBUG [$_NIOREACTOR-3-RW] (MultiNodeQueryHandler.java:171) -received ok response ,executeResponse:true from MySQLConnection [id=3, lastTime=1453052394535, user=root, schema=db2, old shema=db2, borrowed=true, fromSlaveDB=false, threadId=162, charset=latin1, txIsolation=3, autocommit=true, attachment=dn2{insert into company values(3,'baidu')}, respHandler=org.opencloudb.mysql.nio.handler.MultiNodeQueryHandler@42bed1e7, host=127.0.0.1, port=3317, statusSync=null, writeQueue=0, modifiedSQLExecuted=true]
01/18 01:39:54.551 DEBUG [$_NIOREACTOR-3-RW] (NonBlockingSession.java:229) -release connection MySQLConnection [id=3, lastTime=1453052394535, user=root, schema=db2, old shema=db2, borrowed=true, fromSlaveDB=false, threadId=162, charset=latin1, txIsolation=3, autocommit=true, attachment=dn2{insert into company values(3,'baidu')}, respHandler=org.opencloudb.mysql.nio.handler.MultiNodeQueryHandler@42bed1e7, host=127.0.0.1, port=3317, statusSync=null, writeQueue=0, modifiedSQLExecuted=true]
01/18 01:39:54.551 DEBUG [$_NIOREACTOR-3-RW] (PhysicalDatasource.java:403) -release channel MySQLConnection [id=3, lastTime=1453052394535, user=root, schema=db2, old shema=db2, borrowed=true, fromSlaveDB=false, threadId=162, charset=latin1, txIsolation=3, autocommit=true, attachment=null, respHandler=null, host=127.0.0.1, port=3317, statusSync=null, writeQueue=0, modifiedSQLExecuted=false]

Check下主从数据是否都已经写入了:

主库3317端口:



从裤3327端口:


7,主从切换測试

7.1 mycat托管配置例如以下

     <dataHost name="wgq_idc_mon_1_11" maxCon="1000" minCon="10" balance="1"
writeType="0" dbType="mysql" dbDriver="native" switchType="2" slaveThreshold="100">
<heartbeat>show slave status</heartbeat>
<!-- can have multi write hosts -->
<writeHost host="hostM1" url="127.0.0.1:3317" user="root" password="">
<readHost host="hostR1" url="127.0.0.1:3327" user="root" password=""/>
</writeHost>
<writeHost host="hostS2" url="127.0.0.1:3327" user="root" password="" />
</dataHost>

7.2 关闭主库。写操作切换到从裤3327端口

关闭主库

[root@wgq_idc_squid_1_11 conf]# service mysql56m1 stop
Shutting down MySQL.... [确定]
[root@wgq_idc_squid_1_11 conf]#

Mycat后台报错:

01/18 01:50:01.037   INFO [Timer0] (PhysicalDatasource.java:373) -not ilde connection in pool,create new connection for hostM1 of schema db1
01/18 01:50:01.038 INFO [$_NIOConnector] (AbstractConnection.java:458) -close connection,reason:java.net.ConnectException: 拒绝连接 ,MySQLConnection [id=0, lastTime=1453053001035, user=root, schema=db1, old shema=db1, borrowed=false, fromSlaveDB=false, threadId=0, charset=utf8, txIsolation=0, autocommit=true, attachment=null, respHandler=null, host=127.0.0.1, port=3317, statusSync=null, writeQueue=0, modifiedSQLExecuted=false]
01/18 01:50:01.038 INFO [$_NIOConnector] (SQLJob.java:111) -can't get connection for sql :show slave status
01/18 01:50:01.038 WARN [$_NIOREACTOR-1-RW] (MySQLDetector.java:139) -found MySQL master/slave Replication err !!! DBHostConfig [hostName=hostR1, url=127.0.0.1:3327]error reconnecting to master 'repl@127.0.0.1:3317' - retry-time: 60 retries: 1
0

看到主从失效,由于主down了。

由于我们通过mycat配置了主从切换模式,如今3317端口主库down了,那么写库应该自己主动切换到从裤3327上面,假设在mycat上面写入。就应该写到3327端口的从库了,验证例如以下:

(1)在mycat窗体录入数据:



(2)在3327端口从裤验证数据:

看到3327端口的数据有新录入的4,meituan的数据。

表明验证成功。

(3) dnindex属性文件里writeHost已经变成了第二个了,例如以下所看到的:

7.3 再启动原来的主库3317端口,在mycat上做写操作,后台mysql写库还是原来的从库3327端口:

这个时候就会报错,主从连接失败,原因是从库3327会再又一次从主库3317同步所有的数据,可是从库已经有了,所以就会报错,例如以下所看到的:

我採用

stop slave;
set global sql_slave_skip_counter=1;
start slave;
show slave status\G;

一个个忽略过后。就正常了。没有报错:

可是这个时候假设再在mycat窗体上录入数据。debug分析面。还是会写入到3327里面去:

后台debug日志:

OK,至此。mycat主从切换成功。

mycat读写分离与主从切换的更多相关文章

  1. Mycat读写分离、主从切换学习(转)

    http://blog.csdn.net/zhanglei_16/article/details/50707487 Mycat读写分离.主从切换学习问题一:分表.分库的优缺点,以及分表无法成为主流分表 ...

  2. mycat读写分离与主从切换【转】

    什么是mycat,以及mycat的优点和特性本文不做赘述,本文继续本着实战的态度,来分享一些个人对mycat的基础功能实践.本文mycat的读写分离和主从切换的环境为mysql主从环境. 如何安装my ...

  3. Mycat读写分离、主从切换、分库分表的操作记录

    系统开发中,数据库是非常重要的一个点.除了程序的本身的优化,如:SQL语句优化.代码优化,数据库的处理本身优化也是非常重要的.主从.热备.分表分库等都是系统发展迟早会遇到的技术问题问题.Mycat是一 ...

  4. myCat读写分离+传统主从

    1 Mycat介绍: mycat是最近很火的一款国人发明的分布式数据库中间件,它是基于阿里的cobar的基础上进行开发的 准备环境: db01主   10.0.0.51 db02备   10.0.0. ...

  5. redis的主从复制,读写分离,主从切换

    当数据量变得庞大的时候,读写分离还是很有必要的.同时避免一个redis服务宕机,导致应用宕机的情况,我们启用sentinel(哨兵)服务,实现主从切换的功能. redis提供了一个master,多个s ...

  6. Redis哨兵模式(sentinel)部署记录(主从复制、读写分离、主从切换)

    部署环境: CentOS7.5  192.168.94.11 (master) 192.168.94.22 (slave0) 192.168.94.33 (slave1) 192.168.94.44 ...

  7. Redis哨兵模式(sentinel)学习总结及部署记录(主从复制、读写分离、主从切换)

    Redis的集群方案大致有三种:1)redis cluster集群方案:2)master/slave主从方案:3)哨兵模式来进行主从替换以及故障恢复. 一.sentinel哨兵模式介绍Sentinel ...

  8. [转]Redis哨兵模式(sentinel)学习总结及部署记录(主从复制、读写分离、主从切换)

    Redis的集群方案大致有三种:1)redis cluster集群方案:2)master/slave主从方案:3)哨兵模式来进行主从替换以及故障恢复. 一.sentinel哨兵模式介绍Sentinel ...

  9. redis的主从复制(读写分离)/哨兵(主从切换)配置

    准备两个redis服务,两台机器,依次命名文件夹子master,slave1 10.10.10.7 10.10.10.8 1.master修改配置文件 [root@db2 conf]# cat 637 ...

随机推荐

  1. 最简单的DLL

    静态链接库与动态链接库都是共享代码的方式,如果采用静态链接库,则无论你愿不愿意,lib 中的指令都全部被直接包含在最终生成的 EXE 文件中了.但是若使用 DLL,该 DLL 不必被包含在最终 EXE ...

  2. 4.flask数据库

    1.安装MySQL 直接去下载即可,如果是windows建可以下载msi,一路next即可.我已经安装过了,这里就不再演示了. 最后使用Navicat连接测试一下,我这里是没有问题的 2.SQLAlc ...

  3. [ Openstack ] OpenStack-Mitaka 高可用之 认证服务(keystone)

    目录 Openstack-Mitaka 高可用之 概述    Openstack-Mitaka 高可用之 环境初始化    Openstack-Mitaka 高可用之 Mariadb-Galera集群 ...

  4. MATLAB求解常微分方程:ode45函数与dsolve函数

    ode45函数无法求出解析解,dsolve可以求出解析解(若有),但是速度较慢. 1.      ode45函数 ①求一阶常微分方程的初值问题 [t,y] = ode45(@(t,y)y-2*t/y, ...

  5. JSON格式数据的js操作

    第一种方式: 使用js函数eval(); testJson=eval(testJson);是错误的转换方式. 正确的转换方式需要加(): testJson = eval("(" + ...

  6. 极光推送配置(Android Studio),亲测有效

    进行到这里就可以接收到通知了,但是如果你还想根据接收的消息做点什么 step8: public class MyReceiver extends BroadcastReceiver { private ...

  7. C# DataGridView分页显示

    //导入命名空间部分省略 DBClass.DBExecute dbexecute = new DBExecute(); string connectionString = @"Data So ...

  8. 【转】Python unittest数据驱动工具:DDT

    背景 python 的unittest 没有自带数据驱动功能. 所以如果使用unittest,同时又想使用数据驱动,那么就可以使用DDT来完成. DDT是 “Data-Driven Tests”的缩写 ...

  9. Don't Be a Subsequence

    问题 F: Don't Be a Subsequence 时间限制: 1 Sec  内存限制: 128 MB提交: 33  解决: 2[提交] [状态] [讨论版] [命题人:] 题目描述 A sub ...

  10. 【后缀数组】【二分答案】【差分】poj1743 Musical Theme

    差分消除加减一个值得影响,貌似r二分上界要设成(n-2)/2?为啥? sa求不可重叠最长重复子串 给定一个字符串,求最长重复子串,这两个子串不能重叠.算法分析:这题比上一题稍复杂一点.先二分答案,把题 ...