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

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

  • 前言
  • 思路
  • 实操
    • 1.修改主机名,修改主机映射,配置yum源
    • 2.给mycat机安装java服务,给db1和db2安装mysql
    • 3.给db1和db2部署主从数据库集群
      • 3.1启动mysql服务,并初始化(两台)
      • 3.2 配置db1
        • 3.2.1 编辑主节点 db1 虚拟机的数据库配置文件 my.cnf
        • 3.2.2 开放主节点的数据库权限
      • 3.3 配置db2
        • 3.3.1配置从节点 db2 同步主节点 db1
      • 3.4 验证主从数据库的同步功能
    • 4.在mycat机部署 Mycat 数据库中间件服务
  • 总结


前言

学而不思则罔,思而不学则殆。


IP 主机名 节点
192.168.200.10 mycat Mycat中间件服务节点
192.168.200.20 db1 MariaDB 数据库集群主节点
192.168.200.30 db2 MariaDB 数据库集群从节点

思路

1.修改主机名,配置yum源;
2.给mycat机安装java服务,给db1和db2安装mysql;
3.给db1和db2部署主从数据库集群;
4.在mycat机部署 Mycat 数据库中间件服务


实操

1.修改主机名,修改主机映射,配置yum源

代码如下:

Mycat:

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

db1:

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

db2:

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

/etc/hosts文件中添加以下内容:

192.168.200.10 mycat
192.168.200.20 db1
192.168.200.30 db2

配置yum源(ftp)

注意:需要自行准备gpmall-repo文件

mycat:

[root@mycat ~]# rm -rf /etc/yum.repos.d/*
[root@mycat ~]# mv gpmall-repo /opt
[root@mycat ~]# mkdir /opt/cdrom ; mount CentOS-7-x86_64-DVD-1511.iso /opt/cdrom
mount: /dev/loop0 is write-protected, mounting read-only
[root@mycat ~]# cat /etc/yum.repos.d/local.repo 
[centos]
name=centos
baseurl=file:///opt/cdrom
gpgcheck=0
[gpmall]
name=gpmall
baseurl=file:///opt/gpmall-repo
gpgcheck=0
[root@mycat ~]# yum repolist
Loaded plugins: fastestmirror
centos                                                                          | 3.6 kB  00:00:00     
(1/2): centos/group_gz                                                          | 155 kB  00:00:00     
(2/2): centos/primary_db                                                        | 2.8 MB  00:00:00     
Determining fastest mirrors
repo id                                          repo name                                       status
centos                                           centos                                          3,723
gpmall                                           gpmall                                            165
repolist: 3,888

db1:

[root@db1 ~]# rm -rf /etc/yum.repos.d/*
[root@db1 ~]# cat /etc/yum.repos.d/local.repo 
[centos]
name=centos
baseurl=ftp://192.168.200.10/cdrom
gpgcheck=0
[gpmall]
name=gpmall
baseurl=ftp://192.168.200.10/gpmall-repo
gpgcheck=0

db2:

[root@db1 ~]# rm -rf /etc/yum.repos.d/*
[root@db2 ~]# cat /etc/yum.repos.d/local.repo 
[centos]
name=centos
baseurl=ftp://192.168.200.10/cdrom
gpgcheck=0
[gpmall]
name=gpmall
baseurl=ftp://192.168.200.10/gpmall-repo
gpgcheck=0

mycat机上安装ftp,并设置共享文件路径:

[root@mycat ~]# yum -y install vsftpd
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package vsftpd.x86_64 0:3.0.2-10.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=======================================================================================================
 Package               Arch                  Version                       Repository             Size
=======================================================================================================
Installing:
 vsftpd                x86_64                3.0.2-10.el7                  centos                167 k

Transaction Summary
=======================================================================================================
Install  1 Package

Total download size: 167 k
Installed size: 347 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : vsftpd-3.0.2-10.el7.x86_64                                                          1/1 
  Verifying  : vsftpd-3.0.2-10.el7.x86_64                                                          1/1 

Installed:
  vsftpd.x86_64 0:3.0.2-10.el7                                                                         

