MySQL主从复制--实现读写分离

读写分离(Read/Write Splitting)

     原理:让主数据库(master)处理事务性增、改、删操作(INSERT、UPDATE、DELETE),而从数据库(slave)处理SELECT查询操作。
因为在一个数据库中,处理最多的就是查询请求,所以需要将查询请求分摊到一个集群上。
      还可以加一个memcache ,将查询请求缓存起来,以备下次请求时直接使用。
      实现读写分离有3个常用的工具。
     MySQL Proxy :lua ,连接路由,Query分析,查询过滤和修改,负载均衡,HA
     Amoeba(Java):查询路由,查询分析,查询过滤,读写分离,负载均衡,HA
     Cobar(Java)
MySQL主从复制--实现读写分离

配置MySQL Proxy

   主Mysql负责写:       192.168.217.14     master.example.com
   从MySQL负责读:     192.168.217.15     slave.example.com
   代理MySQL服务器: 192.168.217.16     proxy.example.com

   先配置代理服务器。
现在网上下载一个mysql-proxy的源码包
解压。

MySQL主从复制--实现读写分离
创建个链接。

MySQL主从复制--实现读写分离
[[email protected] bin]# pwd
/usr/local/mysql-proxy/bin
到这个目录下来
执行命令
MySQL主从复制--实现读写分离
发现 4040 端口已经打开。


此时,就可以链接代理服务器的4040端口了。
MySQL主从复制--实现读写分离

1.配置MySQL复制基本操作



MySQL的原理:点击打开链接

一.master
1.启动二进制日志。
log-bin = master-bin
log-bin-index = master-bin.index

2.选择一个唯一的server.id
server.id = (0-2^32)

3.创建具有复制权限的用户
REPLTCATION SLAVE
REPLICATION CLIENT

二.slave
1.启用中继日志
relay-log = relay-log
relay-log-index = slave-bin.index

2.选择一个唯一的server-id
server-id =  (0-2^32)

3.连接至主服务器,并开始复制数据。
change master to
master_host = '',master_port='',master_log_file=‘’,master_log_pos='',master_user='',master_password='';

start slave;

MySQL主从复制--实现读写分离

    主服务器的dump线程将数据给从服务器的I/O线程,SQL线程从中继日志读一个数据在MySQL中写。

主服务器的配置。
修改/etc/my.cnf     (mysql的主配置文件)
MySQL主从复制--实现读写分离
设置二进制日志。注意server-id必须要和从服务器区分开来。

最好再加上一句在配置文件中加上,不过不是很重要。

innodb_file_per_table = 1


建立用户账号

[sql] view plain copy
  1. <span style="font-size:18px;">mysql> grant replication slave on *.* to 'master'@'192.168.%.%' identified by '123';  
  2. Query OK, 0 rows affected (0.00 sec)  
  3.   
  4. mysql> flush privileges;  
  5. Query OK, 0 rows affected (0.00 sec)  
  6.   
  7. </span>  

查看主服务器的二进制日志内容。

[sql] view plain copy
  1. <span style="font-size:14px;">mysql> show master status;  
  2. +-------------------+----------+--------------+------------------+  
  3. | File              | Position | Binlog_Do_DB | Binlog_Ignore_DB |  
  4. +-------------------+----------+--------------+------------------+  
  5. | master-bin.000001 |      333 |              |                  |  
  6. +-------------------+----------+--------------+------------------+  
  7. 1 row in set (0.00 sec)  
  8.   
  9. mysql> show binlog events in 'master-bin.000001';  
  10. +-------------------+-----+-------------+-----------+-------------+------------------------------------------------------------------------------+  
  11. | Log_name          | Pos | Event_type  | Server_id | End_log_pos | Info                                                                         |  
  12. +-------------------+-----+-------------+-----------+-------------+------------------------------------------------------------------------------+  
  13. | master-bin.000001 |   4 | Format_desc |         1 |         107 | Server ver: 5.5.12-log, Binlog ver: 4                                        |  
  14. | master-bin.000001 | 107 | Query       |         1 |         258 | grant replication slave on *.* to 'master'@'192.168.%.%' identified by '123' |  
  15. | master-bin.000001 | 258 | Query       |         1 |         333 | flush privileges                                                             |  
  16. +-------------------+-----+-------------+-----------+-------------+------------------------------------------------------------------------------+  
  17. rows in set (0.02 sec)  
  18. </span>  


从服务器的配置

修改/etc/my.cnf

MySQL主从复制--实现读写分离

注意修改server-id 号。


连接至主服务器。

[sql] view plain copy
  1. <span style="font-size:18px;">mysql> change master to  
  2.     -> master_host='192.168.217.11',  
  3.     -> master_user='master',  
  4.     -> master_password='123',  
  5.     -> master_log_file='master-bin.000001',  
  6.     -> master_log_pos=333;  
  7. </span>  

启动slave。

[sql] view plain copy
  1. mysql> start slave;  
  2. Query OK, 0 rows affected (0.02 sec)  

查看slave的状态。

[sql] view plain copy
  1. mysql> show slave status\G  
  2. *************************** 1. row ***************************  
  3.                Slave_IO_State: Waiting for master to send event  
  4.                   Master_Host: 192.168.217.11  
  5.                   Master_User: master  
  6.                   Master_Port: 3306  
  7.                 Connect_Retry: 60  
  8.               Master_Log_File: master-bin.000001  
  9.           Read_Master_Log_Pos: 333  
  10.                Relay_Log_File: relay-log.000002  
  11.                 Relay_Log_Pos: 254  
  12.         Relay_Master_Log_File: master-bin.000001  
  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: 333  
  25.               Relay_Log_Space: 404  
  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. 1 row in set (0.00 sec)  
  44.   
  45. mysql>   

因为从服务器要求不能执行写操作。

所以在从服务器的/etc/my.cnf 里填了read-only = true;


[sql] view plain copy
  1. mysql> show global variables like 'read%';  
  2. +----------------------+---------+  
  3. | Variable_name        | Value   |  
  4. +----------------------+---------+  
  5. | read_buffer_size     | 1048576 |  
  6. | read_only            | ON      |  
  7. | read_rnd_buffer_size | 4194304 |  
  8. +----------------------+---------+  
  9. rows in set (0.00 sec)  


MySQL主从复制--实现读写分离


测试。

发现成功复制。


另外最好配置主服务器同步二进制日志。


sync-binlog=on 在主服务器上设定。用于事务安全。



MySQL 5.5 semi-sync 半同步复制。

主服务器端:

[sql] view plain copy
  1. mysql> install plugin rpl_semi_sync_master soname 'semisync_master.so'  
  2.     -> ;  
  3. Query OK, 0 rows affected (0.04 sec)  

[sql] view plain copy
  1. mysql> show global variables like '%rpl%';  
  2. +------------------------------------+-------+  
  3. | Variable_name                      | Value |  
  4. +------------------------------------+-------+  
  5. | rpl_recovery_rank                  | 0     |  
  6. | rpl_semi_sync_master_enabled       | OFF   |#未启用  
  7. | rpl_semi_sync_master_timeout       | 10000 |#超时时间  
  8. | rpl_semi_sync_master_trace_level   | 32    |#追踪级别  
  9. | rpl_semi_sync_master_wait_no_slave | ON    |  
  10. +------------------------------------+------  

从服务器端:

[sql] view plain copy
  1. mysql> install plugin rpl_semi_sync_slave soname 'semisync_slave.so';  
  2. Query OK, 0 rows affected (0.08 sec)  


主服务器端启动:

[sql] view plain copy
  1. mysql> set global  rpl_semi_sync_master_enabled = 1;  
  2. Query OK, 0 rows affected (0.00 sec)  
从服务器端启动:
[sql] view plain copy
  1. mysql> set global rpl_semi_sync_slave_enabled = 1;  
  2. Query OK, 0 rows affected (0.00 sec)  
从服务器从新启动IO_THREAD
[sql] view plain copy
  1. mysql> stop slave IO_THREAD;  
  2. Query OK, 0 rows affected (0.00 sec)  
  3.   
  4. mysql> start slave IO_THREAD;  
  5. Query OK, 0 rows affected (0.00 sec)  
  6.   
  7. mysql> show global status like '%rpl%';  
  8. +----------------------------+-------------+  
  9. | Variable_name              | Value       |  
  10. +----------------------------+-------------+  
  11. | Rpl_semi_sync_slave_status | ON          |  
  12. | Rpl_status                 | AUTH_MASTER |  
  13. +----------------------------+-------------+  
  14. rows in set (0.00 sec)  

当然也可以只复制某个库或者某张表。

在slave 的  /etc/my.cnf 下

写上

replicate-do-table=wishrp.sku_map #库名.表名

最好在slave上执行。因为在master上执行会造成二进制日志不全的情况


出处:https://blog.csdn.net/ghost_leader/article/category/6544950

http://www.cnblogs.com/zhoujinyi/tag/