老规矩现看理论~~~了解了解了解结构模式~~~
什么是结构模式
MySQL主从同步宏观问题
研究多个数据库服务器之间的关系,分清主和从角色的服务器
结构模式
-
一主一从
-
一主多从
-
链式复制
-
互为主
MySQL一主多从结构搭建
注意:主的防火墙要关掉哦,修改配置记得关闭(stop slave)I/O和sql线程哦~~
构建server50<--server51-->server52结构
server51操作(数据对比ab1.sql已经改变,需重新备份)
[root@server51 ~]# mysqldump -hlocalhost -uroot -p'123qqq...A' -A > ab2.sql #备份数据
[root@server51 ~]# scp ab2.sql 192.168.88.50:/root #发送数据
[root@server51 ~]# mysql -hlocalhost -uroot -p'123qqq...A' \
> -e "SHOW MASTER STATUS;" #查看当前正在使用的binlog日志信息
+------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------+----------+--------------+------------------+-------------------+
| db1.000003 | 28901226 | | | |
+------------+----------+--------------+------------------+-------------------+
[root@server51 ~]#
server50操作
[root@server50 ~]# rm -rf /etc/yum.repos.d/* #清理本地repo文件
[root@server50 ~]# scp 192.168.88.240:/etc/yum.repos.d/*.repo /etc/yum.repos.d/ #从Yum服务器拷贝repo文件到本地
[root@server50 ~]# yum clean all; yum repolist #测试本地Yum可用
...
repolist: 10,085
[root@server50 ~]# yum -y install mysql-community-* #安装MySQL相关软件包
[root@server50 ~]# systemctl enable mysqld #设置MySQL服务开机自启动
[root@server50 ~]# systemctl start mysqld #启动MySQL服务
[root@server50 ~]# ss -antpul | grep 3306 #确认3306端口被监听
tcp LISTEN 0 80 :::3306 :::* users:(("mysqld",pid=1158,fd=19))
[root@server50 ~]#
[root@server50 ~]# cat /var/log/mysqld.log | grep password #获取初始密码
2023-05-16T15:35:21.988849Z 1 [Note] A temporary password is generated for root@localhost: t7f3XGL0Q;Ld
[root@server50 ~]# mysqladmin -hlocalhost -uroot -p't7f3XGL0Q;Ld' password '123qqq...A' #命令行修改初始密码
[root@server50 ~]# vim /etc/my.cnf #修改主配置文件
[root@server50 ~]# sed -rn '4,5p' /etc/my.cnf
[mysqld]
server_id=50 #设置serverid值
[root@server50 ~]# systemctl restart mysqld #重启服务配置生效
[root@server50 ~]# ls ab2.sql
ab2.sql
[root@server50 ~]# mysql -hlocalhost -uroot -p'123qqq...A' < ab2.sql #还原备份数据(确保数据统一)
[root@server50 ~]# mysql -hlocalhost -uroot -p'123qqq...A' #登录数据库服务
mysql> SHOW DATABASES; #确认数据还原情况
mysql> CHANGE MASTER TO
-> MASTER_HOST = "192.168.88.51",
-> MASTER_USER = "repluser",
-> MASTER_PASSWORD = "123qqq...A",
-> MASTER_LOG_FILE = "db1.000003",
-> MASTER_LOG_POS = 28901226; #CHANGE MASTER TO 语句设置主服务器信息
Query OK, 0 rows affected, 2 warnings (0.01 sec)
mysql> START SLAVE; #启动IO/SQL线程
Query OK, 0 rows affected (0.00 sec)
mysql> SHOW SLAVE STATUS\G #确认主从同步状态
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.88.51
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: db1.000003
Read_Master_Log_Pos: 28901226
Relay_Log_File: server50-relay-bin.000002
Relay_Log_Pos: 314
Relay_Master_Log_File: db1.000003