centos7.2系统构建读写分离的数据库集群

就不跟各位说有的没的了,直接进入实验。

实验需要用到的资源软件包已上传至百度网盘。

链接:https://pan.baidu.com/s/1a-7_iAIX0DBAMkF9bhiTcA?pwd=2333 
提取码:2333 
复制这段内容后打开百度网盘手机App,操作更方便哦。

既然是集群又要实现读写分离那么本次实验就准备了3台虚拟机主机。题目给予的ip规划地址如下:

IP

主机名

节点

172.16.51.6

mycat

Mycat中间件服务节点

172.16.51.18

db1

MariaDB数据库集群主节点

172.16.51.30

db2

MariaDB数据库集群从节点

网卡模式不做要求nat或者主机都可以,本次实验的安装都是使用本地源以及本地资源软件包,因此在本文章中不提供网络源的配置。如有需求可以自己搜索其他文章资料或者翻阅我的往期配置文章有详细介绍。

从本次实验开始,我们就要逐步的使用vm虚拟机的克隆功能来让我们快速搭建好基础环境。

首先我们现在已经安装的一台centos7.2主机上进行网卡文件的配置。

一定要重启网络服务,然后我们提前把防火墙关闭和本地镜像源以及主机映射文件都弄好。这样到时候克隆完的机子就可以省去这些步骤啦。

镜像源依旧是先移走本地的网络源然后自己配置一个本地仓库源,创建一个当仓库的目录再挂载即可。在此之前先用ssh上传一下本地的镜像软件包。

接着我们修改主机映射文件,提前添加好映射。

[root@localhost ~]# vi /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
172.16.51.6     mycat
172.16.51.18    db1
172.16.51.30    db2

然后我们关闭当前的这台虚拟机,准备进行克隆,可以关机后直接保存一个快照,之后专门克隆这个快照就行了。

右键需要克隆的主机选项卡找到克隆选项。

使用现有快照克隆。

推荐选择完整克隆,链接克隆容易出现源机在一些实验中配置出问题爆炸,链接的其他机子也跟着爆炸的情况。完整克隆才是完全独立出来的。

再以相同的步骤克隆出db2即可。

最好在vm虚拟机界面提前命名好,以防搞混。

接着直接启动3台虚拟机,修改主机名和各自的ip地址。我这边只需要修改db1和db2的地址就行,因为克隆的时候地址都是基于mycat主机的地址。

db1地址改为

db2地址改为

保存完毕以后记得重启网络服务。

mycat的地址因克隆影响在修改完db1和db2的地址并重启它们俩的网络服务后也记得重启一下mycat的网络服务。

然后修改各自的主机名,受克隆影响,它们的主机名都变成mycat。都给它们改成各自对应的主机名即可。虽然会发现克隆的vm虚拟机非常智能的已经索取替换好了你的主机名,但是为了防止出问题还是自己再手动更改主机名再刷新一遍稳妥。

接着再都挂载一下本地yum仓库,我们本可以正式安装服务进入实验配置了。

3台主机都要这样挂载一下。

在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)

接着部署MariaDB主从数据库集群服务。

先在db1和db2上安装数据库服务。(给我看清楚db1和db2上都要装,别看到我只贴了一个db1的安装图就不管db2了。)

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

然后在两个节点上都启动数据库并且设置为自启动。

然后两个节点都初始化数据库。(这里的密码设置如果要做我后面文章的集群搭建应用商城的话,密码必须是123456。因为那边的项目包里对于mysql配置的连接密码为123456)

看清楚是2个节点都要做哦,别只做一个db1。

[root@db1 ~]# mysql_secure_installation

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:
Re-enter new password:
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!
[root@db1 ~]#

然后改数据库的配置文件,这边我们只改db1的。因为我们拿db1作为主节点。

[root@db1 ~]# vi /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                  #不同步MySQL系统数据库
server_id = 18                            #数据库集群中的每个节点id都要不同,一般使用IP地址
的最后段的数字,例如172.16.51.18,server_id就写18

datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

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

然后重启一下数据库服务。

接着开放主节点db1的数据库权限。授权在任何客户端机器上可以以root用户登录到数据库。在主节点db1数据库上创建一个user用户让从节点db2连接,并赋予从节点同步主节点数据库的权限。

这里密码如果你跟我一样就是123456,如果之前初始化数据库的时候改了,那就跟着变。

[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.000 sec)

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

MariaDB [(none)]>