Complete!
[root@mycat ~]# vi /etc/vsftpd/vsftpd.conf 
anon_root=/opt      ##这一行在配置文件的第一行添加就行
···
[root@mycat ~]# systemctl restart vsftpd

最好把gpmall-repo放到 /opt目录下,这样就不用做过多的操作

注意:关闭防火墙,否则会报错

[root@db1 ~]# yum repolist
Loaded plugins: fastestmirror
ftp://192.168.200.10/cdrom/repodata/repomd.xml: [Errno 14] curl#7 - "Failed connect to 192.168.200.10:21; No route to host"
Trying other mirror.
ftp://192.168.200.10/cdrom/repodata/repomd.xml: [Errno 14] curl#7 - "Failed connect to 192.168.200.10:21; No route to host"
Trying other mirror.
ftp://192.168.200.10/cdrom/repodata/repomd.xml: [Errno 14] curl#7 - "Failed connect to 192.168.200.10:21; No route to host"
Trying other mirror.
ftp://192.168.200.10/cdrom/repodata/repomd.xml: [Errno 14] curl#7 - "Failed connect to 192.168.200.10:21; No route to host"
Trying other mirror.
---

关闭防火墙(三台机子都要做):

[root@mycat ~]# systemctl stop firewalld
[root@mycat ~]# setenforce 0

成功结果:
mycat:

[root@mycat ~]# yum repolist
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
repo id                                          repo name                                       status
centos                                           centos                                          3,723
gpmall                                           gpmall                                            165
repolist: 3,888

db1:

[root@db1 ~]# yum repolist
Loaded plugins: fastestmirror
centos                                                                          | 3.6 kB  00:00:00     
gpmall                                                                          | 2.9 kB  00:00:00     
(1/2): centos/group_gz                                                          | 155 kB  00:00:00     
(2/2): gpmall/primary_db                                                        | 144 kB  00:00:00     
Determining fastest mirrors
repo id                                          repo name                                       status
centos                                           centos                                          3,723
gpmall                                           gpmall                                            165
repolist: 3,888

db2:

[root@db2 ~]# yum repolist
Loaded plugins: fastestmirror
centos                                                                          | 3.6 kB  00:00:00     
gpmall                                                                          | 2.9 kB  00:00:00     
(1/2): centos/group_gz                                                          | 155 kB  00:00:00     
(2/2): gpmall/primary_db                                                        | 144 kB  00:00:00     
Determining fastest mirrors
repo id                                          repo name                                       status
centos                                           centos                                          3,723
gpmall                                           gpmall                                            165
repolist: 3,888

2.给mycat机安装java服务,给db1和db2安装mysql

mycat:

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

db1:

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

db2:

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

3.给db1和db2部署主从数据库集群

3.1启动mysql服务,并初始化(两台)

这里以db1为例,db2相同操作:

[root@db1 ~]# mysql_secure_installation

Set root password? [Y/n]  #直接回车
New password:  #密码必须为123456,因为脚本文件里配置的
Re-enter new password: #再输入一次123456
Password updated successfully!
Reloading privilege tables..
 ... Success!

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.


Remove test database and access to it? [Y/n] #输入y或者直接回车
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!


Reload privilege tables now? [Y/n] #输入y或者直接回车
 ... Success!

3.2 配置db1

3.2.1 编辑主节点 db1 虚拟机的数据库配置文件 my.cnf

在配置文件 my.cnf 中增添下面的内容:

[root@db1 ~]# vi /etc/my.cnf
[mysqld]
log-bin = mysql-bin			#记录操作日志
binlog-ignore-db = mysql	#不同步 MySQL 系统数据库
server-id = 20		
#数据库集群中的每个节点 id 都要不同,一般使用 IP 地址的最后段的数字
#例如 172.16.51.18,server_id 就写 18

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@db1 ~]# systemctl restart mariadb
##  重启mariadb

my.cnf文件详解看——转载自:http://t.csdn.cn/srCVq

3.2.2 开放主节点的数据库权限
[root@db1 ~]# mysql -uroot -p123456

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

MariaDB [(none)]> grant all privileges on *.* to root@'%' identified by '123456';
Query OK, 0 rows affected (0.000 sec)

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

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

