第十九周

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

主从复制
主节点配置
[root@zuoye1 ~]# vi /etc/my.cnf.d/server.cnf 
[mysqld]
server-id=1
log-bin
[root@zuoye1 ~]# mysql
MariaDB [mysql]> grant replication slave on *.* to 'repluser'@'10.0.0.%' identified by '123456';
MariaDB [(none)]> show master logs;
+--------------------+-----------+
| Log_name           | File_size |
+--------------------+-----------+
| mariadb-bin.000002 |       245 |
+--------------------+-----------+
1 row in set (0.00 sec)

从节点
[root@zuoye2 ~]# vi /etc/my.cnf.d/server.cnf 
[mysqld]
server-id=2
[root@zuoye2 ~]# systemctl start mariadb
[root@zuoye2 ~]# mysql
MariaDB [(none)]> change master to master_host='10.0.0.100',master_user='repluser',master_password='123456',master_port=3306,master_log_file='mariadb-bin.000002',master_log_pos=245;
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: 10.0.0.100
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mariadb-bin.000002
          Read_Master_Log_Pos: 245
               Relay_Log_File: mariadb-relay-bin.000002
                Relay_Log_Pos: 531
        Relay_Master_Log_File: mariadb-bin.000002
             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: 245
              Relay_Log_Space: 827
              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)

主主复制
第一个节点
[root@zuoye1 ~]# vi /etc/my.cnf.d/server.cnf
[mysqld]
server-id=1
log-bin
auto_increment_offset=1
auto_increment_increment=2
[root@zuoye1 ~]# systemctl start mariadb
[root@zuoye1 ~]# mysql
MariaDB [(none)]> show master logs;
+--------------------+-----------+
| Log_name           | File_size |
+--------------------+-----------+
| mariadb-bin.000002 |       264 |
| mariadb-bin.000003 |       245 |
+--------------------+-----------+
2 rows in set (0.00 sec)
MariaDB [(none)]> grant replication slave on *.* to repluser@'10.0.0.%' identified by '123456';
MariaDB [(none)]> change master to master_host='10.0.0.101',master_user='repluser',master_password='123456',master_port=3306,master_log_file='mariadb-bin.000001',master_log_pos=245;
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.0.0.101
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mariadb-bin.000001
          Read_Master_Log_Pos: 245
               Relay_Log_File: mariadb-relay-bin.000002
                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: 245
              Relay_Log_Space: 827
              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: 2
1 row in set (0.00 sec)



第二个节点
[root@zuoye2 ~]# vi /etc/my.cnf.d/server.cnf
[mysqld]
server-id=2
log-bin
auto_increment_offset=2         
auto_increment_increment=2
[root@zuoye2 ~]# systemctl start mariadb
[root@zuoye2 ~]# mysql
MariaDB [(none)]> change master to master_host='10.0.0.100',master_user='repluser',master_password='123456',master_port=3306,master_log_file='mariadb-bin.000003',master_log_pos=245;
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> show master logs;
+--------------------+-----------+
| Log_name           | File_size |
+--------------------+-----------+
| mariadb-bin.000001 |       245 |
+--------------------+-----------+
1 row in set (0.00 sec)
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.0.0.100
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mariadb-bin.000003
          Read_Master_Log_Pos: 401
               Relay_Log_File: mariadb-relay-bin.000002
                Relay_Log_Pos: 687
        Relay_Master_Log_File: mariadb-bin.000003
             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: 401
              Relay_Log_Space: 983
              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)

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

1.源主机完全备份到/backup
[root@zuoye1 ~]# xtrabackup -uroot -p123456 --backup --target-dir=/backup/
[root@zuoye1 ~]# scp -r /backup/* 10.0.0.102:/backup
2.目标主机还原
[root@zuoye3 ~]# xtrabackup --prepare --target-dir=/backup/
[root@zuoye3 ~]# chown -R mysql:mysql /var/lib/mysql
[root@zuoye3 ~]# systemctl start mariadb

3、MyCAT实现MySQL读写分离

