MySQL主从复制以及读写分离

1、主从复制及主主复制的实现

1.1 主从复制
1.1.1、主节点设置
#下载mysql5.7
[root@localhost ~]# systemctl stop firewall
[root@localhost ~]# wget https://repo.mysql.com//mysql57-community-release-el7-11.noarch.rpm
#安装mysql rpm源
[root@localhost ~]# rpm -ivh mysql57-community-release-el7-11.noarch.rpm
#安装mysql
[root@localhost ~]# yum -y install mysql-server
[root@localhost ~]# systemctl start mysqld
[root@localhost ~]# systemctl enable mysqld
#查看mysql初始密码
[root@localhost ~]# grep 'root@localhost:' /var/log/mysqld.log
2020-12-01T06:56:57.727527Z 1 [Note] A temporary password is generated for root@localhost: QmtSPzqXW0*m
#使用初始密码登录
[root@localhost ~]# mysql -hlocalhost -uroot -p'QmtSPzqXW0*m'
...
Server version: 5.7.32
...
#修改密码
mysql> alter user root@"localhost" identified by "Jhd2020@";
#使用新密码登录
mysql> grant replication slave on *.* to repluser@'192.168.2.%'
identified by 'Jhd2020@';
#导入数据库文件
[root@localhost ~]# mysql -hlocalhost -uroot -p'Jhd2020@' < hellodb_innodb.sql
#修改配置
[root@localhost ~]# vim /etc/my.cnf
[mysqld]
server-id=8
log-bin
#创建有复制权限的用户账号
mysql> grant replication slave on *.* to repluser@'192.168.2.%' identified by 'Jhd2020@';

#查看二进制文件和位置
mysql> show master logs;
+----------------------+-----------+
| Log_name             | File_size |
+----------------------+-----------+
| localhost-bin.000001 |       759 |
+----------------------+-----------+
1 row in set (0.00 sec)



1.1.2 从节点
#下载mysql5.7
[root@localhost ~]# systemctl stop firewall
[root@localhost ~]# wget https://repo.mysql.com//mysql57-community-release-el7-11.noarch.rpm
#安装mysql rpm源
[root@localhost ~]# rpm -ivh mysql57-community-release-el7-11.noarch.rpm
#安装mysql
[root@localhost ~]# yum -y install mysql-server
[root@localhost ~]# systemctl start mysqld
[root@localhost ~]# systemctl enable mysqld
#查看mysql初始密码
[root@localhost ~]# grep 'root@localhost:' /var/log/mysqld.log
2020-12-01T06:56:57.727527Z 1 [Note] A temporary password is generated for root@localhost: QmtSPzqXW0*m
#使用初始密码登录
[root@localhost ~]# mysql -hlocalhost -uroot -p'QmtSPzqXW0*m'
...
Server version: 5.7.32
...
#修改密码
mysql> alter user root@"localhost" identified by "Jhd2020@";
#使用新密码登录
mysql> grant replication slave on *.* to repluser@'192.168.2.%'
identified by 'Jhd2020@';
#导入数据库文件
[root@localhost ~]# mysql -hlocalhost -uroot -p'Jhd2020@' < hellodb_innodb.sql
#修改配置
[root@localhost ~]# vim /etc/my.cnf
[mysqld]
server-id=18
[root@localhost ~]# systemctl restart mysqld
#使用有复制权限的用户账号连接至主服务器,并启动复制线程
mysql> CHANGE MASTER TO MASTER_HOST='192.168.2.157',MASTER_USER='repluser',MASTER_PASSWORD='Jhd2020@', MASTER_PORT=3306,MASTER_LOG_FILE='localhost-bin.000001', MASTER_LOG_POS=759;

mysql> start slave;

mysql> show slave status\G

