【實驗】 2臺虛擬機(ubuntu)搭建分佈式數據庫集羣環境(二)

5 配置並運行MySQL Server 和 Client

標準的MySQL server不支持 MySQL Cluster 引擎 NDB. 這意味着我們需要安裝含有定製的SQL服務器 MySQL Cluster軟件.
進入包含MySQL Cluster組件的目錄:

cd install

在安裝MySQL server 前,需要安裝兩個依賴庫(如果已經安裝過可忽略):

sudo apt update
sudo apt install libaio1 libmecab2

然後安裝解壓在install目錄的軟件包中的一些MySQL Cluster 依賴包:

sudo dpkg -i mysql-common_8.0.19-1ubuntu16.04_amd64.deb

sudo dpkg -i mysql-cluster-community-client-core_8.0.19-1ubuntu16.04_amd64.deb

sudo dpkg -i mysql-cluster-community-client_8.0.19-1ubuntu16.04_amd64.deb

sudo dpkg -i mysql-client_8.0.19-1ubuntu16.04_amd64.deb

sudo dpkg -i mysql-cluster-community-server-core_8.0.19-1ubuntu16.04_amd64.deb

sudo dpkg -i mysql-cluster-community-server_8.0.19-1ubuntu16.04_amd64.deb

當安裝mysql-cluster-community-server時,會出現配置提示,請求爲mysql數據庫root用戶設置密碼。請選擇一個強安全密碼然後敲 . 提示時重新輸入這個root 密碼,再次敲 ,完成安裝. (實驗爲了避免遺忘,可以:123456)

在這裏插入圖片描述

然後,安裝MySQL server :

sudo dpkg -i mysql-server_8.0.19-1ubuntu16.04_amd64.deb

配置MySQL server.
MySQL Server 配置文件默認爲 /etc/mysql/my.cnf.
打開這個配置文件:

sudo vim /etc/mysql/my.cnf

可以看到下列文本:
/etc/mysql/my.cnf

# Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

#
# The MySQL Cluster Community Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

# * IMPORTANT: Additional settings that can override those from this file!
#   The files must end with '.cnf', otherwise they'll be ignored.
#
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/

向其中追加下列配置信息:
/etc/mysql/my.cnf

[mysqld]
# Options for mysqld process:
ndbcluster                      # run NDB storage engine

[mysql_cluster]
# Options for NDB Cluster processes:
ndb-connectstring=192.168.50.129  # location of management server

保存文件並退出.
重啓 MySQL server,使上面的變化生效:

sudo systemctl restart mysql

MySQL默認開機自動啓動.如果不能啓動,下述命令可以修復:

sudo systemctl enable mysql

下面,運行幾條命令驗證MySQL Cluster正常工作.

6 驗證MySQL Cluster安裝

爲了驗證 MySQL Cluster正確安裝, 登陸 Cluster Manager / SQL Server節點(本指南爲192.168.50.129).
打開MySQL 客戶端連接到root 賬號:

mysql -u root -p 

輸入password並回車.
應該出現類似如下提示:

在這裏插入圖片描述

在MySQL客戶端中, 運行下列命令:

SHOW ENGINE NDB STATUS \G

系統會顯示 NDB引擎的相關信息:

在這裏插入圖片描述
這表示成功連入MySQL Cluster.
注意number of ready_data_nodes= 2. 如果一個數據節點掛了(本例中必須是那個沒有安裝MySQL Cluster管理器的節點),MySQL cluster還是可以繼續工作。
你可以試着shutting down 非管理器節點(192.168.50.128)或停止管理器節點上的ndbd服務來測試 cluster穩定性,如果你重啓機器或服務,在整個過程中你會看到number_of_ready_data_nodes 變爲1並再次變回 2.
退出MySQL 客戶端,使用quit 或按CTRL-D.
在集羣管理器控制檯上查看集羣信息,命令如下:

ndb_mgm

輸出如下:

-- NDB Cluster -- Management Client --
ndb_mgm>

然後輸入SHOW命令並回車:

SHOW

輸出如下:

Connected to Management Server at: 192.168.50.129:1186
Cluster Configuration
---------------------
[ndbd(NDB)] 2 node(s)
id=2    @192.168.50.128  (mysql-×.×.×× ndb-8.0.19, Nodegroup: 0, *)
id=3    @192.168.50.129  (mysql-×.×.×× ndb-8.0.19, Nodegroup: 0)

[ndb_mgmd(MGM)] 1 node(s)
id=1    @192.168.50.129  (mysql-×.×.×× ndb-8.0.19)

[mysqld(API)]   1 node(s)
id=4    @192.168.50.129  (mysql-5.7.22 ndb-7.6.6)

退出管理控制檯用quit命令.
管理控制檯功能很多,有很多其他的管理命令來管理集羣和數據, 包括創建在線備份. 更多信息參考官方official MySQL documentation.

7 向MySQL集羣插入數據

注意爲了使用集羣功能, 必須使用NDB數據庫引擎. 如果使用InnoDB (default)或其他引擎,將不能使用集羣.
首先, 創建數據庫clustertest:

CREATE DATABASE clustertest;

其次轉到新數據庫:

USE clustertest;

再次,創建表test_table:

CREATE TABLE test_table (name VARCHAR(20), value VARCHAR(20)) ENGINE=ndbcluster;

這裏需要顯式規定ndbcluster引擎.
現在可以插入數據了:

INSERT INTO test_table (name,value) VALUES('some_name','some_value');

最後驗證數據插入:

SELECT * FROM test_table;

思考:在本例中,數據被插入到了哪個機器?
可以在my.cnf 文件中設定默認數據存儲引擎爲 ndbcluster. 這樣創建表時就不再規定引擎了. 更多信息參考MySQL Reference Manual.
至此我們在Ubuntu 16.04 servers 上安裝和配置了 a MySQL Cluster. 需要注意的是這是一個很小的簡化體系結構來說明配置過程,部署一個生產環境,還有許多其他的選項和特徵需要去學習. 更多信息請參閱 MySQL Cluster documentation.

註釋:systemctl is command line utility and primary tool to manage the systemd daemons/services such as (start, restart, stop, enable, disable, reload & status).

問題:
1 通過實驗,你對一個分佈式數據庫系統有何理解?分佈式數據庫系統預計有何優越性?
2你能設計一個方案驗證集羣系統在可靠性上優於集中式數據庫系統嗎?
2 同樣是插入數據,你覺得MySQL Cluster和myCAT 在實體完整性保持方面是否可能會有不同?爲什麼?

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章