欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

mysql5.7数据库主从同步、双机热备高可用集群的实现

程序员文章站 2022-04-09 10:29:32
...

mysql5.7数据库主从同步、双机热备高可用集群的实现

两台CentOS7服务器:

  • bigdata1:192.168.239.131
  • bigdata2:192.168.239.132

192.168.239.131和192.168.239.132,
两台服务器上的mysql5.7数据库互为主从,搭建主从同步、双击热备的高可用mysql集群。

1 在两台centos 7服务器上安装mysql 5.7.32

首先按照下面这篇博客在两台centos 7服务器上安装mysql 5.7.32。
CentOS7离线安装mysql5.7.32:
https://blog.csdn.net/zhengzaifeidelushang/article/details/109786495

2 bigdata1服务器mysql数据库为主数据库,bigdata2服务器mysql数据库为从数据库

2.1 bigdata1主服务器的配置

1)配置文件my.cnf

vim /etc/my.cnf

server-id = 1
log_bin = master-bin
log_bin_index = master-bin.index
binlog-do-db = dw

2) 创建从服务器的用户和权限

[aaa@qq.com ~]# mysql -uroot -p
#创建从数据库的masterbackup用户和权限
mysql> grant replication slave on *.* to 'masterbackup'@'192.168.239.132' identi
fied by '123456!a';

mysql> flush privileges;

3)重启mysql服务

[aaa@qq.com ~]# systemctl restart mysqld.service

4) 查看主服务器状态

[aaa@qq.com ~]# mysql -uroot -p
Enter password:

#查看主服务器状态
mysql> show master status;
+------------------+----------+--------------+------------------+---------------                                                                            ----+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_                                                                            Set |
+------------------+----------+--------------+------------------+---------------                                                                            ----+
| master-bin.000001 |      154 | dw           |                  |                                                                                               |
+------------------+----------+--------------+------------------+---------------                                                                            ----+
1 row in set (0.00 sec)

2.2 bigdata2从服务器的配置

1)配置文件my.cnf

[aaa@qq.com ~]# vim /etc/my.cnf
server-id=2
relay-log=slave-relay-bin
relay-log-index=slave-relay-bin.index

2)重启mysql服务

[aaa@qq.com ~]# systemctl restart mysqld.service

3)bigdata2从服务器mysql数据库连接bigdata1主服务器mysql数据库

[aaa@qq.com ~]# mysql -uroot -p
Enter password:

#连接bigdata1主服务器mysql数据库
mysql> change master to master_host='192.168.239.131',master_port=3306,master_user='masterbackup',master_password='123456!a',master_log_file='master-bin.000001',master_log_pos=154;

4) 启动slave数据同步,查看slave信息

mysql> start slave;
Query OK, 0 rows affected (0.01 sec)

#停止slave数据同步可以使用stop slave;

# 查看slave信息
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.239.131
                  Master_User: masterbackup
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 154
               Relay_Log_File: slave-relay-bin.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
         
          Exec_Master_Log_Pos: 154
              Relay_Log_Space: 527
           
             Master_Server_Id: 1
                  Master_UUID: 0e142ba9-29ad-11eb-8fe7-0050562cbc45
             Master_Info_File: /moudle/mysql5.7/data/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
    

mysql5.7数据库主从同步、双机热备高可用集群的实现

2.3 测试mysql5.7数据库主从同步

bigdata1服务器上的mysql为主数据库,bigdata2服务器上的mysql数据库为从数据库

1)登录bigdata1服务器上主mysql数据库,进入dw数据库插入数据,查看数据是否同步到bigdata2服务器上的从mysql数据库

mysql> use dw;
mysql> insert into stu_info values('02','小乔','2001-01-01','女');
mysql> insert into stu_info values('03','黄忠','1991-01-01','男');

bigdata2服务器上的从mysql数据库查看数据

mysql> use dw;

mysql> select * from stu_info;
+------+--------+---------------------+------+
| id   | name   | age                 | sex  |
+------+--------+---------------------+------+
| 01   | 赵云   | 2000-01-01 00:00:00 ||
| 02   | 小乔   | 2001-01-01 00:00:00 ||
| 03   | 黄忠   | 1991-01-01 00:00:00 ||
+------+--------+---------------------+------+
3 rows in set (0.00 sec)

在bigdata2服务器上的从mysql数据库能够成功查询到数据,完成bigdata1服务器mysql数据库为主数据库,bigdata2服务器mysql数据库为从数据库的主从同步、双机热备高可用。

2)现在测试在在bigdata2服务器上的从mysql数据库插入数据,是否能同步到bigdata1服务器的主mysql数据库

bigdata2服务器mysql插入数据

mysql> insert into stu_info values('05','诸葛亮','2008-01-01','男');
Query OK, 1 row affected (0.01 sec)

mysql> select * from stu_info;
+------+-----------+---------------------+------+
| id   | name      | age                 | sex  |
+------+-----------+---------------------+------+
| 01   | 赵云      | 2000-01-01 00:00:00 ||
| 02   | 小乔      | 2001-01-01 00:00:00 ||
| 03   | 黄忠      | 1991-01-01 00:00:00 ||
| 04   | 大乔      | 2000-01-01 00:00:00 ||
| 05   | 诸葛亮    | 2008-01-01 00:00:00 ||
+------+-----------+---------------------+------+
5 rows in set (0.00 sec)

bigdata1服务器mysql查询数据

mysql> select * from stu_info;
+------+--------+---------------------+------+
| id   | name   | age                 | sex  |
+------+--------+---------------------+------+
| 01   | 赵云   | 2000-01-01 00:00:00 ||
| 02   | 小乔   | 2001-01-01 00:00:00 ||
| 03   | 黄忠   | 1991-01-01 00:00:00 ||
| 04   | 大乔   | 2000-01-01 00:00:00 ||
+------+--------+---------------------+------+

查询后发现在bigdata2服务器从mysql数据库插入的诸葛亮这条数据,并未成功同步到bigdata1服务器的mysql1数据库,因此下面要实现bigdata1和bigdata2服务器的mysql数据库互为主从数据库。

3 实现bigdata1和bigdata2服务器的mysql数据库互为主从数据库