mysql架构篇之读写分离

思考:用户在浏览信息的时候,是看得多?还是写的多?

MySQL读写分离

  主从复制的应用局限性

  如何分离MySQL读、写流量

  

读写分离原理:

由MySQL代理面向客户端提供服务

  ---收到SQL写请求时,交给master服务器处理

  ---收到SQL读请求时,交给slave服务器处理

构建读写分离:

  本次我i们使用Maxscale代理软件进行部署

    Maxscale由MySQL的兄弟公司MariaDB开发

    https://downloads.mariadb.com/files/MaxScale

构建思路:

部署MySQL一主一从结构

主:192.168.2.22

从:192.168.2.33

部署MySQL代理服务器

  1:装包

      

rpm   -ivh  maxscale-....

配置主服务器 /etc/my.cnf
  1. [ mysqld ]
  2. server_id = 51     //指定服务器ID号
  3. log - bin = master51 //启用        binlog日志,并指定文件名重定向

  更改授权:

    grant replication slave on *.* to repluser@'localhost' identified by '密码'

  

 配置从服务器

]#mysql - uroot - p123456
mysql >将master更改为master_host = '192.168.4.51'-> master_user = 'repluser'-> master_password = '123456'-> master_log_file = 'master51.000001'-> master_log_pos = 449 ;
查询正常, 受影响的0行, 2条 警告 (0.01秒)
MySQL的>启动奴隶;
查询正常, 受影响0行(0.01秒) 
mysql >显示从属状态 G ;
****************************  1 。行*******************************
               Slave_IO_State :等待的主发送事件
                  主主机: 192.168.4.51
                  Master_User : repluser
                  主端口: 3306
                连接重试: 60
              Master_Log_File : master51 .000001
          Read_Master_Log_Pos : 738
               RELAY_LOG_FILE : slave20 -中继-斌0.000002
                Relay_Log_Pos : 319
        Relay_Master_Log_File : master51 .000001
             Slave_IO_Running :是        // IO线程是
            Slave_SQL_Running :是        // 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 : 738
              Relay_Log_Space : 528
              直到条件:无
               直到_Log_File : 
                直到Log_Pos : 0
           Master_SSL_Allowed :否
           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 :否
                Last_IO_Errno : 0
                Last_IO_Error : 
               Last_SQL_Errno : 0
               Last_SQL_Error : 
  Replicate_Ignore_Server_Ids : 
             Master_Server_Id : 10
                  Master_UUID : 95ada2c2 - bb24 -11e8 - abdb - 525400131c0f
             Master_Info_File : / var / lib / mysql / master 。信息
                    SQL_Delay : 0
          SQL_Remaining_Delay :空
      Slave_SQL_Running_State :从站已读取所有中继日志;等待了更多的更新
           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 : 
                自动位置: 0
         Replicate_Rewrite_DB : 
                 Channel_Name : 
           Master_TLS_Version : 
1行中 集合 (0.00秒)

  2:配置

    

]#VIM /等/ maxscale 。cnf
[ maxscale ]
线程=自动            //运行的线程的数量
[ server1 ]             //定义数据库服务器
类型=服务器
地址= 192.168.4.51         //主服务器ip
端口= 3306
协议= MySQLBackend        
[ server2 ]
类型=服务器
地址= 192.168.4.52         //从服务器IP
端口= 3306
协议= MySQLBackend
[ MySQL Monitor ]                 //定义监控的数据库服务器
类型=监控器
模块= mysqlmon
服务器= server1的,服务器2         //监控的数据库列表,不能写IP
user = maxscalemon                     //监控用户
passwd = 123qqq ... A                 //密码
monitor_interval = 10000        
#[读-唯一的服务]         //不定义只读服务
#type =服务
#router = readconnroute
#servers = server1
#user = myuser
#passwd = mypwd
#router_options =奴隶
[读-写服务]             //定义读写分离服务
类型=服务
路由器= readwritesplit
服务器= server1的,服务器2
user = maxscalerouter //路由            用户
passwd = 123qqq…A                 //密码
max_slave_connections = 100 %
[ MaxAdmin Service ]         //定义管理服务
类型=服务
路由器= cli
#[只读-侦听器]         //不定义替换服务使用的端口号
#type =侦听器
#service =只读-只读服务
#protocol = MySQLClient
#端口= 4008
[ Read - Write Listener ]             //定义读写服务使用的端口号
类型=侦听器
服务=读-写服务
协议= MySQLClient
端口= 4006
[ MaxAdmin Listener ]         //管理服务使用的端口号
类型=侦听器
服务= MaxAdmin服务
协议=最大比例
套接字=默认
port = 4016      //手动添加,不指定时使用的是替换端口在启动服务以后可以知道替换端口是多少

添加授权用户:

  

查看授权用户:

分别在主/从服务器上查看

mysql >选择用户,从mysql托管。用户喜欢“ maxscale % ”的用户;
+ ---------------- + ------ +
| 用户           | 主机|
+ ---------------- + ------ +
| maxscalemon     |  %    |
| maxscalerouter |  %    |
+ ---------------- + ------ +
2排在 组 (0.00秒)

  3:启服务

    maxscale -f /etc/maxscale.cnf

    netstat -antp

测试配置

  1:在代理服务器本机访问管理

    maxadmin -uadmin -pmariadb -p端口(3307/3308)

    list servers

    

[root@host57 ~]# maxadmin -uadmin -pmariadb -P4016
MaxScale> list servers
MaxScale> list servers
Servers.
-------------------+-----------------+-------+-------------+--------------------
Server             | Address         | Port  | Connections | Status              
-------------------+-----------------+-------+-------------+--------------------
server1            | 192.168.4.51    |  3306 |           0 | Master, Running
server2            | 192.168.4.52    |  3306 |           0 | Slave, Running
-------------------+-----------------+-------+-------------+--------------------

  2:客户端连接代理服务器访问数据

    mysql -h代理服务器ip  -P端口  -u用户名  -p密码

  

总结:

总的来说,读写分离属于Mysql提高性能的一种方式,把读和写分开。若添加上半异步复制相辅相成。

原文地址:https://www.cnblogs.com/ahaocloud/p/14696620.html