栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

数据库读写分离(单主单从+双主双从)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

数据库读写分离(单主单从+双主双从)

单主单从
nameip
node1192.168.44.100安装mycat
node5192.168.44.33
node3192.168.44.11

读写分离的前提是首先要先将数据库搭建成主从模式!

实现目标

实现在两台数据库主机上安装数据库实现主从并在另一台主机上搭建mycat组件实现读写分离

搭建主从 1.安装并初始化操作

在两台主机上安装数据库并初始化

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

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

初始化数据库

#####两台主机同样操作

[root@node5 ~]# systemctl start mariadb        ####首先要开启才能初始化
[root@node5 ~]# mysql_secure_installation 
/usr/bin/mysql_secure_installation: line 379: find_mysql_client: command not found
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:                               #输入数据库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      ###在这个要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!

2.修改配置文件

主从的配置文件 /etc/my.cnf

###主节点###

[root@node5 ~]# vim  /etc/my.cnf
[mysqld]
server_id=33						#id随意两台id不同即可
log_bin=mysql-bin

###从节点## 

[root@node3 ~]# vi /etc/my.cnf
[mysqld]
server_id=11
log_bin=mysql-bin

重启所有节点数据库systemctl restart mariadb

一:配置主从

1:添加用来链接的用户

在主与从的链接过程中需要有一个用户进行链接,这个用户也可以是root用户不过使用root链接不安全

[root@node5 ~]# mysql -uroot -p000000
Welcome to the MariaDB monitor.  Commands end with ; or g.
........
MariaDB [(none)]> create user user001;
Query OK, 0 rows affected (0.018 sec)

2:给链接用户授权

为了试验这里赋予所有权限,生产环境不适合

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

MariaDB [(none)]> grant replication slave on *.* to 'user001'@'%' identified by '000000' ;
Query OK, 0 rows affected (0.003 sec)
3:从链接主

首先在主上查看bin-log文件和接入点

MariaDB [(none)]> show master status ;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000004 |     2299 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.000 sec)

到从节点改变“主”人

MariaDB [(none)]> change master to master_user='user001',master_host='192.168.44.33',master_password='000000',master_log_file='mysql-bin.000004',master_log_pos2209; 
Query OK, 0 rows affected (0.003 sec)

在从节点中开启从属的功能

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

查看状态

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

MariaDB [(none)]> show slave status G
*************************** 1. row ***************************
                Slave_IO_State: Waiting for master to send event
                   Master_Host: 192.168.44.33
                   Master_User: root
                   Master_Port: 3306
                 Connect_Retry: 60
               Master_Log_File: mysql-bin.000004
           Read_Master_Log_Pos: 2299
                Relay_Log_File: node3-relay-bin.000007
                 Relay_Log_Pos: 2413
         Relay_Master_Log_File: mysql-bin.000004
              Slave_IO_Running: Yes												##此处两处都为yes才行
             Slave_SQL_Running: Yes
               Replicate_Do_DB: 

可能用到的命令:

slave的命令:
   RESET SLAVE
   SHOW SLAVE HOSTS
   SHOW SLAVE STATUS
   START SLAVE
   STOP SLAVE
master的命令:
   CHANGE MASTER TO
   PURGE BINARY LOGS
   RESET MASTER
   SHOW BINARY LOGS
   SHOW MASTER STATUS
4:验证

在主节点上创建一个表然后看从节点上有没有出现
此处不在演示

二:配置mycat插件

实现读写分离的关键就是将mycat插件与主从相连然后实现读写分离
MyCat 是目前最流行的基于 java 语言编写的数据库中间件,是一个实现了 MySQL 协议 的服务器,前端用户可以把它看作是一个数据库代理,用 MySQL 客户端工具和命令行访问,而其后端可以用 MySQL 原生协议与多个 MySQL 服务器通信,也可以用 JDBC 协议与大多数主流数据库服务器通信,其核心功能是分库分表。配合数据库的主从模式还可实现读写分离。

