1、复制的基本原理

slave会从master读取binlog来进行数据同步
主从复制的三步骤

  1. master将改变记录到二进制日志(binary log)。这些记录过程叫做二进制日志事件(binary log events)
  2. slave将master的binary log events拷贝到它的中继日志(relay log)
  3. slave重做中继日志中的事件,将改变应用到自己的数据库中。MySQL复制是异步的且串行化的

image.png

2、复制的基本原则

  1. 每个slave只有一个master
  2. 每个slave只能有一个唯一的服务器ID
  3. 每个master可以有多个salve

    3、复制最大问题

    因为发生多次 IO, 存在延时问题

    4、一主一从常见配置

    前提

    mysql 版本一致,主从机在同一网段下
    ping 测试
    Linux 中 ping Windows
    1. [root@Heygo 桌面]# ping 10.206.207.131
    2. PING 10.206.207.131 (10.206.207.131) 56(84) bytes of data.
    3. 64 bytes from 10.206.207.131: icmp_seq=1 ttl=128 time=1.27 ms
    4. 64 bytes from 10.206.207.131: icmp_seq=2 ttl=128 time=0.421 ms
    5. 64 bytes from 10.206.207.131: icmp_seq=3 ttl=128 time=1.12 ms
    6. 64 bytes from 10.206.207.131: icmp_seq=4 ttl=128 time=0.515 ms
    Windows 中 ping Linux
    ping 192.168.152.129

    配置

    主机修改 my.ini 配置文件(Windows)

    主从都配置都在 [mysqld] 节点下,都是小写,以下是老师的配置文件
    以下两条为必须配置
    配置主机 id
    server-id=1
    启用二进制日志
    log-bin=C:/Program Files (x86)/MySQL/MySQL Server 5.5/log-bin/mysqlbin
    以下为非必须配置
    启动错误日志
    log-err=C:/Program Files (x86)/MySQL/MySQL Server 5.5/log-bin/mysqlerr
    根目录
    basedir=”C:/Program Files (x86)/MySQL/MySQL Server 5.5/“
    临时目录
    tmpdir=”C:/Program Files (x86)/MySQL/MySQL Server 5.5/“
    数据目录
    datadir=”C:/Program Files (x86)/MySQL/MySQL Server 5.5/Data/“
    主机,读写都可以
    read-only=0
    设置不要复制的数据库
    binlog-ignore-db=mysql
    设置需要复制的数据
    binlog-do-db=需要复制的主数据库名字

    从机修改 my.cnf 配置文件(Linux)

    【默认开启】主从复制,不需要修改
    image.png
    【必须】从服务器唯一ID
    server-id=2
    【可选】启用二进制文件

    修改配置文件后的准备工作

    因修改过配置文件,主机+从机都重启 mysql 服务
    Windows
    net stop mysql
    net start mysql
    Linux
    service mysqld restart

    主机从机都关闭防火墙

    Windows 手动关闭防火墙
    关闭虚拟机 linux 防火墙
    service iptables stop

    在 Windows 主机上简历账户并授权 slave

    创建用户, 并赋予从机 REPLICATION 权限(从主机的数据库表中复制表)
    GRANT REPLICATION SLAVE ON . TO ‘备份账号’@’从机器数据库 IP’ IDENTIFIED BY ‘账号密码’;
    GRANT REPLICATION SLAVE ON . TO ‘Heygo’@’192.168.152.129’ IDENTIFIED BY ‘123456’;
    刷新权限信息
    flush privileges;
    通过 select * from mysql.user where user=’Heygo’\G; 命令可查看:从机只有 Repl_slave_priv 权限为 Y,其余权限均为 N
    1. mysql> select * from mysql.user where user='Heygo'\G;
    2. *************************** 1. row ***************************
    3. Host: %
    4. User: Heygo
    5. Password: *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9
    6. Select_priv: N
    7. Insert_priv: N
    8. Update_priv: N
    9. Delete_priv: N
    10. Create_priv: N
    11. Drop_priv: N
    12. Reload_priv: N
    13. Shutdown_priv: N
    14. Process_priv: N
    15. File_priv: N
    16. Grant_priv: N
    17. References_priv: N
    18. Index_priv: N
    19. Alter_priv: N
    20. Show_db_priv: N
    21. Super_priv: N
    22. Create_tmp_table_priv: N
    23. Lock_tables_priv: N
    24. Execute_priv: N
    25. Repl_slave_priv: Y
    26. Repl_client_priv: N
    27. Create_view_priv: N
    28. Show_view_priv: N
    29. Create_routine_priv: N
    30. Alter_routine_priv: N
    31. Create_user_priv: N
    32. Event_priv: N
    33. Trigger_priv: N
    34. Create_tablespace_priv: N
    35. ssl_type:
    36. ssl_cipher:
    37. x509_issuer:
    38. x509_subject:
    39. max_questions: 0
    40. max_updates: 0
    41. max_connections: 0
    42. max_user_connections: 0
    43. plugin:
    44. authentication_string: NULL

查询 master 的状态,将 File 和 Position 记录下来,在启动 Slave 时需要用到这两个参数
show master status;

  1. mysql> show master status;
  2. +------------------+----------+--------------+------------------+
  3. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
  4. +------------------+----------+--------------+------------------+
  5. | mysql-bin.000001 | 107 | mysql | |
  6. +------------------+----------+--------------+------------------+

在 Linux 从上验证是否能登陆主机的 MySQL

