Mysql 主从复制

为什么使用主从

主从复制、读写分离主要目的就是为了提高数据库的并发性,在主库更新,在从库查询,在出库出现问题时,可以快速切换到从库提供服务,不需要人为参与,提高了系统的可用性,同时数据在从库进行备份,提高数据安全性;

主从原理

  • 1、master 服务器上数据发生改变时,将 sql 记录在 binlog 日志中;
  • 2、slave 服务器在一定的时间间隔内对 master 的 binlog 进行探测,判断是否发生改变,如果改变,则开启一个 I/OThread 请求,获取 master 的 binlog 日志;
  • 3、此时 master 会为 I/O 线程启动一个 dump 线程,将 master 的 binlog 发送给 slave,保存在 slave 的 relay_log 中,salve 启动 SQL 线程读取中继日志中的
    binlog,在本地执行,使得 master 和 slave 数据保持一致;

主从配置步骤

  • 1、采用两台 ubuntu 虚拟机,分别开启一个 mysql 服务,ip 地址分别为 189(主)和 188(从);
  • 2、进入 mysql 主库,添加一个从库的用户,
mysql -uroot -p
// 192.168.100.188 从库 IP 地址
// root 用户名
// test123 密码
grant replication slave on *.* to 'root'@'192.168.100.188' identified by 'test123';
  • 3、修改主库的配置文件,开启 binlog,重启 mysql 服务,查看 binlog 信息;
# The following can be used as easy to replay backup logs or for replication.
# note: if you are setting up a replication slave, see README.Debian about
#       other settings you may need to change.
// 服务器的唯一标识
server-id               = 100
// 开启 binlog
log_bin                 = mysql-bin
// binlog日志保留的天数,清除超过10天的日志
expire_logs_days        = 10
max_binlog_size   = 100M
// 需要同步的数据库
binlog_do_db            = myproject
#binlog_ignore_db       = include_database_name
#
service mysql restart

mysql> show master status\G;
*************************** 1. row ***************************
             File: mysql-bin.000001
         Position: 154
     Binlog_Do_DB: myproject
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 
1 row in set (0.00 sec)
  • 4、修改从库的配置文件中的 server-id,id 不能与主库相同,如果存在多个从库时,也不能与其他从库 id 相同;
server-id=101
  • 5、进入从库,输入一下命令,启动 salve,查看 salve 运行情况;
mysql> change master to
    // 主库 ip
    -> master_host='192.168.100.189',
    // 主库中新建的用户名
    -> master_user='root',
    // 新建用户的密码
    -> master_password='test123',
    // 主库 binlog 日志名称
    -> master_log_file='mysql-bin.000001',
    // binlog 日志偏移量
    -> master_log_pos=862,
    -> master_port=3306;
Query OK, 0 rows affected, 2 warnings (0.00 sec)
start slave;
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.100.189
                  Master_User: root
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000008
          Read_Master_Log_Pos: 476
               Relay_Log_File: ubuntu-relay-bin.000002
                Relay_Log_Pos: 644
        Relay_Master_Log_File: mysql-bin.000008
             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: 476
              Relay_Log_Space: 854
              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: 100
                  Master_UUID: fd430c7c-6cb7-11ea-95dc-000c296fcfe0
             Master_Info_File: mysql.slave_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: 
                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 (0.00 sec)

当 Slave_IO_State 为 Waiting for master to send event 时,则表示主从配置成功;

  • 6、测试主从
    在主库创建新表 demo, 并插入一条记录;
CREATE TABLE `demo` (
    ->   `id` INT NOT NULL AUTO_INCREMENT,
    ->   `a` VARCHAR(45) NULL,
    ->   `b` VARCHAR(45) NULL,
    ->   PRIMARY KEY (`id`))
    -> ENGINE = InnoDB
    -> DEFAULT CHARACTER SET = utf8;

insert into demo5 (a, b) values ('aaa', 'bbb');

查看从库记录;

mysql> show tables;
+---------------------+
| Tables_in_myproject |
+---------------------+
| demo5               |
+---------------------+
1 row in set (0.00 sec)

mysql> select * from demo5;
+----+------+------+
| id | a    | b    |
+----+------+------+
|  1 | aaa  | bbb  |
+----+------+------+
1 row in set (0.00 sec)

配置过程中遇到的问题

  • 1、配置完从库后查看从库运行情况,发现从库正在连接主库,并且有错误提示;
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Connecting to master
                  Master_Host: 192.168.100.189
                  Master_User: root
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000009
          Read_Master_Log_Pos: 756
               Relay_Log_File: ubuntu-relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: mysql-bin.000009
             Slave_IO_Running: Connecting
            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: 756
              Relay_Log_Space: 156
              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: NULL
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 2003
                Last_IO_Error: error connecting to master 'root@192.168.100.189:3306' - retry-time: 60 retries: 1 message: Can't connect to MySQL server on '192.168.100.189' (111)
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 100
                  Master_UUID: fd430c7c-6cb7-11ea-95dc-000c296fcfe0
             Master_Info_File: mysql.slave_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: 210414 01:21:37
     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 (0.00 sec)