1.安装mycat

mycat是由Java写的所以在安装启动前需要安装一下Java依赖

[root@mall ~]# yum install -y java-1.8.0-openjdk java-1.8.0-openjdk-devel
...
[root@mall ~]# 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)

安装mycat的方式有很多种这里采用上传安装包的方式安装

解压到/usr/local下面并授权

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

[root@node1 ~]# chmod -R 777 /usr/local/mycat/

2.修改schema.xml文件

schema.xml文件在mycat目录下的conf(/usr/local/mycat/conf)目录下
schema.xml文件是来决定呢些主机进行读写分离和逻辑数据表的
删除多余部分仅留这部分即可

[root@node1 conf]# vim schema.xml 





        
        
        
        
                select user()
                
                
                        
                        
                
        


在开启mycat之前先验证一下主从这两个数据库能否用远程登录上

###如果不能登录上去将root用户授权即可
[root@node1 ~]# mysql -uroot -p000000 -h192.168.44.11 -P 3306
Welcome to the MariaDB monitor.  Commands end with ; or g.
.......
MariaDB [(none)]> EXIT;
Bye
[root@node1 ~]# mysql -uroot -p000000 -h192.168.44.33 -P 3306
Welcome to the MariaDB monitor.  Commands end with ; or g.
.......
MariaDB [(none)]> EXIT;
Bye

3.修改server.xml文件

server.xml文件是编辑mycat的访问用户,能修改root用户访问的密码和逻辑数据库
在配置文件最后部分修改如下


                000000
                CHINA


删除以下部分


		user
		TESTDB
		true


4.启动mycat

启动mycat到/usr/local/mycat/bin目录下找到mycat

[root@node1 bin]# mycat -h
Usage: /usr/local/mycat/bin/mycat { console | start | stop | restart | status | dump }
[root@node1 bin]# mycat start 
Starting Mycat-server...
Mycat-server is already running.

过命令启动mycat数据库中间件服务,启动后通过netstat -ntpl命令查看虚拟机端口开放情况,如果有开放8066和9066端口说明服务正常启动(记得做域名解析)
在mycat的主机上安装mariadb-client服务登陆mycat的数据库查看逻辑数据库健康情况

[root@node1 ~]# mysql -uroot -p000000 -h127.0.0.1 -P8066
Welcome to the MariaDB monitor.  Commands end with ; or g.
Your MySQL connection id is 3
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 |
+----------+
| CHINA    |
+----------+
1 row in set (0.000 sec)
5.验证mycat对读写分离操作

进入mycat端的数据库

###通过8066端口进入mycat端的数据库
[root@node1 ~]# mysql -uroot -p000000 -h127.0.0.1 -P8066

[root@node1 ~]# mysql -uroot -p000000 -h127.0.0.1 -P8066
Welcome to the MariaDB monitor.  Commands end with ; or g.
Your MySQL connection id is 14
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 |
+----------+
| CHINA    |
+----------+
1 row in set (0.000 sec)

MySQL [(none)]> use CHINA;
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 [CHINA]> SELECT * FROM cluo;
+------+-----------+
| id   | name      |
+------+-----------+
|  333 | node3     |
|    1 | xianggang |
|    2 | aomen     |
|    3 | taiwan    |
+------+-----------+
4 rows in set (0.002 sec)

MySQL [CHINA]> insert into cluo values(0,'china');
Query OK, 1 row affected (0.015 sec)

MySQL [CHINA]> SELECt * FROM cluo;
+------+-----------+
| id   | name      |
+------+-----------+
|  333 | node3     |
|    1 | xianggang |
|    2 | aomen     |
|    3 | taiwan    |
|    0 | china     |
+------+-----------+
5 rows in set (0.002 sec)


