溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么在MySQL中安裝Router

發(fā)布時(shí)間:2021-04-30 15:27:59 來源:億速云 閱讀:155 作者:Leah 欄目:開發(fā)技術(shù)

今天就跟大家聊聊有關(guān)怎么在MySQL中安裝Router,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

MySQL Router是什么?

    為了更好的理解Innodb Replicaset,我們把之前的那張圖搬遷過來,如下: 

怎么在MySQL中安裝Router

通過上面的圖,不難看出,MySQL Router是應(yīng)用程序連接的入口,它的存在,讓底層的架構(gòu)對(duì)應(yīng)用程序透明,應(yīng)用程序只需要連接MySQL Router就可以和底層的數(shù)據(jù)庫(kù)打交道,而數(shù)據(jù)庫(kù)的主從架構(gòu),都是記錄在MySQL Router的原信息里面的。

版本

    MySQL Router是MySQL官方的一款輕量級(jí)的高性能中間件,它介于應(yīng)用程序和MySQL Server之間,應(yīng)用程序通過連接MySQL Router和底層的MySQL Server之間通信,它對(duì)應(yīng)用程序是透明的。官方文檔建議搭配MySQL8.0或者M(jìn)ySQL5.7的Server使用。目前最新的版本是MySQL Router 8,如果您使用過MySQL Router的2.0或者2.1版本,強(qiáng)烈建議升級(jí)到MySQL Router 8。

MySQL Router特點(diǎn)?

1、對(duì)應(yīng)用透明。MySQL Router要做到對(duì)應(yīng)用透明,它必須連接底層的MySQL,并知道當(dāng)前哪個(gè)節(jié)點(diǎn)是Primary,這樣,才能夠在發(fā)生故障的時(shí)候進(jìn)行故障轉(zhuǎn)移。

2、使用場(chǎng)景?;谶@個(gè)特性,它可以用在Innodb Cluster、Innodb Replicaset或者M(jìn)GR的環(huán)境中。

3、MySQL Router會(huì)保留在線的MySQL實(shí)例的緩存列表,或者已經(jīng)配置好的Innodb Cluster集群的拓?fù)潢P(guān)系,除此啟動(dòng)的時(shí)候,這些信息將從MySQL Router的配置表中獲取。

4、為保證緩存中的元信息能夠得到即時(shí)更新,MySQL Router需要保證至少能夠和集群中的一個(gè)正常節(jié)點(diǎn)保持通信,它會(huì)從當(dāng)前實(shí)例的Performance_schema表中獲取數(shù)據(jù)庫(kù)的原信息和實(shí)時(shí)狀態(tài)。

5、當(dāng)集群中和MySQL Router通信的節(jié)點(diǎn)關(guān)閉時(shí),MySQL Router會(huì)嘗試訪問集群中的其他節(jié)點(diǎn)。并重新獲取相關(guān)元數(shù)據(jù)信息。

02 MySQL Router的安裝部署

      為了獲取更好的性能,通常情況下,MySQL  Router會(huì)和應(yīng)用程序部署在一起,這通常是基于下面的考慮:

1、可以通過socket套接字連接到MySQL Router,而不是tcp/ip方法

2、減少了網(wǎng)絡(luò)上的延時(shí)

3、可以配置指定的賬戶來訪問數(shù)據(jù)庫(kù),例如myapp@'host'而不是類似myapp@'%'這種全網(wǎng)段的賬號(hào),有利于提升安全性

4、通常,相比數(shù)據(jù)庫(kù)服務(wù)器,應(yīng)用服務(wù)器更容易擴(kuò)展。

官方給的部署架構(gòu)圖如下:

怎么在MySQL中安裝Router

下載安裝過程:

1、直接登錄官網(wǎng)下載MySQL Router的對(duì)應(yīng)版本。

https://downloads.mysql.com/archives/router/

2、下載完成之后,解壓,以8.0.20為例,由于是tar.xz格式的文件,解壓命令如下:

xz -d xxx.tar.xz  (解壓成tar格式)

tar xvf xxx.tar     (即可)

接下來就是初始化過程了,初始化的時(shí)候,需要我們的MySQL Server部署完畢,在之前的文章中,我們已經(jīng)部署好了一個(gè)Innodb Replicaset架構(gòu),它有一主一從,IP地址分別是:

192.168.1.10  5607 Primary

192.168.1.20  5607 Secondary

初始化過程:

1、利用初始化命令初始化MySQL Router:

mysqlrouter
   --bootstrap superdba@'10.13.3.129':5607 
   --directory /data1/yazhou5/mysql/mysql-router
   --conf-use-sockets 
   --account routerfriend 
   --account-create always

這里,需要解釋一下其中的幾個(gè)參數(shù):

--bootstrap          代表引導(dǎo)的實(shí)例,后面接一個(gè)連接信息的URL;
--directory          代表生成的配置目錄
--conf-use-sockets   代表是否啟用套接字連接(是否生成套接字文件)
--account            代表初始化后MySQL Router使用什么賬號(hào)連接MySQL Server
--account-create     代表賬號(hào)創(chuàng)建策略,always代表只有在account不存在的時(shí)候才進(jìn)行bootstrap操作

這個(gè)命令敲下去之后,返回報(bào)錯(cuò)信息如下:

Error: You are bootstraping as a superuser.
This will make all the result files (config etc.) privately owned by the superuser.
Please use --user=username option to specify the user that will be running the router.
Use --user=root if this really should be the superuser.

系統(tǒng)檢測(cè)到我們使用root賬號(hào)來進(jìn)行的MySQL Router初始化,提示我們?nèi)绻褂胷oot操作,需要在最后面補(bǔ)充--user=root

2、補(bǔ)充--user=root之后,重新執(zhí)行命令,結(jié)果如下:

[root mysql-router]# /usr/local/mysql-router-8.0.20/bin/mysqlrouter --bootstrap superdba@10.185.13.195:5607  --directory /data1/yazhou5/mysql/mysql-router --conf-use-sockets  --account routerfriend  --account-create always --user=root --force
Please enter MySQL password for superdba:   # 這里輸入我們已知的superdba賬號(hào)密碼
# Bootstrapping MySQL Router instance at '/data1/yazhou5/mysql/mysql-router'...

Please enter MySQL password for routerfriend: # 這里創(chuàng)建新的account賬號(hào)的密碼
- Creating account(s) 
- Verifying account (using it to run SQL queries that would be run by Router)
- Storing account in keyring
- Adjusting permissions of generated files
- Creating configuration /data1/yazhou5/mysql/mysql-router/mysqlrouter.conf

# MySQL Router configured for the InnoDB ReplicaSet 'yeyz_test'

After this MySQL Router has been started with the generated configuration

    $ /usr/local/mysql-router-8.0.20/bin/mysqlrouter -c /data1/yazhou5/mysql/mysql-router/mysqlrouter.conf

the cluster 'yeyz_test' can be reached by connecting to:

## MySQL Classic protocol

- Read/Write Connections: localhost:6446, /data1/yazhou5/mysql/mysql-router/mysql.sock
- Read/Only Connections:  localhost:6447, /data1/yazhou5/mysql/mysql-router/mysqlro.sock

## MySQL X protocol

- Read/Write Connections: localhost:64460, /data1/yazhou5/mysql/mysql-router/mysqlx.sock
- Read/Only Connections:  localhost:64470, /data1/yazhou5/mysql/mysql-router/mysqlxro.sock

可以看到,提示我們輸入兩次密碼之后,bootstrap的操作就算成功了。

3、此時(shí)我們進(jìn)入?yún)?shù)中指定的--directory目錄中,查看生成的初始化文件,可以看到:

drwx------ 2 root root 4096 Apr 12 23:15 data
drwx------ 2 root root 4096 Apr 12 23:15 log
-rw------- 1 root root 1532 Apr 12 23:15 mysqlrouter.conf
-rw------- 1 root root  104 Apr 12 23:15 mysqlrouter.key
drwx------ 2 root root 4096 Apr 12 23:15 run
-rwx------ 1 root root  353 Apr 12 23:15 start.sh
-rwx------ 1 root root  209 Apr 12 23:15 stop.sh

生成了一些配置文件和啟停腳本,我們打開這個(gè)配置文件mysqlrouter.conf看看內(nèi)容:

# File automatically generated during MySQL Router bootstrap
[DEFAULT]
user=root
logging_folder=/data1/yazhou5/mysql/mysql-router/log
runtime_folder=/data1/yazhou5/mysql/mysql-router/run
data_folder=/data1/yazhou5/mysql/mysql-router/data
keyring_path=/data1/yazhou5/mysql/mysql-router/data/keyring
master_key_path=/data1/yazhou5/mysql/mysql-router/mysqlrouter.key
connect_timeout=15
read_timeout=30
dynamic_state=/data1/yazhou5/mysql/mysql-router/data/state.json

