• Linux操作文档——MySQL搭建MyCAT(5.7.26)



    一、MyCat基础概念

    1、什么是MyCat

    1. 一个彻底开源的,面向企业应用开发的大数据库集群
    2. 支持事务、ACID、可以替代MySQL的加强版数据库
    3. 一个可以视为MySQL集群的企业级数据库,用来替代昂贵的Oracle集群
    4. 一个融合内存缓存技术、NoSQL技术、HDFS大数据的新型SQL Server
    5. 结合传统数据库和新型分布式数据仓库的新一代企业级数据库产品
    6. 支持MySQL、ORACLE、SQLServer等一些主流的数据库

    2、MyCat的作用

    能满足数据库数据大量存储;提高了查询性能

    1. 读写分离
    2. 数据分片 垂直拆分(分库) 、 水平拆分(分表) 、 垂直+水平拆分(分库分表)
    3. 多数据源整合

    3、MyCat的原理

    MyCAT通过定义表的分片规则来实现分片,每个表格可以捆绑一个分片规则,每个分片规则指定一个分片字段并绑定一个函数,来实现动态分片算法。

    1.Schema:逻辑库,与MySQL中的Database(数据库)对应,一个逻辑库中定义了所包括的Table。

    2.Table:逻辑表,即物理数据库中存储的某一张表,与传统数据库不同,这里的表格需要声明其所存储的逻辑数据节点DataNode。在此可以指定表的分片规则。

    3.DataNode:MyCAT的逻辑数据节点,是存放table的具体物理节点,也称之为分片节点,通过DataSource来关联到后端某个具体数据库上

    4.DataSource:定义某个物理库的访问地址,用于捆绑到Datanode上

    5、分片规则:前面讲了数据切分,一个大表被分成若干个分片表,就需要一定的规则,这样按照某种业务规则把数据分到某个分片的规则就是分片规则,数据切分选择合适的分片规则非常重要,将极大的避免后续数据处理的难点

    二、架构设计

    1、主从规划

    数据库mysql-1mysql-2
    实例3307 3308 3309 33103307 3308 3309 3310
    互为主从mysql-1:3307←→mysql-2:3307mysql-1:3308←→mysql-2:3308
    单向主从mysql-1:3309—→mysql-1:3307mysql-2:3309—→mysql-2:3307
    单向主从mysql-1:3310—→mysql-1:3308mysql-2:3310—→mysql-2:3308

    2、分片规划

    shard1shard2
    Mastermysql-1:3307mysql-2:3308
    slave1mysql-1:3309mysql-2:3310
    Standby Mastermysql-2:3307mysql-1:3308
    slave2mysql-2:3309mysql-1:3310

    三、环境搭建

    1、准备实例

    1、安装MySQL
    [root@mysql-1 ~]# mkdir -p /data/33{07..10}/data
    [root@mysql-1 ~]# mysqld --initialize-insecure  --user=mysql --datadir=/data/3307/data --basedir=/usr/local/mysql
    [root@mysql-1 ~]# mysqld --initialize-insecure  --user=mysql --datadir=/data/3308/data --basedir=/usr/local/mysql
    [root@mysql-1 ~]# mysqld --initialize-insecure  --user=mysql --datadir=/data/3309/data --basedir=/usr/local/mysql
    [root@mysql-1 ~]# mysqld --initialize-insecure  --user=mysql --datadir=/data/3310/data --basedir=/usr/local/mysql
    
    • 1
    • 2
    • 3
    • 4
    • 5
    2、准备mysql-1配置文件
    [root@mysql-1 ~]# cat >/data/3307/my.cnf<
    [mysqld]
    basedir=/usr/local/mysql
    datadir=/data/3307/data
    socket=/data/3307/mysql.sock
    port=3307
    log-error=/data/3307/mysql.log
    log_bin=/data/3307/mysql-bin
    binlog_format=row
    skip-name-resolve
    server-id=7
    gtid-mode=on
    enforce-gtid-consistency=true
    log-slave-updates=1
    EOF
    [root@mysql-1 ~]# cat >/data/3308/my.cnf<
    [mysqld]
    basedir=/usr/local/mysql
    datadir=/data/3308/data
    port=3308
    socket=/data/3308/mysql.sock
    log-error=/data/3308/mysql.log
    log_bin=/data/3308/mysql-bin
    binlog_format=row
    skip-name-resolve
    server-id=8
    gtid-mode=on
    enforce-gtid-consistency=true
    log-slave-updates=1
    EOF
    [root@mysql-1 ~]# cat >/data/3309/my.cnf<
    [mysqld]
    basedir=/usr/local/mysql
    datadir=/data/3309/data
    socket=/data/3309/mysql.sock
    port=3309
    log-error=/data/3309/mysql.log
    log_bin=/data/3309/mysql-bin
    binlog_format=row
    skip-name-resolve
    server-id=9
    gtid-mode=on
    enforce-gtid-consistency=true
    log-slave-updates=1
    EOF
    [root@mysql-1 ~]# cat >/data/3310/my.cnf<
    [mysqld]
    basedir=/usr/local/mysql
    datadir=/data/3310/data
    socket=/data/3310/mysql.sock
    port=3310
    log-error=/data/3310/mysql.log
    log_bin=/data/3310/mysql-bin
    binlog_format=row
    skip-name-resolve
    server-id=10
    gtid-mode=on
    enforce-gtid-consistency=true
    log-slave-updates=1
    EOF
    [root@mysql-1 ~]# cat >/etc/systemd/system/mysqld3307.service<
    [Unit]
    Description=MySQL Server
    Documentation=man:mysqld(8)
    Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
    After=network.target
    After=syslog.target
    [Install]
    WantedBy=multi-user.target
    [Service]
    User=mysql
    Group=mysql
    ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/data/3307/my.cnf
    LimitNOFILE = 5000
    EOF
    [root@mysql-1 ~]# cat >/etc/systemd/system/mysqld3308.service<
    [Unit]
    Description=MySQL Server
    Documentation=man:mysqld(8)
    Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
    After=network.target
    After=syslog.target
    [Install]
    WantedBy=multi-user.target
    [Service]
    User=mysql
    Group=mysql
    ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/data/3308/my.cnf
    LimitNOFILE = 5000
    EOF
    [root@mysql-1 ~]# cat >/etc/systemd/system/mysqld3309.service<
    [Unit]
    Description=MySQL Server
    Documentation=man:mysqld(8)
    Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
    After=network.target
    After=syslog.target
    [Install]
    WantedBy=multi-user.target
    [Service]
    User=mysql
    Group=mysql
    ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/data/3309/my.cnf
    LimitNOFILE = 5000
    EOF
    [root@mysql-1 ~]# cat >/etc/systemd/system/mysqld3310.service<
    [Unit]
    Description=MySQL Server
    Documentation=man:mysqld(8)
    Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
    After=network.target
    After=syslog.target
    [Install]
    WantedBy=multi-user.target
    [Service]
    User=mysql
    Group=mysql
    ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/data/3310/my.cnf
    LimitNOFILE = 5000
    EOF
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    3、准备mysql-2配置文件
    [root@mysql-2 ~]# cat >/data/3307/my.cnf<
    [mysqld]
    basedir=/usr/local/mysql
    datadir=/data/3307/data
    socket=/data/3307/mysql.sock
    port=3307
    log-error=/data/3307/mysql.log
    log_bin=/data/3307/mysql-bin
    binlog_format=row
    skip-name-resolve
    server-id=17
    gtid-mode=on
    enforce-gtid-consistency=true
    log-slave-updates=1
    EOF
    [root@mysql-2 ~]# cat >/data/3308/my.cnf<
    [mysqld]
    basedir=/usr/local/mysql
    datadir=/data/3308/data
    port=3308
    socket=/data/3308/mysql.sock
    log-error=/data/3308/mysql.log
    log_bin=/data/3308/mysql-bin
    binlog_format=row
    skip-name-resolve
    server-id=18
    gtid-mode=on
    enforce-gtid-consistency=true
    log-slave-updates=1
    EOF
    [root@mysql-2 ~]# cat >/data/3309/my.cnf<
    [mysqld]
    basedir=/usr/local/mysql
    datadir=/data/3309/data
    socket=/data/3309/mysql.sock
    port=3309
    log-error=/data/3309/mysql.log
    log_bin=/data/3309/mysql-bin
    binlog_format=row
    skip-name-resolve
    server-id=19
    gtid-mode=on
    enforce-gtid-consistency=true
    log-slave-updates=1
    EOF
    [root@mysql-2 ~]# cat >/data/3310/my.cnf<
    [mysqld]
    basedir=/usr/local/mysql
    datadir=/data/3310/data
    socket=/data/3310/mysql.sock
    port=3310
    log-error=/data/3310/mysql.log
    log_bin=/data/3310/mysql-bin
    binlog_format=row
    skip-name-resolve
    server-id=20
    gtid-mode=on
    enforce-gtid-consistency=true
    log-slave-updates=1
    EOF
    [root@mysql-2 ~]# cat >/etc/systemd/system/mysqld3307.service<
    [Unit]
    Description=MySQL Server
    Documentation=man:mysqld(8)
    Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
    After=network.target
    After=syslog.target
    [Install]
    WantedBy=multi-user.target
    [Service]
    User=mysql
    Group=mysql
    ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/data/3307/my.cnf
    LimitNOFILE = 5000
    EOF
    [root@mysql-2 ~]# cat >/etc/systemd/system/mysqld3308.service<
    [Unit]
    Description=MySQL Server
    Documentation=man:mysqld(8)
    Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
    After=network.target
    After=syslog.target
    [Install]
    WantedBy=multi-user.target
    [Service]
    User=mysql
    Group=mysql
    ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/data/3308/my.cnf
    LimitNOFILE = 5000
    EOF
    [root@mysql-2 ~]# cat >/etc/systemd/system/mysqld3309.service<
    [Unit]
    Description=MySQL Server
    Documentation=man:mysqld(8)
    Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
    After=network.target
    After=syslog.target
    [Install]
    WantedBy=multi-user.target
    [Service]
    User=mysql
    Group=mysql
    ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/data/3309/my.cnf
    LimitNOFILE = 5000
    EOF
    [root@mysql-2 ~]# cat >/etc/systemd/system/mysqld3310.service<
    [Unit]
    Description=MySQL Server
    Documentation=man:mysqld(8)
    Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
    After=network.target
    After=syslog.target
    [Install]
    WantedBy=multi-user.target
    [Service]
    User=mysql
    Group=mysql
    ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/data/3310/my.cnf
    LimitNOFILE = 5000
    EOF
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    4、修改权限,启动多实例
    [root@mysql-1 ~]# chown -R mysql.mysql /data/*
    [root@mysql-1 ~]# systemctl start mysqld3307
    [root@mysql-1 ~]# systemctl start mysqld3308
    [root@mysql-1 ~]# systemctl start mysqld3309
    [root@mysql-1 ~]# systemctl start mysqld3310
    [root@mysql-1 ~]# netstat -anpt | grep mysql
    tcp6       0      0 :::3307                 :::*                    LISTEN      17583/mysqld        
    tcp6       0      0 :::3308                 :::*                    LISTEN      17590/mysqld        
    tcp6       0      0 :::3309                 :::*                    LISTEN      17597/mysqld        
    tcp6       0      0 :::3310                 :::*                    LISTEN      17685/mysqld        
    [root@mysql-1 ~]# mysql -S /data/3307/mysql.sock -e "show variables like 'server_id'"
    +---------------+-------+
    | Variable_name | Value |
    +---------------+-------+
    | server_id     | 7     |
    +---------------+-------+
    [root@mysql-1 ~]# mysql -S /data/3308/mysql.sock -e "show variables like 'server_id'"
    +---------------+-------+
    | Variable_name | Value |
    +---------------+-------+
    | server_id     | 8     |
    +---------------+-------+
    [root@mysql-1 ~]# mysql -S /data/3309/mysql.sock -e "show variables like 'server_id'"
    +---------------+-------+
    | Variable_name | Value |
    +---------------+-------+
    | server_id     | 9     |
    +---------------+-------+
    [root@mysql-1 ~]# mysql -S /data/3310/mysql.sock -e "show variables like 'server_id'"
    +---------------+-------+
    | Variable_name | Value |
    +---------------+-------+
    | server_id     | 10    |
    +---------------+-------+
    [root@mysql-2 ~]# mysql -S /data/3307/mysql.sock -e "show variables like 'server_id'"
    +---------------+-------+
    | Variable_name | Value |
    +---------------+-------+
    | server_id     | 17    |
    +---------------+-------+
    [root@mysql-2 ~]# mysql -S /data/3308/mysql.sock -e "show variables like 'server_id'"
    +---------------+-------+
    | Variable_name | Value |
    +---------------+-------+
    | server_id     | 18    |
    +---------------+-------+
    [root@mysql-2 ~]# mysql -S /data/3309/mysql.sock -e "show variables like 'server_id'"
    +---------------+-------+
    | Variable_name | Value |
    +---------------+-------+
    | server_id     | 19    |
    +---------------+-------+
    [root@mysql-2 ~]# mysql -S /data/3310/mysql.sock -e "show variables like 'server_id'"
    +---------------+-------+
    | Variable_name | Value |
    +---------------+-------+
    | server_id     | 20    |
    +---------------+-------+
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58

    2、配置主从

    1、配置互为主从

    1、mysql-1:3307←→mysql-2:3307

    [root@mysql-2 ~]# mysql  -S /data/3307/mysql.sock -e "grant replication slave on *.* to repl@'192.168.1.%' identified by '123';"
    [root@mysql-2 ~]# mysql  -S /data/3307/mysql.sock -e "grant all  on *.* to root@'192.168.1.%' identified by '123'  with grant option;"
    [root@mysql-1 ~]# mysql  -S /data/3307/mysql.sock -e "CHANGE MASTER TO MASTER_HOST='192.168.1.20', MASTER_PORT=3307, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='123';"
    [root@mysql-1 ~]# mysql  -S /data/3307/mysql.sock -e "start slave;"
    [root@mysql-1 ~]# mysql  -S /data/3307/mysql.sock -e "show slave status\G"
    *************************** 1. row ***************************
                   Slave_IO_State: Waiting for master to send event
                      Master_Host: 192.168.1.20
                      Master_User: repl
                      Master_Port: 3307
                    Connect_Retry: 60
                  Master_Log_File: mysql-bin.000001
              Read_Master_Log_Pos: 1045
                   Relay_Log_File: mysql-1-relay-bin.000002
                    Relay_Log_Pos: 1258
            Relay_Master_Log_File: mysql-bin.000001
                 Slave_IO_Running: Yes
                Slave_SQL_Running: Yes
    [root@mysql-2 ~]# mysql  -S /data/3307/mysql.sock -e "CHANGE MASTER TO MASTER_HOST='192.168.1.10', MASTER_PORT=3307, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='123';"
    [root@mysql-2 ~]# mysql  -S /data/3307/mysql.sock -e "start slave;"
    [root@mysql-2 ~]# mysql  -S /data/3307/mysql.sock -e "show slave status\G"
    *************************** 1. row ***************************
                   Slave_IO_State: Waiting for master to send event
                      Master_Host: 192.168.1.10
                      Master_User: repl
                      Master_Port: 3307
                    Connect_Retry: 60
                  Master_Log_File: mysql-bin.000001
              Read_Master_Log_Pos: 1045
                   Relay_Log_File: mysql-2-relay-bin.000002
                    Relay_Log_Pos: 414
            Relay_Master_Log_File: mysql-bin.000001
                 Slave_IO_Running: Yes
                Slave_SQL_Running: Yes
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    2、mysql-1:3308←→mysql-2:3308

    [root@mysql-1 ~]# mysql  -S /data/3308/mysql.sock -e "grant replication slave on *.* to repl@'192.168.1.%' identified by '123';"
    [root@mysql-1 ~]# mysql  -S /data/3308/mysql.sock -e "grant all  on *.* to root@'192.168.1.%' identified by '123'  with grant option;"
    [root@mysql-2 ~]# mysql  -S /data/3308/mysql.sock -e "CHANGE MASTER TO MASTER_HOST='192.168.1.10', MASTER_PORT=3308, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='123';"
    [root@mysql-2 ~]# mysql  -S /data/3308/mysql.sock -e "start slave;"
    [root@mysql-2 ~]# mysql  -S /data/3308/mysql.sock -e "show slave status\G"
    *************************** 1. row ***************************
                   Slave_IO_State: Waiting for master to send event
                      Master_Host: 192.168.1.10
                      Master_User: repl
                      Master_Port: 3308
                    Connect_Retry: 60
                  Master_Log_File: mysql-bin.000001
              Read_Master_Log_Pos: 755
                   Relay_Log_File: mysql-2-relay-bin.000002
                    Relay_Log_Pos: 968
            Relay_Master_Log_File: mysql-bin.000001
                 Slave_IO_Running: Yes
                Slave_SQL_Running: Yes
    [root@mysql-1 ~]# mysql  -S /data/3308/mysql.sock -e "CHANGE MASTER TO MASTER_HOST='192.168.1.20', MASTER_PORT=3308, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='123';"
    [root@mysql-1 ~]# mysql  -S /data/3308/mysql.sock -e "start slave;"
    [root@mysql-1 ~]# mysql  -S /data/3308/mysql.sock -e "show slave status\G"
    *************************** 1. row ***************************
                   Slave_IO_State: Waiting for master to send event
                      Master_Host: 192.168.1.20
                      Master_User: repl
                      Master_Port: 3308
                    Connect_Retry: 60
                  Master_Log_File: mysql-bin.000001
              Read_Master_Log_Pos: 755
                   Relay_Log_File: mysql-1-relay-bin.000002
                    Relay_Log_Pos: 414
            Relay_Master_Log_File: mysql-bin.000001
                 Slave_IO_Running: Yes
                Slave_SQL_Running: Yes
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    2、配置单向主从

    1、mysql-1:3309—→mysql-1:3307

    [root@mysql-1 ~]# mysql  -S /data/3309/mysql.sock  -e "CHANGE MASTER TO MASTER_HOST='192.168.1.10', MASTER_PORT=3307, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='123';"
    [root@mysql-1 ~]# mysql  -S /data/3309/mysql.sock  -e "start slave;"
    [root@mysql-1 ~]# mysql  -S /data/3309/mysql.sock  -e "show slave status\G"
    *************************** 1. row ***************************
                   Slave_IO_State: Waiting for master to send event
                      Master_Host: 192.168.1.10
                      Master_User: repl
                      Master_Port: 3307
                    Connect_Retry: 60
                  Master_Log_File: mysql-bin.000001
              Read_Master_Log_Pos: 1045
                   Relay_Log_File: mysql-1-relay-bin.000002
                    Relay_Log_Pos: 1258
            Relay_Master_Log_File: mysql-bin.000001
                 Slave_IO_Running: Yes
                Slave_SQL_Running: Yes
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    2、mysql-2:3309—→mysql-2:3307

    [root@mysql-2 ~]# mysql  -S /data/3309/mysql.sock -e "CHANGE MASTER TO MASTER_HOST='192.168.1.20', MASTER_PORT=3307, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='123';"
    [root@mysql-2 ~]# mysql  -S /data/3309/mysql.sock -e "start slave;"
    [root@mysql-2 ~]# mysql  -S /data/3309/mysql.sock -e "show slave status\G"
    *************************** 1. row ***************************
                   Slave_IO_State: Waiting for master to send event
                      Master_Host: 192.168.1.20
                      Master_User: repl
                      Master_Port: 3307
                    Connect_Retry: 60
                  Master_Log_File: mysql-bin.000001
              Read_Master_Log_Pos: 1045
                   Relay_Log_File: mysql-2-relay-bin.000002
                    Relay_Log_Pos: 1258
            Relay_Master_Log_File: mysql-bin.000001
                 Slave_IO_Running: Yes
                Slave_SQL_Running: Yes
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    3、mysql-2:3310—→mysql-2:3308

    [root@mysql-2 ~]# mysql  -S /data/3310/mysql.sock -e "CHANGE MASTER TO MASTER_HOST='192.168.1.20', MASTER_PORT=3308, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='123';"
    [root@mysql-2 ~]# mysql  -S /data/3310/mysql.sock -e "start slave;"
    [root@mysql-2 ~]# mysql  -S /data/3310/mysql.sock -e "show slave status\G"
    *************************** 1. row ***************************
                   Slave_IO_State: Waiting for master to send event
                      Master_Host: 192.168.1.20
                      Master_User: repl
                      Master_Port: 3308
                    Connect_Retry: 60
                  Master_Log_File: mysql-bin.000001
              Read_Master_Log_Pos: 755
                   Relay_Log_File: mysql-2-relay-bin.000002
                    Relay_Log_Pos: 968
            Relay_Master_Log_File: mysql-bin.000001
                 Slave_IO_Running: Yes
                Slave_SQL_Running: Yes
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    4、mysql-1:3310—→mysql-1:3308

    [root@mysql-1 ~]# mysql -S /data/3310/mysql.sock -e "CHANGE MASTER TO MASTER_HOST='192.168.1.10', MASTER_PORT=3308, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='123';"
    [root@mysql-1 ~]# mysql -S /data/3310/mysql.sock -e "start slave;"
    [root@mysql-1 ~]# mysql -S /data/3310/mysql.sock -e "show slave status\G"
    *************************** 1. row ***************************
                   Slave_IO_State: Waiting for master to send event
                      Master_Host: 192.168.1.10
                      Master_User: repl
                      Master_Port: 3308
                    Connect_Retry: 60
                  Master_Log_File: mysql-bin.000001
              Read_Master_Log_Pos: 755
                   Relay_Log_File: mysql-1-relay-bin.000002
                    Relay_Log_Pos: 968
            Relay_Master_Log_File: mysql-bin.000001
                 Slave_IO_Running: Yes
                Slave_SQL_Running: Yes
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    3、检测主从状态
    [root@mysql-1 ~]# mysql -S /data/3307/mysql.sock -e "show slave status\G"|grep Yes
                 Slave_IO_Running: Yes
                Slave_SQL_Running: Yes
    [root@mysql-1 ~]# mysql -S /data/3308/mysql.sock -e "show slave status\G"|grep Yes
                 Slave_IO_Running: Yes
                Slave_SQL_Running: Yes
    [root@mysql-1 ~]# mysql -S /data/3309/mysql.sock -e "show slave status\G"|grep Yes
                 Slave_IO_Running: Yes
                Slave_SQL_Running: Yes
    [root@mysql-1 ~]# mysql -S /data/3310/mysql.sock -e "show slave status\G"|grep Yes
                 Slave_IO_Running: Yes
                Slave_SQL_Running: Yes
    [root@mysql-2 ~]# mysql -S /data/3307/mysql.sock -e "show slave status\G"|grep Yes
                 Slave_IO_Running: Yes
                Slave_SQL_Running: Yes
    [root@mysql-2 ~]# mysql -S /data/3308/mysql.sock -e "show slave status\G"|grep Yes
                 Slave_IO_Running: Yes
                Slave_SQL_Running: Yes
    [root@mysql-2 ~]# mysql -S /data/3309/mysql.sock -e "show slave status\G"|grep Yes
                 Slave_IO_Running: Yes
                Slave_SQL_Running: Yes
    [root@mysql-2 ~]# mysql -S /data/3310/mysql.sock -e "show slave status\G"|grep Yes
                 Slave_IO_Running: Yes
                Slave_SQL_Running: Yes
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    四、MyCAT安装

    [root@mysql-1 ~]# yum -y install java
    [root@mysql-1 ~]# wget http://dl.mycat.org.cn/1.6.7.4/Mycat-server-1.6.7.4-release/Mycat-server-1.6.7.4-release-20200105164103-linux.tar.gz
    [root@mysql-1 ~]# tar zxf Mycat-server-1.6.7.4-release-20200105164103-linux.tar.gz -C /usr/local/
    [root@mysql-1 ~]# vim /etc/profile
    export PATH=/usr/local/mycat/bin:$PATH
    [root@mysql-1 ~]# source /etc/profile
    [root@mysql-1 ~]# mycat start
    Starting Mycat-server...
    [root@mysql-1 ~]# mysql -uroot -p123456 -h 127.0.0.1 -P8066
    mysql> 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    Mycat的默认端口号为:8066

    五、Mycat基础应用

    1、实现1主1从读写分离

    Mycat的读写分离是建立在Mysq的主从复制的基础上的,修改配置文件 schema.xml

    [root@mysql-1 ~]# cd /usr/local/mycat/conf
    [root@mysql-1 conf]# mv schema.xml schema.xml.bak
    [root@mysql-1 conf]# vim schema.xml 
    <!DOCTYPE mycat:schema SYSTEM "schema.dtd">
    <mycat:schema xmlns:mycat="http://io.mycat/">
    #逻辑库定义
    <schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100" dataNode="sh1">
    </schema>
    #数据节点定义
            <dataNode name="sh1" dataHost="mysql-1" database= "world" />
    #后端主机定义
            <dataHost name="mysql-1" maxCon="1000" minCon="10" balance="1"  writeType="0" dbType="mysql"  dbDriver="native" switchType="1">
                    <heartbeat>select user()</heartbeat>
            <writeHost host="db1" url="192.168.1.10:3307" user="root" password="123">
                            <readHost host="db2" url="192.168.1.10:3309" user="root" password="123" />
            </writeHost>
            </dataHost>
    </mycat:schema>
    [root@mysql-1 ~]# mycat start
    Starting Mycat-server...
    Mycat-server is already running.
    [root@mysql-1 ~]# mysql -uroot -p123456 -h 192.168.1.10 -P 8066
    Your MySQL connection id is 2
    Server version: 5.6.29-mycat-1.6.7.4-release-20200105164103 MyCat Server (OpenCloudDB)
    mysql> select @@server_id;       //测试读
    +-------------+
    | @@server_id |
    +-------------+
    |           9 |
    +-------------+
    1 row in set (0.05 sec)
    
    mysql> begin;select @@server_id;commit;       //测试写
    Query OK, 0 rows affected (0.01 sec)
    
    +-------------+
    | @@server_id |
    +-------------+
    |           7 |
    +-------------+
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    属性说明
    schema name定义mycat实例中的逻辑库,mycat可以有多个逻辑库,每个逻辑库都有自己的相关配置。可以使用schema标签来划分这些不同的逻辑库
    dataNode该标签用于绑定逻辑库到某个具体的database上
    dataHost定义了具体数据库实例,读写分离配置和心跳语句
    属性说明
    maxCon最大的并发连接数
    minConmycat在启动之后,会在后端节点上自动开启的连接线程
    tempReadHostAvailable这个一主一从时(1个writehost,1个readhost时),可以开启这个参数,如果2个writehost,2个readhost时
    select user()监测心跳

    2、实现高可用+读写分离

    [root@mysql-1 ~]# cd /usr/local/mycat/conf/
    [root@mysql-1 conf]# vim schema.xml 
    <!DOCTYPE mycat:schema SYSTEM "schema.dtd">  
    <mycat:schema xmlns:mycat="http://io.mycat/">
    <schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100" dataNode="sh1"> 
    </schema>  
            <dataNode name="sh1" dataHost="mysql-1" database= "world" />         
            <dataHost name="mysql-1" maxCon="1000" minCon="10" balance="1"  writeType="0" dbType="mysql"  dbDriver="native" switchType="1">    
                    <heartbeat>select user()</heartbeat>  
                    #真正的写节点,负责写操作
            <writeHost host="db1" url="192.168.1.10:3307" user="root" password="123">
                            <readHost host="db2" url="192.168.1.10:3309" user="root" password="123" /> 
            </writeHost> 
                   #准备写节点,负责读,当192.168.1.10:3307宕掉,会切换成为真正的写节点
    		 <writeHost host="db3" url="192.168.1.20:3307" user="root" password="123">
                            <readHost host="db4" url="192.168.1.20:3309" user="root" password="123" /> 
            </writeHost> 
            </dataHost>  
    </mycat:schema>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    取值balance(负载均衡类型)writeType(负载均衡类型)switchType
    -1//不自动切换
    0不开启读写分离机制,所有读操作都发送到当前可用的writeHost上/默认值,自动切换
    1全部的readHost与standby writeHost参与select语句的负载均衡所有写操作发送到配置的第一个writeHost基于MySQL主从同步的状态决定是否切换 ,心跳语句为 show slave status
    2所有读操作都随机的在writeHost、readhost上分发所有写操作都随机的发送到配置的writeHost/

    六、Mycat高级应用-分布式解决方案

    1、垂直切分

    作用:是指按照业务将表进行分类, 分布到不同的数据库上面,这样也就将数据或者说压力分担到不同的库上面
    如何划分表分库的原则: 有紧密关联关系的表应该在一个库里,相互没有关联关系的表可以分到不同的库里。

    [root@mysql-1 conf]# vim schema.xml
    <?xml version="1.0"?>
    <!DOCTYPE mycat:schema SYSTEM "schema.dtd">
    <mycat:schema xmlns:mycat="http://io.mycat/">
    <schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100" dataNode="sh1">
            <table name="user" dataNode="sh1"/>
            <table name="order_t" dataNode="sh2"/>
    </schema>
        <dataNode name="sh1" dataHost="mysql-1" database= "taobao" />
        <dataNode name="sh2" dataHost="mysql-2" database= "taobao" />
        <dataHost name="mysql-1" maxCon="1000" minCon="10" balance="1"  writeType="0" dbType="mysql"  dbDriver="native" switchType="1">
            <heartbeat>select user()</heartbeat>
        <writeHost host="db1" url="192.168.1.10:3307" user="root" password="123">
                <readHost host="db2" url="192.168.1.10:3309" user="root" password="123" />
        </writeHost>
        <writeHost host="db3" url="192.168.1.20:3307" user="root" password="123">
                <readHost host="db4" url="192.168.1.20:3309" user="root" password="123" />
        </writeHost>
        </dataHost>
    	
        <dataHost name="mysql-2" maxCon="1000" minCon="10" balance="1"  writeType="0" dbType="mysql"  dbDriver="native" switchType="1">
            <heartbeat>select user()</heartbeat>
        <writeHost host="db1" url="192.168.1.10:3308" user="root" password="123">
                <readHost host="db2" url="192.168.1.10:3310" user="root" password="123" />
        </writeHost>
        <writeHost host="db3" url="192.168.1.20:3308" user="root" password="123">
                <readHost host="db4" url="192.168.1.20:3310" user="root" password="123" />
        </writeHost>
        </dataHost>	
    </mycat:schema>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    2、水平拆分

    目的:将大数据量进行分布存储,提供均衡的访问路由
    是指按照某个字段的某种规则来分散到多个库之中, 每个表中包含一部分数据。简单来说,我们可以将数据的水平切分理解为是按照数据行的切分,就 是将表中的某些行切分到一个数据库,而另外的某些行又切分到其他的数据库中。

    1、范围分片

    目的:根据某个字段的数值范围来确定这些数据到底存放在哪一个分片上

    [root@mysql-1 conf]# vim schema.xml
    <?xml version="1.0"?>
    <!DOCTYPE mycat:schema SYSTEM "schema.dtd">
    <mycat:schema xmlns:mycat="http://io.mycat/">
    <schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100" dataNode="sh1">
            <table name="t3" dataNode="sh1,sh2" rule="auto-sharding-long" />
    [root@mysql-1 conf]# vim rule.xml
             <tableRule name="auto-sharding-long">
                    <rule>
                            <columns>id</columns>     //以id作为数值范围
                            <algorithm>rang-long</algorithm>
                    </rule>
            <function name="rang-long"
                              class="io.mycat.route.function.AutoPartitionByLong">
                    <property name="mapFile">autopartition-long.txt</property>
            </function>
    [root@mysql-1 conf]# vim autopartition-long.txt
    # range start-end ,data node index
    # K=1000,M=10000.    K=千,M=万
    0-500M=0
    500M-1000M=1
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    注意:分片规则配置文件autopartition-long.txt中配置的节点要跟dataNode数量对应上,不然可能会报错

    2、取模分片

    目的:切分规则根据配置中输入的数值n。此种分片规则将数据分成n份,从而将数据均匀的分布于各个节点上
    取余分片方式:分片键(一个列)与节点数量进行取余,得到余数,将数据写入对应节点

    [root@mysql-1 conf]# vim schema.xml
    <?xml version="1.0"?>
    <!DOCTYPE mycat:schema SYSTEM "schema.dtd">
    <mycat:schema xmlns:mycat="http://io.mycat/">
    <schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100" dataNode="sh1">
    <table name="t4" dataNode="sh1,sh2" rule="mod-long" />
    [root@mysql-1 conf]# vim rule.xml
            <tableRule name="mod-long">
                    <rule>
                            <columns>id</columns>
                            <algorithm>mod-long</algorithm>
                    </rule>
            </tableRule>
            <function name="mod-long" class="io.mycat.route.function.PartitionByMod">
                    <!-- how many data nodes -->
                    <property name="count">2</property>     //2即为节点数
            </function>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    3、枚举分片

    目的:将符合条件的记录分配到对应节点上,不满足条件的分配到默认节点

    [root@mysql-1 conf]# vim schema.xml
    <?xml version="1.0"?>
    <!DOCTYPE mycat:schema SYSTEM "schema.dtd">
    <mycat:schema xmlns:mycat="http://io.mycat/">
    <schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100" dataNode="sh1">
    <table name="t5" dataNode="sh1,sh2" rule="sharding-by-intfile" />
    [root@mysql-1 conf]# vim rule.xml
            <tableRule name="sharding-by-intfile">
                    <rule>
                            <columns>sharding_id</columns>
                            <algorithm>hash-int</algorithm>
                    </rule>
            <function name="hash-int"
                              class="io.mycat.route.function.PartitionByFileMap">
                    <property name="mapFile">partition-hash-int.txt</property>
                    <property name="type">1</property>     //支持字符串(中文)
                    <property name="defaultNode">0</property>
            </function>
    [root@mysql-1 conf]# vim partition-hash-int.txt
    10000=0
    10010=1
    DEFAULT_NODE=1    //情况之外
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    参数说明
    columns标识将要分片的表字段
    algorithm分片函数
    mapFile标识配置文件名称

    3、全局表

    指在所有节点都保存一份一样的数据

    [root@mysql-1 conf]# vim schema.xml 
    <table name="t6" primaryKey="id" type="global" dataNode="sh1,sh2" /> 
    
    • 1
    • 2
  • 相关阅读:
    Vue 双向绑定、diff和nextTick原理
    【性能优化】单一接口优化过程全记录(主要涉及Redis)
    java中的深度复制和浅复制的BUG
    2022-Q3
    SettingsIntelligence
    基于netmap的用户态协议栈(一)
    git工作常用命令
    Leo赠书活动-02期 【信息科技风险管理:合规管理、技术防控与数字化】
    【ruoyi】微服务关闭登录验证码
    ORA_00604
  • 原文地址:https://blog.csdn.net/g950904/article/details/109266973