在mycat虚拟机节点使用mysql命令通过9066端口查询对数据库读写操作的分离信息。可以看到所有的写入操作WRITE_LOAD数都在db1主数据库节点上,所有的读取操作READ_LOAD数都在db2主数据库节点上。由此可见数据库读写操作已经分离到db1和db2节点上了
mysql -u root -p000000 -h127.0.0.1 -P9066 -e ‘show @@datasource’ #9066管理端口

上面的操作都会进行分流
###通过查看数据来源来验证###
[root@node1 bin]# mysql -u root -p000000 -h127.0.0.1 -P9066 -e 'show @@datasource'
+----------+--------+-------+---------------+------+------+--------+------+------+---------+-----------+------------+
| DATANODE | NAME   | TYPE  | HOST          | PORT | W/R  | ACTIVE | IDLE | SIZE | EXECUTE | READ_LOAD | WRITE_LOAD |
+----------+--------+-------+---------------+------+------+--------+------+------+---------+-----------+------------+
| dn1      | hostM1 | mysql | 192.168.44.33 | 3306 | W    |      0 |   10 | 1000 |      31 |         0 |          2 |
| dn1      | hostS1 | mysql | 192.168.44.11 | 3306 | R    |      0 |    4 | 1000 |      35 |        13 |          0 |
+----------+--------+-------+---------------+------+------+--------+------+------+---------+-----------+------------+

READ_LOAD			读的次数
WRITE_LOAD    写的次数

可以发现当从mycat端进入后对数据库的操作会自动分发到指定的读写分离服务器上

双主双从

环境介绍
nameip
node5192.168.44.33master1
node2192.168.44.200slave1
node3192.168.44.11master2
node4192.168.44.22slave2

搭建双主双从其实就是在单主单从的基础上 扩展一点

原理:
当node2作为从的时候master是node5,这样node2技术node5的从可用在node5进行同步数据,这种情况node5现在的身份是主,当然node5也可以是从呀互不冲突的,当node5作为从的时候将node3作为主这样node5就可以与node3同步数据,实现双主双从的原理就是将两个互相认主将对方当做主机来获取对方的数据。

一:配置双主双从 1.修改配置文件

当master主机即作为主机时也作为从机在配置文件中就要特别声明:
master主机配置文件:

###node5配置文件####

[mysqld]
server_id=33
log_bin=mysql-bin
log-slave-updates								#作为从库的时候有写入操作也要更新二进制日志文件
auto-increment-increment=2      #表示自增字段每次递增的量,
auto-increment-offset=1					#表示自增字段从哪个数开始指字段一次递增多少(要与另一台master不同)

###node3文件###

[mysqld]
server_id=11
log_bin=mysql-bin
log-slave-updates
auto-increment-increment=2
auto-increment-offset=2

salve主机:
node4主机:

[mysqld]
server_id=22
log_bin=mysql-bin

node2主机:

[mysqld]
server_id=200
log_bin=mysql-bin

修改完配置文件后重启服务

systemctl restart mariadb 2.配置master与node

先将两两做成主从(node5与node2,node3与node4),然后再将两台master主机互相认主(node3与node5)
一下演示node5与node2,node3与node4与这相同
查看node5的主信息:

# node5 查看主信息

MariaDB [(none)]> show master status  ;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000004 |     5843 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.000 sec)

node2认主并开启slave

MariaDB [(none)]> change master to master_user='root',master_password='000000',master_host='192.168.44.33',master_log_file='mysql-bin.000004',master_log_pos=5843;
Query OK, 0 rows affected (0.013 sec)

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

查看slave链接情况

 MariaDB [(none)]> show slave status G
*************************** 1. row ***************************
一下两项为yes就OK
 Slave_IO_Running: Yes
 Slave_SQL_Running: Yes

3.配置master与master

由于我们在配置文件中添加了相应的信息所以master节点既可以作主也可以作为从
查看node5节点主信息:

###node5节点信息

MariaDB [(none)]> show master status ;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000004 |     5843 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.000 sec)

