1. 程式人生 > >Mysql 高可用 InnoDB Cluster 多節點搭建過程

Mysql 高可用 InnoDB Cluster 多節點搭建過程

1. 簡介

InnoDB Cluster 的搭建可以分為兩種情況:

(1)實驗環境

使用 sandbox 沙箱模擬資料庫例項,這個非常簡單,可以參考之前的一篇文章《體驗 MySQL InnoDB Cluster》,也可以看 mysql 的官方文件,其中就是使用 sandbox 來介紹搭建過程的

(2)真實多伺服器節點環境

真實環境下的搭建要更復雜一些,過程中我也遇到了一些問題,這方面的資料還很少,花費了不少時間才搭建成功

下面總結了多節點 InnoDB Cluster 搭建的詳細過程,供有需要的朋友參考

2. 目標

準備4臺伺服器,node01、node02、node03 作為 cluster 節點,node04 作為管理節點,負責建立 cluster,並作為 cluster 的路由

最後,會搭建出一個高可用叢集,通過 router 連線到這個cluster,MySQL客戶端通過 router 與 cluster 進行溝通

3. 搭建思路

(1)安裝基礎環境

node 01、02、03 上安裝好 mysql 與 mysql-shell

node04 上安裝 mysql-shellmysql-router

(2)建立叢集

在 node01 上建立叢集,先配置好其 mysql 並啟動,然後通過 node01 上的 shell 連線 node01 的 mysql,執行配置命令

dba.configureLocalInstance();

使其具備建立叢集的條件

最後通過 node04 的 shell 連線 node01 的 mysql,執行建立叢集的命令 

dba.createCluster()

(3)向叢集中新增節點

叢集建立起來後,接下來就是向其中新增節點

配置 node02、node03 的 mysql 並啟動,然後使用各自的 mysql-shell 對其進行配置

最後通過 node04 的 mysql-shell 執行新增例項的命令 dba.addInstance() 把 node02,node03 新增到叢集中

(4)使用 router 連線叢集

叢集搭建完成後,把 node04 的 router 啟動起來,並連線到叢集

client 就可以連線到 router,通過其操作叢集了

4. 具體搭建過程

(1)環境安裝

在各臺伺服器中配置好 hosts,如:

192.168.31.13 node03 
192.168.31.228 node02 
192.168.31.36 node01 

需要準備的軟體:

  1. mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz

  2. mysql-shell-1.0.9-linux-glibc2.12-x86-64bit.tar.gz

  3. mysql-router-2.1.3-linux-glibc2.12-x86-64bit.tar.gz

根據上面的結構圖,在各個伺服器中安裝好所需的軟體

安裝方法:

1. MySQL

# 解壓
tar zxf mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz
mv mysql-5.7.17-linux-glibc2.5-x86_64 /usr/local/mysql-5.7
cd /usr/local

# 初始化資料庫例項
mkdir data
mysql-5.7/bin/mysqld --initialize-insecure --basedir=$PWD/mysql-5.7 --datadir=$PWD/data

# 建立mysql使用者
groupadd mysql5.7
useradd -g mysql5.7 mysql5.7
chown -R mysql5.7:mysql5.7 /usr/local/mysql-5.7
chown -R mysql5.7:mysql5.7 /usr/local/data

2. shell

# 直接解壓即可
tar zxf mysql-shell-1.0.9-linux-glibc2.12-x86-64bit.tar.gz mysql-shell

3. router

# 直接解壓即可
tar zxf mysql-router-2.1.3-linux-glibc2.12-x86-64bit.tar.gz mysql-router

(2)建立叢集

  • 配置 node01 的 mysql 並啟動

切換到mysql使用者

su mysql5.7

編輯配置檔案 vi /user/local/data/my.cnf,內容:

[mysqld]

# server configuration
datadir=/usr/local/data
basedir=/usr/local/mysql-5.7/

port=3306
socket=/usr/local/data/mysql.sock

