mysql主从复制读写分离

实现主从复制的读写分离

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

架构角色

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  |
+------+------+
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 148,827评论 1 317
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 63,511评论 1 266
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 99,318评论 0 218
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 42,108评论 0 189
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 50,112评论 1 266
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 39,387评论 1 185
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 30,905评论 2 283
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 29,657评论 0 177
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 33,144评论 0 223
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 29,744评论 2 225
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,100评论 1 236
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 27,565评论 2 222
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,041评论 3 216
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 25,769评论 0 9
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,301评论 0 178
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 34,173评论 2 239
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 34,313评论 2 242

推荐阅读更多精彩内容