美文网首页
Mysql实战之主从复制的读写分离

Mysql实战之主从复制的读写分离

作者: 芷_念 | 来源:发表于2017-09-19 11:18 被阅读0次

    ProxySQL构建主从复制的读写分离

    ProxySQL官网及下载地址 http://www.proxysql.com/

    原理

    mysql的读写分离的基本原理是:让master(主数据库)来响应事务性操作,让slave(从数据库)来响应select非事务性操作,然后再采用主从复制来把master上的事务性操作同步到slave数据库中

    架构角色
    mysql-slave2    172.16.252.92
    mysql-slave1    172.16.252.82
    ProxySQL        172.16.253.105
    mysql-master    172.16.252.30
    
    环境准备
    各节点服务端都需提前同步时间及安装mariadb数据库
        [root@mysql-master ~]# ntpdate 172.16.0.1
        [root@mysql-slave1 ~]# yum -y install mariadb-server 
    关闭防火墙
        [root@mysql-master ~]# iptables -F
    下载ProxySQL程序包
        http://www.proxysql.com/
    
    配置

    mysql-master

    [root@mysql-master ~]# vim /etc/my.cnf.d/server.cnf 
    [mysqld]
    server_id=1
    log_bin = master-log
    skip_name_resolve = ON
    innodb_file_per_table = ON  \\每表使用单独的表文件存储
    [root@mysql-master ~]# systemctl start mariadb 
    
    [root@mysql-master ~]# mysql
    
    创建用户并授权连接主节点复制数据的权限
    MariaDB [(none)]> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO 'repluser'@'172.16.252.%' IDENTIFIED BY 'replpass'; 
    MariaDB [(none)]> FLUSH PRIVILEGES;
    显示master节点的状态,记录当前节点的位置
    MariaDB [(none)]> SHOW MASTER STATUS;
    +-------------------+----------+--------------+------------------+
    | File              | Position | Binlog_Do_DB | Binlog_Ignore_DB |
    +-------------------+----------+--------------+------------------+
    | master-log.000004 |      498 |              |                  |
    +-------------------+----------+--------------+------------------+
    
    创建连接ProxySQL代理的用户和密码,并同步到各节点主机
    MariaDB [(none)]> GRANT ALL ON *.* TO 'proxysql'@'172.16.%.%' IDENTIFIED BY 'proxypass';
    

    mysql-slave1

    [root@mysql-slave1 ~]# vim /etc/my.cnf.d/server.cnf 
    [mysqld]
    server_id = 2
    relay-log = relay-log
    skip_name_resolve = ON
    innodb_file_per_table = ON
    read_only = ON     \\普通用户在slave节点上只有只读权限
    [root@mysql-slave1 ~]# systemctl start mariadb
    
    [root@mysql-slave1 ~]# mysql
    从master的当前节点开始同步复制数据
    MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='172.16.252.30',MASTER_USER='repluser',MASTER_PASSWORD='replpass',MASTER_LOG_FILE='master-log.000004',MASTER_LOG_POS=498;  
    开启复制进程
    MariaDB [(none)]> START SLAVE;
    显示slave节点的状态信息
    MariaDB [(none)]> SHOW SLAVE STATUS\G;
    *************************** 1. row ***************************
        Slave_IO_State: Waiting for master to send event
        Master_Host: 172.16.252.30
        Master_User: repluser
        Master_Port: 3306
        Connect_Retry: 60
        Master_Log_File: master-log.000004
        Read_Master_Log_Pos: 498
        Relay_Log_File: relay-log.000002
        Relay_Log_Pos: 530
        Relay_Master_Log_File: master-log.000004
        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: 498
        Relay_Log_Space: 818
        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: 1
    

    mysql-slave2

    [root@mysql-slave1 ~]# vim /etc/my.cnf.d/server.cnf 
    [mysqld]
    server_id = 3
    relay-log = relay-log
    skip_name_resolve = ON
    innodb_file_per_table = ON
    read_only = ON      
    [root@mysql-slave2 ~]# systemctl start mariadb
    
    [root@mysql-slave2 ~]# mysql
    从master的当前节点开始同步复制数据
    MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='172.16.252.30',MASTER_USER='repluser',MASTER_PASSWORD='replpass',MASTER_LOG_FILE='master-log.000004',MASTER_LOG_POS=498;  
    开启复制进程
    MariaDB [(none)]> START SLAVE;
    显示slave节点的状态信息
    MariaDB [(none)]> SHOW SLAVE STATUS\G;
    *************************** 1. row ***************************
        Slave_IO_State: Waiting for master to send event
        Master_Host: 172.16.252.30
        Master_User: repluser
        Master_Port: 3306
        Connect_Retry: 60
        Master_Log_File: master-log.000004
        Read_Master_Log_Pos: 498
        Relay_Log_File: relay-log.000002
        Relay_Log_Pos: 530
        Relay_Master_Log_File: master-log.000004
        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: 498
        Relay_Log_Space: 818
        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: 1
    

    mysql-master

    创建连接ProxySQL代理的用户和密码,并同步到各节点主机
    MariaDB [(none)]> GRANT ALL ON *.* TO 'proxysql'@'172.16.%.%' IDENTIFIED BY 'proxypass';
    

    ProxySQL

    下载ProxySQL程序包到本地并安装
    [root@ProxySQL ~]# ls proxysql-1.4.2-1-centos7.x86_64.rpm 
    proxysql-1.4.2-1-centos7.x86_64.rpm
    [root@ProxySQL ~]# yum -y install ./proxysql-1.4.2-1-centos7.x86_64.rpm 
    [root@ProxySQL ~]# rpm -ql proxysql 
    /etc/init.d/proxysql  \\启动脚本文件
    /etc/proxysql.cnf     \\配置文件
    /usr/bin/proxysql 
    /usr/share/proxysql/tools/proxysql_galera_checker.sh
    /usr/share/proxysql/tools/proxysql_galera_writer.pl
    
    配置Proxy
    [root@ProxySQL ~]# vim /etc/proxysql.cnf
    datadir="/var/lib/proxysql"
    
    admin_variables=
    {
        admin_credentials="admin:admin"
        mysql_ifaces="127.0.0.1:6032;/tmp/proxysql_admin.sock" \\若本机mysql服务没有启用,则使用/tmp/proxysql_admin.sock套接字文件连接管理通信
    }
    
    mysql_variables=
    {
        threads=4                   \\开启的线程数
        max_connections=2048
        default_query_delay=0
        default_query_timeout=36000000
        have_compress=true
        poll_timeout=2000
        interfaces="0.0.0.0:3306;/tmp/proxysql.sock"  \\若本机mysql服务没有启用,则使用/tmp/proxysql.sock套接字文件连接通信
        default_schema="hidb"
        stacksize=1048576
        server_version="5.5.30"
        connect_timeout_server=3000
    }
    
    mysql_servers =
    (
        {
            address = "172.16.253.105"
            port = 3306
            hostgroup = 0          
                status = "ONLINE"     
                weight = 1           
                compression = 0
        },
        {
            address = "172.16.252.82"
            port = 3306
            hostgroup = 0          
                status = "ONLINE"     
                weight = 1           
                compression = 0
        },
        {
            address = "172.16.252.82"
            port = 3306
            hostgroup = 0          
                status = "ONLINE"     
                weight = 1           
                compression = 0
        }
    )
    
    mysql_users:
    (
        {
            username = "proxysql"  # no default , required
            password = "proxypass" # default: ''
            default_hostgroup = 0  # default: 0
            active = 1             # default: 1
        }
    )
    
    mysql_replication_hostgroups=
    (
       {
            writer_hostgroup=0
            reader_hostgroup=1
            comment="repl cluster 1"
       }
    )
    
    [root@ProxySQL ~]# service proxysql start
    [root@ProxySQL ~]# ss -ntl
    State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
    LISTEN     0      128             *:3306                        *:*                  
    LISTEN     0      128             *:3306                        *:*                  
    LISTEN     0      128             *:3306                        *:*                  
    LISTEN     0      128             *:3306                        *:*       
    
    测试
    登录本机数据库,因为本机并没有安装mysql,故使用-S参数指定配置文件中指定的/tmp/proxysql.sock套接字文件通信,使用定义连接ProxySQL代理的账号密码登录
    [root@ProxySQL ~]# mysql -S /tmp/proxysql.sock -uproxysql -pproxypass
    Your MySQL connection id is 212
    Server version: 5.5.30 (ProxySQL) \\显示mysql服务器的版本号
    
    MariaDB [(none)]> CREATE DATABASE hidb;
    MariaDB [(none)]> USE hidb;
    MariaDB [hidb]> CREATE TABLE mytb(id INT,name CHAR(50));
    MariaDB [hidb]> INSERT INTO mytb VALUES (1,'tom');
    MariaDB [hidb]> SELECT * FROM mytb;
    +------+------+
    | id   | name |
    +------+------+
    |    1 | tom  |
    +------+------+
    
    CLAVE服务端数据实现了同步,即ProxySQL实现了代理数据库的效果,并达到了数据分离的效果
    mysql-slave1
    MariaDB [(none)]> SELECT * FROM hidb.mytb;
    +------+------+
    | id   | name |
    +------+------+
    |    1 | tom  |
    +------+------+
    

    相关文章

      网友评论

          本文标题:Mysql实战之主从复制的读写分离

          本文链接:https://www.haomeiwen.com/subject/ujrwsxtx.html