OpenShift 4 - 部署MySQL主从复制数据库(附视频)

89 篇文章 3 订阅
7 篇文章 0 订阅

OpenShift 4.x HOL教程汇总
说明:本文已经在 OpenShift 4.11 环境中验证。

通过模板部署数据库

  1. 创建 Template
$ oc create -f https://raw.githubusercontent.com/liuxiaoyu-git/mysql-container/master/examples/replica/mysql_replica.json
  1. 在OpenShift控制台Developer Catalog中先创建mysql项目,然后找到mysql-replication-example的Template。
    在这里插入图片描述

  2. 根据以下配置创建 MySQL 数据库,其中项目选中 mysql。
    在这里插入图片描述

  3. 完成后查看创建的对象。

$ oc get all -n mysql
NAME                        READY   STATUS      RESTARTS   AGE
pod/mysql-master-1-deploy   0/1     Completed   0          2m21s
pod/mysql-master-1-nz75p    1/1     Running     0          2m9s
pod/mysql-slave-1-deploy    0/1     Completed   0          2m21s
pod/mysql-slave-1-kpk4r     1/1     Running     0          2m9s
 
NAME                                   DESIRED   CURRENT   READY   AGE
replicationcontroller/mysql-master-1   1         1         1       2m21s
replicationcontroller/mysql-slave-1    1         1         1       2m21s
 
NAME                   TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)    AGE
service/mysql-master   ClusterIP   None         <none>        3306/TCP   2m22s
service/mysql-slave    ClusterIP   None         <none>        3306/TCP   2m22s
 
NAME                                              REVISION   DESIRED   CURRENT   TRIGGERED BY
deploymentconfig.apps.openshift.io/mysql-master   1          1         1         config
deploymentconfig.apps.openshift.io/mysql-slave    1          1         1         config
  1. 查看PVC
$ oc get pvc -n mysql
NAME           STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
mysql-master   Bound    pvc-33fdf122-163e-43e5-8047-c86e38d9281b   1Gi        RWO            gp2            3m55s
mysql-slave    Bound    pvc-ef03875c-6eb0-4b0e-8993-84c387a02832   1Gi        RWO            gp2            3m55s

数据复制验证

  1. 在第一个Terminal中进入运行master的pod。
$ oc rsh dc/mysql-master
  1. 登录MySQL,然后查看复制的Slave数据库主机地址。注意主机的IP地址是对应MySQL Slave的Pod地址。
sh-4.2$ mysql -u master -ppassword
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 8.0.26 Source distribution
 
mysql> SHOW SLAVE HOSTS;
+------------+-------------+------+-----------+--------------------------------------+
| Server_id  | Host        | Port | Master_id | Slave_UUID                           |
+------------+-------------+------+-----------+--------------------------------------+
| 3941291735 | 10.131.0.24 | 3306 |         1 | 850d0c61-c9b2-11ea-b3b1-0a580a83000e |
+------------+-------------+------+-----------+--------------------------------------+
1 row in set (0.00 sec)
 
mysql> exit
Bye
  1. 在Master MySQL中的 userdb 数据库中创建一个表,并添加记录。
sh-4.2$ mysql -u user -ppassword
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 16
Server version: 8.0.26 Source distribution

mysql> use userdb;
Database changed
 
mysql> create table test(a char(2));
Query OK, 0 rows affected (0.03 sec)
 
mysql> insert into test value('1');
Query OK, 1 row affected (0.01 sec)
 
mysql> insert into test value('2');
Query OK, 1 row affected (0.01 sec)

mysql> select * from test;
+------+
| a    |
+------+
| 2    |
+------+
1 row in set (0.00 sec)
  1. 在第二个Terminal中进入运行 MySQL Slave 的 pod。
$ oc rsh dc/mysql-slave
  1. 登录MySQL数据库,确认可以在Slave的userdb数据库的test表中看到在master节点实例上添加的记录。
sh-4.2$ mysql -u user -ppassword
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 8.0.26 Source distribution
 
mysql> use userdb;
Database changed

mysql> select * from test;
+------+
| a    |
+------+
| 1    |
+------+
| 2    |
+------+
1 row in set (0.00 sec)
  1. 在第一个Terminal中执行命令,从MySQL Slave的测试表中删除一个记录。
mysql> delete from test where a='1';
Query OK, 1 row affected (0.00 sec)

mysql> select * from test;
+------+
| a    |
+------+
| 2    |
+------+
  1. 在第二个Terminal中执行命令确认MySQL Slave的测试表也删除了对应数据记录。
mysql> select * from test;
+------+
| a    |
+------+
| 2    |
+------+

演示视频

视频

参考

https://docs.openshift.com/online/pro/using_images/db_images/mysql.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值