网站配套学习视频请点击后方网址直接跳转https://edu.51cto.com/lecturer/14123887.html

我是ken,致力于开源技术

MySQL系列详解六:MySQL主从复制/半同步演示-技术流ken

 

前言

 

随着技术的发展,在实际的生产环境中,由单台MySQL数据库服务器不能满足实际的需求。此时数据库集群就很好的解决了这个问题了。采用MySQL分布式集群,能够搭建一个高并发、负载均衡的集群服务器。在此之前我们必须要保证每台MySQL服务器里的数据同步。数据同步我们可以通过MySQL内部配置就可以轻松完成,主要有主从复制和主主复制。

 

演示:实现mysql主从复制

 

配置主节点

1.修改主节点配置文件

 ...
#binlog
 54 #binlog_format = STATEMENT
 55 binlog_format = row         <<主从节点保持一致,要么都是row,要么都是mixed
 56 server-id = 1003306         <<主从节点的server_id必须保证不能一致
 57 log-bin = /data/mysql/mysql3306/logs/mysql-bin  <<定义二进制日志文件保存文件和格式
 58 binlog_cache_size = 4M
 59 max_binlog_size = 256M
 60 max_binlog_cache_size = 1M
 61 sync_binlog = 0
 62 expire_logs_days = 10
 63 #procedure 
 64 log_bin_trust_function_creators=1
 65 
...

 

2.重启mysql

[root@ken home]# mysqladmin -uroot -pxx shutdown
[root@ken home]# mysqld &
[root@ken home]# ss -tnl | grep 3306
LISTEN     0      70          :::3306                    :::*   

 

3.主节点上授权具有复制权限的用户

[root@ken home]# mysql -uroot -p                  <<登录mysql
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 5.7.23-log MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> grant replication slave on *.* to ken@'%' identified by 'xx'; <<创建ken用户,并设置密码xx

 

配置从节点

 

1.修改从节点和主机点时间同步

可以使用NTP服务或者date直接设置。此步省略。

 

2.修改丛节点配置文件

 ...
#binlog
 54 #binlog_format = STATEMENT
 55 binlog_format = row         <<主从节点保持一致,要么都是row,要么都是mixed
 56 server-id = 1003307        <<主从节点的server_id必须保证不能一致
 57 log-bin = /data/mysql/mysql3306/logs/mysql-bin  <<定义二进制日志文件保存文件和格式
 58 binlog_cache_size = 4M
 59 max_binlog_size = 256M
 60 max_binlog_cache_size = 1M
 61 sync_binlog = 0
 62 expire_logs_days = 10
 63 #procedure 
 64 log_bin_trust_function_creators=1
 65 
...

 

3.重启mysql

[root@ken home]# mysqladmin -uroot -pxx shutdown
[root@ken home]# mysqld &
[root@ken home]# ss -tnl | grep 3306
LISTEN     0      70          :::3306                    :::*   

 

4.连接主服务器

链接主服务器命令详解

 1 格式:CHANGE MASTER TO 选项
 2 选项:
 3 MASTER_HOST = 'host_name'         指定主服务的ip或者主机名
 4 MASTER_USER = 'user_name'         指定主服务器的用户名
 5 MASTER_PASSWORD = 'password'      指定用户名的密码
 6 MASTER_PORT = port_num            指定连接的端口,默认是3306
 7 MASTER_CONNECT_RETRY = interval   指定连接失败的时候的重试间隔时间
MySQL [(none)]> change master to master_host='10.220.5.137',master_user='root',master_password='xx'

 

5.启动 从节点

MySQL [(none)]> start salve;

 

6.查看从节点链接状态

主要看IO以及SQL线程是否启动

MySQL [(none)]> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.220.5.137
                  Master_User: root
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 928
               Relay_Log_File: relay-bin.000005
                Relay_Log_Pos: 1141
        Relay_Master_Log_File: mysql-bin.000003
             Slave_IO_Running: Yes             <<<IO线程启动成功
            Slave_SQL_Running: Yes             <<<SQL线程启动成功
              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: 928
              Relay_Log_Space: 1508
              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: 1003306
                  Master_UUID: e2357094-d6d9-11e8-ba06-000c292218ec
             Master_Info_File: /data/mysql/mysql3306/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
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 2a88f089-cd97-11e8-a862-000c29492f7b:1-45
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

 

7.测试

在主服务器端建库建表,查看从服务器端是否同步

