連線路由外掛程式執行基於連線的路由,這表示它會將封包轉送到伺服器,而不會檢查它們。這是一種簡單的方法,可提供高輸送量。有關連線路由的其他一般資訊,請參閱第 1.3 節,「連線路由」。
以下顯示簡單的基於連線的路由設定。這些和其他選項記錄在第 4.3.3 節,「設定檔選項」中。
[logger]
level = INFO
[routing:secondary]
bind_address = localhost
bind_port = 7001
destinations = foo.example.org:3306,bar.example.org:3306,baz.example.org:3306
routing_strategy = round-robin
[routing:primary]
bind_address = localhost
bind_port = 7002
destinations = foo.example.org:3306,bar.example.org:3306
routing_strategy = first-available
在這裡,我們使用連線路由,透過 round-robin routing_strategy
定義的方式,將 MySQL 連線以循環方式傳送到連接埠 7001 上的三個 MySQL 伺服器。此範例也使用連接埠 7002 為兩個伺服器設定 first-available 策略。first-available 策略使用目的地清單中的第一個可用伺服器。destinations
中指派的 MySQL 執行個體數量取決於您,因為這只是一個範例。Router 不會檢查封包,也不會根據路由策略限制連線,因此應用程式必須決定將讀取和寫入請求傳送到何處,在我們的範例中,這可以是連接埠 7001 或 7002。
假設所有三個 MySQL 執行個體都在執行中,接下來傳入設定檔來啟動 MySQL Router
$> ./bin/mysqlrouter -config=/etc/mysqlrouter-config.conf
現在,MySQL Router 會監聽連接埠 7001 和 7002,並將請求傳送到適當的 MySQL 執行個體。例如
$> ./bin/mysql --user=root --port 7001 --protocol=TCP
這將首先連線到 foo.example.org,然後是 bar.example.org,接著是 baz.example.org,第四次呼叫會回到 foo.example.org。相反地,我們以不同的方式設定連接埠 7002 的行為
$> ./bin/mysql --user=root --port 7002 --protocol=TCP
這將首先連線到 foo.example.org,其他請求將繼續連線到 foo.example.org,直到發生故障,此時會使用 bar.example.org。有關此行為的其他資訊,請參閱routing_strategy
。