在从机上执行 mysql -h 10.206.207.131 -uHeygo -p 命令,发现无法连接主机的 MySQL 数据库

  1. [root@Heygo 桌面]# mysql -h 10.206.207.131 -uHeygo -p
  2. Enter password:
  3. ERROR 1130 (HY000): Host 'windows10.microdone.cn' is not allowed to connect to this MySQL server

查阅资料发现:当你远程登录 MySQL 时,使用的账号要有特殊要求,如果要使用某个账号来远程登录,必须将账号的 host 属性值更改成 %。我敲,阳哥怎么就成功了呢?可以看到:我们在执行了 GRANT REPLICATION SLAVE ON . TO ‘Heygo’@’192.168.152.129’ IDENTIFIED BY ‘123456’; 命令之后,Heygo 账户的 host 属性为 192.168.152.129

  1. mysql> select user,host,plugin from user;
  2. +-------+-----------------+--------+
  3. | user | host | plugin |
  4. +-------+-----------------+--------+
  5. | root | localhost | |
  6. | root | 192.168.152.129 | |
  7. | Heygo | 192.168.152.129 | |
  8. +-------+-----------------+--------+
  9. 3 rows in set (0.00 sec)

于是我先使用 update user set host = ‘%’ where user = ‘Heygo’; 命令将 Heygo 账户的 host 字段设置为 %;然后使用 flush privileges; 命令刷新权限信息

  1. mysql> update user set host = '%' where user = 'Heygo';
  2. Query OK, 1 row affected (0.00 sec)
  3. Rows matched: 1 Changed: 1 Warnings: 0
  4. mysql> flush privileges;
  5. Query OK, 0 rows affected (0.00 sec)
  6. mysql> select user,host,plugin from user;
  7. +-------+-----------------+--------+
  8. | user | host | plugin |
  9. +-------+-----------------+--------+
  10. | root | localhost | |
  11. | root | 192.168.152.129 | |
  12. | Heygo | % | |
  13. +-------+-----------------+--------+
  14. 3 rows in set (0.00 sec)

在 Linux 从机上使用 mysql -h 10.206.207.131 -uHeygo -p 命令能够成功连接上主机上的 MySQL 数据库。我敲,谜一样
[root@Heygo 桌面]# mysql -h 10.206.207.131 -uHeygo -p

在 Linux 从机上配置需要复制的主机

从机进行认证

  1. CHANGE MASTER TO
  2. MASTER_HOST='主机 IP',
  3. MASTER_USER='创建用户名',
  4. MASTER_PASSWORD='创建的密码',
  5. MASTER_LOG_FILE='File 名字',
  6. MASTER_LOG_POS=Position数字;
  7. CHANGE MASTER TO
  8. MASTER_HOST='10.206.207.131',
  9. MASTER_USER='Heygo',
  10. MASTER_PASSWORD='123456',
  11. MASTER_LOG_FILE='mysql-bin.000001',
  12. MASTER_LOG_POS=107;

启动从服务器复制功能
start slave;
查看从机复制功能是否启动成功:使用 show slave status\G; 命令查看
Slave_SQL_Running:Yes 和 Slave_IO_Running:Yes 说明从机连接主机成功(第一次测试没有成功,这是隔了半年之后的测试,因此某些数据会有出入)

  1. mysql> show slave status\G;
  2. *************************** 1. row ***************************
  3. Slave_IO_State: Waiting for master to send event
  4. Master_Host: 10.206.207.131
  5. Master_User: Heygo
  6. Master_Port: 3306
  7. Connect_Retry: 60
  8. Master_Log_File: mysql-bin.000052
  9. Read_Master_Log_Pos: 4274
  10. Relay_Log_File: mysqld-relay-bin.000063
  11. Relay_Log_Pos: 2998
  12. Relay_Master_Log_File: mysql-bin.000052
  13. Slave_IO_Running: Yes
  14. Slave_SQL_Running: Yes
  15. Replicate_Do_DB:
  16. Replicate_Ignore_DB:
  17. Replicate_Do_Table:
  18. Replicate_Ignore_Table:
  19. Replicate_Wild_Do_Table:
  20. Replicate_Wild_Ignore_Table:
  21. Last_Errno: 0
  22. Last_Error:
  23. Skip_Counter: 0
  24. Exec_Master_Log_Pos: 4274
  25. Relay_Log_Space: 4749
  26. Until_Condition: None
  27. Until_Log_File:
  28. Until_Log_Pos: 0
  29. Master_SSL_Allowed: No
  30. Master_SSL_CA_File:
  31. Master_SSL_CA_Path:
  32. Master_SSL_Cert:
  33. Master_SSL_Cipher:
  34. Master_SSL_Key:
  35. Seconds_Behind_Master: 0
  36. Master_SSL_Verify_Server_Cert: No
  37. Last_IO_Errno: 0
  38. Last_IO_Error:
  39. Last_SQL_Errno: 0
  40. Last_SQL_Error:
  41. Replicate_Ignore_Server_Ids:
  42. Master_Server_Id: 1
  43. Master_UUID:
  44. Master_Info_File: /var/lib/mysql/master.info
  45. SQL_Delay: 0
  46. SQL_Remaining_Delay: NULL
  47. Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
  48. Master_Retry_Count: 86400
  49. Master_Bind:
  50. Last_IO_Error_Timestamp:
  51. Last_SQL_Error_Timestamp:
  52. Master_SSL_Crl:
  53. Master_SSL_Crlpath:
  54. Retrieved_Gtid_Set:
  55. Executed_Gtid_Set:
  56. Auto_Position: 0

如何停止从服务复制功能
stop slave;