刷新权限很重要:

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.000 sec)

3.3 配置db2

3.3.1配置从节点 db2 同步主节点 db1

在从节点 db2 虚拟机上使用 mysql 命令登录 MariaDB 数据库,配置从节点连接主节点的连接信息。master_host 为主节点主机名 db1master_user 为在步骤3.3.2中创建的用户 user

[root@db2 ~]# mysql -uroot -p123456

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
*************************** 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: 1133
                Relay_Log_File: db2-relay-bin.000002
                 Relay_Log_Pos: 1432
         Relay_Master_Log_File: mysql-bin.000001
              Slave_IO_Running: Yes
             Slave_SQL_Running: Yes
               Replicate_Do_DB: 
           Replicate_Ignore_DB: 
            Replicate_Do_Table: 
        Replicate_Ignore_Table: 
       Replicate_Wild_Do_Table: 
   Replicate_Wild_Ignore_Table: 
                    Last_Errno: 0
                    Last_Error: 
                  Skip_Counter: 0
           Exec_Master_Log_Pos: 1133
               Relay_Log_Space: 1739
               Until_Condition: None
                Until_Log_File: 
                 Until_Log_Pos: 0
            Master_SSL_Allowed: No
            Master_SSL_CA_File: 
            Master_SSL_CA_Path: 
               Master_SSL_Cert: 
             Master_SSL_Cipher: 
                Master_SSL_Key: 
         Seconds_Behind_Master: 0
 Master_SSL_Verify_Server_Cert: No
                 Last_IO_Errno: 0
                 Last_IO_Error: 
                Last_SQL_Errno: 0
                Last_SQL_Error: 
   Replicate_Ignore_Server_Ids: 
              Master_Server_Id: 20
                Master_SSL_Crl: 
            Master_SSL_Crlpath: 
                    Using_Gtid: No
                   Gtid_IO_Pos: 
       Replicate_Do_Domain_Ids: 
   Replicate_Ignore_Domain_Ids: 
                 Parallel_Mode: conservative
                     SQL_Delay: 0
           SQL_Remaining_Delay: NULL
       Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
              Slave_DDL_Groups: 5
Slave_Non_Transactional_Groups: 0
    Slave_Transactional_Groups: 0
1 row in set (0.000 sec)

这里之前可能忘记把db1和db2的防火墙关闭了,所以报了个错(本篇文章暂不展示)
具体可以看https://blog.csdn.net/famudd/article/details/123690498

3.4 验证主从数据库的同步功能

先在主节点 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 167
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.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.002 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.000 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 167
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.000 sec)

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

4.在mycat机部署 Mycat 数据库中间件服务

上传并解压 Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz ,解压到 /usr/local/

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

