實現MySQL主從複製

  • MySQL主從概述:

MySQL數據庫自身提供的主從複製功能可以方便的實現數據的多處自動備份,實現數據庫的拓展。多個數據備份不僅可以加強數據的安全性,通過實現讀寫分離還能進一步提升數據庫的負載性能。

  • 下圖就描述了一個多個數據庫間主從複製與讀寫分離的模型
    在這裏插入圖片描述

在一主多從的數據庫體系中,多個從服務器採用異步的方式更新主數據庫的變化,業務服務器在執行寫或者相關修改數據庫的操作是在主服務器上進行的,讀操作則是在各從服務器上進行。如果配置了多個從服務器或者多個主服務器又涉及到相應的負載均衡問題,關於負載均衡具體的技術細節還沒有研究過,今天就先簡單的實現一主一從的主從複製功能。

  • Mysql主從複製的實現原理圖大致如下(來源網絡):
    在這裏插入圖片描述

MySQL之間數據複製的基礎是二進制日誌文件(binary log file)。一臺MySQL數據庫一旦啓用二進制日誌後,其作爲master,它的數據庫中所有操作都會以“事件”的方式記錄在二進制日誌中,其他數據庫作爲slave通過一個I/O線程與主服務器保持通信,並監控master的二進制日誌文件的變化,如果發現master二進制日誌文件發生變化,則會把變化複製到自己的中繼日誌中,然後slave的一個SQL線程會把相關的“事件”執行到自己的數據庫中,以此實現從數據庫和主數據庫的一致性,也就實現了主從複製。

  • 下面開始MySQL的安裝和MySQL主從服務的部署
  • 實驗環境如下
服務 IP
MySQL主 192.168.20.10
MySQL從 192.168.20.20
  • 實現MySQL主從複製需要進行的配置如下:
  • 主服務器:
    1.1、開始二進制日誌
    1.2、配置唯一的server-id
    1.3、獲得master二進制日誌文件名及位置
    1.4、 創建一個用於slave和master通信的用戶賬號
  • 從服務器:
    2.1、配置唯一的server-id
    2.2、使用master分配的用戶賬號讀取master二進制日誌
    2.3 啓用slave服務
主:啓動服務 創建用戶  
[root@lpj1 ~]# /etc/init.d/mysqld start
Starting MySQL.Logging to '/usr/local/mysql/data/lpj1.err'.
 SUCCESS! 
[root@lpj1 ~]#  mysqladmin -uroot password 123456
Warning: Using a password on the command line interface can be insecure.
[root@lpj1 ~]#  mysql -uroot -p123456
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 2
Server version: 5.6.46 Source distribution

Copyright (c) 2000, 2019, 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> create database course;
Query OK, 1 row affected (0.04 sec)

mysql>  grant replication slave on *.* to 'bakcuper'@'192.168.20.20' identified by  '12345678';
Query OK, 0 rows affected (0.00 sec)

mysql>  flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye
找到主數據庫的配置文件my.cnf(或者my.ini),我的在/etc/mysql/my.cnf,在[mysqld]部分插入如下幾行:
[root@localhost ~]# vim /etc/my.cnf
[mysqld]
server-id=1  #服務器id編號
binlog-do-db=course  ## 同步的庫
binlog-ignore-db=mysql  ## 不同步的庫
log-bin=mysql-bin ## 二進制日誌的文件名
重啓服務
[root@lpj1 ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS! 
進入數據庫 查看主庫狀態
[root@lpj1 ~]#  mysql -uroot -p123456
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 2
Server version: 5.6.46 Source distribution

Copyright (c) 2000, 2019, 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> create database course;
Query OK, 1 row affected (0.04 sec)

mysql>  grant replication slave on *.* to 'bakcuper'@'192.168.20.20' identified by  '12345678';
Query OK, 0 rows affected (0.00 sec)

mysql>  flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye
[root@lpj1 ~]# vim /etc/my.cnf
[root@lpj1 ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS! 
[root@lpj1 ~]# mysql -uroot -p123456
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.46-log Source distribution

Copyright (c) 2000, 2019, 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 master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      120 | course       | mysql            |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)




從:啓動服務 創建用戶 創庫 授權:
[root@lpj1 ~]# /etc/init.d/mysqld start
Starting MySQL.Logging to '/usr/local/mysql/data/lpj1.err'.
 SUCCESS! 
[root@lpj1 ~]#  mysqladmin -uroot password 123456
Warning: Using a password on the command line interface can be insecure.
[root@lpj1 ~]#  mysql -uroot -p123456
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 2
Server version: 5.6.46 Source distribution

Copyright (c) 2000, 2019, 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>  create database course;
Query OK, 1 row affected (0.01 sec)

mysql> 
修改mysql從的配置  和mysql主的配置基本一樣 如下
[root@localhost ~]# vim /etc/my.cnf
[mysqld]
server-id=2  ##id號不同
binlog-do-db=course
binlog-ignore-db=mysql
log-bin=mysql-bin
重啓服務
[root@lpj2 ~]# /etc/init.d/mysqld restart
Shutting down MySQL. SUCCESS! 
Starting MySQL. SUCCESS! 
做主從設置
[root@lpj2 ~]# mysql -uroot -p123456
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.46-log Source distribution

Copyright (c) 2000, 2019, 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> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> change master to 
    -> master_user='bakcuper',
    ->  master_password='12345678',
    ->  master_host='192.168.20.10',
    -> master_port=3306,
    -> master_log_file='mysql-bin.000001',
    -> master_log_pos=120;
Query OK, 0 rows affected, 2 warnings (0.00 sec)

mysql>  start slave;
Query OK, 0 rows affected (0.00 sec)
完事後進行驗證
mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.20.10
                  Master_User: bakcuper
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 120
               Relay_Log_File: lpj2-relay-bin.000002
                Relay_Log_Pos: 283
        Relay_Master_Log_File: mysql-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: 120
              Relay_Log_Space: 455
              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
                  Master_UUID: 9a62f3fb-a47e-11ea-98d5-000c2947d185
             Master_Info_File: /usr/local/mysql/data/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
1 row in set (0.00 sec)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章