Mysql主從

MySQL主從複製雖好,能完美解決數據庫單點問題嗎?

1、單個數據庫服務器的缺點mysql

  • 數據庫服務器存在單點問題;
  • 數據庫服務器資源沒法知足增加的讀寫請求;
  • 高峯時數據庫鏈接數常常超過上限。

2、如何解決單點問題linux

  • 增長額外的數據庫服務器,組建數據庫集羣;
  • 同一集羣中的數據庫服務器須要具備相同的數據;
  • 集羣中的任一服務器宕機後,其它服務器能夠取代宕機服務器。

3、MySQL主從複製架構
一、主庫將變動寫入到主庫的binlog中
sql

  • 一些MySQL版本並不會開啓二進制日誌,因此必定要檢查是否開啓;
  • 若是剛開始沒有開啓,後面再進行開啓的話,須要重啓數據庫才能生效,並且數據庫的重啓每每會對業務形成很大的影響;
  • 儘管二進制日誌對性能有稍許的影響,因此仍是建議你們不管是否使用複製功能,都要開啓MySQL二進制日誌,由於增量備份也須要二進制日誌。

二、從庫的IO線程在指定位置讀取主庫binlog內容存儲到本地的中繼日誌(Relay Log)中數據庫

  • 要完成二進制日誌的傳輸過程,MySQL會在從服務器上啓動一個工做線程,稱爲IO線程,這個IO線程會跟主數據庫創建一個普通的客戶端鏈接,而後在主服務器上啓動一個特殊的二進制轉儲線程稱爲binlogdown線程。
  • 從庫上的IO線程經過這個二進制轉儲線程來讀取主庫上的二進制事件,若是該事件追遇上主庫,則會進入sleep狀態,直到主庫發起信號通知有新事件產生時,纔會被喚醒,relay log的格式和binlog格式是徹底相同的,
  • 可使用mysqlbinlog來讀取relay log中的內容。
    三、從庫的SQL線程讀取Relay Log日誌中的內容,並在從庫中重放
    SQL線程所執行的事件,咱們能夠經過配置選項來決定是否要寫入到從服務器的二進制日誌中。
    目前MySQL支持兩種複製類型:vim

  • 基於二進制日誌點的複製
  • 基於GTID的複製(MySQL>=5.7推薦使用)

四. 主從複製的一些缺點

雖然主從複製增長了一個數據庫副本,但從數據庫和主數據庫的數據最終會是一致的。之因此說是最終一致,由於MySQL複製是異步的,正常狀況下主從複製數據之間會有一個微小的延遲。
經過這個數據庫副本看似解決了數據庫單點問題,但並不完美:由於這種架構下,若是主服務器宕機,須要手動切換從服務器,業務中斷不能忍受,不能知足應用高可用的要求。服務器

1.主從簡介

在現代企業中,數據顯得尤其重要,而存儲數據的數據庫選擇又五花八門,但不管是何種數據庫,均存在着一種隱患。
想幾個問題:架構

  • 用一臺數據庫存放數據,若此數據庫服務器宕機了致使數據丟失怎麼辦?
  • 業務量大了,數據多了,訪問的人多了,一臺數據庫沒法保證服務質量了怎麼辦?

1.1 主從做用

  • 實時災備,用於故障切換
  • 讀寫分離,提供查詢服務
  • 備份,避免影響業務(這裏說的備份是備份sql服務器,而不是備份數據)
  • 1.2 主從形式
    Mysql主從異步

  • 一主一從
  • 主主複製
  • 一主多從—擴展系統讀取的性能,由於讀是在從庫讀取的
  • 多主一從—5.7開始支持
  • 聯級複製

2. 主從複製原理

主從複製步驟:socket

  • 主庫將全部的寫操做記錄到binlog日誌中並生成一個log dump線程,將binlog日誌傳給從庫的I/O線程
  • 從庫生成兩個線程,一個I/O線程,一個SQL線程
  • I/O線程去請求主庫的binlog,並將獲得的binlog日誌寫到relay log(中繼日誌) 文件中
  • SQL線程,會讀取relay log文件中的日誌,並解析成具體操做,而後重放,來實現主從的操做一致,達到最終數據一致的目的

3. 主從複製配置

主從複製配置步驟:ide

  • 確保從數據庫與主數據庫裏的數據同樣
  • 在主數據庫裏建立一個同步帳號受權給從數據庫使用
  • 配置主數據庫(修改配置文件)
  • 配置從數據庫(修改配置文件)

需求:
搭建兩臺MySQL服務器,一臺做爲主服務器,一臺做爲從服務器,主服務器進行寫操做,從服務器進行讀操做
環境說明:Red Hat Enterprise Linux 8.0 (Ootpa)
Mysql主從

3.1mysql安裝

分別在主從兩臺機上二進制安裝mysql-5.7版本,此處略過安裝步驟

3.2 mysql主從配置(2個新的mysql服務器)

這裏的都是二進制安裝的Mysql

3.2.1 確保從數據庫與主數據庫裏的數據同樣

爲確保從數據庫與主數據庫裏的數據同樣,先全備主數據庫並還原到從數據庫中

//查看數據庫
主庫的數據庫
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| wlw                |
| www                |
+--------------------+
6 rows in set (0.00 sec)
從庫的數據庫
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)
//全備主庫
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)
//備份主庫並將備份文件傳送到從庫
[root@zhu ~]# mysqldump -uroot -pw --all-databases > /opt/all-202005071900.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@zhu ~]# ls /opt/
1  all-202005071900.sql  data
[root@zhu ~]# scp /opt/all-202005071900.sql root@192.168.66.128:/opt
The authenticity of host '192.168.66.128 (192.168.66.128)' can't be established.
ECDSA key fingerprint is SHA256:lpKqeNs7QFkySWsYsJ1IMnidcmZotljVvyB/y1YuWW4.
ECDSA key fingerprint is MD5:ad:d8:3d:e7:7e:aa:fd:07:79:3c:7f:ca:82:0c:42:2f.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.66.128' (ECDSA) to the list of known hosts.
root@192.168.66.128's password: 
all-202005071900.sql                                                            100%  774KB  47.5MB/s   00:00    