node3认node5作为主

###node3节点

MariaDB [(none)]> change master to master_user='root',master_host='192.168.44.33',master_password='000000',master_log_file='mysql-bin.000004',master_log_pos=5483;
Query OK, 0 rows affected (0.011 sec)

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

###查看slave情况

 MariaDB [(none)]> show slave status G
*************************** 1. row ***************************
一下两项为yes就OK
 Slave_IO_Running: Yes
 Slave_SQL_Running: Yes

目前只是node3认node5为主了,这样node5上面的信息会同步到node3上但node3主机上面的无法到node5上面所以还要反向认一下。
查看node3主信息:

MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000002 |     1129 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.001 sec)

node5认node3为主

affected (0.009 sec)

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

MariaDB [(none)]> show slave status G
*************************** 1. row ***************************
###一下里两项为yes就OK了
              Slave_IO_Running: Yes
             Slave_SQL_Running: Yes

4.验证

当两个master互相认主后呢么谁便在一台 master主机上创建个数据库就可以在任意一台主机上查看到

###在master(node3)节点上创建一个库

MariaDB [(none)]> create database chainskill;
Query OK, 1 row affected (0.006 sec)

MariaDB [(none)]> show databases;
+-----------------------------------------+
| Database                                |
+-----------------------------------------+
| chainskill                              |
| information_schema                      |
| mysql                                   |                     
+-----------------------------------------+
14 rows in set (0.007 sec)

在其他master和salve上查看

### 在salve(node4)节点上查看已经出现

MariaDB [(none)]> show databases;
+-----------------------------------------+
| Database                                |
+-----------------------------------------+
| chainskill                              |
| cr7                                     |
| information_schema                      |
| mysql                                   |
+-----------------------------------------+
13 rows in set (0.018 sec)

…最终在每台机子上都可看到

二:配置mycat插件

mycat的schema.xml文件中将读写部分在添加一份

[root@node1 conf]# vim schema.xml 





        
        
        
        
                select user()
                
                
                        
                        
                
                
                        
                        
                
        


修改完后要想保证mycat正常启动并运行要检查各个主机的IO与SQL状态是否都为yes

在mycat的bin目录下用前台启动查看是否成功

[root@node1 bin]# mycat console
Running Mycat-server...
wrapper  | --> Wrapper Started as Console
wrapper  | Launching a JVM...
........
........
jvm 1    | MyCAT Server startup successfully. see logs in logs/mycat.log
已经正常启动了

这样就完成操作了但是这四台数据库服务器到底是是怎么协同工作的呢?
正常情况下master1承担写的操作其余三台都是承担读的操作,当master1有问题后master2才起到写的作用

1.验证读写分离情况
查看读写分离情况

[root@node1 bin]# mysql -u root -p000000 -h127.0.0.1 -P9066 -e 'show @@datasource'
+----------+--------+-------+----------------+------+------+--------+------+------+---------+-----------+------------+
| DATANODE | NAME   | TYPE  | HOST           | PORT | W/R  | ACTIVE | IDLE | SIZE | EXECUTE | READ_LOAD | WRITE_LOAD |
+----------+--------+-------+----------------+------+------+--------+------+------+---------+-----------+------------+
| dn1      | hostM1 | mysql | 192.168.44.33  | 3306 | W    |      0 |   10 | 1000 |     849 |         0 |          0 |
| dn1      | hostM2 | mysql | 192.168.44.11  | 3306 | W    |      0 |    1 | 1000 |     839 |         0 |          0 |
| dn1      | hostS1 | mysql | 192.168.44.200 | 3306 | R    |      0 |    8 | 1000 |     850 |         4 |          0 |
| dn1      | hostS2 | mysql | 192.168.44.22  | 3306 | R    |      0 |    8 | 1000 |     852 |         5 |          0 |
+----------+--------+-------+----------------+------+------+--------+------+------+---------+-----------+------------+

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/657916.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号