亚马逊云 RDB数据库故障转移(多可用区)

RDB关系数据库(Relational Database,RDB)

创建名为VPC for RDS的vpc

两个可用区,两组公内网

创建安全组

创建RDS数据库实例用的数据库子网组

创建RDS数据库实例

创建数据库

连接RDS数据库实例并给数据库test添加数据

1.创建安全组

2.创建用来连接数据库实例的EC2


选择vpc for rds那个vpc的public subnet2,因为mysql的安全组只允许这一个网段,然后自动分配公有IP

3.连接并修改数据库

复制endpoint,去到ec2的终端连接数据库

[ec2-user@ip-10-1-3-139 ~]$ sudo -i
[root@ip-10-1-3-139 ~]# yum -y install mysql

配置数据库

[root@ip-10-1-3-139 ~]# mysql -h hello-db.c9d168zhminz.rds.cn-northwest-1.amazonaws.com.cn -u cstor -p12345678
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 19
Server version: 8.0.28 Source distribution

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)]> 
MySQL [(none)]> 
MySQL [(none)]> use test
Database changed
MySQL [test]> create table user (name varchar(20), address varchar(20), primary key (name));
Query OK, 0 rows affected (0.10 sec)

MySQL [test]> show tables;
+----------------+
| Tables_in_test |
+----------------+
| user           |
+----------------+
1 row in set (0.00 sec)

MySQL [test]> insert into user (name, address) values ("mqy12", "wuhan" );
Query OK, 1 row affected (0.01 sec)

MySQL [test]> select * from user;
+-------+---------+
| name  | address |
+-------+---------+
| mqy12 | wuhan   |
+-------+---------+
1 row in set (0.00 sec)

MySQL [test]> 

部署web应用

上传文件

部署

[root@ip-10-1-3-139 ~]# cd /home/ec2-user/
[root@ip-10-1-3-139 ec2-user]# ls
server.conf  webServer
[root@ip-10-1-3-139 ec2-user]# chmod a+x webServer 
[root@ip-10-1-3-139 ec2-user]# vim server.conf 
[root@ip-10-1-3-139 ec2-user]# cat server.conf 
[mysql_server]
mysql_host = "hello-db.c9d168zhminz.rds.cn-northwest-1.amazonaws.com.cn"
mysql_port = "3306"
mysql_user = "cstor"
mysql_password = "12345678"
mysql_db = "test"
[root@ip-10-1-3-139 ec2-user]# nohup ./webServer &
[1] 2787
[root@ip-10-1-3-139 ec2-user]# nohup: ignoring input and appending output to ‘nohup.out’

[root@ip-10-1-3-139 ec2-user]# 
[root@ip-10-1-3-139 ec2-user]# curl localhost
hello mqy12, from wuhan[root@ip-10-1-3-139 ec2-user]# curl localhost
hello mqy12, from wuhan[root@ip-10-1-3-139 ec2-user]# curl localhost
hello mqy12, from wuhan[root@ip-10-1-3-139 ec2-user]# 

实施故障转移


reboot mysql实施故障转移

可以发现ip地址发生了变化,变化到另一个可用区的私有地址

[root@ip-10-1-3-139 ec2-user]# while true; do host hello-db.c9d168zhminz.rds.cn-northwest-1.amazonaws.com.cn; sleep 1; done
hello-db.c9d168zhminz.rds.cn-northwest-1.amazonaws.com.cn has address 10.1.2.160
hello-db.c9d168zhminz.rds.cn-northwest-1.amazonaws.com.cn has address 10.1.2.160
hello-db.c9d168zhminz.rds.cn-northwest-1.amazonaws.com.cn has address 10.1.2.160
hello-db.c9d168zhminz.rds.cn-northwest-1.amazonaws.com.cn has address 10.1.4.87
hello-db.c9d168zhminz.rds.cn-northwest-1.amazonaws.com.cn has address 10.1.4.87
hello-db.c9d168zhminz.rds.cn-northwest-1.amazonaws.com.cn has address 10.1.4.87


等待一会儿后,可用区也会发生改变

使用多可用区部署时,AmazonRDS会在与子网关联的一个可用区创建主数据库实例。然后,AmazonRDS在与不同子网关联的不同可用区中创建备用数据库实例

转移的过程时延很小。

posted @ 2022-10-26 17:35  supermao12  阅读(114)  评论(0编辑  收藏  举报