1.修改master和slave的配置文件
[root@zuoye1 yum.repos.d]# vi /etc/my.cnf.d/server.cnf
[mysqld]
server-id=1
log-bin
[root@zuoye2 yum.repos.d]# vi /etc/my.cnf.d/server.cnf
[mysqld]
server-id=2
[root@zuoye1 ~]# systemctl restart mariadb
[root@zuoye2 ~]# systemctl restart mariadb
2.master建立复制用户
MariaDB [(none)]> grant replication slave on *.* to 'repluser'@'10.0.0.101' identified by '123456';
MariaDB [(none)]> show master status;
+--------------------+----------+--------------+------------------+
| File               | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+--------------------+----------+--------------+------------------+
| mariadb-bin.000003 |      400 |              |                  |
+--------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
3.slave上执行
MariaDB [(none)]> change master to master_host='10.0.0.100',master_user='repluser',master_password='123456',master_port=3306,master_log_file='mariadb-bin.000003',master_log_pos=400;
Query OK, 0 rows affected (0.03 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: 10.0.0.100
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mariadb-bin.000003
          Read_Master_Log_Pos: 400
               Relay_Log_File: mariadb-relay-bin.000002
                Relay_Log_Pos: 531
        Relay_Master_Log_File: mariadb-bin.000003
             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: 400
              Relay_Log_Space: 827
              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)

4.安装mycat
[root@zuoye3 ~]# yum install -y java mariadb
[root@zuoye3 ~]# 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@zuoye3 ~]# mkdir /app
[root@zuoye3 ~]# tar xvf Mycat-server-1.6.7.4-release-20200105164103-linux.tar.gz -C /app
[root@zuoye3 ~]# echo 'PATH=/app/mycat/bin:$PATH' > /etc/profile.d/mycat.sh
[root@zuoye3 ~]# source /etc/profile.d/mycat.sh
[root@zuoye3 ~]# mysql -uroot -p123456 -h 10.0.0.102 -P8066
5.修改连接信息
[root@zuoye3 ~]# vi /app/mycat/conf/server.xml 
   <user name="root" defaultAccount="true">
                <property name="password">123456</property>
                <property name="schemas">TESTDB</property>
                <property name="defaultSchema">TESTDB</property>
6.修改schema.xml实现读写分离
[root@zuoye3 ~]# vi /app/mycat/conf/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="dn1"></schema>
        <dataNode name="dn1" dataHost="localhost1" database="mycat" />
        <dataHost name="localhost1" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
        <heartbeat>select user()</heartbeat>
        <writeHost host="host1" url="10.0.0.100:3306" user="root" password="123456">
        <readHost host="host2" url="10.0.0.101:3306" user="root" password="123456" />
        </writeHost>
        </dataHost>
</mycat:schema>

[root@zuoye3 ~]# mycat restart
Stopping Mycat-server...
Stopped Mycat-server.
Starting Mycat-server...
7.主服务器创建用户并对mycat授权
[root@zuoye1 yum.repos.d]# mysql -uroot -p
MariaDB [(none)]> create database mycat;
MariaDB [(none)]> grant all on *.* to 'root'@'10.0.0.102' identified by '123456' with grant option;
MariaDB [(none)]> flush privileges;
8.在mycat服务器上连接测试

MySQL [TESTDB]> show databases;
+----------+
| DATABASE |
+----------+
| TESTDB   |
+----------+
1 row in set (0.01 sec)
[root@zuoye3 ~]# mysql -uroot -p123456 -h127.0.0.1 -P8066 -DTESTDB
MySQL [TESTDB]> use TESTDB
Database changed
MySQL [TESTDB]> create table t1(id int);
Query OK, 0 rows affected (0.08 sec)

MySQL [TESTDB]> select @@server_id;
+-------------+
| @@server_id |
+-------------+
|           2 |
+-------------+
1 row in set (0.09 sec)

MySQL [TESTDB]> select @@hostname;
+------------+
| @@hostname |
+------------+
| zuoye2     |
+------------+
1 row in set (0.00 sec)

9.停止从节点,看是否调度
[root@zuoye2 ~]# systemctl stop mariadb
[root@zuoye3 ~]# mysql -uroot -p123456 -h10.0.0.102 -P8066 -DTESTDB
MySQL [TESTDB]>  select @@server_id;
+-------------+
| @@server_id |
+-------------+
|           1 |
+-------------+
1 row in set (0.00 sec)