1.2 主主复制
1.2.1、第一个master点安装配置
#下载mysql5.7
[root@localhost ~]# systemctl stop firewall
[root@localhost ~]# wget https://repo.mysql.com//mysql57-community-release-el7-11.noarch.rpm
#安装mysql rpm源
[root@localhost ~]# rpm -ivh mysql57-community-release-el7-11.noarch.rpm
#安装mysql
[root@localhost ~]# yum -y install mysql-server
[root@localhost ~]# systemctl start mysqld
[root@localhost ~]# systemctl enable mysqld
#查看mysql初始密码
[root@localhost ~]# grep 'root@localhost:' /var/log/mysqld.log
2020-12-01T06:56:57.727527Z 1 [Note] A temporary password is generated for root@localhost: QmtSPzqXW0*m
#使用初始密码登录
[root@localhost ~]# mysql -hlocalhost -uroot -p'QmtSPzqXW0*m'
...
Server version: 5.7.32
...
#修改密码
mysql> alter user root@"localhost" identified by "Jhd2020@";
#使用新密码登录
mysql> grant replication slave on *.* to repluser@'192.168.2.%'
identified by 'Jhd2020@';
#导入数据库文件
[root@localhost ~]# mysql -hlocalhost -uroot -p'Jhd2020@' < hellodb_innodb.sql
#修改配置
[root@localhost ~]# vim /etc/my.cnf
[mysqld]
server-id=8
log-bin
auto_increment_offset=1 #开始点
auto_increment_increment=2 #增长幅度

#重启
[root@localhost ~]# systemctl restart mysqld
#查看节点
[root@localhost ~]# mysql -hlocalhost -uroot -p
mysql> show master logs;
+----------------------+-----------+
| Log_name             | File_size |
+----------------------+-----------+
| localhost-bin.000001 |       456 |
+----------------------+-----------+
1 row in set (0.00 sec)
#创建可复制账户
mysql> grant replication slave on *.* to repluser@'192.168.2.%' identified by 'Jhd2020@';
1.2.2、在第二个master节点上配置
[root@localhost ~]# vim /etc/my.cnf
[mysqld]
server-id=18
log-bin
auto_increment_offset=2 #开始点
auto_increment_increment=2 #增长幅度

[root@localhost ~]# systemctl restart mysqld
[root@localhost ~]# mysql -hlocalhost -uroot -p
#创建可复制账户
mysql> grant replication slave on *.* to repluser@'192.168.2.%' identified by 'Jhd2020@';
mysql> CHANGE MASTER TO
MASTER_HOST='192.168.2.157',
MASTER_USER='repluser',
MASTER_PASSWORD='Jhd2020@',
MASTER_PORT=3306,
MASTER_LOG_FILE='localhost-bin.000001',
MASTER_LOG_POS=456;
#启动节点
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
#查看二进制文件的位置
mysql> show master logs;
+----------------------+-----------+
| Log_name             | File_size |
+----------------------+-----------+
| localhost-bin.000001 |       456 |
+----------------------+-----------+
1 row in set (0.00 sec)

1.2.3、在第一个msater 节点上配置
[root@localhost ~]# mysql -hlocalhost -uroot -p
mysql> CHANGE MASTER TO
CHANGE MASTER TO
MASTER_HOST='192.168.2.158',
MASTER_USER='repluser',
MASTER_PASSWORD='Jhd2020@',
MASTER_PORT=3306,
MASTER_LOG_FILE='localhost-bin.000001',
MASTER_LOG_POS=456;
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

#在第一个master节点操作
mysql> create database db1;
Query OK, 1 row affected (0.00 sec)

mysql> 
mysql> use db1
Database changed
mysql> create table t1(id int auto_increment primary key,name char(10));
Query OK, 0 rows affected (0.01 sec)

mysql> insert t1 (name) values('user1');
Query OK, 1 row affected (0.00 sec)

mysql> insert t1 (name) values('user2');
Query OK, 1 row affected (0.00 sec)

#在第二个master节点进行查询
mysql> select * from t1;
+----+-------+
| id | name  |
+----+-------+
|  1 | user1 |
+----+-------+
1 row in set (0.00 sec)

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

2.1 下载安装xtrabackup
#下载xtrabackup的RPM包
[root@localhost ~]# wget https://downloads.percona.com/downloads/Percona-XtraBackup-2.4/Percona-XtraBackup-2.4.22/binary/redhat/7/x86_64/percona-xtrabackup-24-2.4.22-1.el7.x86_64.rpm
#安装
[root@localhost ~]# yum -y install percona-xtrabackup-24-2.4.22-1.el7.x86_64.rpm

