构建读写分离的数据库集群

目录

1. 基础环境配置

(1)修改主机名

(2)编辑hosts文件

(3)配置Yum安装源

(4)安装JDK环境

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

(1)安装MariaDB服务

(2)初始化MariaDB数据库

(3)配置数据库集群主节点

(4)开放主节点的数据库权限

(5)配置从节点db2同步主节点db1

(6)验证主从数据库的同步功能

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

(1)安装Mycat服务

(2)编辑Mycat的逻辑库配置文件

(3)修改配置文件权限

(4)编辑mycat的访问用户

(5)启动Mycat服务

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

(1)用Mycat服务查询数据库信息

(2)用Mycat服务添加表数据

(3)验证Mycat服务对数据库读写操作分离


1. 规划节点

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

IP主机名节点
192.168.100.10mycatMycat中间服务节点
192.168.100.20 db1MariaDB数据库集群主节点
192.168.100.30db2MariaDB数据库集群从节点

2. 基础准备

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

1. 基础环境配置

(1)修改主机名

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

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

(2)编辑hosts文件

3台集群虚拟机的/etc/hosts文件配置部分:

# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.100.10 mycat
192.168.100.20 db1
192.168.100.30 db2

(3)配置Yum安装源

数据库集群需要安装MariaDB数据库服务,需要给集群虚拟机配置Yum安装源文件,使用提 供的gpmall-repo文件上传至3个虚拟机的/opt目录下,设置本地Yum源。 首先将3个节点/etc/yum.repo.d目录下的文件移动到/media下,命令如下:

# mv /etc/yum.repos.d/* /media/

3台集群虚拟机的Yum安装源文件配置部分:

# mkdir /opt/centos
# mount /dev/sr0 /opt/centos/
mount: /dev/sr0 is write-protected, mounting read-only
# vi /etc/yum.repos.d/local.repo
# cat /etc/yum.repos.d/local.repo
[centos]
name=centos
baseurl=file:///opt/centos
gpgcheck=0
enabled=1
[mariadb]
name=mariadb
baseurl=file:///opt/gpmall-repo
gpgcheck=0
enabled=1

(4)安装JDK环境

部署Mycat中间件服务需要先部署JDK 1.7或以上版本的JDK软件环境,这里部署JDK 1.8版 本。 Mycat节点安装Java环境:

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

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

(1)安装MariaDB服务

通过YUM命令在db1和db2虚拟机节点上安装MariaDB服务,命令如下:

# yum install -y mariadb mariadb-server

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

# systemctl start mariadb
# systemctl enable mariadb

(2)初始化MariaDB数据库

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

# mysql_secure_installation …
Enter current password for root (enter for none): ##默认按回车
Set root password? [Y/n] y
New password: ##输入数据库root密码123456
Re-enter new password: ##重复输入密码123456
Remove anonymous users? [Y/n] y
Disallow root login remotely? [Y/n] n
Remove test database and access to it? [Y/n] y
Reload privilege tables now? [Y/n] y

(3)配置数据库集群主节点

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

[root@db1 ~]# vi /etc/my.cnf
[root@db1 ~]# cat /etc/my.cnf
#
# This group is read both both by the client and the server
# use it for options that affect everything
#
[client-server]

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
[mysqld]
log_bin = mysql-bin
binlog_ignore_db = mysql
server_id = 20
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

[root@db2 ~]# vi /etc/my.cnf
[root@db2 ~]# cat /etc/my.cnf
#
# This group is read both both by the client and the server
# use it for options that affect everything
#
[client-server]

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
[mysqld]
log_bin = mysql-bin
binlog_ignore_db = mysql
server_id = 30
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
[root@db1 ~]# systemctl stop firewalld
[root@db1 ~]# setenforce 0

[root@db2 ~]# systemctl restart mariadb
[root@db2 ~]# systemctl stop firewalld
[root@db2 ~]# setenforce 0

(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 9
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";
Query OK, 0 rows affected (0.002 sec)

MariaDB [(none)]> grant replication slave on *.* to 'user'@'db2' identified by '123456';
Query OK, 0 rows affected (0.001 sec)

(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 9
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';
Query OK, 0 rows affected (0.040 sec)

(6)验证主从数据库的同步功能

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

[root@db1 ~]# mysql -uroot -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 11
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)]> 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.016 sec)

MariaDB [test]> insert into company values(1,"facebook","usa")
    -> ;
Query OK, 1 row affected (0.005 sec)

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

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

[root@db2 ~]# mysql -uroot -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 14
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)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.001 sec)

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

(1)安装Mycat服务


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

[root@mycat ~]# systemctl stop firewalld
[root@mycat ~]# setenforce 0
[root@mycat ~]# tar -zxvf Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz -C /usr/local/
[root@mycat ~]# chown -R 777 /usr/local/mycat/

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

[root@mycat ~]# echo export MYCAT_HOME=/usr/local/mycat/ >> /etc/profile
[root@mycat ~]# source /etc/profile

(2)编辑Mycat的逻辑库配置文件

<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
    <!-- 数据库配置 与server.xml 中的数据库对应 -->
 <schema name="db_test" checkSQLschema="false" dataNode="db_node" sqlMaxLimit="100" 
 </schema>
 
    <!-- 分片配置 -->
 <dataNode name="db_node" dataHost="db_host" database="db_test" />
 
    <!-- 物理数据库配置 -->
 <dataHost name="db_host" maxCon="1000"  minCon="10" balance="0"
  writeType="0" dbType="mysql"  dbDriver="native" switchType="1"  slaveThreshold="100">
   <heartbeat>select user()</heartbeat>
   <writeHost host="hostM1"  url="192.168.66.101:3339" user="root"  password="123456">
   <readHost host="hostS2" url="192.168.66.101:3340" user="root" password="123456" />
   </writeHost>
 </dataHost>
 
</mycat:schema>

(3)修改配置文件权限

修改schema.xml的用户权限,命令如下:

[root@mycat ~]# chown root:root /usr/local/mycat/conf/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>

保存并退出server.xml配置文件。

(5)启动Mycat服务

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

[root@mycat ~]# /bin/bash /usr/local/mycat/bin/mycat start

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

(1)用Mycat服务查询数据库信息

先在Mycat虚拟机上使用Yum安装mariadb-client服务。

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

在Mycat虚拟机上使用mysql命令查看Mycat服务的逻辑库 USERDB,因为Mycat的逻辑库USERDB对应数据库test, 所以可以查看库中已经创建的表company。命令如下

mysql -h127.0.0.1 -P8066 -uroot -p123456

(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;'

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值