作者:冰河
星球:http://m6z.cn/6aeFbs
博客:https://binghe.gitcode.host
文章匯總:https://binghe.gitcode.host/md/all/all.html
星球項目地址:https://binghe.gitcode.host/md/zsxq/introduce.html
沉淀,成長,突破,幫助他人,成就自我。
- 本章難度:★★☆☆☆
- 本章重點:介紹Mycat在使用過程中常見的問題,讓大家少走彎路,在使用Mycat的過程中盡快規避這些問題。
大家好,我是冰河~~
今天給大家介紹《Mycat核心技術》的第06章:給大家簡單介紹下處理真實場景使用Mycat的問題的一點小小的總結,好了,開始今天的內容。
一、報錯1
mysql> INSERT INTO t_order(ID,SN,CREATE_TIME) VALUES(1,'2BJ0001001',NOW());ERROR 1064 (HY000): For input string: "2BJ0001001"
mysql>
報錯是因為分片字段是str字符串,所以需要修改分片規則1中type從0改成1,0是數字型分片,1是字符串分片。
二、報錯2
mysql> INSERT INTO t_order_detail(ID,ORDER_ID,ORD_STATUS,ADDRESS,CREATE_TIME) VALUES (1,1,'1','test data of ORDER1(ID=1,2BJ0001001) ',NOW());ERROR 1064 (HY000): can't find (root) parent sharding node for sql:INSERT INTO t_order_detail(ID,ORDER_ID,ORD_STATUS,ADDRESS,CREATE_TIME) VALUES (1,1,'1','test data of ORDER1(ID=1,2BJ0001001) ',NOW())
mysql>
解決方案1: 此類ER表的插入操作不能作為一個事務進行數據提交,如果父子表在一個事務中進行提交,顯然在事務沒有提交前子表是無法查詢附表的數據的,因此就無法確定分片節點,如果是ER關系的表在插入數據時不能再同一個事務中提交數據,需要分開提交。
解決方案2: 上面的方案2個事務搞不定,所以查看后臺Mycat日志
08/13 10:52:09.378 DEBUG [$_NIOREACTOR-3-RW] (ServerQueryHandler.java:56) -ServerConnection [id=1, schema=TESTDB, host=127.0.0.1, user=test,txIsolation=3, autocommit=true, schema=TESTDB]INSERT INTO t_order_detail(ID,ORDER_ID,ORD_STATUS,ADDRESS,CREATE_TIME) VALUES (1,1,'1','test data of ORDER1(ID=1,2BJ0001001) ',NOW())
08/13 10:52:09.506 DEBUG [$_NIOREACTOR-3-RW] (RouterUtil.java:1188) -find root parent's node sql select t_order.id from t_order where t_order.id=1
08/13 10:52:09.513 DEBUG [BusinessExecutor5] (EnchachePool.java:76) -ER_SQL2PARENTID miss cache ,key:TESTDB:select t_order.id from t_order where t_order.id=1
08/13 10:52:09.513 DEBUG [BusinessExecutor5] (FetchStoreNodeOfChildTableHandler.java:73) -find child node with sql:select t_order.id from t_order where t_order.id=1
08/13 10:52:09.514 DEBUG [BusinessExecutor5] (FetchStoreNodeOfChildTableHandler.java:81) -execute in datanode dn21
08/13 10:52:09.514 DEBUG [BusinessExecutor5] (PhysicalDBPool.java:452) -select read source hostM2 for dataHost:m1
08/13 10:52:09.535 WARN [$_NIOREACTOR-3-RW] (FetchStoreNodeOfChildTableHandler.java:135) -errorResponse 1146 Table 'db3.t_order' doesn't exist
08/13 10:52:09.535 DEBUG [$_NIOREACTOR-3-RW] (PhysicalDatasource.java:403) -release channel MySQLConnection [id=3, lastTime=1455258729497, user=root, schema=db3, old shema=db3, borrowed=true, fromSlaveDB=false, threadId=290, charset=latin1, txIsolation=0, autocommit=true, attachment=null, respHandler=null, host=192.168.209.137, port=3317, statusSync=null, writeQueue=0, modifiedSQLExecuted=false]
08/13 10:52:09.715 DEBUG [BusinessExecutor5] (FetchStoreNodeOfChildTableHandler.java:81) -execute in datanode dn22
08/13 10:52:09.715 DEBUG [BusinessExecutor5] (PhysicalDBPool.java:452) -select read source hostM2 for dataHost:m2
08/13 10:52:09.716 WARN [$_NIOREACTOR-1-RW] (FetchStoreNodeOfChildTableHandler.java:135) -errorResponse 1146 Table 'db3.t_order' doesn't exist
后臺執行sql確實找不到
mysql> select t_order.id from t_order where t_order.id=1;ERROR 1105 (HY000): Table 'db3.t_order' doesn't exist
mysql
原因是2個datanode是互為主從的關系,dn1和dn2是mm結構,然后停止mm結構,2個都是單獨的MySQL,然后重建t_order和t_order_detail表,再執行insert操作,問題解決了。
三、報錯4
mysql> INSERT INTO t_order_detail(ID,ORDER_ID,ORD_STATUS,ADDRESS,CREATE_TIME) VALUES (1,1,'1','test data of t_order(ID=1,2BJ0001001) ',NOW());ERROR 1064 (HY000): can't find (root) parent sharding node for sql:INSERT INTO t_order_detail(ID,ORDER_ID,ORD_STATUS,ADDRESS,CREATE_TIME) VALUES (1,1,'1','test data of t_order(ID=1,2BJ0001001) ',NOW())
mysql>
原因:表名字大小寫的問題導致
解決方案:
打開MySQL的my.cnf配置文件,在[mysqld]節點下加上如下配置:
[mysqld]
lower_case_table_names = 1
問題解決。
四、報錯4
Mycat啟動后,不能進行任何數據庫的操作,報Unknown charsetIndex:224錯誤。
從錯誤看是因為字符集問題引起的 ,因為我MYSQL服務器默認使用的是utf8mb4,所以修改Mycat字符集的配置文件。
vim index_to_charset.properties
在配置文件最后加入224=utf8mb4,重啟服務即正常。
好了,今天就到這兒吧,我是冰河,我們下期見~~