1+X 云计算运维与开发(中级)案例实战——构建读写分离的数据库集群

传送门
教育部职业教育将启动“1+X”证书制度改革
职业教育改革1+X证书制度试点启动
1+X成绩/证书查询入口

1. 案例目标

  • 了解mycat提供的读写分离功能
  • 了解mysql数据库的主从架构
  • 构建以mycat为中间件的读写分离数据库集群

2. 案例分析

2.1 规划节点

使用Mycat作为数据库中间件服务构建读写分离的数据库集群,节点规划如下

IP 主机名 节点
172.16.51.170 mycat Mycat中间件服务节点
172.16.51.171 db1 MariaDB数据库集群主节点
172.16.51.172 db2 MariaDB数据库集群从节点

2.2 基础准备

使用CentOS7.2系统,flavor使用2vCPU/4G内存/50G硬盘,创建三台虚拟机进行实验。
其中两台虚拟机db1和db2部署MariaDB数据库服务,搭建主从数据库集群;一台作为主节点,负责写入数据库信息;另一台作为从节点,负责读取数据库信息。
使用一台虚拟机部署Mycat数据库中间件服务,将用户提交的读写操作识别分发给相应的数据库节点。这样将用户的访问操作、数据库的读与写操作分给三台主机,只有数据库集群的主节点接收增删改SQL语句,从节点接收查询语句,分担了主节点的查询压力。
YUM源使用提供的gpmall-repo文件夹作为本地源,Mycat组件使用提供的Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz压缩包安装。

3. 案例实施

3.1 基础环境配置

  • 1.修改主机名

使用hostnamectl命令修改三台主机的主机名

mycat节点

[root@localhost ~]# hostnamectl set-hostname mycat
[root@localhost ~]# bash
[root@mycat ~]#

db1节点

[root@localhost ~]# hostnamectl set-hostname db1
[root@localhost ~]# bash
[root@db1 ~]#

db2节点

[root@localhost ~]# hostnamectl set-hostname db1
[root@localhost ~]# bash
[root@db2 ~]#
  • 2.编辑hosts文件

修改三台集群虚拟机/etc/hosts配置文件

vi /etc/hosts

//添加以下三行
172.16.51.170 mycat
172.16.51.171 db1
172.16.51.172 db2
所有节点都要配置
  • 3.配置yum源

数据库集群需要安装MariaDB数据库服务,需要给集群虚拟机配置yum安装源文件,使用提供的gpmall-repo文件上传至三个虚拟机的/opt目录下,配置本地yum源。

扫描二维码关注公众号,回复: 11194459 查看本文章

修改三台集群虚拟机yum源配置文件

cat /etc/yum.repos.d/local.repo 

[base]
name=centos7
baseurl=file:///mnt/cd
gpgcheck=0
enabled=1
[mariadb]
name=mariadb
baseurl=file:///opt/gpmall-repo
gpgcheck=0
enabled=1
  • 4.安装JDK环境

部署Mycat中间件服务需要先部署JDK1.7或以上版本的JDK软件环境,这里部署JDK1.8版本

mycat节点安装java环境

[root@mycat ~]# yum install -y java java-devel

//验证
[root@mycat ~]# java -version
openjdk version "1.8.0_222"
OpenJDK Runtime Environment (build 1.8.0_222-b10)
OpenJDK 64-Bit Server VM (build 25.222-b10, mixed mode)

3.2 部署MariaDB主从数据库集群服务

  • 1.安装MariaDB服务

通过yum命令在db1和db2虚拟机节点上安装MariaDB服务

[root@db1 ~]# yum install -y mariadb mariadb-server
[root@db2 ~]# yum install -y mariadb mariadb-server

两个节点启动MariaDB服务,并设置MariaDB服务为开机自启

[root@db1 ~]# systemctl start mariadb
[root@db1 ~]# systemctl enable mariadb
[root@db2 ~]# systemctl start mariadb
[root@db2 ~]# systemctl enable mariadb
  • 2.初始化MariaDB数据库

在db1和db2虚拟机节点上初始化MariaDB数据库,并设置MariaDB数据库root访问用户的密码为123456

[root@db1 ~]# mysql_secure_installation 
/usr/bin/mysql_secure_installation: line 379: find_mysql_client: command not found
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):    #默认按回车
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
Set root password? [Y/n] y
New password:                               #输入数据库root密码123456
Re-enter new password:                      #重复输入密码123456
Password updated successfully!
Reloading privilege tables..
 ... Success!
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] y
 ... Success!
Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] n
 ... skipping.
By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] y
 ... Success!
Cleaning up...
All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!
  • 3.配置数据库集群主节点

编辑主节点db1虚拟机的数据库配置文件server.cnf,在配置文件server.cnf中增添下面的内容

[root@db1 ~]# vi /etc/my.cnf.d/server.cnf
[mysqld]
log_bin=mysql-bin                        #记录操作日志
binlog_ignore_db=mysql                   #不同步mysql系统数据库
server_id=170                            #数据库集群中的每个节点id都要不同,一般使用IP地址的最后段的数字,例如172.16.51.18,server_id就写170 
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

