简介:

主从复制的工作机制:
① Master将改变记录到二进制日志(binary log)中,这些记录叫做二进制日志事件(binary log events);
② Slave将master的binary log events拷贝到它的中继日志(relay log);
③ Slave重做中继日志中的事件,将改变反映它自己的数据。

读写分离概念:
基本原理是让主数据库处理事务性增、改、删操作(INSERT、UPDATE、DELETE),而从数据库处理SELECT查询操作。
读写分离的作用:
可以解决数据库写入时影响查询效率的问题

构建读写分离的数据库

ip 主机名 / 节点
10.30.59.193 mycat / Mycat中间件服务节点
10.30.59.194 db1 / MariaDB数据库集群主节点
10.30.59.195 db2 / MariaDB数据库集群从节点

一、基础环境配置

(一)、修改主机名

hostnamectl set-hostname mycat
hostnamectl set-hostname db1
hostnamectl set-hostname db2

(二)、 所有节点编辑hosts节点

cat >>/etc/hosts << EOF
10.30.59.193 mycat
10.30.59.194 db1
10.30.59.195 db2
EOF

(三)、所有节点关闭防火墙

#关闭selinx
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config #关闭firewalld
systemctl stop firewalld && systemctl disable firewalld#关闭iptables
iptables -F
iptables -X
iptables -Z
iptables-save

(四)、所有节点配置Yum源

将gpmall-repo文件上传至虚拟机的/opt目录下


