1. 程式人生 > 實用技巧 >Mysql的主從複製

Mysql的主從複製

1、主從同步(主從複製的)的原理。

答:主從同步的核心是二進位制日誌檔案binary log,對資料庫所有的增加、修改、刪除操作都會在日誌表裡面記錄一下的。mysql主從複製是非同步的,序列化的,有延遲的,並不是實時的。

  第一步,master主節點將改變的資料記錄在本地的二進位制日誌中binary log,該過程稱為二進位制日誌事件。
  第二步,slave將master的binary log拷貝到自己的relay log(中繼日誌檔案)中。
  第三步,中繼日誌事件,將資料讀取到自己的資料庫之中。

2、mysql叢集的優點,如下所示:

  1)、負載均衡。
  2)、失敗遷移。

3、由於我的機器一臺是window10安裝的mysql,一臺是centos7安裝的mysql,所以它們的配置檔案分別是windows的配置檔案是my.ini,linux的配置檔案是my.cnf。

首先,我需要配置一下允許遠端連線我的window的mysql,如下所示:

 1 Enter password: ******
 2 Welcome to the MySQL monitor.  Commands end with ; or \g.
 3 Your MySQL connection id is 3
 4 Server version: 5.7.24-log MySQL Community Server (GPL)
 5 
 6 Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
 7 
 8
Oracle is a registered trademark of Oracle Corporation and/or its 9 affiliates. Other names may be trademarks of their respective 10 owners. 11 12 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. 13 14 mysql> use mysql; 15 Database changed 16 mysql> grant all privileges on *.* to '
root'@'%' identified by '123456' with grant option; 17 Query OK, 0 rows affected, 1 warning (0.03 sec) 18 19 mysql> flush privileges; 20 Query OK, 0 rows affected (0.03 sec) 21 22 mysql> select host,user from user; 23 +-----------+---------------+ 24 | host | user | 25 +-----------+---------------+ 26 | % | root | 27 | % | user1 | 28 | localhost | mysql.session | 29 | localhost | mysql.sys | 30 | localhost | root | 31 +-----------+---------------+ 32 5 rows in set (0.00 sec) 33 34 mysql>

然後使用linux的遠端連線一下window的mysql,可以連線即可,如果不可以連線,需要關閉window的防火牆。反之,開啟Linux的mysql可以遠端連線並關閉防火牆。

 1 [root@k8s-node3 ~]# mysql -uroot -h192.168.0.116 -p123456
 2 mysql: [Warning] Using a password on the command line interface can be insecure.
 3 Welcome to the MySQL monitor.  Commands end with ; or \g.
 4 Your MySQL connection id is 4
 5 Server version: 5.7.24-log MySQL Community Server (GPL)
 6 
 7 Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
 8 
 9 Oracle is a registered trademark of Oracle Corporation and/or its
10 affiliates. Other names may be trademarks of their respective
11 owners.
12 
13 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
14 
15 mysql> show databases;
16 +

4、由於我的mysql是預設安裝的,所以my.ini在這個路徑下面C:\ProgramData\MySQL\MySQL Server 5.7\my.ini,根據個人需求進行配置吧,開始配置,如下所示:

 1 [mysqld]
 2 # 主節點master增加唯一識別符號
 3 server-id=1
 4 # 開始配置二進位制日誌檔案
 5 log-bin="D:/program/mysql/mysql-bin"
 6 # 開始配置二進位制日誌錯誤檔案
 7 log-error="D:/program/mysql/mysql-error"
 8 # 主從同步的時候忽略的資料庫
 9 binlog-ignore-db=mysql
10 # 可選引數,指定主從同步的時候,同步那些資料庫
11 binlog-do-db=test

我上面的配置一開始配置錯了,導致mysql重啟起不來了。還有下面的配置和自己的配置衝突了,我這裡將預設的先註釋了,如下所示:

Windows中的資料庫授權那臺計算機中的資料庫是自己的從資料庫。

1 mysql> grant replication slave,reload,super on *.* to 'root'@'192.168.110.%' identified by 'root';
2 Query OK, 0 rows affected, 1 warning (0.00 sec)
3 
4 mysql>
5 mysql> flush privileges;
6 Query OK, 0 rows affected (0.00 sec)
7 
8 mysql>
9 mysql>

檢視主資料庫的狀態,每次在做主從同步前,需要觀察主機狀態的最新值,需要記住File、Position的值的。命令如下所示:

1 mysql> show master status;
2 +--------------------+----------+--------------+------------------+-------------------+
3 | File               | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
4 +--------------------+----------+--------------+------------------+-------------------+
5 | mysql-error.000001 |      154 | biehl        | mysql            |                   |
6 +--------------------+----------+--------------+------------------+-------------------+
7 1 row in set (0.00 sec)
8 
9 mysql>

如果my.ini配置正確的話,重啟window的mysql之後,會在自己的指定目錄D:\program\mysql生成下面三個檔案,mysql-bin.000001、mysql-error.err、mysql-bin.index。

5、開始在linux的mysql配置,如下所示:

1 [mysqld]
2 # 從節點slave增加唯一識別符號
3 server-id=2
4 # 配置日誌路徑
5 log-bin=mysql-bin
6 # 配置主從同步的資料庫名稱
7 replicate-do-db=test

Linux中的資料庫授權那臺計算機中的資料庫是自己的主資料庫,由於修改了my.ini所以這裡重啟一下Mysql的資料庫。

 1 [root@k8s-node3 ~]# systemctl restart mysqld.service 
 2 [root@k8s-node3 ~]# systemctl status mysqld.service 
 3 ● mysqld.service - MySQL Server
 4    Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
 5    Active: active (running) since Fri 2020-07-31 12:21:36 CST; 7s ago
 6      Docs: man:mysqld(8)
 7            http://dev.mysql.com/doc/refman/en/using-systemd.html
 8   Process: 93026 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
 9   Process: 92998 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
10  Main PID: 93028 (mysqld)
11     Tasks: 27
12    Memory: 246.4M
13    CGroup: /system.slice/mysqld.service
14            └─93028 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
15 
16 Jul 31 12:21:28 k8s-node3 systemd[1]: Starting MySQL Server...
17 Jul 31 12:21:36 k8s-node3 systemd[1]: Started MySQL Server.
18 [root@k8s-node3 ~]# 

由於沒有重啟Mysql的資料庫之前報錯了,自己重啟一下然後執行命令即可。

 1 mysql> change master to master_host='192.168.0.116',master_user='root',master_password='123456',master_port=3306,master_log_file='mysql-error.000001',master_log_pos=154 ;
 2 ERROR 1794 (HY000): Slave is not configured or failed to initialize properly. You must at least set --server-id to enable either a master or a slave. Additional error messages can be found in the MySQL error log.
 3 mysql> exit
 4 Bye
 5 [root@k8s-node3 ~]# mysql -uroot -h127.0.0.1 -p123456
 6 mysql: [Warning] Using a password on the command line interface can be insecure.
 7 Welcome to the MySQL monitor.  Commands end with ; or \g.
 8 Your MySQL connection id is 2
 9 Server version: 5.7.30-log MySQL Community Server (GPL)
10 
11 Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
12 
13 Oracle is a registered trademark of Oracle Corporation and/or its
14 affiliates. Other names may be trademarks of their respective
15 owners.
16 
17 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
18 
19 mysql> change master to master_host='192.168.0.116',master_user='root',master_password='123456',master_port=3306,master_log_file='mysql-error.000001',master_log_pos=154 ;
20 Query OK, 0 rows affected, 1 warning (0.01 sec)
21 
22 mysql> 

6、開啟主從同步,這裡在linux執行即可,如下所示:

1 mysql> start slave;
2 Query OK, 0 rows affected (0.01 sec)
3 
4 mysql> 

然後檢驗是否真的執行了,檢查從節點的工作狀態,如下所示:

 1 mysql> show slave status \G
 2 *************************** 1. row ***************************
 3                Slave_IO_State: 
 4                   Master_Host: 192.168.0.116
 5                   Master_User: root
 6                   Master_Port: 3306
 7                 Connect_Retry: 60
 8               Master_Log_File: mysql-error.000001
 9           Read_Master_Log_Pos: 154
10                Relay_Log_File: k8s-node3-relay-bin.000001
11                 Relay_Log_Pos: 4
12         Relay_Master_Log_File: mysql-error.000001
13              Slave_IO_Running: No
14             Slave_SQL_Running: Yes
15               Replicate_Do_DB: biehl
16           Replicate_Ignore_DB: 
17            Replicate_Do_Table: 
18        Replicate_Ignore_Table: 
19       Replicate_Wild_Do_Table: 
20   Replicate_Wild_Ignore_Table: 
21                    Last_Errno: 0
22                    Last_Error: 
23                  Skip_Counter: 0
24           Exec_Master_Log_Pos: 154
25               Relay_Log_Space: 154
26               Until_Condition: None
27                Until_Log_File: 
28                 Until_Log_Pos: 0
29            Master_SSL_Allowed: No
30            Master_SSL_CA_File: 
31            Master_SSL_CA_Path: 
32               Master_SSL_Cert: 
33             Master_SSL_Cipher: 
34                Master_SSL_Key: 
35         Seconds_Behind_Master: NULL
36 Master_SSL_Verify_Server_Cert: No
37                 Last_IO_Errno: 1236
38                 Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'Could not find first log file name in binary log index file'
39                Last_SQL_Errno: 0
40                Last_SQL_Error: 
41   Replicate_Ignore_Server_Ids: 
42              Master_Server_Id: 1
43                   Master_UUID: 262f593f-746e-11e9-b769-d8c497e293c1
44              Master_Info_File: /var/lib/mysql/master.info
45                     SQL_Delay: 0
46           SQL_Remaining_Delay: NULL
47       Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
48            Master_Retry_Count: 86400
49                   Master_Bind: 
50       Last_IO_Error_Timestamp: 200731 12:25:35
51      Last_SQL_Error_Timestamp: 
52                Master_SSL_Crl: 
53            Master_SSL_Crlpath: 
54            Retrieved_Gtid_Set: 
55             Executed_Gtid_Set: 
56                 Auto_Position: 0
57          Replicate_Rewrite_DB: 
58                  Channel_Name: 
59            Master_TLS_Version: 
60 1 row in set (0.00 sec)
61 
62 mysql> 