给解压出来的 **/mycat/**赋权限

[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

编辑 Mycat 的逻辑库配置文件 schema.xml

[root@mycat ~]# vi /usr/local/mycat/conf/schema.xml
##原文件展示
[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="TESTDB" checkSQLschema="false" sqlMaxLimit="100">
                <!-- auto sharding by id (long) -->
                <table name="travelrecord" dataNode="dn1,dn2,dn3" rule="auto-sharding-long" />

                <!-- global table is auto cloned to all defined data nodes ,so can join
                        with any table whose sharding node is in the same data node -->
                <table name="company" primaryKey="ID" type="global" dataNode="dn1,dn2,dn3" />
                <table name="goods" primaryKey="ID" type="global" dataNode="dn1,dn2" />
                <!-- random sharding using mod sharind rule -->
                <table name="hotnews" primaryKey="ID" autoIncrement="true" dataNode="dn1,dn2,dn3"
                           rule="mod-long" />
                <!-- <table name="dual" primaryKey="ID" dataNode="dnx,dnoracle2" type="global"
                        needAddLimit="false"/> <table name="worker" primaryKey="ID" dataNode="jdbc_dn1,jdbc_dn2,jdbc_dn3"
                        rule="mod-long" /> -->
                <table name="employee" primaryKey="ID" dataNode="dn1,dn2"
                           rule="sharding-by-intfile" />
                <table name="customer" primaryKey="ID" dataNode="dn1,dn2"
                           rule="sharding-by-intfile">
                        <childTable name="orders" primaryKey="ID" joinKey="customer_id"
                                                parentKey="id">
                                <childTable name="order_items" joinKey="order_id"
                                                        parentKey="id" />
                        </childTable>
                        <childTable name="customer_addr" primaryKey="ID" joinKey="customer_id"
                                                parentKey="id" />
                </table>
                <!-- <table name="oc_call" primaryKey="ID" dataNode="dn1$0-743" rule="latest-month-calldate"
                        /> -->
        </schema>
        <!-- <dataNode name="dn1$0-743" dataHost="localhost1" database="db$0-743"
                /> -->
        <dataNode name="dn1" dataHost="localhost1" database="db1" />
        <dataNode name="dn2" dataHost="localhost1" database="db2" />
        <dataNode name="dn3" dataHost="localhost1" database="db3" />
        <!--<dataNode name="dn4" dataHost="sequoiadb1" database="SAMPLE" />
         <dataNode name="jdbc_dn1" dataHost="jdbchost" database="db1" />
        <dataNode       name="jdbc_dn2" dataHost="jdbchost" database="db2" />
        <dataNode name="jdbc_dn3"       dataHost="jdbchost" database="db3" /> -->
        <dataHost name="localhost1" maxCon="1000" minCon="10" balance="0"
                          writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">
                <heartbeat>select user()</heartbeat>
                <!-- can have multi write hosts -->
                <writeHost host="hostM1" url="localhost:3306" user="root"
                                   password="123456">
                        <!-- can have multi read hosts -->
                        <readHost host="hostS2" url="192.168.1.200:3306" user="root" password="xxx" />
                </writeHost>
                <writeHost host="hostS1" url="localhost:3316" user="root"
                                   password="123456" />
                <!-- <writeHost host="hostM2" url="localhost:3316" user="root" password="123456"/> -->
        </dataHost>
        <!--
                <dataHost name="sequoiadb1" maxCon="1000" minCon="1" balance="0" dbType="sequoiadb" dbDriver="jdbc">
                <heartbeat>             </heartbeat>
                 <writeHost host="hostM1" url="sequoiadb://1426587161.dbaas.sequoialab.net:11920/SAMPLE" user="jifeng"  password="jifeng"></writeHost>
                 </dataHost>

          <dataHost name="oracle1" maxCon="1000" minCon="1" balance="0" writeType="0"   dbType="oracle" dbDriver="jdbc"> <heartbeat>select 1 from dual</heartbeat>
                <connectionInitSql>alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss'</connectionInitSql>
                <writeHost host="hostM1" url="jdbc:oracle:thin:@127.0.0.1:1521:nange" user="base"       password="123456" > </writeHost> </dataHost>

                <dataHost name="jdbchost" maxCon="1000"         minCon="1" balance="0" writeType="0" dbType="mongodb" dbDriver="jdbc">
                <heartbeat>select       user()</heartbeat>
                <writeHost host="hostM" url="mongodb://192.168.0.99/test" user="admin" password="123456" ></writeHost> </dataHost>

                <dataHost name="sparksql" maxCon="1000" minCon="1" balance="0" dbType="spark" dbDriver="jdbc">
                <heartbeat> </heartbeat>
                 <writeHost host="hostM1" url="jdbc:hive2://feng01:10000" user="jifeng"         password="jifeng"></writeHost> </dataHost> -->

        <!-- <dataHost name="jdbchost" maxCon="1000" minCon="10" balance="0" dbType="mysql"
                dbDriver="jdbc"> <heartbeat>select user()</heartbeat> <writeHost host="hostM1"
                url="jdbc:mysql://localhost:3306" user="root" password="123456"> </writeHost>
                </dataHost> -->
</mycat:schema>

正确更改完的样子:

      1 <?xml version="1.0"?>
      2 <!DOCTYPE mycat:schema SYSTEM "schema.dtd">
      3 <mycat:schema xmlns:mycat="http://io.mycat/">
      4 
      5         <schema name="USERDB" checkSQLschema="true" sqlMaxLimit="100" dataNode='dn1'>
      6         </schema>
      7         <dataNode name="dn1" dataHost="localhost1" database="test" />
      8         <dataHost name="localhost1" maxCon="1000" minCon="10" balance="3"
      9                           writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveT        hreshold="100">
     10                 <heartbeat>select user()</heartbeat>
     11                 <!-- can have multi write hosts -->
     12                 <writeHost host="hostM1" url="192.168.200.20:3306" user="root"
     13                                    password="123456">
     14                         <!-- can have multi read hosts -->
     15                         <readHost host="hostS1" url="192.168.200.30:3306" user="root" password=        "123456" />
     16                 </writeHost>
     17         </dataHost>
     18 </mycat:schema> 

这里我总结一下我的修改思路
1.修改第5行的TESTDB"为 USERDBfalse改为 true
在’ >'前面添加 dataNode=‘dn1’.
2.将光标移到第6行,进入 vi编辑器的命令行模式,输入 27dd,意思是删除(事实上是剪切)从这行开始到下面的27行,dd必须小写
3.在第2步的基础上将光标移到第7行,使用dd(不需要输入数字)两次也就是删除第7行和第8行
4.在第3步的基础上编辑第7行db1,将其改为 test
5.将光标放到第8行,进入到命令行模式,输入 6dd
6.在第5步的基础上,修改第8行balance,将它的值从0改为3
7.将光标移到第12行,将localhost改为db1的IP地址,这行认准hostM1
8.将光标移到第15行,将hostS2改为hostS1;将url的值改为:db2的IP地址:3306;将password的值改为123456
9.将光标移到第17行,进入命令行模式,输入3dd
10.在第9步的基础上,将光标移到第18行命令行模式,输入22dd

该文件的详解转载:https://blog.csdn.net/weixin_42098207/article/details/105454315

修改 schema.xml 的用户权限

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

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

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

删除部分:

     95         <user name="user">
     96                 <property name="password">user</property>
     97                 <property name="schemas">TESTDB</property>
     98                 <property name="readOnly">true</property>
     99         </user>

启动Mycat服务:

[root@mycat ~]# /bin/bash /usr/local/mycat/bin/mycat start
Starting Mycat-server...
[root@mycat ~]# jps
4162 Jps
4126 WrapperSimpleApp

安装net-tools,使用netstat -ntpl | grep 8066 or 9066 来查看服务端口

[root@mycat ~]# yum -y install net-tools
[root@mycat ~]# netstat -ntpl | grep 9066
tcp6       0      0 :::9066                 :::*                    LISTEN      4126/java   
[root@mycat ~]# netstat -ntpl | grep 8066
tcp6       0      0 :::8066                 :::*                    LISTEN      4126/java    

安装mariadb-client服务

[root@mycat ~]# yum -y install 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 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 table company ;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'company' at line 1
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.026 sec)

MySQL [USERDB]> insert into company values(2,"abook","aus");
Query OK, 1 row affected (0.004 sec)

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

在 Mycat 虚拟机节点使用 mysql 命令,通过 9066 端口查询对数据库读写操作的分离信息。可以看到所有的写入操作WRITE_LOAD 数都在 db1 主数据库节点上,所有的读取操作READ_LOAD 数都在 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 | 192.168.200.20 | 3306 | W    |      0 |    6 | 1000 |      71 |         0 |          1 |
| dn1      | hostS1 | mysql | 192.168.200.30 | 3306 | R    |      0 |    6 | 1000 |      65 |         5 |          0 |
+----------+--------+-------+----------------+------+------+--------+------+------+---------+-----------+------------+

说明:
我尝试过在主数据库部署了一个单节点应用商城系统
然后使用浏览器打开商城访问了很多商品信息
最后得到的结果是:
EXECUTE 的值会随着访问商品信息的次数变化而变大;而 READ_LOAD 不会
经过和老师的探讨,暂时确定 READ_LOAD 表示的是客户端与数据库建立连接的次数,而非我们读取数据库中的数据的次数 !!!

总结

咬定青山不放松,立根原在破岩中。
千磨万击还坚劲,任尔东西南北风。
----郑燮《竹石》

你可能感兴趣的:(1+x,云计算,运维,linux)