一、第一台节点安装MYCAT服务

  1. [root@mycat ~]# yum install -y java-1.8.0-openjdk java-1.8.0-openjdk-devel
  2. [root@mycat ~]# tar -zxvf Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz -C /usr/local/ //解压mycat安装包至/usr/local目录下
  3. [root@mycat ~]# chmod -R 777 /usr/local/mycat/ //将mycat下的包赋予所有权限
  4. [root@mycat ~]# echo export MYCAT_HOME=/usr/local/mycat/ >> /etc/profile //在系统环境下添加Mycat变量
  5. [root@mycat ~]# source /etc/profile

二、编辑Mycat 的逻辑库配置文件

  1. [root@mycat ~]# vi /usr/local/mycat/conf/schema.xml //编辑mycat逻辑库(将原来的文件直接删除然后进行覆盖添加即可)
  2. <?xml version="1.0"?>
  3. <!DOCTYPE mycat:schema SYSTEM "schema.dtd">
  4. <mycat:schema xmlns:mycat="http://io.mycat/">
  5. <schema name="USERDB" checkSQLschema="true" sqlMaxLimit="100" dataNode="dn1"></schema>
  6. <dataNode name="dn1" dataHost="localhost1" database="test" />
  7. <dataHost name="localhost1" maxCon="1000" minCon="10" balance="3" dbType="mysql" dbDriver="native" writeType="0" switchType="1" slaveThreshold="100">
  8. <heartbeat>select user()</heartbeat>
  9. <writeHost host="hostM1" url="192.168.100.22:3306" user="root" password="123456">
  10. <readHost host="hostS1" url="192.168.100.33:3306" user="root" password="123456" />
  11. </writeHost>
  12. </dataHost>
  13. </mycat:schema>
  14. [root@mycat ~]# chown root:root /usr/local/mycat/conf/schema.xml //修改逻辑库配置文件权限

三、编辑Mycta访问用户

  1. [root@mycat ~]# vi /usr/local/mycat/conf/server.xml //编辑文件
  2. 在配置文件的最后部分
  3. <user name="root">
  4. <property name="password">123456</property>
  5. <property name="schemas">USERDB</property> //将TESTDB改为USERDB
  6. 然后删除以下几行内容
  7. <user name="user">
  8. <property name="password">user</property>
  9. <property name="schemas">TESTDB</property>
  10. <property name="readOnly">true</property>
  11. </user>

四、启动Mycat服务

  1. [root@mycat ~]# /bin/bash /usr/local/mycat/bin/mycat start
  2. 启动之后使用netstat -ntpl命令查询端口号,寻找暴露端口80669066端口,如果还想端口暴露则服务正常
  3. [root@mycat ~]# netstat -ntlp
  4. Active Internet connections (only servers)
  5. Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
  6. tcp 0 0 127.0.0.1:32000 0.0.0.0:* LISTEN 11495/java
  7. tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 914/sshd
  8. tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1159/master
  9. tcp6 0 0 :::1984 :::* LISTEN 11495/java
  10. tcp6 0 0 :::8066 :::* LISTEN 11495/java
  11. tcp6 0 0 :::43078 :::* LISTEN 11495/java
  12. tcp6 0 0 :::46792 :::* LISTEN 11495/java
  13. tcp6 0 0 :::9066 :::* LISTEN 11495/java
  14. tcp6 0 0 :::22 :::* LISTEN 914/sshd
  15. tcp6 0 0 ::1:25 :::* LISTEN 1159/master
  16. [root@mycat ~]#

五、验证mycat读写分离信息

  1. [root@mycat ~]# yum install -y MariaDB-client //现在第一台节点上安装数据库插件
  2. 如果安装不上就需要配置gpmall yum
  3. 上传gpmall-reporoot
  4. [root@mycat ~]# cat /etc/yum.repos.d/local.repo
  5. [centos]
  6. name=centos
  7. baseurl=file:///opt/centos
  8. gpgcheck=0
  9. enabled=1
  10. [gpmall]
  11. name=gpmall
  12. baseurl=file:///root/gpmall-repo
  13. gpgcheck=0
  14. enabled=1
  15. [root@mycat ~]#
  16. [root@mycat ~]# mysql -h127.0.0.1 -P8066 -uroot -p123456
  17. Welcome to the MariaDB monitor. Commands end with ; or \g.
  18. Your MySQL connection id is 2
  19. Server version: 5.6.29-mycat-1.6-RELEASE-20161028204710 MyCat Server (OpenCloundDB)
  20. Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
  21. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  22. MySQL [(none)]> show databases;
  23. +----------+
  24. | DATABASE |
  25. +----------+
  26. | USERDB |
  27. +----------+
  28. 1 row in set (0.001 sec)
  29. MySQL [(none)]> use USERDB
  30. Reading table information for completion of table and column names
  31. You can turn off this feature to get a quicker startup with -A
  32. Database changed
  33. MySQL [USERDB]> show tables;
  34. +----------------+
  35. | Tables_in_test |
  36. +----------------+
  37. | company |
  38. +----------------+
  39. 1 row in set (0.003 sec)
  40. MySQL [USERDB]> select * from company;
  41. +----+----------+------+
  42. | id | name | addr |
  43. +----+----------+------+
  44. | 1 | facebook | usa |
  45. +----+----------+------+
  46. 1 row in set (0.005 sec)

六、使用Mycat服务添加数据表

  1. MySQL [USERDB]> insert into company values(2,"bastetball","usa");
  2. Query OK, 1 row affected (0.050 sec)
  3. MySQL [USERDB]> select * from company;
  4. +----+------------+------+
  5. | id | name | addr |
  6. +----+------------+------+
  7. | 1 | facebook | usa |
  8. | 2 | bastetball | usa |
  9. +----+------------+------+
  10. 2 rows in set (0.002 sec)

七、验证读写分离操作

  1. [root@mycat ~]# mysql -h127.0.0.1 -P9066 -uroot -p123456 -e 'show @@datasource;
  2. +----------+--------+-------+----------------+------+------+--------+------+------+---------+-----------+------------+
  3. | DATANODE | NAME | TYPE | HOST | PORT | W/R | ACTIVE | IDLE | SIZE | EXECUTE | READ_LOAD | WRITE_LOAD |
  4. +----------+--------+-------+----------------+------+------+--------+------+------+---------+-----------+------------+
  5. | dn1 | hostM1 | mysql | 192.168.100.22 | 3306 | W | 0 | 10 | 1000 | 64 | 0 | 1 |
  6. | dn1 | hostS1 | mysql | 192.168.100.33 | 3306 | R | 0 | 5 | 1000 | 62 | 4 | 0 |
  7. +----------+--------+-------+----------------+------+------+--------+------+------+---------+-----------+------------+
  8. 在 Mycat 虚拟机节点使用 mysql 命令,通过 9066 端口查询对数据库读写操作的分离信 息。可以看到所有的写入操作 WRITE_LOAD 数都在 db1 主数据库节点上,所有的读取操作 READ_LOAD 数都在 db2 主数据库节点上。