server_id=1
gtid_mode=ON
enforce_gtid_consistency=ON
master_info_repository=TABLE
relay_log_info_repository=TABLE
binlog_checksum=NONE
log_slave_updates=ON
log_bin=binlog
binlog_format=ROW

transaction_write_set_extraction=XXHASH64

啟動

nohup /usr/local/mysql-5.7/bin/mysqld --defaults-file=data/my.cnf >data/nohup.out 2>&1 &

# 退回root使用者
exit

登入 MySQL

/usr/local/mysql-5.7/bin/mysql -uroot -h127.0.0.1 --skip-password

修改root預設密碼

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'A123456';
  • 通過本機 mysql-shell 對 mysql 進行配置

進到 mysql-shell 的安裝目錄,登入 shell ,執行配置

bin/mysqlsh

連線到本機MySQL,執行配置命令

# 連線,需要輸入密碼(A123456)
mysql-js> shell.connect('[email protected]:3306');

# 執行配置命令,也需要密碼
# 然後需要輸入MySQL配置檔案路徑,本示例中的路徑是 /usr/local/data/s1/s1.cnf
# 接下來需要建立供其他主機訪問的使用者,這裡選擇第1項,為root使用者授權

mysql-js> dba.configureLocalInstance();
Please provide the password for '[email protected]:3306':

Detecting the configuration file...
Default file not found at the standard locations.
Please specify the path to the MySQL configuration file: /usr/local/data/s1/s1.cnf
MySQL user 'root' cannot be verified to have access to other hosts in the network.

1) Create [email protected]% with necessary grants
2) Create account with different name
3) Continue without creating account
4) Cancel
Please select an option [1]: 1
Password for new account:
Confirm password:
Validating instance...

The instance 'localhost:3306' is valid for Cluster usage
You can now use it in an InnoDB Cluster.

{
    "status": "ok"
}

status 為 ok 說明配置沒問題了,可以用來建立cluster

  • 通過 node04 的 mysql-shell 連線 node01 建立 cluster

進入 node04 上的 mysql-shell 安裝目錄,登入 shell,連線 node01,建立 cluster

bin/mysqlsh 

# 連線01
mysql-js> shell.connect('[email protected]:3306');

# 建立一個 cluster,命名為 'myCluster'
mysql-js> var cluster = dba.createCluster('myCluster');

# 建立成功後,檢視cluster狀態
mysql-js> cluster.status();

(3)新增例項節點 node02

  • 配置 node02 的 mysql 並啟動

編輯配置檔案 vi /usr/local/data/my.cnf,內容與 node01 上的一樣,只有一行不同

server_id=2
  • 通過本機 mysql-shell 對 mysql 進行配置

登入 shell ,執行配置

bin/mysqlsh 

mysql-js> shell.connect('[email protected]:3306');
mysql-js> dba.configureLocalInstance();
  • 停掉 mysql,修改 my.cnf,新增配置項

vi data/my.cnf

# 在末尾新增
group_replication_allow_local_disjoint_gtids_join=ON

重啟MySQL

  • 通過 node04 的 mysql-shell 新增 node02 到 cluster

# 新增例項
cluster.addInstance('[email protected]:3306');

# 建立成功後,檢視cluster狀態
mysql-js> cluster.status();

(4)新增例項節點 node03

過程與 node02 完全相同,只需要注意 my.cnf 中的 'server_id' 值改為 3,和 addInstance 時改為 node03

(5)安裝 router

進入 node04 中 mysql-router 安裝目錄,啟動 router 

mysqlrouter --bootstrap [email protected]:3306 --directory myrouter --name=myrouter
myrouter/start.sh

然後就可以使用MySQL客戶端連線router了

5. 小結

mysql 下載地址:

https://dev.mysql.com/downloads/mysql/

mysql-shell 下載地址:

https://dev.mysql.com/downloads/shell/

mysql-router 下載地址:

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

都選擇 Linux Generic 版本即可