然后我们配置从节点db2同步主节点db1。配置从节点连接主节点的连接信息。master_host为主节点主机名db1,master_user为在步骤(4)中创建的用户user。之后使用命令查看节点服务是否开启成功。(注意这里是在db2上的数据库配置哦,别做晕了。)

[root@db2 ~]# mysql -uroot -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 15
Server version: 10.3.18-MariaDB 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.014 sec)

MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.002 sec)

MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
                Slave_IO_State: Waiting for master to send event
                   Master_Host: db1
                   Master_User: user
                   Master_Port: 3306
                 Connect_Retry: 60
               Master_Log_File: mysql-bin.000001
           Read_Master_Log_Pos: 702
                Relay_Log_File: db2-relay-bin.000002
                 Relay_Log_Pos: 1001
         Relay_Master_Log_File: mysql-bin.000001
              Slave_IO_Running: Yes
             Slave_SQL_Running: Yes

如果Slave_IO_Running和Slave_SQL_Running的状态都为YES,则从节点服务开启成功。

实验末尾我们来验证主从数据库的同步功能。

先在主节点db1的数据库中创建库test,并在库test中创建表company,插入表数据。创建完成后,查看表company数据。(注意哦,这里回到db1节点数据库上做了。)

这边就是建库插入数据用于测试使用。

MariaDB [(none)]> create database test;
Query OK, 1 row affected (0.000 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.003 sec)

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

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

MariaDB [test]>

然后我们回到db2的数据库上查看。可以发现从节点db2自动更新了主节点db1创建的库以及插入的数据。

接着我们开始实现读写分离,回到mycat节点上进行部署。

先安装mycat服务,通过ssh上传本地资源安装包。我这边依旧上传至root目录下。

将其解压至/use/local目录下方便系统调动使用。然后给满文件权限防止文件权限问题导致服务启不来,同时添加好系统变量并马上用命令刷新系统变量使其生效。

[root@mycat ~]# tar -zxvf Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz -C /usr/local/
[root@mycat ~]# chown -R 777 /usr/local/mycat/
[root@mycat ~]# echo export MYCAT_HOME=/usr/local/mycat/ >> /etc/profile
[root@mycat ~]# source /etc/profile
[root@mycat ~]#

编辑mycat的逻辑库配置文件实现读写分离。把里面默认的内容全删了,我们自己重新配置。(vi进入该文件编辑界面以后按小d键位再按大写键位按G键即可删除文件所有内容。简短来说就是dG键删除所有内容。)

[root@mycat ~]# vi /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="USERDB" 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.18:3306" user="root" password="123456">
        <readHost host="hostS1" url="172.16.51.30:3306" user="root" password="123456" />
    </writeHost>
</dataHost>
</mycat:schema>

重点的命令参数是这两条。一条是写主机一条是读主机,很明确的标明谁做写、谁做读来实现读写分离。你如果ip地址跟我不一样就要自己看着改,还有mysql的端口如果你不是3306也要改(一般mysql默认端口都是3306)。密码也是不跟我一样的123456也要修改。

拉一手我们刚才修改配置文件的权限,将其设为root用户专享。

[root@mycat ~]# chown root:root /usr/local/mycat/conf/schema.xml

然后编辑mycat的访问用户。进入配置文件后直接shift键加g到文件末尾。修改root用户的访问密码与数据库,密码设置为123456,访问Mycat的逻辑库。然后把一些默认模板文件给删掉。(注释掉也行)

[root@mycat ~]# vi /usr/local/mycat/conf/server.xml
        <!-- 全局SQL防火墙设置 -->
        <!--
        <firewall>
           <whitehost>
              <host host="127.0.0.1" user="mycat"/>
              <host host="127.0.0.2" user="mycat"/>
           </whitehost>
       <blacklist check="false">
       </blacklist>
        </firewall>
        -->

        <user name="root">
                <property name="password">123456</property>
                <property name="schemas">USERDB</property>

        </user>
</mycat:server>

就是在这个sql防火墙设置的下面和末尾</mycat:server>上面,它们的中间内容都删除然后添加配置上这几句即可。

保存退出文件后,我们启动一下mycat服务,提前安装好网络工具包,在启动mycat服务后使用命令查看对应的8066和9066端口是否开放。有的话说明mycat服务启动成功。

[root@mycat ~]# yum install -y net-tools
[root@mycat ~]# /bin/bash /usr/local/mycat/bin/mycat start
Starting Mycat-server...
[root@mycat ~]#

