准备三台虚拟机
一台 mycat节点中间键 一台db1主从数据库主节点 一台db2主从数据库的从节点。
修改命令(三台的都要修改一般分清)
hostnamectl set-hostname mycat
bash
修改映射文件
[root@mycat ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
10.30.59.210 mycat
10.30.59.209 db1
10.30.59.208 db2
配置yum3个都要。使用提供的gpmall-repo文件上传至3个虚拟机的/opt目录下
mv /etc/yum.repos.d/* /media/
cat /etc/yum.repos.d/local.repo
[mariadb]
name=mariadb
baseurl=file:///opt/gpmall-repo
gpgcheck=0
enabled=1
在mycat节点安装java环境
[root@mycat ~]# yum -y install java-1.8.0-openjdk java-1.8.0-openjdk-devel
[root@mycat ~]# java -version
openjdk version "1.8.0_222-ea"
OpenJDK Runtime Environment (build 1.8.0_222-ea-b03)
OpenJDK 64-Bit Server VM (build 25.222-b03, mixed mode)
关闭防火墙
[root@mycat ~]# systemctl stop firewalld
[root@mycat ~]# systemctl disable firewalld
[root@mycat ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Docs: man:firewalld(1)
[root@mycat ~]# iptables -F
[root@mycat ~]# iptables -X
[root@mycat ~]# iptables -Z
[root@mycat ~]# iptables-save
# Generated by iptables-save v1.4.21 on Wed Jun 30 00:17:11 2021
*filter
:INPUT ACCEPT [44:2848]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [24:1872]
COMMIT
# Completed on Wed Jun 30 00:17:11 2021
[root@mycat ~]# vi /etc/selinux/config
[root@mycat ~]# setenforce 0
在db1 和 db2。配置数据库
[root@db1 ~]# yum -y install mariadb mariadb-server
启动mariadb
[root@db2 ~]# systemctl start mariadb
[root@db2 ~]# systemctl enable mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
编译数据库
[root@db2 ~]# mysql_secure_installation
默认回车!
y
y
n
y
y
修改主节点配置文件
[mysqld]
log_bin = mysql-bin
binlog_ignore _db = mysql
server_id = 209
重启配置文件
[root@db1 ~]# systemctl restart mariadb
开放主节点的数据权限
[root@db1 ~]# mysql -uroot -p000000
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 5.5.64-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)]> grant all privileges on *.* to root@'%' identified by "000000";
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> grant replication slave on *.* to 'user'@'db2' identified by '000000';
Query OK, 0 rows affected (0.00 sec)
配置从节点db2 同步db1
[root@db2 ~]# mysql -uroot -p000000
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 5.5.68-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
MariaDB [(none)]> change master to master_host='db1',master_user='user',master_password='123456';
Query OK, 0 rows affected (0.02 sec)
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.00 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: 526
Relay_Log_File: mariadb-relay-bin.000002
Relay_Log_Pos: 810
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
验证主从同步,在db1节点创建库test,并创建表company,插入数据并查看表
[root@db1 ~]# mysql -uroot -p000000
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 5.5.68-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)]> create database test;
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.01 sec)
MariaDB [test]> insert into company values(1,"facebook","usa");
Query OK, 1 row affected (0.00 sec)
MariaDB [test]> select * from company;
+----+----------+------+
| id | name | addr |
+----+----------+------+
| 1 | facebook | usa |
+----+----------+------+
1 row in set (0.00 sec)
在db2 同步主节点数据库创建,查看company
[root@db2 ~]# mysql -uroot -p000000
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 11
Server version: 5.5.68-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)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.01 sec)
MariaDB [(none)]> select * from test.company;
+----+----------+------+
| id | name | addr |
+----+----------+------+
| 1 | facebook | usa |
+----+----------+------+
1 row in set (0.00 sec)
部署安装mycat 实现分离中间件服务,上传安装包解压到/usr/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 ~]# vi /etc/profile
export MYCAT_HOME=/usr/local/mycat/
[root@mycat ~]# source /etc/profile
配置Mycat服务读写分离的schema.xml配置文件在/usr/local/mycat/conf/目录下,可以在文件中定义一个逻辑库,使用户可以通过Mycat服务管理该逻辑库对应的MariaDB数据库。在这里定义一个逻辑库schema,name为USERDB;该逻辑库USERDB对应数据库database为test(在部署主从数据库时已安装);设置数据库写入节点为主节点db1;设置数据库读取节点为从节点db2(节点ip是先主在从)
[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="10.30.59.209:3306" user="root" password="123456">
<readHost host="hostS1" url="10.30.59.208: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。
修改用户权限
[root@mycat ~]# chown root:root /usr/local/mycat/conf/schema.xml
编辑mycat访问用户。xuigaipeizhiwenjian
[root@mycat ~]# vi /usr/local/mycat/conf/server.xml
在配置文件的倒数第二部分,
<user name="root">
<property name="password">123456</property>
<property name="schemas">USERDB</property>
然后删除如下几行:
<user name="user">
<property name="password">user</property>
<property name="schemas">TESTDB</property>
<property name="readOnly">true</property>
</user>
启动mycat服务,查看虚拟机端口开放情况,如果有开放8066和9066端口
[root@mycat ~]# /usr/local/mycat/bin/mycat start
netstat -tnlp
验证数据库集群的读写分离功能
[root@mycat ~]# yum install -y MariaDB-client
在Mycat虚拟机上使用mysql命令查看Mycat服务的逻辑库USERDB
[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 |
+----------+
| 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.003 sec)
MySQL [USERDB]> select * from company;
+----+----------+------+
| id | name | addr |
+----+----------+------+
| 1 | facebook | usa |
+----+----------+------+
1 row in set (0.005 sec)
用mycat 添加数据库
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)
验证Mycat服务对数据库读写操作分离
[root@mycat ~]# mysql -h127.0.0.1 -P9066 -uroot -p000000 -e 'show @@datasource;'