如何基于 MySQL 主从模式搭建上万并发的系统架构?

作者:惨绿少年
出处:http://clsn.io
一、主从复制基础概念在了解主从复制之前必须要了解的就是数据库的二进制日志(binlog),主从复制架构大多基于二进制日志进行 。
1.1 二进制日志管理说明
二进制日志在哪?如何设置位置和命名?
在my.cnf文件中使用 log-bin = 指定;命名规则为 MySQL-bin.000000 (后为6位数字)
二进制日志位置:
mysql> show variables like '%log_bin%' ;+---------------------------------+-----------------------------------------+| Variable_name | Value |+---------------------------------+-----------------------------------------+| log_bin | ON || log_bin_basename | /Application/mysql/data/mysql-bin || log_bin_index | /application/mysql/data/mysql-bin.index || log_bin_trust_function_creators | OFF || log_bin_use_v1_row_events | OFF || sql_log_bin | ON |+---------------------------------+-----------------------------------------+6 rows in set (0.06 sec)日志命名:
mysql> show binary logs;+------------------+-----------+| Log_name | File_size |+------------------+-----------+| mysql-bin.000001 | 2979 || mysql-bin.000002 | 120 |+------------------+-----------+2 rows in set (0.00 sec)二进制日志记录什么?二进制日志中记录的是一个个完成的事件
二进制日志格式是怎样的?推荐使用row格式
查看当前使用的日志格式:
mysql> show variables like '%format%';+--------------------------+-------------------+| Variable_name | Value |+--------------------------+-------------------+| binlog_format | ROW || date_format | %Y-%m-%d || datetime_format | %Y-%m-%d %H:%i:%s || default_week_format | 0 || innodb_file_format | Antelope || innodb_file_format_check | ON || innodb_file_format_max | Antelope || time_format | %H:%i:%s |+--------------------------+-------------------+8 rows in set (0.00 sec)二进制日志如何滚动?每次重启都会刷新日志,也可以通过命令进行刷新 reset master;
二进制日志用来干嘛?备份恢复,起始点的备份恢复
二进制日志的操作命令?查看都有哪些二进制日志
mysql> show binary logs;+------------------+-----------+| Log_name | File_size |+------------------+-----------+| mysql-bin.000001 | 2979 || mysql-bin.000002 | 167 || mysql-bin.000003 | 120 |+------------------+-----------+3 rows in set (0.00 sec)查看当前使用的二进制日志文件:
mysql> show master status;+------------------+----------+--------------+------------------+-------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |+------------------+----------+--------------+------------------+-------------------+| mysql-bin.000003 | 120 | | | |+------------------+----------+--------------+------------------+-------------------+1 row in set (0.00 sec)


    推荐阅读