MysqlmysqlMySQL数据库

MySql主从从同步结构配置

2023-02-22  本文已影响0人  技术老男孩

一、环境准备:

Ip地址 主机名 角色 数据管理员密码 公共配置
192.168.88.50 Host50 Master服务器 123qqq…A 关闭防火墙
禁用selinux
192.168.88.51 Host51 Slave服务器 123qqq…A 关闭防火墙
禁用selinux
192.168.88.52 Host52 Slave服务器 123qqq…A 关闭防火墙
禁用selinux

二、配置流程:

1、50主机配置Master
2、51主机配置Slave
3、51主机配置Master
4、52主机配置Slave

三、实操:

第一步:50主机配置Master

[root@host50 ~]# vim /etc/my.cnf
[mysqld]
server_id=50  # 指定server_id
log_bin=master50   # 定义日志名
:wq
[root@host50 ~# systemctl  restart mysqld
# 进入mysql服务
[root@host50 ~]# mysql -uroot -p123qqq...A
# 创建用户并授权
mysql> grant replication slave on *.*  
    -> to repluser@"%" identified by "123qqq…A";
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> show master status;
+-----------------+----------+--------------+------------------+-------------------+
| File            | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-----------------+----------+--------------+------------------+-------------------+
| master50.000001 |      154 |              |                  |                   |
+-----------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

第二步:51主机配置Slave

[root@host51 ~]# vim /etc/my.cnf
[mysqld]
server_id=51  # 指定server_id
log_slave_updates # 允许级联复制(host51主机主服务器的数据拷贝给自己的从服务器)
:wq
[root@host51 ~]# systemctl  restart mysqld
[root@host51 ~]# mysql -uroot -p123qqq...A
mysql> change master to  
 -> master_host="192.168.88.50", # 主服务器Ip地址
 -> master_user="repluser" , # 主服务器添加用户名
 -> master_password="123qqq...A", # 用户密码
 -> master_log_file="master50.000001" , # 主服务器binlog日志名
 -> master_log_pos=154 ; # 偏移量
mysql> start slave;
mysql> show slave status \G 

第二步:51主机配置Master

[root@host51 ~]# mysql -uroot -p123qqq...A
# 创建用户并授权
mysql> grant replication slave on *.*  
    -> to repluser@"%" identified by "123qqq…A";
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> show master status;
+-----------------+----------+--------------+------------------+-------------------+
| File            | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-----------------+----------+--------------+------------------+-------------------+
| master51.000001 |      154 |              |                  |                   |
+-----------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

第三步:52主机配置Slave

[root@host52 ~]# vim /etc/my.cnf
[mysqld]
server_id=52  # 指定server_id
:wq
[root@host52 ~]# systemctl  restart mysqld
[root@host52 ~]# mysql -uroot -p123qqq...A
mysql> change master to  
 -> master_host="192.168.88.51", # 主服务器Ip地址
 -> master_user="repluser" , # 主服务器添加用户名
 -> master_password="123qqq...A", # 用户密码
 -> master_log_file="master51.000001" , # 主服务器binlog日志名
 -> master_log_pos=154 ; # 偏移量
mysql> start slave;
mysql> show slave status \G 
[root@host50 ~]# mysql -uroot -p123qqq...A 
create database  gamedb;
create table  gamedb.a(id int);
insert into gamedb.a values(888);

[root@host51~]# mysql -uroot -p123qqq...A 
Mysql>select * from gamedb.a;
+------+
| id   |
+------+
|  888 |
+------+
        
[root@host52~]# mysql -uroot -p123qqq...A 
Mysql>select * from gamedb.a'
+------+
| id   |
+------+
|  888 |
+------+
上一篇下一篇

猜你喜欢

热点阅读