溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

mysql傳統(tǒng)主從復制的部署過程

發(fā)布時間:2021-08-24 17:38:09 來源:億速云 閱讀:136 作者:chen 欄目:MySQL數(shù)據(jù)庫

這篇文章主要講解了“mysql傳統(tǒng)主從復制的部署過程”,文中的講解內(nèi)容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“mysql傳統(tǒng)主從復制的部署過程”吧!



部署過程

一、保證Master庫和Slave庫的數(shù)據(jù)和結(jié)構(gòu)一致
可以通過mysqldump或者XtraBackup工具對Master庫進行全備,再把備份文件傳到Slave庫,Slave庫利用改備份進行恢復,使得兩者一致。
我這里的Slave庫是直接復制Master庫生成的,所以不需要做這一步了。

二、配置兩個庫的參數(shù)文件

  • Master庫需要打開二進制日志

  • Master庫需要設置與Slave庫不同的server-id

  1. [root@potato data]vi /etc/my.cnf

  2. binlog_format = mixed

  3. server-id = 203306

  4. log-bin = mybinlog

  • Slave庫只需配置server-id即可

  1. [root@tomato data]vi /etc/my.cnf

  2. server-id = 203307


三、創(chuàng)建用來進行復制的用戶并且賦權(quán)

  • 必須給予用戶replication slave權(quán)限,指定Slave庫的主機地址

點擊(此處)折疊或打開

  1. root@localhost:mysql.sock  05:02:32 [(none)]>grant replication slave on *.* to repl@'192.168.161.128' identified by 'repl';

四、讓Slave庫change到Master庫

  • 查看Master庫此時的日志位置

點擊(此處)折疊或打開

  1. root@localhost:mysql.sock  05:02:32 [(none)]>show master status ;

  2. +-----------------+----------+--------------+------------------+-------------------+

  3. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

  4. +-----------------+----------+--------------+------------------+-------------------+

  5. | mybinlog.000004 | 331 | | | |

  6. +-----------------+----------+--------------+------------------+-------------------+

  7. 1 row in set (0.00 sec)

  • 從庫開始change到master庫上

點擊(此處)折疊或打開

  1. root@localhost:mysql.sock  04:29:15 [(none)]>change master to

  2.     -> master_host='192.168.161.128',

  3.     -> master_port=3306,

  4.     -> master_user='repl',

  5.     -> master_password='repl',

  6.     -> master_log_file='mybinlog.000004',

  7.     -> master_log_pos=331;

  8. ERROR 2006 (HY000): MySQL server has gone away

  9. No connection. Trying to reconnect...

  10. Connection id:    2

  11. Current database: *** NONE ***


  12. Query OK, 0 rows affected, 2 warnings (0.17 sec)

五、Slave庫執(zhí)行開始復制命令

點擊(此處)折疊或打開

  1. root@localhost:mysql.sock  04:40:11 [(none)]>start slave;

到此,主從復制就已經(jīng)配置完畢了

六、對搭建的環(huán)境進行測試測試

  • 主庫開始插入數(shù)據(jù)測試

點擊(此處)折疊或打開

  1. root@localhost:mysql.sock  04:40:11 [(none)]>use lala;

  2. root@localhost:mysql.sock  04:51:23 [(lala)]>create table haha(id int);

  3. root@localhost:mysql.sock  04:51:23 [(lala)]>insert into haha values(1);

  • 從庫是否接受到數(shù)據(jù)

點擊(此處)折疊或打開

  1. root@localhost:mysql.sock  04:52:55 [(none)]>select * from lala.haha;

  2. +------+

  3. | id |

  4. +------+

  5. | 1 |

  6. +------+

  7. 1 row in set (0.00 sec)

  • 查看Slave狀態(tài)

點擊(此處)折疊或打開

  1. root@localhost:mysql.sock  04:57:23 [(none)]>show slave status\G

  2. *************************** 1. row ***************************

  3.                Slave_IO_State: Waiting for master to send event

  4.                   Master_Host: 192.168.161.128

  5.                   Master_User: repl

  6.                   Master_Port: 3306

  7.                 Connect_Retry: 60

  8.               Master_Log_File: mybinlog.000004

  9.           Read_Master_Log_Pos: 620

  10.                Relay_Log_File: mysql-relay-bin.000003

  11.                 Relay_Log_Pos: 571

  12.         Relay_Master_Log_File: mybinlog.000004

  13.              Slave_IO_Running: Yes

  14.             Slave_SQL_Running: Yes

  15.               Replicate_Do_DB:

  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: 620

  25.               Relay_Log_Space: 744

  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: 0

  36. Master_SSL_Verify_Server_Cert: No

  37.                 Last_IO_Errno: 0

  38.                 Last_IO_Error:

  39.                Last_SQL_Errno: 0

  40.                Last_SQL_Error:

  41.   Replicate_Ignore_Server_Ids:

  42.              Master_Server_Id: 203306

  43.                   Master_UUID: af3609cd-b426-11e6-a997-000c29d55626

  44.              Master_Info_File: /data/mysql/mytest_3306/data/master.info

  45.                     SQL_Delay: 0

  46.           SQL_Remaining_Delay: NULL

  47.       Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it

  48.            Master_Retry_Count: 86400

  49.                   Master_Bind:

  50.       Last_IO_Error_Timestamp:

  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. 1 row in set (0.00 sec)

主從復制部署測試成功

感謝各位的閱讀,以上就是“mysql傳統(tǒng)主從復制的部署過程”的內(nèi)容了,經(jīng)過本文的學習后,相信大家對mysql傳統(tǒng)主從復制的部署過程這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI