你們要的MyCat實現MySQL分庫分表來了

?

借助MyCat來實現mysql的分庫分表落地,沒有實現過的,或者沒了解過的可以看看

?

前言

在之前寫過一篇關于mysql分庫分表的文章,那篇文章只是給大家提供了一個思路,但是回復下面有很多說是細節問題沒有提到。所以咔咔就在出了這篇文章。

本文只是針對其中的一個細節而已,比如如何落地MySQL的分庫分表,本文使用數據庫中間件MyCat來實現。

其中還有最多的回復是分庫分表后的分頁查詢如何操作,這個問題也會在后文繼續寫到。

關于MySQL分庫分表這個真正落地使用中間還有很多的問題,咔咔盡量把解決方案都提供給大家,如你有更好的實現方案,評論區見哈!

本文使用環境

  • MySQL8.0
  • Centos7.3

一、MyCat安裝

避免提示廣告問題,自己去打開MyCat的官方地址去復制下載地址,這里咔咔就不提供了。

咔咔使用的虛擬機是centos7.3,需要環境的jdk已經有了,就不在寫教程了。如沒有的話,百度搜搜就可以了哈!

下圖就是安裝成功的安裝包你們要的MyCat實現MySQL分庫分表來了進行解壓:tar -zxvf Mycat-server-1.6.7.1-release-20200209222254-linux.tar.gz,解壓成功后會在目錄下存在一個mycat目錄。

二、創建一個新的MySQL用戶

需要創建一個新的MySQL用戶用來連接Mycat

以下就是創建用戶的流程

<span style="display: block; background: url(https://my-wechat.mdnice.com/point.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #272822; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span>//?創建mycat用戶<br>CREATE?USER?<span class="hljs-string" style="color: #a6e22e; line-height: 26px;">'mycat'</span>@<span class="hljs-string" style="color: #a6e22e; line-height: 26px;">'%'</span>?IDENTIFIED?BY?<span class="hljs-string" style="color: #a6e22e; line-height: 26px;">'mycat'</span>;<br>//?修改密碼<br>ALTER?USER?<span class="hljs-string" style="color: #a6e22e; line-height: 26px;">'mycat'</span>@<span class="hljs-string" style="color: #a6e22e; line-height: 26px;">'%'</span>?IDENTIFIED?WITH?mysql_native_password?BY?’Fang,1996<span class="hljs-string" style="color: #a6e22e; line-height: 26px;">';?<br>//?刷新權限<br>FLUSH?PRIVILEGES;<br></span>

三、配置MyCat

配置項一:server.xml

此處使用上邊創建的新的MySQL用戶mycat,可以管理的邏輯庫為mycat_order,對應schema.xml中的

框出的來的解讀一下

第一行:name值后邊是上邊創建的MySQL用戶 第二行:是mycat用戶的密碼 第三行:是數據庫

你們要的MyCat實現MySQL分庫分表來了配置項二:schema.xml

這個文件主要修改連接其他數據庫的倆個節點

使用規則是mod-long這個需要注意一下子

在這塊的配置咔咔卡了很久,下面這個是咔咔已經配置好的你們要的MyCat實現MySQL分庫分表來了

配置項三:rule.xml

這里是order_id使用mod-long規則你們要的MyCat實現MySQL分庫分表來了這個修改就是你有幾個節點就寫多少即可你們要的MyCat實現MySQL分庫分表來了

三、數據庫信息準備

在倆臺服務器114.55.103.25和192.168.253.129創建數據庫kaka和kaka

分別創建t_order和t_order_detail倆張表

表結構如下

<span style="display: block; background: url(https://my-wechat.mdnice.com/point.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #272822; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">CREATE</span>?<span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">TABLE</span>?<span class="hljs-string" style="color: #a6e22e; line-height: 26px;">`t_order_detail`</span>?(<br>??<span class="hljs-string" style="color: #a6e22e; line-height: 26px;">`od_id`</span>?<span class="hljs-built_in" style="color: #a6e22e; line-height: 26px;">int</span>(<span class="hljs-number" style="line-height: 26px;">11</span>)?<span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">NOT</span>?<span class="hljs-literal" style="color: #f92672; font-weight: bold; line-height: 26px;">NULL</span>,<br>??<span class="hljs-string" style="color: #a6e22e; line-height: 26px;">`order_id`</span>?<span class="hljs-built_in" style="color: #a6e22e; line-height: 26px;">int</span>(<span class="hljs-number" style="line-height: 26px;">11</span>)?<span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">default</span>?<span class="hljs-literal" style="color: #f92672; font-weight: bold; line-height: 26px;">NULL</span>,<br>??<span class="hljs-string" style="color: #a6e22e; line-height: 26px;">`goods_id`</span>?<span class="hljs-built_in" style="color: #a6e22e; line-height: 26px;">int</span>(<span class="hljs-number" style="line-height: 26px;">11</span>)?<span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">DEFAULT</span>?<span class="hljs-literal" style="color: #f92672; font-weight: bold; line-height: 26px;">NULL</span>,<br>??<span class="hljs-string" style="color: #a6e22e; line-height: 26px;">`unit_price`</span>?<span class="hljs-built_in" style="color: #a6e22e; line-height: 26px;">float</span>?<span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">DEFAULT</span>?<span class="hljs-literal" style="color: #f92672; font-weight: bold; line-height: 26px;">NULL</span>,<br>??<span class="hljs-string" style="color: #a6e22e; line-height: 26px;">`qty`</span>?<span class="hljs-built_in" style="color: #a6e22e; line-height: 26px;">int</span>(<span class="hljs-number" style="line-height: 26px;">11</span>)?<span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">DEFAULT</span>?<span class="hljs-literal" style="color: #f92672; font-weight: bold; line-height: 26px;">NULL</span>,<br>??PRIMARY?<span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">KEY</span>?(<span class="hljs-string" style="color: #a6e22e; line-height: 26px;">`od_id`</span>)<br>)?<span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">ENGINE</span>=<span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">InnoDB</span>?<span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">DEFAULT</span>?<span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">CHARSET</span>=utf8;<br><br><span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">CREATE</span>?<span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">TABLE</span>?<span class="hljs-string" style="color: #a6e22e; line-height: 26px;">`t_order`</span>?(<br>??<span class="hljs-string" style="color: #a6e22e; line-height: 26px;">`order_id`</span>?bagint(<span class="hljs-number" style="line-height: 26px;">20</span>)?<span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">NOT</span>?<span class="hljs-literal" style="color: #f92672; font-weight: bold; line-height: 26px;">NULL</span>,<br>??<span class="hljs-string" style="color: #a6e22e; line-height: 26px;">`user_id`</span>?<span class="hljs-built_in" style="color: #a6e22e; line-height: 26px;">int</span>(<span class="hljs-number" style="line-height: 26px;">11</span>)?<span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">DEFAULT</span>?<span class="hljs-literal" style="color: #f92672; font-weight: bold; line-height: 26px;">NULL</span>,<br>??<span class="hljs-string" style="color: #a6e22e; line-height: 26px;">`pay_mode`</span>?<span class="hljs-built_in" style="color: #a6e22e; line-height: 26px;">tinyint</span>(<span class="hljs-number" style="line-height: 26px;">4</span>)?<span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">DEFAULT</span>?<span class="hljs-literal" style="color: #f92672; font-weight: bold; line-height: 26px;">NULL</span>,<br>??<span class="hljs-string" style="color: #a6e22e; line-height: 26px;">`amount`</span>?<span class="hljs-built_in" style="color: #a6e22e; line-height: 26px;">float</span>?<span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">DEFAULT</span>?<span class="hljs-literal" style="color: #f92672; font-weight: bold; line-height: 26px;">NULL</span>,<br>??<span class="hljs-string" style="color: #a6e22e; line-height: 26px;">`order_date`</span>?datetime?<span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">DEFAULT</span>?<span class="hljs-literal" style="color: #f92672; font-weight: bold; line-height: 26px;">NULL</span>,<br>??PRIMARY?<span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">KEY</span>?(<span class="hljs-string" style="color: #a6e22e; line-height: 26px;">`order_id`</span>)<br>)?<span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">ENGINE</span>=<span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">InnoDB</span>?<span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">DEFAULT</span>?<span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">CHARSET</span>=utf8;<br>