编辑完成配置文件my.cnf后,重启MariaDB服务

[root@db1 ~]# systemctl restart mariadb
  • 4.开放主节点的数据库权限

主节点db1虚拟机上使用mysql命令登录MariaDB数据库,授权在任何客户端机器上可以以root用户登录到数据库

[root@db1 ~]# mysql -uroot –p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 137
Server version: 10.3.18-MariaDB-log MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> grant all privileges  on *.* to root@'%' identified by "123456";

主节点db1数据库上创建一个user用户让从节点db2连接,并赋予从节点同步主节点数据库的权限,命令如下

MariaDB [(none)]> grant replication slave on *.* to 'user'@'db2' identified by '123456';
  • 5.配置从节点db2同步主节点db1

从节点db2虚拟机上使用mysql命令登录MariaDB数据库,配置从节点连接主节点的连接信息
master_host为主节点主机名db1,master_user为在步骤(4)中创建的用户user,命令如下

[root@db2 ~]# mysql -uroot –p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 88
Server version: 10.3.18-MariaDB-log MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>  change master to master_host='db1',master_user='user',master_password='123456';

配置完毕主从数据库之间的连接信息之后,开启从节点服务。使用命令show slave status\G;并查看从节点服务状态,如果Slave_IO_Running和Slave_SQL_Running的状态都为YES,则从节点服务开启成功。查询结果如下图所示

MariaDB [(none)]> start slave;
MariaDB [(none)]> show slave status\G;
  • 6.验证主从数据库的同步功能

先在主节点db1的数据库中创建库test,并在库test中创建表company,插入表数据。创建完成后,查看表company数据,如下所示

MariaDB [(none)]> create database test;
Query OK, 1 row affected (0.001 sec)
MariaDB [(none)]> use test;
Database changed
MariaDB [test]> create table company(id int not null primary key,name varchar(50),addr varchar(255));
Query OK, 0 rows affected (0.165 sec)
MariaDB [test]> insert into company values(1,"facebook","usa");
Query OK, 1 row affected (0.062 sec)
MariaDB [test]> select * from company;
+----+----------+------+
| id | name     | addr |
+----+----------+------+
|  1 | facebook | usa  |
+----+----------+------+
1 row in set (0.000 sec)

这时从节点db2的数据库就会同步主节点数据库创建的test库,可以在从节点查询test数据库与表company,如果可以查询到信息,就能验证主从数据库集群功能在正常运行。查询结果如下所示

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.001 sec)

MariaDB [(none)]> select * from test.company;
+----+----------+------+
| id | name     | addr |
+----+----------+------+
|  1 | facebook | usa  |
+----+----------+------+
1 row in set (0.001 sec)

3.3 部署Mycat读写分离中间件服务

  • 1.安装Mycat服务

将mycat服务的二进制软件包Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz上传到mycat虚拟机的/root目录下,并将软件包解压到 /use/local 目录中。赋予解压后的mycat目录权限

[root@mycat ~]# tar -zxvf Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz -C /usr/local/
[root@mycat ~]# chmod -R 777 /usr/local/mycat/

在/etc/profile系统变量文件中添加mycat服务的系统变量,并生效变量

[root@mycat ~]# echo export MYCAT_HOME=/usr/local/mycat/ >> /etc/profile
[root@mycat ~]# source /etc/profile
//检查
[root@mycat local]# echo $MYCAT_HOME
/usr/local/mycat/
  • 2.编辑mycat的逻辑库配置文件

配置mycat服务读写分离的schema.xml配置文件在/usr/local/mycat/conf/目录下,可以在文件中定义一个逻辑库,使用户可以通过mycat服务管理该逻辑库对应的MariaDB数据库。在这里定义一个逻辑库schema,name为TESTDB;该逻辑库TESTDB对应数据库database为test(在部署主从数据库时已安装);设置数据库写入节点为主节点db1;设置数据库读取节点为从节点db2。(可以直接删除原来schema.xml的内容,替换为如下。注:IP需要修改成实际的IP地址)

[root@mycat ~]# cat /usr/local/mycat/conf/schema.xml 
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
<schema name="TESTDB" checkSQLschema="true" sqlMaxLimit="100" dataNode="dn1"></schema> 
<dataNode name="dn1" dataHost="localhost1" database="test" />  
<dataHost name="localhost1" maxCon="1000" minCon="10" balance="3" dbType="mysql" dbDriver="native" writeType="0" switchType="1"  slaveThreshold="100">  
    <heartbeat>select user()</heartbeat>
    <writeHost host="hostM1" url="172.16.51.171:3306" user="root" password="123456">
        <readHost host="hostS1" url="172.16.51.172:3306" user="root" password="123456" />
    </writeHost>
</dataHost>
</mycat:schema>

配置文件说明:

sqlMaxLimit: 配置默认查询数量
database: 为真实数据库名
balance=“0”, 不开启读写分离机制,所有读操作都发送到当前可用的writeHost上。
balance=“1”, 全部的readHost与stand by writeHost参与select语句的负载均衡,简单的说,当双主双从模式(M1->S1,M2->S2,并且M1与M2互为主备),正常情况下,M2,S1,S2都参与select语句的负载均衡。
balance=“2”, 所有读操作都随机的在writeHost、readhost上分发。
balance=“3”, 所有读请求随机的分发到wiriterHost对应的readhost执行,writerHost不负担读压力,注意balance=3只在1.4及其以后版本有,1.3没有。
writeType=“0”, 所有写操作发送到配置的第一个 writeHost,第一个挂了切到还生存的第二个writeHost,重新启动后已切换后的为准,切换记录在配置文件中:dnindex.properties .
writeType=“1”, 所有写操作都随机的发送到配置的writeHost。

  • 3.修改配置文件权限
[root@mycat ~]# chown root:root schema.xml
  • 4.编辑mycat的访问用户

修改/usr/local/mycat/conf/目录下的server.xml文件,修改root用户的访问密码与数据库,密码设置为123456,访问mycat的逻辑库为USERDB,命令如下

[root@mycat ~]# cat /usr/local/mycat/conf/server.xml 
在配置文件的最后部分,
<user name="root">
		<property name="password">123456</property>
		<property name="schemas">USERDB</property>
然后删除如下几行:
<user name="root">
		<property name="password">user</property>
		<property name="schemas">TESTDB</property>
		<property name="readOnly">true</property>
</user>
保存并退出server.xml配置文件
  • 5.启动mycat服务

通过命令启动mycat数据库中间件服务,启动后通过netstat -ntpl命令查看虚拟机端口开放情况,如果有开放8066和9066端口,则表示mycat服务开启成功。端口查询情况如下图所示

[root@mycat ~]# /bin/bash /usr/local/mycat/bin/mycat start
Starting Mycat-server...
[root@mycat conf]# netstat -ntpl   

在这里插入图片描述

3.4 验证数据库集群服务读写分离功能

  • 1.用mycat服务查询数据库信息

先在mycat虚拟机上使用yum安装mariadb-client服务

[root@mycat ~]# yum install -y MariaDB-client

在mycat虚拟机上使用mysql命令查看mycat服务的逻辑库USERDB,因为mycat的逻辑库USERDB对应数据库test(在部署主从数据库时已安装),所以可以查看库中已经创建的表company。命令如下

[root@mycat ~]# mysql -h127.0.0.1 -P8066 -uroot –p123456
//必须手动输入,不能复制粘贴,复制粘贴会报错!!!
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.29-mycat-1.6-RELEASE-20161028204710 MyCat Server (OpenCloundDB)

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

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

MySQL [(none)]> show databases;
+----------+
| DATABASE |
+----------+
| TESTDB   |
+----------+
1 row in set (0.001 sec)

MySQL [(none)]> use TESTDB
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MySQL [USERDB]> show tables;
+----------------+
| Tables_in_test |
+----------------+
| company        |
+----------------+
1 row in set (0.003 sec)
MySQL [USERDB]> select * from company;
+----+----------+------+
| id | name     | addr |
+----+----------+------+
|  1 | facebook | usa  |
+----+----------+------+
1 row in set (0.005 sec)
  • 2.用mycat服务添加表数据

在mycat虚拟机上使用mysql命令对表company添加一条数据(2,“basketball”,“usa”),添加完毕后查看表信息。命令如下

MySQL [USERDB]> insert into company values(2,"bastetball","usa");
Query OK, 1 row affected (0.050 sec)

MySQL [USERDB]> select * from company;
+----+------------+------+
| id | name       | addr |
+----+------------+------+
|  1 | facebook   | usa  |
|  2 | bastetball | usa  |
+----+------------+------+
2 rows in set (0.002 sec)
  • 3.验证mycat服务对数据库读写操作分离

在mycat虚拟机节点使用mysql命令通过9066端口查询对数据库读写操作的分离信息。可以看到 所有的写入操作WRITE_LOAD数都在db1主数据库节点上,所有的读取操作READ_LOAD数都在db2主数据库节点上 由此可见数据库读写操作已经分离到db1和db2节点上了。命令如下

[root@mycat ~]# mysql -h127.0.0.1 -P9066 -uroot –p123456 -e 'show  @@datasource;'
//必须手动输入,不能复制粘贴,复制粘贴会报错!!!

在这里插入图片描述

至此,mycat读写分离数据库案例完成

持续更新ing!!!

要是有用别忘了点个关注哦!!!

上一篇:1+X 云计算运维与开发(中级)案例实战——应用系统部署

下一篇:1+X云计算平台运维与开发认证(初级)样卷A

原创文章 28 获赞 326 访问量 4万+

猜你喜欢

转载自blog.csdn.net/Destiny_425/article/details/105612634