Mysql實(shí)戰(zhàn)之主從復(fù)制的讀寫分離

ProxySQL構(gòu)建主從復(fù)制的讀寫分離

ProxySQL官網(wǎng)及下載地址 http://www.proxysql.com/

原理

mysql的讀寫分離的基本原理是:讓master(主數(shù)據(jù)庫(kù))來響應(yīng)事務(wù)性操作,讓slave(從數(shù)據(jù)庫(kù))來響應(yīng)select非事務(wù)性操作,然后再采用主從復(fù)制來把master上的事務(wù)性操作同步到slave數(shù)據(jù)庫(kù)中

架構(gòu)角色
mysql-slave2    172.16.252.92
mysql-slave1    172.16.252.82
ProxySQL        172.16.253.105
mysql-master    172.16.252.30
環(huán)境準(zhǔn)備
各節(jié)點(diǎn)服務(wù)端都需提前同步時(shí)間及安裝mariadb數(shù)據(jù)庫(kù)
    [root@mysql-master ~]# ntpdate 172.16.0.1
    [root@mysql-slave1 ~]# yum -y install mariadb-server 
關(guān)閉防火墻
    [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  \\每表使用單獨(dú)的表文件存儲(chǔ)
[root@mysql-master ~]# systemctl start mariadb 

[root@mysql-master ~]# mysql

創(chuàng)建用戶并授權(quán)連接主節(jié)點(diǎn)復(fù)制數(shù)據(jù)的權(quán)限
MariaDB [(none)]> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO 'repluser'@'172.16.252.%' IDENTIFIED BY 'replpass'; 
MariaDB [(none)]> FLUSH PRIVILEGES;
顯示master節(jié)點(diǎn)的狀態(tài),記錄當(dāng)前節(jié)點(diǎn)的位置
MariaDB [(none)]> SHOW MASTER STATUS;
+-------------------+----------+--------------+------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-log.000004 |      498 |              |                  |
+-------------------+----------+--------------+------------------+

創(chuàng)建連接ProxySQL代理的用戶和密碼,并同步到各節(jié)點(diǎn)主機(jī)
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節(jié)點(diǎn)上只有只讀權(quán)限
[root@mysql-slave1 ~]# systemctl start mariadb

[root@mysql-slave1 ~]# mysql
從master的當(dāng)前節(jié)點(diǎn)開始同步復(fù)制數(shù)據(jù)
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;  
開啟復(fù)制進(jìn)程
MariaDB [(none)]> START SLAVE;
顯示slave節(jié)點(diǎn)的狀態(tài)信息
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的當(dāng)前節(jié)點(diǎn)開始同步復(fù)制數(shù)據(jù)
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;  
開啟復(fù)制進(jìn)程
MariaDB [(none)]> START SLAVE;
顯示slave節(jié)點(diǎn)的狀態(tài)信息
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

創(chuàng)建連接ProxySQL代理的用戶和密碼,并同步到各節(jié)點(diǎn)主機(jī)
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  \\啟動(dòng)腳本文件
/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" \\若本機(jī)mysql服務(wù)沒有啟用,則使用/tmp/proxysql_admin.sock套接字文件連接管理通信
}

mysql_variables=
{
    threads=4                   \\開啟的線程數(shù)
    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"  \\若本機(jī)mysql服務(wù)沒有啟用,則使用/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                        *:*       
測(cè)試
登錄本機(jī)數(shù)據(jù)庫(kù),因?yàn)楸緳C(jī)并沒有安裝mysql,故使用-S參數(shù)指定配置文件中指定的/tmp/proxysql.sock套接字文件通信,使用定義連接ProxySQL代理的賬號(hào)密碼登錄
[root@ProxySQL ~]# mysql -S /tmp/proxysql.sock -uproxysql -pproxypass
Your MySQL connection id is 212
Server version: 5.5.30 (ProxySQL) \\顯示mysql服務(wù)器的版本號(hào)

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服務(wù)端數(shù)據(jù)實(shí)現(xiàn)了同步,即ProxySQL實(shí)現(xiàn)了代理數(shù)據(jù)庫(kù)的效果,并達(dá)到了數(shù)據(jù)分離的效果
mysql-slave1
MariaDB [(none)]> SELECT * FROM hidb.mytb;
+------+------+
| id   | name |
+------+------+
|    1 | tom  |
+------+------+
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容