2.2 安装mysql
#下载mysql5.7
[root@localhost ~]# systemctl stop firewall
[root@localhost ~]# wget https://repo.mysql.com//mysql57-community-release-el7-11.noarch.rpm
#安装mysql rpm源
[root@localhost ~]# rpm -ivh mysql57-community-release-el7-11.noarch.rpm
#安装mysql
[root@localhost ~]# yum -y install mysql-server
[root@localhost ~]# systemctl start mysqld
[root@localhost ~]# systemctl enable mysqld
#查看mysql初始密码
[root@localhost ~]# grep 'root@localhost:' /var/log/mysqld.log
2020-12-01T06:56:57.727527Z 1 [Note] A temporary password is generated for root@localhost: QmtSPzqXW0*m
#使用初始密码登录
[root@localhost ~]# mysql -hlocalhost -uroot -p'QmtSPzqXW0*m'
...
Server version: 5.7.32
...
#修改密码
mysql> alter user root@"localhost" identified by "Jhd2020@";
#使用新密码登录
2.3 导入测试数据,实现增量备份
#导入数据库
[root@localhost ~]# mysql -hlocalhost -uroot -p'Jhd2020@' < hellodb_innodb.sql
#创建数据库
[root@localhost ~]# mysql -hlocalhost -uroot -p
mysql> create database db1;
Query OK, 1 row affected (0.00 sec)

mysql> 
mysql> use db1
Database changed
mysql> create table t1(id int auto_increment primary key,name char(10));
Query OK, 0 rows affected (0.01 sec)

mysql> insert t1 (name) values('user1');
Query OK, 1 row affected (0.00 sec)

mysql> insert t1 (name) values('user2');
Query OK, 1 row affected (0.00 sec)

# 1、完全备份
[root@localhost ~]# mkdir /backup/
[root@localhost ~]# xtrabackup -uroot -p'Jhd2020@' --backup --target-dir=/backup/base

# 2、第一次修改数据
mysql> insert t1 (name) values('user3');
Query OK, 1 row affected (0.00 sec)

mysql> insert t1 (name) values('user4');
Query OK, 1 row affected (0.01 sec)
# 3、第一次增量备份
[root@localhost ~]# xtrabackup -uroot -p'Jhd2020@' --backup --target-dir=/backup/inc1 --incremental-basedir=/backup/base

# 4、第二次修改数据
mysql> insert t1 (name) values('user5');
Query OK, 1 row affected (0.00 sec)

mysql> insert t1 (name) values('user6');
Query OK, 1 row affected (0.00 sec)

# 5、第二次增量备份
[root@localhost ~]# xtrabackup -uroot -p'Jhd2020@' --backup --target-dir=/backup/inc2 --incremental-basedir=/backup/inc1

# 6、src传输到目标主机
[root@localhost ~]# scp -r /backup/* 192.168.2.158:/backup/
#备份过程生成三个备份目录
/backup/{base,inc1,inc2}

# 2还原过程
1)预准备完成备份,此选项--apply-log-only 阻止回滚未完成的事务
[root@localhost ~]# xtrabackup --prepare --apply-log-only --target-dir=/backup/base
2)合并第1次增量备份到完全备份,
[root@localhost ~]# xtrabackup --prepare --apply-log-only --target-dir=/backup/base --incremental-dir=/backup/inc1
3)合并第2次增量备份到完全备份:最后一次还原不需要加选项--apply-log-only
[root@localhost ~]# xtrabackup --prepare --target-dir=/backup/base --incremental-dir=/backup/inc2
4)复制到数据库目录,注意数据库目录必须为空,MySQL服务不能启动
[root@localhost ~]# xtrabackup --copy-back --target-dir=/backup/base
5)还原属性:
[root@localhost ~]# chown -R mysql:mysql /var/lib/mysql
6)启动服务:
[root@localhost ~]# systemctl start mysqld
2.4、完全备份
#下载xtrabackup的RPM包
[root@localhost ~]# wget https://downloads.percona.com/downloads/Percona-XtraBackup-2.4/Percona-XtraBackup-2.4.22/binary/redhat/7/x86_64/percona-xtrabackup-24-2.4.22-1.el7.x86_64.rpm
#安装
[root@localhost ~]# yum -y install percona-xtrabackup-24-2.4.22-1.el7.x86_64.rpm

