MySQL集群配置

1、MySQL Ver 8.0.21主从复制及主主复制的实现

1.1 主节点

[root@centos8-8 ~]# yum -y install mysql-server
[root@centos8-8 ~]# vi /etc/my.cnf.d/mysql-server.cnf
[mysqld]
log_bin
server_id=8

[root@centos8-8 ~]# systemctl restart mysqld.service
[root@centos8-8 ~]# mysql
#查看二进制日志文件和位置
mysql> show master logs;
+----------------------+-----------+-----------+
| Log_name             | File_size | Encrypted |
+----------------------+-----------+-----------+
| centos8-8-bin.000001 |       179 | No        |
| centos8-8-bin.000002 |       179 | No        |
| centos8-8-bin.000003 |       179 | No        |
| centos8-8-bin.000004 |       179 | No        |
| centos8-8-bin.000005 |       156 | No        |
+----------------------+-----------+-----------+
5 rows in set (0.00 sec)

#创建复制用户并添加slave复制权限
mysql> create user 'repluser'@'192.168.108.%' identified by 'ddq.com';
Query OK, 0 rows affected (0.01 sec)

mysql> grant replication slave on *.* to 'repluser'@'192.168.108.%';
Query OK, 0 rows affected (0.01 sec)


1.2 从节点

[root@centos8-18 ~]# yum -y install mysql-server
[root@centos8-18 ~]# vi /etc/my.cnf.d/mysql-server.cnf
[mysqld]
server_id=18
[root@centos8-18 ~]# systemctl restart mysqld.service
[root@centos8-18 ~]# mysql
mysql> change master to
    -> master_host='192.168.108.8',
    -> master_user='repluser',
    -> master_password='ddq.com',
    -> master_port=3306,
    -> master_log_file='centos8-8-bin.000005',
    -> master_log_pos=156,
    -> master_connect_retry=10;
Query OK, 0 rows affected, 2 warnings (0.02 sec)

mysql> start slave;
Query OK, 0 rows affected (0.02 sec)

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.108.8
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 10
              Master_Log_File: centos8-8-bin.000005
          Read_Master_Log_Pos: 691
               Relay_Log_File: centos8-18-relay-bin.000002
                Relay_Log_Pos: 863
        Relay_Master_Log_File: centos8-8-bin.000005
             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: 691
              Relay_Log_Space: 1077
              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: 8
                  Master_UUID: 5b058811-f8dd-11eb-994d-52540099d568
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           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:
                Auto_Position: 0
         Replicate_Rewrite_DB:
                 Channel_Name:
           Master_TLS_Version:
       Master_public_key_path:
        Get_master_public_key: 0
            Network_Namespace:
1 row in set (0.00 sec)

  • 到这一步已完成主从复制

1.3 实现主主复制

#在第一个主节点上实现
[root@centos8-8 ~]# vi /etc/my.cnf.d/mysql-server.cnf
[mysqld]
log_bin
server_id=8
auto_increment_offset=1
auto_increment_increment=2

[root@centos8-8 ~]# systemctl restart mysqld.service
[root@centos8-8 ~]# mysql
mysql> show master logs;
+----------------------+-----------+-----------+
| Log_name             | File_size | Encrypted |
+----------------------+-----------+-----------+
| centos8-8-bin.000001 |       179 | No        |
| centos8-8-bin.000002 |       179 | No        |
| centos8-8-bin.000003 |       179 | No        |
| centos8-8-bin.000004 |       179 | No        |
| centos8-8-bin.000005 |       714 | No        |
| centos8-8-bin.000006 |       156 | No        |
+----------------------+-----------+-----------+
6 rows in set (0.00 sec)
#在第二个主节点上实现
[root@centos8-18 ~]# vi /etc/my.cnf.d/mysql-server.cnf
[mysqld]
server_id=18
log_bin
auto_increment_offset=2
auto_increment_increment=2

[root@centos8-18 ~]# systemctl restart mysqld.service
[root@centos8-18 ~]# mysql
mysql> show master logs;
+-----------------------+-----------+-----------+
| Log_name              | File_size | Encrypted |
+-----------------------+-----------+-----------+
| centos8-18-bin.000001 |       156 | No        |
+-----------------------+-----------+-----------+
1 row in set (0.00 sec)

#在第一个主节点上实现
mysql> change master to
    -> master_host='192.168.108.18',
    -> master_user='repluser',
    ->  master_password='ddq.com',
    -> master_port=3306,
    -> master_log_file='centos8-18-bin.000001',
    -> master_log_pos=156,
    -> master_connect_retry=10;