主服务器建库建表

mysql> create database ken;                  <<创建数据库ken
Query OK, 1 row affected (0.00 sec)

mysql> use ken;                              <<进入数据库
Database changed
mysql> create table ken1 as select * from jobs.teachers; <<创建ken1表
Query OK, 6 rows affected (0.04 sec)
Records: 6  Duplicates: 0  Warnings: 0

mysql> select * from ken1;            <<查看ken1表数据
+-----+------------+-----+--------+
| TID | Name       | Age | Gender |
+-----+------------+-----+--------+
|   1 | Song Jiang |  45 | M      |
|   2 | ken        |  25 | M      |
|   3 | ken        |  45 | M      |
|   4 | hah        |  67 | M      |
|   5 | ken2       |  76 | M      |
|   6 | ken2       |  76 | M      |
+-----+------------+-----+--------+
6 rows in set (0.00 sec)

 

8.检查从服务器

MySQL [(none)]> show databases;    <<查看数据库,已经同步过来ken
+--------------------+
| Database           |
+--------------------+
| information_schema |
| jobs               |
| ken                |
| liu                |
| mysql              |
| performance_schema |
| sys                |
| test               |
| test5              |
+--------------------+
9 rows in set (0.00 sec)

MySQL [(none)]> use ken;         <<进入到ken数据库中
Database changed
MySQL [ken]> show tables;        <<查看库中的表,可以看到ken1表
+---------------+
| Tables_in_ken |
+---------------+
| ken1          |
+---------------+
1 row in set (0.00 sec)

MySQL [ken]> select * from ken1;  <<检查ken1表中的数据完整
+-----+------------+-----+--------+
| TID | Name       | Age | Gender |
+-----+------------+-----+--------+
|   1 | Song Jiang |  45 | M      |
|   2 | ken        |  25 | M      |
|   3 | ken        |  45 | M      |
|   4 | hah        |  67 | M      |
|   5 | ken2       |  76 | M      |
|   6 | ken2       |  76 | M      |
+-----+------------+-----+--------+
6 rows in set (0.00 sec)

 

到这里主从复制的演示就结束了,接下来再演示一个慢同步的架构

 

演示:实现mysql慢同步演示

 

半同步:N多个从节点中,只要任意一个从节点给主节点返回信息告知自己已经将数据存储成功,那么主节点会立刻给客户端反向执行结果信息。

 

配置主节点

1.安装主模块

mysql> install plugin rpl_semi_sync_master soname "semisync_master.so";

 

2.查看主端有关semi的变量

mysql> show global variables like '%semi%';
+-------------------------------------------+------------+
| Variable_name                             | Value      |
+-------------------------------------------+------------+
| rpl_semi_sync_master_enabled              | OFF         |
| rpl_semi_sync_master_timeout              | 10000      |
| rpl_semi_sync_master_trace_level          | 32         |
| rpl_semi_sync_master_wait_for_slave_count | 1          |
| rpl_semi_sync_master_wait_no_slave        | ON         |
| rpl_semi_sync_master_wait_point           | AFTER_SYNC |
+-------------------------------------------+------------+
6 rows in set (0.02 sec)

 

3.启动rpl_semi_sync_master_enabled  

mysql> set global  rpl_semi_sync_master_enabled=1;

 

4.安装从模块

mysql> install plugin rpl_semi_sync_slave soname "semisync_slave.so";

 

5.启动rpl_semi_sync_slave_enabled  

mysql> set global  rpl_semi_sync_slave_enabled=1;

 

6.重启从端slave

MySQL [ken]> stop slave;
MySQL [ken]> start slave;

 

7.查看IO/SQL启动状态

MySQL [ken]> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.220.5.137
                  Master_User: root
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 154
               Relay_Log_File: relay-bin.000003
                Relay_Log_Pos: 367
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes            <<IO线程启动成功
            Slave_SQL_Running: Yes            <<SQL线程启动成功
              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: 154
              Relay_Log_Space: 568
              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: 1003306
                  Master_UUID: e2357094-d6d9-11e8-ba06-000c292218ec
             Master_Info_File: /data/mysql/mysql3306/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
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 2a88f089-cd97-11e8-a862-000c29492f7b:1-45
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

 

8.测试

略。详情见上主从复制。

posted @ 2018-10-24 09:48  技术流ken  阅读(446)  评论(0编辑  收藏  举报