#下载mysql5.7
[root@localhost ~]# systemctl stop firewall
[root@localhost ~]# wget https://repo.mysql.com//mysql57-community-release-el7-11.noarch.rpm
#安装mysql rpm源
[root@localhost ~]# rpm -ivh mysql57-community-release-el7-11.noarch.rpm
#安装mysql
[root@localhost ~]# yum -y install mysql-server
[root@localhost ~]# systemctl start mysqld
[root@localhost ~]# systemctl enable mysqld
#查看mysql初始密码
[root@localhost ~]# grep 'root@localhost:' /var/log/mysqld.log
2020-12-01T06:56:57.727527Z 1 [Note] A temporary password is generated for root@localhost: QmtSPzqXW0*m
#使用初始密码登录
[root@localhost ~]# mysql -hlocalhost -uroot -p'QmtSPzqXW0*m'
...
Server version: 5.7.32
...
#修改密码
mysql> alter user root@"localhost" identified by "Jhd2020@";

# 在原主机上进行备份到/backup
[root@localhost ~]# xtrabackup -uroot -p'Jhd2020@' --backup --target-dir=/backup/
#复制到目标主机
[root@localhost ~]# scp -r /backup/* 192.168.2.158:/backup/

# 在目标主机上还原
[root@localhost ~]# xtrabackup --prepare --target-dir=/backup/

[root@localhost ~]# xtrabackup --copy-back --target-dir=/backup/
[root@localhost ~]# chown -R mysql:mysql /var/lib/mysql

3、MyCAT实现MySQL读写分离

准备三台服务器

mysql-master 10.0.0.7
mysql-slave 10.0.0.17
mycat-server 10.0.0.27 内存建议2G以上

关闭防火墙、SELinux,并进行时间同步
systemctl stop firewalld
setenforce 0
nntpdate ntp.aliyun.com
3.1、创建mysql 主从数据库
#下载mysql5.7
[root@centos7 ~]# systemctl stop firewall
[root@centos7 ~]# wget https://repo.mysql.com//mysql57-community-release-el7-11.noarch.rpm
#安装mysql rpm源
[root@centos7 ~]# rpm -ivh mysql57-community-release-el7-11.noarch.rpm
#安装mysql
[root@centos7 ~]# yum -y install mysql-server
[root@centos7 ~]# systemctl start mysqld
[root@centos7 ~]# systemctl enable mysqld
#查看mysql初始密码
[root@centos7 ~]# grep 'root@localhost:' /var/log/mysqld.log
2020-12-01T06:56:57.727527Z 1 [Note] A temporary password is generated for root@localhost: QmtSPzqXW0*m
#使用初始密码登录
[root@centos7 ~]# mysql -hlocalhost -uroot -p'QmtSPzqXW0*m'
...
Server version: 5.7.32
...
#修改密码
mysql> alter user root@"localhost" identified by "Jhd2020@";
#使用新密码登录
[root@centos7 ~]# mysql -uroot -p
3.2.1、修改master和slave上的配置文件
#master上的my.cnf
[root@centos7 ~]# vim /etc/my.cnf
[mysqld]
server-id = 1
log-bin

#slave上的my.cnf
[root@centos17 ~]# vim /etc/my.cnf
[mysqld]
server-id = 2

3.2.2、master上创建复制用户
[root@centos7 ~]# mysql -uroot -p
mysql> grant replication slave on *.* to repluser@'10.0.0.%' identified by 'Jhd2020@';
mysql> show master status;
+--------------------+----------+--------------+------------------+-------------------+
| File               | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+--------------------+----------+--------------+------------------+-------------------+
| centos7-bin.000002 |      306 |              |                  |                   |
+--------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)


mysql> CHANGE MASTER TO
MASTER_HOST='10.0.0.7',
MASTER_USER='repluser',
MASTER_PASSWORD='Jhd2020@',
MASTER_LOG_FILE='centos7-bin.000002',
MASTER_LOG_POS=306;
3.2.3、slave上执行
[root@centos17 ~]# mysql -uroot -p
mysql> CHANGE MASTER TO
MASTER_HOST='10.0.0.7',
MASTER_USER='repluser',
MASTER_PASSWORD='Jhd2020@',
MASTER_LOG_FILE='centos7-bin.000002',
MASTER_LOG_POS=306;

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: 10.0.0.7
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: centos7-bin.000002
          Read_Master_Log_Pos: 306
               Relay_Log_File: centos17-relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: centos7-bin.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

