【实验】 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 在实体完整性保持方面是否可能会有不同?为什么?

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