案例:配置一主多从结构

1 问题

1)基于一主一从的案例,把结构配置为一主多从结构

  • 配置192.168.88.55为192.168.88.53主机的从服务器
  • 客户端测试配置。

2 方案

准备新的服务器,要求如表-2所示

mysql主从同步(一主多从)_一主多从

mysql主从同步(一主多从)_一主多从_02

表-2

在真机创建虚拟机

实验拓扑如图-2所示。

mysql主从同步(一主多从)_mysql_03

图-2

3 步骤

实现此案例需要按照如下步骤进行。

步骤一:配置192.168.88.55主机

1)指定MySQL55主机的server-id 并重启数据库服务

[root@mysql55 ~]# vim /etc/my.cnf.d/mysql-server.cnf
 [mysqld]
 server-id=55
 :wq
 [root@mysql55 ~]# systemctl  restart mysqld

步骤二:配置192.168.88.53主机

1)查看正在使用的binlog日志

[root@mysql53 ~]# mysql -e 'show master status'
 +----------------+----------+--------------+------------------+-------------------+
 | File           | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
 +----------------+----------+--------------+------------------+-------------------+
 | mysql53.000002 |      156 |              |                  |                   |
 +----------------+----------+--------------+------------------+-------------------+
 [root@mysql53 ~]#

2)完全备份

[root@mysql53 ~]# mysqldump  -B gamedb > /root/gamedb.sql

3)把备份文件拷贝给从服务器mysql55

[root@mysql53 ~]# scp  /root/gamedb.sql  root@192.168.88.55:/root/

步骤三:配置192.168.88.55主机

1)mysql55主机使用备份文件恢复数据

[root@mysql55 ~]# mysql < /root/gamedb.sql

2)在MySQL55主机指定主服务器信息

注意:日志名和偏移量 要写 在mysql53主机执行完全备份之前查看到的日志名和偏移量

[root@mysql55 ~]# mysql 
 mysql> change master to  
 master_host="192.168.88.53",
 master_user="repluser",
 master_password="123qqq...A",
 master_log_file="mysql53.000002", 
 master_log_pos=156;

3)启动slave进程

mysql> start slave;

4)查看状态信息

mysql> show slave status \G
 *************************** 1. row ***************************
                Slave_IO_State: Waiting for source to send event
                   Master_Host: 192.168.88.53
                   Master_User: repluser
                   Master_Port: 3306
                 Connect_Retry: 60
               Master_Log_File: mysql53.000002
           Read_Master_Log_Pos: 156
                Relay_Log_File: mysql55-relay-bin.000002
                 Relay_Log_Pos: 322
         Relay_Master_Log_File: mysql53.000002
              Slave_IO_Running: Yes   //正常
             Slave_SQL_Running: Yes   //正常
               Replicate_Do_DB: 
           Replicate_Ignore_DB: 
            Replicate_Do_Table: 
        Replicate_Ignore_Table: 
       Replicate_Wild_Do_Table: 
   Replicate_Wild_Ignore_Table: 
                    Last_Errno: 0
                    Last_Error: 
                  Skip_Counter: 0
           Exec_Master_Log_Pos: 156
               Relay_Log_Space: 533
               Until_Condition: None
                Until_Log_File: 
                 Until_Log_Pos: 0
            Master_SSL_Allowed: No
            Master_SSL_CA_File: 
            Master_SSL_CA_Path: 
               Master_SSL_Cert: 
             Master_SSL_Cipher: 
                Master_SSL_Key: 
         Seconds_Behind_Master: 0
 Master_SSL_Verify_Server_Cert: No
                 Last_IO_Errno: 0
                 Last_IO_Error: 
                Last_SQL_Errno: 0
                Last_SQL_Error: 
   Replicate_Ignore_Server_Ids: 
              Master_Server_Id: 53
                   Master_UUID: 38c02165-005e-11ee-bd2d-525400007271
              Master_Info_File: mysql.slave_master_info
                     SQL_Delay: 0
           SQL_Remaining_Delay: NULL
       Slave_SQL_Running_State: Replica has read all relay log; waiting for more updates
            Master_Retry_Count: 86400
                   Master_Bind: 
       Last_IO_Error_Timestamp: 
      Last_SQL_Error_Timestamp: 
                Master_SSL_Crl: 
            Master_SSL_Crlpath: 
            Retrieved_Gtid_Set: 
             Executed_Gtid_Set: 
                 Auto_Position: 0
          Replicate_Rewrite_DB: 
                  Channel_Name: 
            Master_TLS_Version: 
        Master_public_key_path: 
         Get_master_public_key: 0
             Network_Namespace: 
 1 row in set, 1 warning (0.00 sec)

5)在mysql55添加客户端访问时使用的用户

mysql> create user plj@"%" identified by "123456";
 mysql> grant insert,update,delete,select on gamedb.* to plj@"%" ;

步骤四:客户端测试配置

1)在client50 连接主服务器mysql53 存储数据

//连接主服务器存储数据

[root@mysql50 ~]# mysql -h192.168.88.53 -uplj -p123456
 mysql> insert into  gamedb.user values("tt","aid");
 mysql> insert into  gamedb.user values("mm","uid");

2)在client50 分别连接2个从服务器查看数据

//连接从服务器54查看数据

[root@mysql50 ~]# mysql -h192.168.88.54 -uplj -p123456 
 Mysql> select * from gamedb.user;
 +------+-------+
 | name | class |
 +------+-------+
 | yaya | nsd   |
 | tt   | aid   |
 | mm   | uid   |
 +------+-------+

//连接从服务器55查看数据

[root@mysql50 ~]# mysql -h192.168.88.55 -uplj -p123456 
 Mysql> select * from gamedb.user;
 +------+-------+
 | name | class |
 +------+-------+
 | yaya | nsd   |
 | tt   | aid   |
 | mm   | uid   |
 +------+-------+