3.3、在10.0.0.27的服务器上安装mycat 并启动
[root@centos27 ~]# yum -y install mysql
[root@centos27 ~]# yum -y install java
#确认安装成功
[root@centos27 ~]# java -version
openjdk version "1.8.0_282"
OpenJDK Runtime Environment (build 1.8.0_282-b08)
OpenJDK 64-Bit Server VM (build 25.282-b08, mixed mode)
#下载并安装
[root@centos27 ~]# 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@centos27 ~]# mkdir /app
[root@centos27 ~]# tar xvf Mycat-server-1.6.7.4-release-20200105164103-linux.tar.gz -C /app/

[root@centos27 ~]# echo 'PATH=/app/mycat/bin:$PATH' > /etc/profile.d/mycat.sh

[root@centos27 ~]# source /etc/profile.d/mycat.sh 

[root@centos27 ~]# ss -ntl
State      Recv-Q Send-Q     Local Address:Port                    Peer Address:Port              
LISTEN     0      100            127.0.0.1:25                                 *:*                  
LISTEN     0      128                    *:111                                *:*                  
LISTEN     0      128                    *:22                                 *:*                  
LISTEN     0      100                [::1]:25                              [::]:*                  
LISTEN     0      128                 [::]:111                             [::]:*                  
LISTEN     0      128                 [::]:22                              [::]:* 


[root@centos27 ~]# mycat start
Starting Mycat-server...
[root@centos27 ~]# ss -ntlp
State      Recv-Q Send-Q     Local Address:Port                    Peer Address:Port              
LISTEN     0      100            127.0.0.1:25                                 *:*                   users:(("master",pid=2626,fd=13))
LISTEN     0      1              127.0.0.1:32000                              *:*                   users:(("java",pid=2720,fd=4))
LISTEN     0      128                    *:111                                *:*                   users:(("rpcbind",pid=708,fd=4),("systemd",pid=1,fd=54))
LISTEN     0      128                    *:22                                 *:*                   users:(("sshd",pid=1127,fd=3))
LISTEN     0      100                [::1]:25                              [::]:*                   users:(("master",pid=2626,fd=14))
LISTEN     0      50                  [::]:1984                            [::]:*                   users:(("java",pid=2720,fd=65))
LISTEN     0      100                 [::]:8066                            [::]:*                   users:(("java",pid=2720,fd=86))
LISTEN     0      100                 [::]:9066                            [::]:*                   users:(("java",pid=2720,fd=82))
LISTEN     0      50                  [::]:44523                           [::]:*                   users:(("java",pid=2720,fd=66))
LISTEN     0      128                 [::]:111                             [::]:*                   users:(("rpcbind",pid=708,fd=6),("systemd",pid=1,fd=58))
LISTEN     0      50                  [::]:39636                           [::]:*                   users:(("java",pid=2720,fd=64))
LISTEN     0      128                 [::]:22                              [::]:*                   users:(("sshd",pid=1127,fd=4))
#查看日志,确定成功,可能需要等一会儿才能看到成功的提示
[root@centos27 ~]# tail -f /app/mycat/logs/wrapper.log 
STATUS | wrapper  | 2021/04/18 16:38:10 | --> Wrapper Started as Daemon
STATUS | wrapper  | 2021/04/18 16:38:10 | Launching a JVM...
INFO   | jvm 1    | 2021/04/18 16:38:10 | Wrapper (Version 3.2.3) http://wrapper.tanukisoftware.org
INFO   | jvm 1    | 2021/04/18 16:38:10 |   Copyright 1999-2006 Tanuki Software, Inc.  All Rights Reserved.
INFO   | jvm 1    | 2021/04/18 16:38:10 | 
INFO   | jvm 1    | 2021/04/18 16:38:12 | MyCAT Server startup successfully. see logs in logs/mycat.log

#用默认密码123456来连接mycat
[root@centos27 ~]# mysql -uroot -p'123456' -h10.0.0.27 -P8066
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.29-mycat-1.6.7.4-release-20200105164103 MyCat Server (OpenCloudDB)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

mysql> show databases;
+----------+
| DATABASE |
+----------+
| TESTDB   |
+----------+
1 row in set (0.00 sec)


mysql> use TESTDB
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+------------------+
| Tables in TESTDB |
+------------------+
| address          |
| travelrecord     |
+------------------+
2 rows in set (0.00 sec)