通过命令查看端口可以发现mycat服务启动成功了。

[root@mycat ~]# netstat -ntpl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1139/sshd  
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1678/master
tcp        0      0 127.0.0.1:32000         0.0.0.0:*               LISTEN      10190/java 
tcp6       0      0 :::9066                 :::*                    LISTEN      10190/java 
tcp6       0      0 :::44690                :::*                    LISTEN      10190/java 
tcp6       0      0 :::52083                :::*                    LISTEN      10190/java 
tcp6       0      0 :::22                   :::*                    LISTEN      1139/sshd  
tcp6       0      0 ::1:25                  :::*                    LISTEN      1678/master
tcp6       0      0 :::1984                 :::*                    LISTEN      10190/java 
tcp6       0      0 :::8066                 :::*                    LISTEN      10190/java 
[root@mycat ~]#

最后我们来验证集群的读写分离功能。在mycat节点上安装一下数据库客户端。

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

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

这里不用纳闷地址为什么是127.0.0.1即mycat本地,虽然数据库是在db1和db2但是我们已经在mycat节点上对mycat服务进行了文件的配置。使其Mycat的逻辑库USERDB对应数据库test。

如果手敲命令的话记得确认好参数大写P指端口,小写p指密码。以前刚做的时候没注意大小写的p都用小写p,然后访问一直报错还以为哪里配置错了浪费了半天时间。

[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 1
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 |
+----------+
| USERDB   |
+----------+
1 row in set (0.001 sec)

MySQL [(none)]> use USERDB
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.001 sec)

MySQL [USERDB]> select * from company;
+----+----------+------+
| id | name     | addr |
+----+----------+------+
|  1 | facebook | usa  |
+----+----------+------+
1 row in set (0.024 sec)

MySQL [USERDB]>

通过上面的命令可以发现,我们的mycat服务已经获取连接到了db节点数据库上的数据,现在我们尝试在mycat节点上插入数据然后验证读写分离。

注意这边是继续在8066端口中的USERDB数据库插入数据然后退出切换到9066上查看读写分离。

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

MySQL [USERDB]> select * from company;
+----+------------+------+
| id | name       | addr |
+----+------------+------+
|  1 | facebook   | usa  |
|  2 | bastetball | usa  |
+----+------------+------+
2 rows in set (0.001 sec)

MySQL [USERDB]> Ctrl-C -- exit!
Aborted
[root@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;'
+----------+--------+-------+--------------+------+------+--------+------+------+---------+-----------+------------+
| DATANODE | NAME   | TYPE  | HOST         | PORT | W/R  | ACTIVE | IDLE | SIZE | EXECUTE | READ_LOAD | WRITE_LOAD |
+----------+--------+-------+--------------+------+------+--------+------+------+---------+-----------+------------+
| dn1      | hostM1 | mysql | 172.16.51.18 | 3306 | W    |      0 |   10 | 1000 |     121 |         0 |          1 |
| dn1      | hostS1 | mysql | 172.16.51.30 | 3306 | R    |      0 |    8 | 1000 |     121 |         4 |          0 |
+----------+--------+-------+--------------+------+------+--------+------+------+---------+-----------+------------+
[root@mycat ~]#

观看末尾的地方会发现一个只做读一个只做写。

如果还想再次确认可以自己再回到刚才8066端口的连接中再次使用数据库查看表数据和插入写一条数据进去。再回到9066端口来查看一次。

我这边又到8066端口里面插入了一条数据和读取调用了。

MySQL [(none)]> use USERDB
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]> select * from company;
+----+------------+------+
| id | name       | addr |
+----+------------+------+
|  1 | facebook   | usa  |
|  2 | bastetball | usa  |
+----+------------+------+
2 rows in set (0.001 sec)

MySQL [USERDB]> insert into company values(3,"tiktok","china");
Query OK, 1 row affected (0.003 sec)

MySQL [USERDB]> select * from company;
+----+------------+-------+
| id | name       | addr  |
+----+------------+-------+
|  1 | facebook   | usa   |
|  2 | bastetball | usa   |
|  3 | tiktok     | china |
+----+------------+-------+
3 rows in set (0.001 sec)

MySQL [USERDB]>

然后再次回到9066端口查看会发现,读增加了3次,写多了1次。读正好就是我use调用使用了USERDB数据库1次,读取看表数据2次。写的则是插入了一条tiktok china的数据。

那么本次centos7.2系统构建读写分离的数据库集群实验到此结束。

  • 42
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值