主要觀察這兩個都是yes即可,Slave_IO_Running: No和 Slave_SQL_Running: Yes,這裡如果不都是yes,看Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'Could not find first log file name in binary log index file'報錯資訊。

 1 mysql> stop slave;
 2 Query OK, 0 rows affected (0.00 sec)
 3 
 4 mysql> reset slave;
 5 Query OK, 0 rows affected (0.00 sec)
 6 
 7 mysql> start slave;
 8 Query OK, 0 rows affected (0.01 sec)
 9 
10 mysql> show slave status \G
11 *************************** 1. row ***************************
12                Slave_IO_State: Waiting for master to send event
13                   Master_Host: 192.168.0.116
14                   Master_User: root
15                   Master_Port: 3306
16                 Connect_Retry: 60
17               Master_Log_File: mysql-bin.000001
18           Read_Master_Log_Pos: 154
19                Relay_Log_File: k8s-node3-relay-bin.000003
20                 Relay_Log_Pos: 367
21         Relay_Master_Log_File: mysql-bin.000001
22              Slave_IO_Running: Yes
23             Slave_SQL_Running: Yes
24               Replicate_Do_DB: biehl
25           Replicate_Ignore_DB: 
26            Replicate_Do_Table: 
27        Replicate_Ignore_Table: 
28       Replicate_Wild_Do_Table: 
29   Replicate_Wild_Ignore_Table: 
30                    Last_Errno: 0
31                    Last_Error: 
32                  Skip_Counter: 0
33           Exec_Master_Log_Pos: 154
34               Relay_Log_Space: 578
35               Until_Condition: None
36                Until_Log_File: 
37                 Until_Log_Pos: 0
38            Master_SSL_Allowed: No
39            Master_SSL_CA_File: 
40            Master_SSL_CA_Path: 
41               Master_SSL_Cert: 
42             Master_SSL_Cipher: 
43                Master_SSL_Key: 
44         Seconds_Behind_Master: 0
45 Master_SSL_Verify_Server_Cert: No
46                 Last_IO_Errno: 0
47                 Last_IO_Error: 
48                Last_SQL_Errno: 0
49                Last_SQL_Error: 
50   Replicate_Ignore_Server_Ids: 
51              Master_Server_Id: 1
52                   Master_UUID: 262f593f-746e-11e9-b769-d8c497e293c1
53              Master_Info_File: /var/lib/mysql/master.info
54                     SQL_Delay: 0
55           SQL_Remaining_Delay: NULL
56       Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
57            Master_Retry_Count: 86400
58                   Master_Bind: 
59       Last_IO_Error_Timestamp: 
60      Last_SQL_Error_Timestamp: 
61                Master_SSL_Crl: 
62            Master_SSL_Crlpath: 
63            Retrieved_Gtid_Set: 
64             Executed_Gtid_Set: 
65                 Auto_Position: 0
66          Replicate_Rewrite_DB: 
67                  Channel_Name: 
68            Master_TLS_Version: 
69 1 row in set (0.00 sec)
70 
71 mysql> 

如果報了主從使用了相同的server-id,需要進行檢查,在主從中分別檢視serverid,show variables like '%server_id%';

 1 Enter password: ******
 2 Welcome to the MySQL monitor.  Commands end with ; or \g.
 3 Your MySQL connection id is 4
 4 Server version: 5.7.24-log MySQL Community Server (GPL)
 5 
 6 Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
 7 
 8 Oracle is a registered trademark of Oracle Corporation and/or its
 9 affiliates. Other names may be trademarks of their respective
10 owners.
11 
12 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
13 
14 mysql> show variables like '%server_id%';
15 +----------------+-------+
16 | Variable_name  | Value |
17 +----------------+-------+
18 | server_id      | 1     |
19 | server_id_bits | 32    |
20 +----------------+-------+
21 2 rows in set, 1 warning (0.01 sec)
22 
23 mysql>

然後在從節點上進行檢視,如下所示:

 1 mysql> show variables like '%server_id%';
 2 +----------------+-------+
 3 | Variable_name  | Value |
 4 +----------------+-------+
 5 | server_id      | 2     |
 6 | server_id_bits | 32    |
 7 +----------------+-------+
 8 2 rows in set (0.00 sec)
 9 
10 mysql> 

7、測試一下主節點和從節點是否可以同步,可以在主節點的資料表插入一條資料,觀察從節點的資料庫是否有資料同步,需要注意的是從節點需要建立好資料庫和資料表的,不然總是報一些莫名其妙的錯誤。在主節點新增資料,可以發現從節點已經新增資料了。

可以測試修改,刪除操作,發現都是可以正常執行的。