1. 程式人生 > >mysql 主從複製優化、並行複製

mysql 主從複製優化、並行複製

實驗環境:

server2 為 server3 的 master

server3 為 server1 的 master

即server1、2、3的身份如下:

server2:master

server3:master / slave

server2:slave

由於先前做過其他資料庫實驗,環境是server2為master, server1和server3為slave

實驗操作:

server1、2、3配置檔案修改如下:

  • server2
    這裡寫圖片描述
  • server3
    這裡寫圖片描述
  • server1
    這裡寫圖片描述
    這裡寫圖片描述
  • server3
mysql> grant replication slave on *.* to 'tutu'
@'172.25.54.%' identified by '[email protected]#'; Query OK, 0 rows affected, 1 warning (0.05 sec)
  • server1
mysql> change master to master_host='172.25.54.3',master_user='tutu',master_password='[email protected]#',master_auto_position=1;
Query OK, 0 rows affected, 2 warnings (0.17 sec)

若出現Slave_SQL_Running: No
(原因:資料不同步,
在 server3 上 reset msater;
server1 上 shop slave; –> reset slave; –>start slave;)

  • server3
    這裡寫圖片描述
  • server1
    這裡寫圖片描述

測試

  • server2
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql> create database test;
Query OK, 1
row affected (0.13 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | test | +--------------------+ 5 rows in set (0.00 sec)
  • server3 和 server1
    這裡寫圖片描述
  • server2
mysql> use test;
Database changed
mysql> create table userlist (
    -> username varchar(15) not null,
    -> password varchar(25) not null);
Query OK, 0 rows affected (0.41 sec)

mysql> desc userlist;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| username | varchar(15) | NO   |     | NULL    |       |
| password | varchar(25) | NO   |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
2 rows in set (0.01 sec)

mysql> insert into userlist values ('user1','147');
Query OK, 1 row affected (0.09 sec)

mysql> insert into userlist values ('user2','258');
Query OK, 1 row affected (0.16 sec)

mysql> insert into userlist values ('user3','369');
Query OK, 1 row affected (0.08 sec)

mysql> select * from userlist;
+----------+----------+
| username | password |
+----------+----------+
| user1    | 147      |
| user2    | 258      |
| user3    | 369      |
+----------+----------+
3 rows in set (0.00 sec)

mysql> Bye
[root@server2 ~]#
  • server3 和 server1
    這裡寫圖片描述
    注:bin log索引檔案: /var/lib/mysql/mysql-bin.index
    檢視二進位制日誌:mysqlbinlog -vv –base64-output=DECODE-ROWS mysql-bin.000006 //-vv詳細顯示,–base64檢視加密的資料

並行複製

  • server3——單執行緒–>多執行緒工作
    mysql> show processlist;
    這裡寫圖片描述
[root@server3 ~]# vim /etc/my.cnf
[root@server3 ~]# cat /etc/my.cnf | tail -n 11
server-id=3
log-bin=mysql-bin
log-slave-updates

gtid_mode=ON
enforce-gtid-consistency=true
slave-parallel-type=LOGICAL_CLOCK
slave-parallel-workers=16
master_info_repository=TABLE
relay_log_info_repository=TABLE
relay_log_recovery=ON
[root@server3 ~]# /etc/init.d/mysqld restart
Stopping mysqld:                                           [  OK  ]
Starting mysqld:                                           [  OK  ]
[root@server3 ~]# mysql -p5820hhXM\!\@#
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 23
Server version: 5.7.17-log MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

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> show processlist;
+----+-------------+---------------+------+------------------+-------+---------------------------------------------------------------+------------------+
| Id | User        | Host          | db   | Command          | Time  | State                                                         | Info             |
+----+-------------+---------------+------+------------------+-------+---------------------------------------------------------------+------------------+
|  1 | system user |               | NULL | Connect          |    58 | Slave has read all relay log; waiting for more updates        | NULL             |
|  2 | system user |               | NULL | Connect          |    60 | Waiting for master to send event                              | NULL             |
|  3 | system user |               | NULL | Connect          | 83590 | Waiting for an event from Coordinator                         | NULL             |
|  4 | system user |               | NULL | Connect          |    60 | Waiting for an event from Coordinator                         | NULL             |
|  6 | system user |               | NULL | Connect          |    60 | Waiting for an event from Coordinator                         | NULL             |
|  7 | system user |               | NULL | Connect          |    60 | Waiting for an event from Coordinator                         | NULL             |
|  8 | system user |               | NULL | Connect          |    60 | Waiting for an event from Coordinator                         | NULL             |
|  9 | system user |               | NULL | Connect          |    60 | Waiting for an event from Coordinator                         | NULL             |
| 10 | system user |               | NULL | Connect          |    60 | Waiting for an event from Coordinator                         | NULL             |
| 11 | system user |               | NULL | Connect          |    60 | Waiting for an event from Coordinator                         | NULL             |
| 12 | system user |               | NULL | Connect          |    60 | Waiting for an event from Coordinator                         | NULL             |
| 13 | system user |               | NULL | Connect          |    60 | Waiting for an event from Coordinator                         | NULL             |
| 16 | system user |               | NULL | Connect          |    60 | Waiting for an event from Coordinator                         | NULL             |
| 17 | system user |               | NULL | Connect          |    60 | Waiting for an event from Coordinator                         | NULL             |
| 18 | system user |               | NULL | Connect          |    60 | Waiting for an event from Coordinator                         | NULL             |
| 19 | system user |               | NULL | Connect          |    60 | Waiting for an event from Coordinator                         | NULL             |
| 20 | system user |               | NULL | Connect          |    60 | Waiting for an event from Coordinator                         | NULL             |
| 21 | system user |               | NULL | Connect          |    60 | Waiting for an event from Coordinator                         | NULL             |
| 22 | tutu        | server1:48836 | NULL | Binlog Dump GTID |     5 | Master has sent all binlog to slave; waiting for more updates | NULL             |
| 23 | root        | localhost     | NULL | Query            |     0 | starting                                                      | show processlist |
+----+-------------+---------------+------+------------------+-------+---------------------------------------------------------------+------------------+
20 rows in set (0.00 sec)