Mycat连接Mysql双主双从 小鱼儿 2023-07-07 03:59 2阅读 0赞 # Mycat 安装 # * 准备Mycat-server-1.6.7.4-release-20200105164103-linux.tar.gz,官网下载即可 * 上传到/usr/local下 tar –zxvf Mycat-server-1.6.7.4-release-20200105164103-linux.tar.gz * 修改配置文件 /usr/local/mycat/conf/schema.xml 、 rule.xml、server.xml * 启动mycat /usr/local/mycat/bin/mycat start * 连接mycat mysql -uroot -pdigdeep -h127.0.0.1 -P8066 –DTESTDB **Mycat****读写分离(一主一从):** **1./usr/local/mycat/conf/schema.xml** <table style="margin-left:18pt;"> <tbody> <tr> <td style="vertical-align:top;width:414.8pt;"> <p style="margin-left:0cm;"><strong><span style="color:#333300;"><mycat:schema xmlns:mycat="http://io.mycat/"></span></strong></p> <p style="margin-left:0cm;"> </p> <p style="margin-left:0cm;"><strong><span style="color:#333300;"> <schema name="TESTDB" checkSQLschema="true" sqlMaxLimit="100" dataNode="dn1"></span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#333300;"> <!-- auto sharding by id (long) --></span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#333300;"> <!--splitTableNames </span></strong><strong><span style="color:#333300;">启用</span></strong><strong><span style="color:#333300;"><table name </span></strong><strong><span style="color:#333300;">属性使用逗号分割配置多个表</span></strong><strong><span style="color:#333300;">,</span></strong><strong><span style="color:#333300;">即多个表使用这个配置</span></strong><strong><span style="color:#333300;">--></span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#333300;"> </schema></span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#333300;"> <!-- <dataNode name="dn1$0-743" dataHost="localhost1" database="db$0-743"</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#333300;"> /> --></span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#333300;"> <dataNode name="dn1" dataHost="localhost1" database="testdb" /></span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#333300;"> <dataHost name="localhost1" maxCon="1000" minCon="10" </span><span style="color:#f33b45;">balance="0"</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#333300;"> writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100"></span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#333300;"> <heartbeat>select user()</heartbeat></span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#333300;"> <!-- can have multi write hosts --></span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#333300;"> <writeHost host="hostM1" url="douzi9:3306" user="root"</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#333300;"> password="123456"></span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#333300;"> <readHost host="hostS1" url="douzi10:3306" user="root" password="123456" /></span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#333300;"> </writeHost></span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#333300;"> <!-- <writeHost host="hostM2" url="localhost:3316" user="root" password="123456"/> --></span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#333300;"> </dataHost></span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#333300;"></mycat:schema></span></strong></p> </td> </tr> </tbody> </table> **2.启动mycat ./usr/local/mycat/bin/mycat console** **3.验证读写查询哪台数据库服务器** **通过 mysql -uroot -p123456 -h douzi9 -P 8066****登录mycat****服务器命令窗:** **切换数据库: Use TESTDB;** <table> <tbody> <tr> <td style="vertical-align:top;width:414.8pt;"> <p style="margin-left:0cm;"><strong>Insert into mytbl(id,name) values(1,@@hostname);</strong></p> <p style="margin-left:0cm;"><strong>Select * from mytbl; </strong></p> <p style="margin-left:0cm;"> </p> <p style="margin-left:0cm;"><strong>发现读取的时候是写主机douzi9</strong></p> <p style="margin-left:0cm;"><strong>原因是:</strong><strong><span style="color:#333300;">schema.xml</span></strong><strong><span style="color:#333300;">中的</span></strong><strong><span style="color:#333300;">dataHost </span></strong><strong><span style="color:#333300;">下</span></strong><strong><span style="color:#333300;"> balance</span></strong><strong><span style="color:#333300;">的值需要修改:</span></strong></p> <ol> <li> <p><strong>当balance=0 时,<span style="color:#333300;">不开启读写分离,</span>所有读操作都发生在当前的writeHost上</strong><strong> </strong></p> </li> <li> <p><strong>当balance=1 时,<span style="color:#333300;">全部的readHost </span></strong><strong><span style="color:#333300;">和</span></strong><strong><span style="color:#333300;"> stand by writeHost </span></strong><strong><span style="color:#333300;">(</span></strong><strong><span style="color:#333300;">M1 -> S1, M2->S2 </span></strong><strong><span style="color:#333300;">双主双从时,</span></strong><strong><span style="color:#333300;"> M1 </span></strong><strong><span style="color:#333300;">为写,</span></strong><strong><span style="color:#333300;"> S1 S2 M2</span></strong><strong><span style="color:#333300;">都参与</span></strong><strong><span style="color:#333300;">select</span></strong><strong><span style="color:#333300;">的负载均衡)</span></strong></p> </li> <li> <p><strong>当balance=2 时,<span style="color:#333300;">所有机器随机分发</span></strong></p> </li> <li> <p><strong>当balance=3 时,<span style="color:#333300;">所有读随机分发到readHost</span></strong><strong><span style="color:#333300;">,</span></strong><strong><span style="color:#333300;">writeHost</span></strong><strong><span style="color:#333300;">不参与分发;</span></strong></p> </li> </ol></td> </tr> </tbody> </table> **我们只有一主一从时,修改为2,看是否随机读取到两台数据库服务器;** ![watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzE2MTE2NTQ5_size_16_color_FFFFFF_t_70][] # 双主双从: # ## **1./usr/local/mycat/conf/schema.xml** ## <table> <tbody> <tr> <td style="vertical-align:top;width:414.8pt;"> <p style="margin-left:0cm;"><strong><span style="color:#333300;"><mycat:schema xmlns:mycat="http://io.mycat/"></span></strong></p> <p style="margin-left:0cm;"> </p> <p style="margin-left:0cm;"><strong><span style="color:#333300;"> <schema name="TESTDB" checkSQLschema="true" sqlMaxLimit="100" dataNode="dn1"></span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#333300;"> <!-- auto sharding by id (long) --></span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#333300;"> <!--splitTableNames </span></strong><strong><span style="color:#333300;">启用</span></strong><strong><span style="color:#333300;"><table name </span></strong><strong><span style="color:#333300;">属性使用逗号分割配置多个表</span></strong><strong><span style="color:#333300;">,</span></strong><strong><span style="color:#333300;">即多个表使用这个配置</span></strong><strong><span style="color:#333300;">--></span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#333300;"> </schema></span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#333300;"> <!-- <dataNode name="dn1$0-743" dataHost="localhost1" database="db$0-743"</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#333300;"> /> --></span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#333300;"> <dataNode name="dn1" dataHost="localhost1" database="testdb" /></span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#333300;"> <dataHost name="localhost1" maxCon="1000" minCon="10" </span><span style="color:#f33b45;">balance="1"</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#333300;"> writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100"></span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#333300;"> <heartbeat>select user()</heartbeat></span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#333300;"> <!-- can have multi write hosts --></span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#333300;"> <writeHost host="hostM1" url="douzi9:3306" user="root"</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#333300;"> password="123456"></span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#333300;"> <readHost host="hostS1" url="douzi10:3306" user="root" password="123456" /></span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#333300;"> </writeHost></span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#333300;"> <writeHost host="hostM2" url="douzi11:3306" user="root"</span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#333300;"> password="123456"></span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#333300;"> <readHost host="hostS2" url="douzi12:3306" user="root" password="123456" /></span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#333300;"> </writeHost></span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#333300;"> </dataHost></span></strong></p> <p style="margin-left:0cm;"><strong><span style="color:#333300;"></mycat:schema></span></strong></p> </td> </tr> </tbody> </table> **Balance** **需要使用****1-** **全部的****readHost** **和** **stand by writeHost** **(****M1 -> S1, M2->S2** **双主双从时,** **M1** **为写,** **S1 S2 M2****都参与****select****的负载均衡)** **writeType = 0** **表示,****M1****挂了,****M2****自动变成写主机;** **switchType="1"** **表示做自动切换** **-1****表示不切换,****2****表示根据****mysql****主从状态切换** ## **2.验证读写查询哪台数据库服务器** ## **通过 mysql -uroot -p123456 -h douzi9 -P 8066****登录mycat****服务器命令窗:** **切换数据库:Use TESTDB;** <table> <tbody> <tr> <td style="vertical-align:top;width:414.8pt;"> <p style="margin-left:0cm;"><strong>Insert into mytbl(id,name) values(1,@@hostname);</strong></p> <p style="margin-left:0cm;"><strong>Select * from mytbl; </strong><strong>查询不到写主机M1 douzi9</strong></p> </td> </tr> </tbody> </table> ![watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzE2MTE2NTQ5_size_16_color_FFFFFF_t_70 1][] ## **3.抗风险验证** ## * **停止M1****的mysql****服务 systemctl stop mysqld** ![watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzE2MTE2NTQ5_size_16_color_FFFFFF_t_70 2][] * **继续mycat****插入数据** **Insert into mytbl(id,name) values(2,@@hostname);** ![watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzE2MTE2NTQ5_size_16_color_FFFFFF_t_70 3][] **依然成功** * **启动M1 systemctl start mysqld,****确认M2****变成写主机,M1****变为 stand by writeHost****。** ![watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzE2MTE2NTQ5_size_16_color_FFFFFF_t_70 4][] **查询不到M2 douzi11****,并且数据有少许延迟;** ![watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzE2MTE2NTQ5_size_16_color_FFFFFF_t_70 5][] [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzE2MTE2NTQ5_size_16_color_FFFFFF_t_70]: https://img-blog.csdnimg.cn/20200220214613572.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzE2MTE2NTQ5,size_16,color_FFFFFF,t_70 [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzE2MTE2NTQ5_size_16_color_FFFFFF_t_70 1]: https://img-blog.csdnimg.cn/2020022021470064.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzE2MTE2NTQ5,size_16,color_FFFFFF,t_70 [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzE2MTE2NTQ5_size_16_color_FFFFFF_t_70 2]: https://img-blog.csdnimg.cn/2020022021470079.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzE2MTE2NTQ5,size_16,color_FFFFFF,t_70 [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzE2MTE2NTQ5_size_16_color_FFFFFF_t_70 3]: https://img-blog.csdnimg.cn/2020022021470019.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzE2MTE2NTQ5,size_16,color_FFFFFF,t_70 [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzE2MTE2NTQ5_size_16_color_FFFFFF_t_70 4]: https://img-blog.csdnimg.cn/2020022021470094.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzE2MTE2NTQ5,size_16,color_FFFFFF,t_70 [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzE2MTE2NTQ5_size_16_color_FFFFFF_t_70 5]: https://img-blog.csdnimg.cn/2020022021470055.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzE2MTE2NTQ5,size_16,color_FFFFFF,t_70
还没有评论,来说两句吧...