mysql> select * from travelrecord;
ERROR 1105 (HY000): backend connect: java.lang.IllegalArgumentException: Invalid DataSource:0
3.4、在mycat服务器上修改server.xml文件配置mycat的连接信息
[root@centos27 ~]# vim /app/mycat/conf/server.xml 
...省略...
        
                Jhd2020@
                TESTDB
                TESTDB
        

3.5、修改schema.xml实现读写分离策略
[root@centos27 ~]# vim /app/mycat/conf/schema.xml 






select user()






#重启mycat
[root@centos27 ~]# mycat restart
3.6、在后端主服务器创建用户并对mycat授权
[root@centos7 ~]# mysql -uroot -p < mycat.sql 
[root@centos7 ~]# mysql -uroot -p 
mysql>GRANT ALL ON *.* TO 'root'@'10.0.0.%' IDENTIFIED BY 'Jhd2020@' WITH GRANT OPTION;
mysql> flush privileges;
3.7、在mycat服务器上连接并测试
[root@centos27 ~]# mysql -uroot -p'Jhd2020@' -h127.0.0.1 -P8066 -DTESTDB
mysql> show databases;
+----------+
| DATABASE |
+----------+
| TESTDB   |        #只能看到一个虚拟数据库
+----------+
1 row in set (0.00 sec)

mysql> use TESTDB
Database changed
mysql> show tables;
+-----------------+
| Tables_in_mycat |
+-----------------+
| classes         |
| coc             |
| courses         |
| scores          |
| students        |
| teachers        |
| toc             |
+-----------------+
7 rows in set (0.01 sec)
mysql> show databases;
+----------+
| DATABASE |
+----------+
| TESTDB   |
+----------+
1 row in set (0.00 sec)

mysql> use TESTDB
Database changed
mysql> show tables;
+-----------------+
| Tables_in_mycat |
+-----------------+
| classes         |
| coc             |
| courses         |
| scores          |
| students        |
| teachers        |
| toc             |
+-----------------+
7 rows in set (0.01 sec)
mysql> select @@server_id;
+-------------+
| @@server_id |
+-------------+
|           2 |
+-------------+
1 row in set (0.00 sec)

mysql> select @@hostname;
+------------+
| @@hostname |
+------------+
| centos17    |
+------------+
1 row in set (0.01 sec)

3.8、通过通用日志确认实现读写分离
mysql> show variables like 'general_log';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| general_log   | OFF   |
+---------------+-------+
1 row in set (0.00 sec)

mysql> set global general_log=on;
Query OK, 0 rows affected (0.00 sec)

mysql> show variables like 'general_log_file';
+------------------+----------------------------+
| Variable_name    | Value                      |
+------------------+----------------------------+
| general_log_file | /var/lib/mysql/centos7.log |
+------------------+----------------------------+
1 row in set (0.00 sec)

在主和从服务器分别启用通用日志,查看读写分离

[root@centos7 ~]# vim /etc/my.cnf
[mysqld]
general_log=ON
[root@centos7 ~]# systemctl restart mysqld
[root@centos7 ~]# tail -f /var/lib/mysql/centos7.log 


[root@centos17 ~]# vim /etc/my.cnf
[mysqld]
general_log=ON
[root@centos17 ~]# systemctl restart mysqld
[root@centos17 ~]# tail -f /var/lib/mysql/centos17.log 
3.9、停止从节点,MyCAT自动调度读请求至主节点
#主节点停止前
[root@centos27 ~]# mysql -uroot -p'Jhd2020@' -h127.0.0.1 -P8066 
mysql> select @@server_id;
+-------------+
| @@server_id |
+-------------+
|           2 |
+-------------+
1 row in set (0.00 sec)

mysql> select @@hostname;
+------------+
| @@hostname |
+------------+
| centos17   |
+------------+
1 row in set (0.00 sec)

#停止从节点后
[root@centos17 ~]# systemctl stop mysqld
mysql> select @@server_id;
+-------------+
| @@server_id |
+-------------+
|           1 |
+-------------+
1 row in set (0.00 sec)

mysql> select @@hostname;
+------------+
| @@hostname |
+------------+
| centos7    |
+------------+
1 row in set (0.00 sec)


你可能感兴趣的:(MySQL主从复制以及读写分离)