Query OK, 0 rows affected, 2 warnings (0.02 sec)

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.108.18
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 10
              Master_Log_File: centos8-18-bin.000001
          Read_Master_Log_Pos: 156
               Relay_Log_File: centos8-8-relay-bin.000002
                Relay_Log_Pos: 329
        Relay_Master_Log_File: centos8-18-bin.000001
             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: 156
              Relay_Log_Space: 542
              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: 18
                  Master_UUID: 5507bc09-f8d7-11eb-8f0f-5254005a4312
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           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:
                Auto_Position: 0
         Replicate_Rewrite_DB:
                 Channel_Name:
           Master_TLS_Version:
       Master_public_key_path:
        Get_master_public_key: 0
            Network_Namespace:
1 row in set (0.00 sec)

#在主节点一上创建数据库
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql> create database db1;
Query OK, 1 row affected (0.01 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| db1                |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> show master logs;
+----------------------+-----------+-----------+
| Log_name             | File_size | Encrypted |
+----------------------+-----------+-----------+
| centos8-8-bin.000001 |       179 | No        |
| centos8-8-bin.000002 |       179 | No        |
| centos8-8-bin.000003 |       179 | No        |
| centos8-8-bin.000004 |       179 | No        |
| centos8-8-bin.000005 |       714 | No        |
| centos8-8-bin.000006 |       343 | No        |
+----------------------+-----------+-----------+
6 rows in set (0.00 sec)

mysql>  show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.108.18
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 10
              Master_Log_File: centos8-18-bin.000001
          Read_Master_Log_Pos: 350
               Relay_Log_File: centos8-8-relay-bin.000002
                Relay_Log_Pos: 329
        Relay_Master_Log_File: centos8-18-bin.000001
             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: 350
              Relay_Log_Space: 542
              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: 18
                  Master_UUID: 5507bc09-f8d7-11eb-8f0f-5254005a4312
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           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:
                Auto_Position: 0
         Replicate_Rewrite_DB:
                 Channel_Name:
           Master_TLS_Version:
       Master_public_key_path:
        Get_master_public_key: 0
            Network_Namespace:
1 row in set (0.00 sec)

#在主节点二上查看
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| db1                |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.108.8
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 10
              Master_Log_File: centos8-8-bin.000006
          Read_Master_Log_Pos: 343
               Relay_Log_File: centos8-18-relay-bin.000006
                Relay_Log_Pos: 515
        Relay_Master_Log_File: centos8-8-bin.000006
             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: 343
              Relay_Log_Space: 729
              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: 8
                  Master_UUID: 5b058811-f8dd-11eb-994d-52540099d568
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           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:
                Auto_Position: 0
         Replicate_Rewrite_DB:
                 Channel_Name:
           Master_TLS_Version:
       Master_public_key_path:
        Get_master_public_key: 0
            Network_Namespace:
1 row in set (0.00 sec)

mysql> show master logs;
+-----------------------+-----------+-----------+
| Log_name              | File_size | Encrypted |
+-----------------------+-----------+-----------+
| centos8-18-bin.000001 |       350 | No        |
+-----------------------+-----------+-----------+
1 row in set (0.00 sec)

2、xtrabackup实现全量+增量+binlog恢复数据库

2.1 安装Percona XtraBackup

percona提供的mysql数据库备份工具,惟一开源的能够对innodb和xtradb数据库进行热备的工具
手册:https://www.percona.com/doc/percona-xtrabackup/LATEST/index.html
下载: https://www.percona.com/downloads/

[root@centos8-8 ~]# yum install https://repo.percona.com/yum/percona-release-latest.noarch.rpm
[root@centos8-8 ~]# percona-release enable-only tools release
[root@centos8-8 ~]# yum install percona-xtrabackup-80
[root@centos8-8 ~]# yum install qpress

2.2 xtrabackup 完全+增量备份还原

1、备份过程
#在原主机做完全备份到/backup
[root@centos8-8 ~]# mkdir /backup
[root@centos8-8 ~]# xtrabackup -uroot  --backup --target-dir=/backup/base
#第一次修改数据
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| db1                |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> create database db2;
Query OK, 1 row affected (0.01 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| db1                |
| db2                |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
6 rows in set (0.00 sec)

#做第一次增量备份
[root@centos8-8 ~]# xtrabackup -uroot --backup --target-dir=/backup/inc1 --incremental-basedir=/backup/base
[root@centos8-8 ~]# cat /backup/inc1/xtrabackup_info
uuid = d69c3dd7-fa4b-11eb-ab25-52540099d568
name =
tool_name = xtrabackup
tool_command = -uroot --backup --target-dir=/backup/inc1 --incremental-basedir=/backup/base
tool_version = 8.0.25-17
ibbackup_version = 8.0.25-17
server_version = 8.0.21
start_time = 2021-08-11 10:28:37
end_time = 2021-08-11 10:28:39
lock_time = 0
binlog_pos = filename 'centos8-8-bin.000009', position '156'
innodb_from_lsn = 17923714
innodb_to_lsn = 17925785
partial = N
incremental = Y
format = file
compressed = N
encrypted = N
[root@centos8-8 ~]# cat /backup/inc1/xtrabackup_checkpoints
backup_type = incremental
from_lsn = 17923714
to_lsn = 17925785
last_lsn = 17925785
flushed_lsn = 17925785

#第二次修改数据
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| db1                |
| db2                |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
6 rows in set (0.00 sec)

mysql> create database db3;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| db1                |
| db2                |
| db3                |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
7 rows in set (0.00 sec)

#做第二次增量备份
[root@centos8-8 ~]# xtrabackup -uroot --backup --target-dir=/backup/inc2 --incremental-basedir=/backup/inc1
[root@centos8-8 ~]# cat /backup/inc2/xtrabackup_info
uuid = 4225652a-fa4c-11eb-ab25-52540099d568
name =
tool_name = xtrabackup
tool_command = -uroot --backup --target-dir=/backup/inc2 --incremental-basedir=/backup/inc1
tool_version = 8.0.25-17
ibbackup_version = 8.0.25-17
server_version = 8.0.21
start_time = 2021-08-11 10:31:38
end_time = 2021-08-11 10:31:39
lock_time = 0
binlog_pos = filename 'centos8-8-bin.000010', position '156'
innodb_from_lsn = 17925785
innodb_to_lsn = 17927306
partial = N
incremental = Y
format = file
compressed = N
encrypted = N
[root@centos8-8 ~]# cat /backup/inc2/xtrabackup_checkpoints
backup_type = incremental
from_lsn = 17925785
to_lsn = 17927306
last_lsn = 17927306
flushed_lsn = 17927306

#复制备份文件到目标主机
[root@centos8-8 ~]# scp -r /backup/ 192.168.108.28:/

2、还原过程
#1)预准备完成备份,此选项--apply-log-only 阻止回滚未完成的事务
[root@centos8-28 ~]# xtrabackup --prepare --apply-log-only --target-dir=/backup/base
#2)合并第1次增量备份到完全备份
[root@centos8-28 ~]# xtrabackup --prepare --apply-log-only --target-dir=/backup/base --incremental-dir=/backup/inc1
#3)合并第2次增量备份到完全备份:最后一次还原不需要加选项--apply-log-only
[root@centos8-28 ~]# xtrabackup --prepare  --target-dir=/backup/base --incremental-dir=/backup/inc2
#4)复制到数据库目录,注意数据库目录必须为空,MySQL服务不能启动
[root@centos8-28 ~]# xtrabackup --copy-back --target-dir=/backup/base
#5)还原属性
[root@centos8-28 ~]# chown -R mysql:mysql /var/lib/mysql
#6)启动服务
[root@centos8-28 ~]# systemctl start mysqld.service


3、MyCAT实现MySQL读写分离

  • mysql-master 192.168.100.7
  • mysql-slave1 192.168.100.17
  • mysql-mycat 192.168.100.37
    mysql-master
[root@mysql-master ~]# yum -y install mariadb-server
[root@mysql-master ~]# vi /etc/my.cnf.d/server.cnf
[mysqld]
log_bin

[root@mysql-master ~]# systemctl start mariadb
[root@mysql-master ~]# mysql < hellodb_innodb.sql
[root@mysql-master ~]# mysql
MariaDB [(none)]> show master logs;
+--------------------+-----------+
| Log_name           | File_size |
+--------------------+-----------+
| mariadb-bin.000001 |       404 |
+--------------------+-----------+
1 row in set (0.00 sec)

MariaDB [(none)]> grant replication slave on *.* to 'repluser'@'192.168.100.%' identified by 'ddq.com';
MariaDB [mysql]> grant all on *.* to 'root'@'192.168.100.%' identified by 'ddq.com';
MariaDB [mysql]> flush privileges;

mysql-slave1

[root@mysql-slave1 ~]# echo 192.168.100.7 masterhost >> /etc/hosts
[root@mysql-slave1 ~]# yum -y install mariadb-server
[root@mysql-slave1 ~]# vim /etc/my.cnf.d/server.cnf
[mysqld]
server-id=2
log-bin
read_only=ON
relay_log=relay-log
relay_log_index=relay-log.index

[root@mysql-master ~]# systemctl start mariadb
[root@mysql-master ~]# mysql
MariaDB [(none)]> change master to master_host='masterhost',
    -> master_user='repluser',
    -> master_password='ddq.com',
    -> master_log_file='mariadb-bin.000001',
    -> master_log_pose=563;
Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> show slave status \G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: masterhost
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mariadb-bin.000001
          Read_Master_Log_Pos: 563
               Relay_Log_File: relay-log.000004
                Relay_Log_Pos: 531
        Relay_Master_Log_File: mariadb-bin.000001
             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: 563
              Relay_Log_Space: 819
              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
1 row in set (0.00 sec)

mysql-mycat
下载安装JDK

[root@mysql-mycat ~]# yum -y install java
[root@mysql-mycat ~]# java -version
openjdk version "1.8.0_302"
OpenJDK Runtime Environment (build 1.8.0_302-b08)
OpenJDK 64-Bit Server VM (build 25.302-b08, mixed mode)

下载安装mycat
Mycat 官网:http://www.mycat.org.cn/

[root@mysql-mycat ~]# wget http://dl.mycat.org.cn/1.6.7.6/20210730131311/Mycat-server-1.6.7.6-release-20210730131311-linux.tar.gz
[root@mysql-mycat ~]# tar zxf Mycat-server-1.6.7.6-release-20210730131311-linux.tar.gz
[root@mysql-mycat ~]# mkdir /apps
[root@mysql-mycat ~]# mv mycat/ /apps/
[root@mysql-mycat ~]# echo 'PATH=/apps/mycat/bin:$PATH' > /etc/profile.d/mycat.sh
[root@mysql-mycat ~]# source /etc/profile.d/mycat.sh
[root@mysql-mycat conf]# vi server.xml
......
                        <property name="serverPort">3306</property>
                        <property name="managerPort">9066</property>
                        <property name="idleTimeout">300000</property>
                        <property name="authTimeout">15000</property>
                        <property name="bindIp">0.0.0.0</property>
......
        <user name="root" defaultAccount="true">
                <property name="password">ddq.com</property>
                <property name="schemas">TESTDB</property>
                <property name="defaultSchema">TESTDB</property>
......

[root@mysql-mycat conf]# vi schema.xml
#修改为以下内容,balance改为1,表示读写分离
<?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="dn1"></schema>
        <dataNode name="dn1" dataHost="localhost1" database="hellodb" />
        <dataHost name="localhost1" maxCon="1000" minCon="10" balance="1"
                          writeType="0" dbType="mysql" dbDriver="jdbc" switchType="1"  slaveThreshold="100">
                <heartbeat>select user()</heartbeat>
                <writeHost host="hostM1" url="jdbc:mysql://192.168.100.7:3306" user="root" password="ddq.com">
                <readHost host="hostM2" url="jdbc:mysql://192.168.100.17:3306" user="root" password="ddq.com" />
                </writeHost>
        </dataHost>
</mycat:schema>

[root@mysql-mycat conf]# mycat start

在client上测试

[root@client ~]# mysql -uroot -pddq.com -h192.168.100.37 -P3306
MySQL [(none)]> use TESTDB;
Database changed
MySQL [TESTDB]> show tables;
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes           |
| coc               |
| courses           |
| scores            |
| students          |
| teachers          |
| toc               |
+-------------------+
7 rows in set (0.00 sec)

MySQL [TESTDB]> create table t1(id int);
Query OK, 0 rows affected (0.01 sec)
 OK!

MySQL [TESTDB]> select @@hostname;
+--------------+
| @@hostname   |
+--------------+
| mysql-slave1 |
+--------------+
1 row in set (0.01 sec)

MySQL [TESTDB]> show tables;
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes           |
| coc               |
| courses           |
| scores            |
| students          |
| t1                |
| teachers          |
| toc               |
+-------------------+
8 rows in set (0.00 sec)

停止从节点,MyCAT自动调度读请求至主节点

[root@mysql-slave1 ~]# systemctl stop mariadb
[root@client ~]# mysql -uroot -pddq.com -h192.168.100.37 -P3306
MySQL [(none)]> select @@hostname;
+--------------+
| @@hostname   |
+--------------+
| mysql-master |
+--------------+
1 row in set (0.09 sec)

MySQL [(none)]> use TESTDB;
MySQL [TESTDB]> show tables;
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes           |
| coc               |
| courses           |
| scores            |
| students          |
| t1                |
| teachers          |
| toc               |
+-------------------+
8 rows in set (0.00 sec)


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

推荐阅读更多精彩内容