在命令行直接连接主库,发现无法连接成功;

yzw@ubuntu:/etc$ mysql -h192.168.100.189 -uroot -p
Enter password: 
ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.100.189' (111)

此时只需要修改主库配置文件中的 bind-address,将其注释或者改为 0.0.0.0,此项配置意思是 mysql 服务只监听本地,因此从库无法连接成功;

# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
#bind-address           = 127.0.0.1
  • 2、Slave_IO_State 无值并有错误信息;
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: 192.168.100.189
                  Master_User: root
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000016
          Read_Master_Log_Pos: 862
               Relay_Log_File: ubuntu-relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: mysql-bin.000016
             Slave_IO_Running: No
            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: 862
              Relay_Log_Space: 156
              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: NULL
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 13114
                Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'Client requested master to start replication from position > file size'
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 100
                  Master_UUID: fd430c7c-6cb7-11ea-95dc-000c296fcfe0
             Master_Info_File: mysql.slave_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: 210414 01:50:22
     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 (0.00 sec)

查看主库,发现主库的 Position 为 154,从库设置了 862,因此才会报错,修改从库设置后重新启动即可成功;

mysql> show master status\G;
*************************** 1. row ***************************
             File: mysql-bin.000016
         Position: 154
     Binlog_Do_DB: myproject
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 
1 row in set (0.01 sec)

mysql> stop slave;
Query OK, 0 rows affected (0.01 sec)

mysql> change master to master_log_pos=154;
Query OK, 0 rows affected (0.00 sec)

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
  • 3、从库数据库名称和主库不同,主库操作后,从库无法同步;
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.100.189
                  Master_User: root
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000016
          Read_Master_Log_Pos: 476
               Relay_Log_File: ubuntu-relay-bin.000002
                Relay_Log_Pos: 322
        Relay_Master_Log_File: mysql-bin.000016
             Slave_IO_Running: Yes
            Slave_SQL_Running: No
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 1049
                   Last_Error: Error 'Unknown database 'myproject'' on query. Default database: 'myproject'. Query: 'CREATE TABLE `demo7` (   `id` INT NOT NULL AUTO_INCREMENT,   `a` VARCHAR(45) NULL,   `b` VARCHAR(45) NULL,   PRIMARY KEY (`id`)) ENGINE = InnoDB DEFAULT CHARACTER SET = utf8'
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 154
              Relay_Log_Space: 854
              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: NULL
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 1049
               Last_SQL_Error: Error 'Unknown database 'myproject'' on query. Default database: 'myproject'. Query: 'CREATE TABLE `demo7` (   `id` INT NOT NULL AUTO_INCREMENT,   `a` VARCHAR(45) NULL,   `b` VARCHAR(45) NULL,   PRIMARY KEY (`id`)) ENGINE = InnoDB DEFAULT CHARACTER SET = utf8'
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 100
                  Master_UUID: fd430c7c-6cb7-11ea-95dc-000c296fcfe0
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: 
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 210414 01:58:11
               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 (0.00 sec)

此时必须在从库创建和主库一样的数据库名称,重新同步一下即可(主库执行 flush logs 即生成新的 binlog 日志,修改主库读取的 binlog 名称);

主从同步机制

  • 1、binlog+pos(默认为异步)

  • 2、半同步复制(5.5版本后)
    master 执行完一个事务后不是立即将结果返回客户端,而是等待至少一个 slave 接收到了主库 binlog,并保存在 slave 的 relay_log 后,才会向客户端返回结果;

  • 3、全同步复制
    当主库执行完一个事务,所有的从库都执行了该事务才返回给客户端。

  • 4、数据库中间件
    所有的读写请求都走中间件,由中间件去分配请求是到主库还是从库,记录所有请求主库的 key,如果还没有同步时,就有读请求进来,此时从库书库还没有更新,则中间件会把请求发到主库,如果已经同步,则将请求发到从库,这样就能保证数据的一致性;

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,425评论 4 361
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,058评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,186评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,848评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,249评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,554评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,830评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,536评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,239评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,505评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,004评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,346评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,999评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,060评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,821评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,574评论 2 271
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,480评论 2 267

推荐阅读更多精彩内容

  • 今天感恩节哎,感谢一直在我身边的亲朋好友。感恩相遇!感恩不离不弃。 中午开了第一次的党会,身份的转变要...
    迷月闪星情阅读 10,498评论 0 11
  • 彩排完,天已黑
    刘凯书法阅读 4,146评论 1 3
  • 表情是什么,我认为表情就是表现出来的情绪。表情可以传达很多信息。高兴了当然就笑了,难过就哭了。两者是相互影响密不可...
    Persistenc_6aea阅读 120,505评论 2 7