四、測試連接并插入數據,查看數據分布狀態

連接:mysql -umycat -p -P8066 -h192.168.253.129 –default-auth=mysql_native_password

連接成功就可以看到我們的邏輯庫你們要的MyCat實現MySQL分庫分表來了然后添加三個數據

<span style="display: block; background: url(https://my-wechat.mdnice.com/point.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #272822; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">insert</span>?<span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">into</span>?t_order?(order_id,user_id,pay_mode,amount)?<span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">values</span>?(<span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">next</span>?<span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">value</span>?<span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">for</span>?MYCATSEQ_ORDER,<span class="hljs-number" style="line-height: 26px;">103</span>,<span class="hljs-number" style="line-height: 26px;">1</span><br><span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">Query</span>?OK,?<span class="hljs-number" style="line-height: 26px;">1</span>?<span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">row</span>?affected?(<span class="hljs-number" style="line-height: 26px;">0.01</span>?sec)<br><br><span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">insert</span>?<span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">into</span>?t_order?(order_id,user_id,pay_mode,amount)?<span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">values</span>?(<span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">next</span>?<span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">value</span>?<span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">for</span>?MYCATSEQ_ORDER,<span class="hljs-number" style="line-height: 26px;">103</span>,<span class="hljs-number" style="line-height: 26px;">1</span><br><span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">Query</span>?OK,?<span class="hljs-number" style="line-height: 26px;">1</span>?<span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">row</span>?affected?(<span class="hljs-number" style="line-height: 26px;">0.01</span>?sec)<br><br><span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">insert</span>?<span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">into</span>?t_order?(order_id,user_id,pay_mode,amount)?<span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">values</span>?(<span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">next</span>?<span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">value</span>?<span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">for</span>?MYCATSEQ_ORDER,<span class="hljs-number" style="line-height: 26px;">103</span>,<span class="hljs-number" style="line-height: 26px;">1</span><br><span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">Query</span>?OK,?<span class="hljs-number" style="line-height: 26px;">1</span>?<span class="hljs-keyword" style="color: #f92672; font-weight: bold; line-height: 26px;">row</span>?affected?(<span class="hljs-number" style="line-height: 26px;">0.01</span>?sec)<br>

這個時候我們查看一下邏輯庫的t_order數據,這里的order_id本應該是從1000開始的,之前咔咔做測試使用了一些。你們要的MyCat實現MySQL分庫分表來了這時我們在來看192.168.253.129和114.55.103.25這倆臺數據庫的數據分布

「192.168.253.129數據庫」這個可以看到進入了倆條數據

你們要的MyCat實現MySQL分庫分表來了「144.55.103.25數據庫」

這臺數據庫只進入了一條數據你們要的MyCat實現MySQL分庫分表來了

五、分頁查詢,需要的數據在不同表的查詢

其實這一切mycat都已經幫你做好了

比如現在user_id為103的數據分布分倆臺數據庫上,我們進行查詢

你們要的MyCat實現MySQL分庫分表來了還有分頁問題,都跟平時一樣的你們要的MyCat實現MySQL分庫分表來了

六、總結

  • 數據庫使用MySQL8.0需要注意密碼問題
  • 在配置schema.xml時需要注意
  • 配置server.xml時邏輯庫在user用戶雖然不需要但是也需要改為一致
  • 修改MySQL用戶的host為%
  • 密碼驗證規則設置為mysql_native_password
  • 連接mycat時一定要帶著mysql_native_password這個參數

本文使用的mycat也是咔咔第一次接觸,也有很多的東西不明白。后邊咔咔也會一點點的補充這方面的知識,希望大家一起共同進步。

?

堅持學習、堅持寫博、堅持分享是咔咔從業以來一直所秉持的信念。希望在諾大互聯網中咔咔的文章能帶給你一絲絲幫助。

?

? 版權聲明
THE END
喜歡就支持一下吧
點贊7 分享