//在從庫上恢復主庫的備份並查看,與主庫保持一致
[root@cong ~]# ls /opt/
all-202005071900.sql  data
[root@cong ~]# mysql < /opt/all-202005071900.sql 
[root@cong ~]# mysql -e 'show databases;'
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| wlw                |
| www                |
+--------------------+

3.2.2在主數據庫裏建立一個同步帳號受權給從數據庫使用

//在主庫裏面建立一個同步帳號受權給從數據庫
mysql> create user 'wlw'@'192.168.66.128' identified by 'w' ;
Query OK, 0 rows affected (0.00 sec)

mysql> grant replication slave on *.* to 'wlw'@'192.168.66.128';
Query OK, 0 rows affected (0.00 sec)

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

3.2.3配置主數據庫

//配置主庫
[root@zhu ~]# vim /etc/my.cnf
[mysql]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
log-bin=mysql-bin  //啓用binlog日誌
server-id=1     //數據庫服務器惟一的標識符,主庫的值必定要大於從庫
relay-log=mysql-relay-bin
symbolic-links=0

log-error=/var/log/mysqld.log

//重啓mysql服務
[root@zhu ~]# service mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS!
Failed to restart mysqld.service: Unit not found.
[root@zhu ~]# ss -antl
State      Recv-Q Send-Q            Local Address:Port                           Peer Address:Port              
LISTEN     0      128                           *:22                                        *:*                  
LISTEN     0      100                   127.0.0.1:25                                        *:*                  
LISTEN     0      128                          :::22                                       :::*                  
LISTEN     0      100                         ::1:25                                       :::*                  
LISTEN     0      80                           :::3306                                     :::*

[root@zhu ~]# systemctl stop firewalld
[root@zhu ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@zhu ~]# vim /etc/selinux/config 
[root@zhu ~]# setenforce 0

3.2.4配置從數據庫

//配置從庫
[root@cong ~]# vim /etc/my.cnf
[mysql]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
log-bin=mysql-bin  //啓用binlog日誌
server-id=2     //數據庫服務器惟一的標識符,主庫的值必定要大於從庫
relay-log=mysql-relay-bin
symbolic-links=0

log-error=/var/log/mysqld.log

//重啓mysql服務
[root@cong ~]# service mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS!
[root@cong ~]# ss -antl
State      Recv-Q Send-Q            Local Address:Port                           Peer Address:Port              
LISTEN     0      128                           *:22                                        *:* 
[root@cong ~]# systemctl stop firewalld
[root@cong ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@cong ~]# vim /etc/selinux/config 
[root@cong ~]# setenforce 0

LISTEN     0      100                   127.0.0.1:25                                        *:*                  
LISTEN     0      128                          :::22                                       :::*                  
LISTEN     0      100                         ::1:25                                       :::*                  
LISTEN     0      80                           :::3306                                     :::*
//配置並啓動主從複製
mysql> CHANGE MASTER TO
 -> MASTER_HOST='192.168.66.128',
 -> MASTER_USER='wlw',
 -> MASTER_PASSWORD='w',
 -> MASTER_LOG_FILE='mysql-bin.000001',
 -> MASTER_LOG_POS=154;
Query OK, 0 rows affected, 2 warnings (0.33 sec)
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
//查看服務器狀態
mysql> show slave status \G
*************************** 1. row ***************************
 Slave_IO_State: Waiting for master to send event
 Master_Host: 192.168.66128
 Master_User: wlw
 Master_Port: 3306
 Connect_Retry: 60
 Master_Log_File: mysql-bin.000001
 Read_Master_Log_Pos: 154
 Relay_Log_File: mysql-relay-bin.000002
 Relay_Log_Pos: 320
 Relay_Master_Log_File: mysql-bin.000001
 Slave_IO_Running: Yes //ྌ॒஠ᶳԅYes
 Slave_SQL_Running: Yes //ྌ॒஠ᶳԅYes
 Replicate_Do_DB:
 Replicate_Ignore_DB:

3.2.5測試驗證

//驗證
 在主庫表中建立內容
 mysql> use wlw;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from w;
+----+------+------+
| id | name | age  |
+----+------+------+
|  1 | q    |   20 |
|  2 | w    |   23 |
|  3 | e    |   25 |
|  4 | r    |   28 |
+----+------+------+
4 rows in set (0.00 sec)

mysql>  insert into w values (5,'sean',20),(6,'tom',23),(7,'jerry',30);
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from w;
+----+-------+------+
| id | name  | age  |
+----+-------+------+
|  1 | q     |   20 |
|  2 | w     |   23 |
|  3 | e     |   25 |
|  4 | r     |   28 |
|  5 | sean  |   20 |
|  6 | tom   |   23 |
|  7 | jerry |   30 |
+----+-------+------+
7 rows in set (0.00 sec)

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

在從庫查看是否同步
mysql> select * from w;
+----+-------+------+
| id | name  | age  |
+----+-------+------+
|  1 | q     |   20 |
|  2 | w     |   23 |
|  3 | e     |   25 |
|  4 | r     |   28 |
|  5 | sean  |   20 |
|  6 | tom   |   23 |
|  7 | jerry |   30 |
+----+-------+------+
7 rows in set (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
相關文章
相關標籤/搜索