#移走yum源
mv /etc/yum.repos.d/* /media/#建立镜像挂载点并挂载镜像
mkdir /opt/centos
mount /dev/sr0 /opt/centos/  #配置yum源
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

(五)、mycat节点安装JDK环境

yum install -y java-1.8.0-openjdk java-1.8.0-openjdk-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节点安装mariadb服务

yum install -y mariadb mariadb-server

(二)、db1节点和db2节点启动mariadb服务

systemctl start mariadb
systemctl enable mariadb

(三)、db1节点和db2节点初始化MariaDB数据库

[root@db1 ~]# mysql_secure_installation NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDBSERVERS 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:                          #输入数据库root密码000000
Re-enter new password:                 #重复输入密码000000
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!

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

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


[mysqld]
log_bin = mysql-bin                       #记录操作日志
binlog_ignore_db = mysql                  #不同步MySQL系统数据库
server_id = 194                           #数据库集群中的每个节点id都要不同,一般使用IP地址的最后段的数字datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

注意:编译完成配置文件,重启mariadb服务

[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: 10.3.18-MariaDB-log MariaDB ServerCopyright (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';     #授权在任何客户端机器上可以以root用户登录到数据库
Query OK, 0 rows affected (0.001 sec)MariaDB [(none)]> grant replication slave on *.* to user@'db2' identified by '000000';   #创建一个user用户让从节点db2连接,并赋予从节点同步主节点数据库的权限
Query OK, 0 rows affected (0.001 sec)

(六)、配置从节点的数据库权限

[root@db2 ~]# mysql -uroot -p000000
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 15
Server version: 10.3.18-MariaDB MariaDB ServerCopyright (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='000000';    #配置从节点连接主节点的连接信息
Query OK, 0 rows affected (0.052 sec)MariaDB [(none)]> start slave;              #开启从节点服务
Query OK, 0 rows affected (0.001 sec)MariaDB [(none)]> show slave status\G         #查看从节点服务状态
*************************** 1. row ***************************Slave_IO_State: Waiting for master to send eventMaster_Host: db1Master_User: userMaster_Port: 3306Connect_Retry: 60Master_Log_File: mysql-bin.000001Read_Master_Log_Pos: 699Relay_Log_File: db2-relay-bin.000002Relay_Log_Pos: 998Relay_Master_Log_File: mysql-bin.000001Slave_IO_Running: YesSlave_SQL_Running: YesReplicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0Last_Error: Skip_Counter: 0Exec_Master_Log_Pos: 699Relay_Log_Space: 1305Until_Condition: NoneUntil_Log_File: Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0Master_SSL_Verify_Server_Cert: NoLast_IO_Errno: 0Last_IO_Error: Last_SQL_Errno: 0Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 194Master_SSL_Crl: Master_SSL_Crlpath: Using_Gtid: NoGtid_IO_Pos: Replicate_Do_Domain_Ids: Replicate_Ignore_Domain_Ids: Parallel_Mode: conservativeSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update itSlave_DDL_Groups: 2
Slave_Non_Transactional_Groups: 0Slave_Transactional_Groups: 0
1 row in set (0.000 sec)

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

`主节点db1的数据库中创建库test,并在库test中创建表company,插入表数据`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.014 sec)MariaDB [test]>  insert into company values(1,"facebook","usa");
Query OK, 1 row affected (0.003 sec)MariaDB [test]> select * from company;
+----+----------+------+
| id | name     | addr |
+----+----------+------+
|  1 | facebook | usa  |
+----+----------+------+
1 row in set (0.000 sec)`在从节点db2查询test数据库与表company`
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.002 sec)MariaDB [(none)]> select * from test.company;
+----+----------+------+
| id | name     | addr |
+----+----------+------+
|  1 | facebook | usa  |
+----+----------+------+
1 row in set (0.001 sec)可以查询到信息,说明主从数据库集群正常运行。

三、部署Mycat读写分离中间件服务

(一)、安装mycat服务

软件包Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz上传到mycat的/节点root目录下

[root@mycat ~]# tar -zxf Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz -C /usr/local
[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的逻辑库配置文件

[root@mycat ~]# cat >/usr/local/mycat/conf/schema.xml << EOF
> <?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>     #定义一个逻辑库schema,name为USERDB
> <dataNode name="dn1" dataHost="localhost1" database="test" />          #该逻辑库USERDB对应数据库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.194:3306" user="root" password="000000">   #设置数据库写入节点为主节点db1
>         <readHost host="hostS1" url="10.30.59.195:3306" user="root" password="000000" />   #设置数据库读取节点为从节点db2
>     </writeHost>
> </dataHost>
> </mycat:schema>
> EOF
 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。

(三)、修改Mycat的逻辑库配置文件权限

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

(四)、编辑mycat的访问用户

修改/usr/local/mycat/conf/server.xml配置文件

`修改root用户的访问密码与数据库,密码设置为123456,访问Mycat的逻辑库为USERDB`····································
····························<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服务

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

使用netstat -ntpl命令查看虚拟机端口开放情况,如果有开放8066和9066端口,则表示Mycat服务开启成功。

四、验证数据库集群服务读写分离功能

(一)、在mycat节点上安装MariaDB-client服务。

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 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.002 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 -ADatabase changed
MySQL [USERDB]> show tables;     # 查看所有表
+----------------+
| Tables_in_test |
+----------------+
| company        |
+----------------+
1 row in set (0.002 sec)MySQL [USERDB]> select * from company;
+----+----------+------+
| id | name     | addr |
+----+----------+------+
|  1 | facebook | usa  |
+----+----------+------+
1 row in set (0.055 sec)

(三)、用Mycat服务添加表数据

MySQL [USERDB]>  insert into company values(2,"bastetball","usa");
Query OK, 1 row affected (3.541 sec)MySQL [USERDB]> select * from company;
+----+------------+------+
| id | name       | addr |
+----+------------+------+
|  1 | facebook   | usa  |
|  2 | bastetball | usa  |
+----+------------+------+
2 rows in set (0.002 sec)

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

在mycat节点使用mysql命令,通过9066端口查询对数据库读写操作的分离信息。

[root@mycat ~]#  mysql -h127.0.0.1 -P9066 -uroot -p123456 -e 'show  @@datasource;'

mycat-读写分离相关推荐

  1. MyCat读写分离-笔记(四)

    概述 Mycat能够实现数据库读写分离,不能实现主从同步,数据库的备份还是基于数据库层面的.Mycat只是数据库的中间件: Mycat读写分离配置 在MySQL中间件出现之前,对于MySQL主从集群, ...

  2. mycat读写分离部署步骤

    1.下载mycat: https://raw.githubusercontent.com/MyCATApache/Mycat-download/master/1.5-RELEASE/Mycat-ser ...

  3. 应用 | 同学,该学MyCat实际应用案例与MyCat读写分离了

    Hi!我是小小,一个双鱼座的佛系程序猿,今日的blog将会写关于MyCat最后一点学习内容,读写分离,与MyCat实际应用案例和一个小小的例子. MyCat 读写分离 MyCat的读写分离是建立在My ...

  4. Mycat读写分离笔记Windows

    Mycat读写分离笔记Windows 自己搭了一个运用Mycat中间件搭建了一个读写分离的demo,昨晚还在奋战当中,因为连接mycat的时候老是报错:No Mycat DataBases selec ...

  5. Mycat 读写分离、主从切换、分库分表的操作记录

    Mycat 读写分离.主从切换.分库分表的操作记录 系统开发中,数据库是非常重要的一个点.除了程序的本身的优化,如:SQL语句优化.代码优化,数据库的处理本身优化也是非常重要的.主从.热备.分表分库等 ...

  6. Mycat读写分离、主从切换、分库分表的操作记录 https://www.cnblogs.com/kevingrace/p/9365840.html

    [此篇文章写得不错] 系统开发中,数据库是非常重要的一个点.除了程序的本身的优化,如:SQL语句优化.代码优化,数据库的处理本身优化也是非常重要的.主从.热备.分表分库等都是系统发展迟早会遇到的技术问 ...

  7. mysql主从和mycat读写分离的安装及验证

    目录 一.背景介绍 二.安装mysql数据库(主从机器都需要先这样安装) 三.主从机配置 1.主服务器进行如下操作 2.从服务器进行如下操作 四.代理服务器安装和配置mycat读写分离 五.主从复制. ...

  8. MyCAT读写分离分库分表

    MyCAT读写分离及分库分表 第1章 MyCAT分布式系统解决方案 1.1 分布式系统介绍: 分布式系统特性: 1. 透明性: a) 分布式系统对用户来说是透明的,一个分布式系统在用户面前的表现就像一 ...

  9. web 项目 连接mycat 读写分离失效问题

    eg:mycat 读写分离已配好,在navicat工具上查询操作是可以的,但是在项目中,查数据就走写库 mycat 读写分离不支持走事务,查询方法开启了事务管理,因此造成读写数据都走了读库

  10. 运维之道 | Mysql主从复制+mycat读写分离

    运维之道 | Mysql主从复制 + Mycat读写分离 1.什么是读写分离 读写分离,基本的原理是让主数据库处理事务性增.删.改操作(INSERT.UPDATE.DROP),而从数据库处理SELEC ...

最新文章

  1. python小游戏源码-python21点小游戏源码免费下载
  2. [OS复习]设备管理3
  3. sqlserver 实现数据库全文检索
  4. 二叉树:HDU1754
  5. WPF中MVVM模式的 Event 处理
  6. 分号是不是c语言的一部分,问什么C程序里总是提示缺少分号;,而明明有分号?...
  7. Log4J 1.x 配置详解
  8. archman linux教程,Archman GNU/Linux 2020-01 发布,基于Arch的Xfce桌面发行版
  9. db2 c语言,DB2数据库安全(二)——身份认证
  10. 适合iOS的15大网站推荐
  11. Kali学习 | 密码攻击:6.10 创建密码字典
  12. 画法几何,工程制图基础.....多角度平面投影图推断立体空间结构,实际距离的判别等
  13. 地下城与勇士(DNF)安图恩副本(黑雾之源、震颤的大地、舰炮防御战、擎天之柱、能量阻截战、黑色火山、安徒恩的心脏)(童年的回忆)
  14. MCU简单控制DAC芯片应用(以DAC8550为例)
  15. SQL中row_numer、rank、dense_rank的区别与用途
  16. 看美国无线路由器品牌用户满意度排行榜
  17. 自动化测试学习笔记1
  18. python3.7安装keras教程_keras教程-02-tensorflow和keras安装
  19. 详细解读阿里云开源PolarDB总体架构和企业级特性
  20. PID控制中P、I、D参数的作用是什么(转载)

热门文章

  1. iStat Menus 6.51.1164 优秀的系统监控工具
  2. Preisach模型
  3. USB媒体设备端口绑定(以海康会议摄像头为例)
  4. 【QZSS L6E 增强服务改正数支持的 PPP 性能评估】
  5. 以太坊模拟器Ganache v7重磅发布!
  6. 详细nginx配置websocket的wss协议
  7. 台达DVP-EH3系列PLC如何实现远程编程调试和程序上下载
  8. 暗黑下品中文图文全集,mo下载。没听说过的就别进了,不是爱好者打不开
  9. 马克思主义基本原理概论——导论
  10. 创业公司感叹养不起程序员:他们的工资有泡沫吗? | 百度宣布开源自主驾驶软件...