[logger]
level = INFO

[metadata_cache:yeyz_test]
cluster_type=rs
router_id=1
user=routerfriend
metadata_cluster=yeyz_test
ttl=0.5
auth_cache_ttl=-1
auth_cache_refresh_interval=2

[routing:yeyz_test_rw]
bind_address=0.0.0.0
bind_port=6446
socket=/data1/yazhou5/mysql/mysql-router/mysql.sock
destinations=metadata-cache://yeyz_test/?role=PRIMARY
routing_strategy=first-available
protocol=classic

[routing:yeyz_test_ro]
bind_address=0.0.0.0
bind_port=6447
socket=/data1/yazhou5/mysql/mysql-router/mysqlro.sock
destinations=metadata-cache://yeyz_test/?role=SECONDARY
routing_strategy=round-robin-with-fallback
protocol=classic

[routing:yeyz_test_x_rw]
bind_address=0.0.0.0
bind_port=64460
socket=/data1/yazhou5/mysql/mysql-router/mysqlx.sock
destinations=metadata-cache://yeyz_test/?role=PRIMARY
routing_strategy=first-available
protocol=x

[routing:yeyz_test_x_ro]
bind_address=0.0.0.0
bind_port=64470
socket=/data1/yazhou5/mysql/mysql-router/mysqlxro.sock
destinations=metadata-cache://yeyz_test/?role=SECONDARY
routing_strategy=round-robin-with-fallback
protocol=x

4、分析生成的配置文件,不難發(fā)現(xiàn),MySQL Router配置了4個(gè)端口,分別是6446、6447、64460、64470和對(duì)應(yīng)的套接字文件。

當(dāng)然,我們可以通過一些參數(shù)的配置改變默認(rèn)的端口和套接字,例如:

--conf-use-sockets:(可選)為所有四種連接類型啟用UNIX域套接字,。
--conf-skip-tcp:   (可選)禁用TCP端口,如果只希望使用套接字,則可以通過--conf-use-sockets傳遞該選項(xiàng)。
--conf-base-port:  (可選)更改端口范圍,而不使用默認(rèn)端口。 默認(rèn)為6446。
--conf-bind-address:(可選)更改每個(gè)路由的bind_address值。

5、使用命令在本地啟動(dòng)MySQL Router,指定本地生成的配置文件,命令如下:

[root@ mysql-router]# /usr/local/mysql-router-8.0.20/bin/mysqlrouter -c /data1/yazhou5/mysql/mysql-router/mysqlrouter.conf &

啟動(dòng)之后,我們使用剛才創(chuàng)建的routerfriend賬號(hào)以及6446這個(gè)讀寫端口來連接MySQL Router:

[root@ mysql-router]# mysql -u routerfriend -h 127.0.0.1 -P 6446  -p 
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 95696
Server version: 8.0.19 MySQL Community Server - GPL

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

routerfriend@127.0.0.1 [(none)] 23:42:00>
routerfriend@127.0.0.1 [(none)] 23:42:01>select @@port;
+--------+
| @@port |
+--------+
|   5607 |
+--------+
1 row in set (0.00 sec)

我們通過6446端口連接MySQL Router之后,然后在MySQL Router中執(zhí)行select @@port命令查看當(dāng)前的端口號(hào)信息,可以看到,返回值是5607,說明MySQL Router已經(jīng)幫我們路由到了底層的MySQL Server上面。

   這個(gè)routerfriend賬號(hào)的權(quán)限可能不夠,我們也可以換成superdba的高權(quán)限賬號(hào)去連接mysqlrouter,這樣就可以對(duì)MySQL Server中的庫(kù)表進(jìn)行讀寫操作。

03 查看MySQL Router的元信息

    MySQL Router搭建完畢后,可以通過查看元信息庫(kù)mysql_innodb_cluster_metadata里面的表信息,包含cluster表、router表、以及instances表,對(duì)應(yīng)的如下:

superdba@127.0.0.1 [mysql_innodb_cluster_metadata] 23:51:20>select * from instances;
+-------------+--------------------------------------+--------------------+--------------------------------------+--------------------+----------------------------------------+------------+-------------+
| instance_id | cluster_id                           | address            | mysql_server_uuid                    | instance_name      | addresses                              | attributes | description |
+-------------+--------------------------------------+--------------------+--------------------------------------+--------------------+----------------------------------------+------------+-------------+
|           1 | 94d5f935-990e-11eb-8832-fa163ebd2444 | 192.168.1.10:5607 | 0609f966-690f-11eb-bd89-fa163ebd2444 | 192.168.1.10:5607 | {"mysqlClassic": "192.168.1.10:5607"} | {}         | NULL        |
|           2 | 94d5f935-990e-11eb-8832-fa163ebd2444 | 192.168.1.20:5607  | c6ba0bf0-6d4d-11eb-aa4b-b00875209c1c | 192.168.1.20:5607  | {"mysqlClassic": "192.168.1.20:5607"}  | {}         | NULL        |
+-------------+--------------------------------------+--------------------+--------------------------------------+--------------------+----------------------------------------+------------+-------------+
2 rows in set (0.00 sec)

superdba@127.0.0.1 [mysql_innodb_cluster_metadata] 23:51:30>
superdba@127.0.0.1 [mysql_innodb_cluster_metadata] 23:51:30>select * from clusters;
+--------------------------------------+--------------+--------------------+---------+------------------------------------------------+--------------+--------------+----------------+
| cluster_id                           | cluster_name | description        | options | attributes                                     | cluster_type | primary_mode | router_options |
+--------------------------------------+--------------+--------------------+---------+------------------------------------------------+--------------+--------------+----------------+
| 94d5f935-990e-11eb-8832-fa163ebd2444 | yeyz_test    | Default ReplicaSet | NULL    | {"adopted": 0, "opt_gtidSetIsComplete": false} | ar           | pm           | NULL           |
+--------------------------------------+--------------+--------------------+---------+------------------------------------------------+--------------+--------------+----------------+
1 row in set (0.00 sec)

superdba@127.0.0.1 [mysql_innodb_cluster_metadata] 23:51:57>
superdba@127.0.0.1 [mysql_innodb_cluster_metadata] 23:51:58>select * from routers;
+-----------+-------------+--------------+-------------+---------+---------------------+------------------------------------------------------------------------------------------------------------------------------+--------------------------------------+---------+
| router_id | router_name | product_name | address     | version | last_check_in       | attributes                                                                                                                   | cluster_id                           | options |
+-----------+-------------+--------------+-------------+---------+---------------------+------------------------------------------------------------------------------------------------------------------------------+--------------------------------------+---------+
|         1 |             | MySQL Router | 10.13.3.129 | 8.0.20  | 2021-04-12 23:52:29 | {"ROEndpoint": "6447", "RWEndpoint": "6446", "ROXEndpoint": "64470", "RWXEndpoint": "64460", "MetadataUser": "routerfriend"} | 94d5f935-990e-11eb-8832-fa163ebd2444 | NULL    |
+-----------+-------------+--------------+-------------+---------+---------------------+------------------------------------------------------------------------------------------------------------------------------+--------------------------------------+---------+
1 row in set (0.00 sec)

還可以從表中查看當(dāng)前的primary節(jié)點(diǎn)信息,primary_master字段為1的,即為primary節(jié)點(diǎn)。

superdba@127.0.0.1 [mysql_innodb_cluster_metadata] 23:52:29>select * from async_cluster_members;
+--------------------------------------+---------+-------------+--------------------+----------------+------------------------------------------------------------------------------------------------------------------+
| cluster_id                           | view_id | instance_id | master_instance_id | primary_master | attributes                                                                                                       |
+--------------------------------------+---------+-------------+--------------------+----------------+------------------------------------------------------------------------------------------------------------------+
| 94d5f935-990e-11eb-8832-fa163ebd2444 |       2 |           1 |               NULL |              1 | {"instance.address": "192.168.1.10:5607", "instance.mysql_server_uuid": "0609f966-690f-11eb-bd89-fa163ebd2444"} |
| 94d5f935-990e-11eb-8832-fa163ebd2444 |       3 |           1 |               NULL |              1 | {"instance.address": "192.168.1.10:5607", "instance.mysql_server_uuid": "0609f966-690f-11eb-bd89-fa163ebd2444"} |
| 94d5f935-990e-11eb-8832-fa163ebd2444 |       3 |           2 |                  1 |              0 | {"instance.address": "192.168.1.20:5607", "instance.mysql_server_uuid": "c6ba0bf0-6d4d-11eb-aa4b-b00875209c1c"}  |
+--------------------------------------+---------+-------------+--------------------+----------------+------------------------------------------------------------------------------------------------------------------+
3 rows in set (0.01 sec)

看完上述內(nèi)容,你們對(duì)怎么在MySQL中安裝Router有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI