Mysql主從

Mysql主從sql

1. 主從簡介

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

  • 用一臺數據庫服務器存放數據,若是服務器宕機則服務器內數據庫的數據則會丟失
  • 業務量大了,數據多了,訪問的人多了,一臺數據庫沒法保證服務質量

1.1 主從做用

  • 實時災備,用於故障切換
  • 讀寫分離,提供查詢服務
  • 備份,避免影響業務(這裏的備份並非只全量,增量,差別等對數據庫內數據備份,而是對服務器的一種備份)

1.2 主從形式

image

  • 一主一從

一主一從:在主服務器上的寫操做會被複制進從服務器vim

  • 主主複製

主主複製:互爲主備,相互複製寫入的信息centos

  • 一主多從---擴展系統讀取的性能,由於讀是在從庫讀取的

一主多從:當公司某業務讀取量特別大時,可將數據庫主備作成一主多從,將從服務器用做業務的讀服務器,能夠在流量大時保證服務質量服務器

  • 多主一從---5.7開始支持

多主一從:公司業務寫入量大的時候,如春運搶票軟件,雙十一電商平臺購物。可將服務器作成多主一從,保證流量大時寫入業務的服務質量socket

  • 聯級複製

2. 主從複製原理

image
主從複製步驟:ide

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

3. 主從複製配置

主從複製配置步驟:性能

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

3.1 主從複製兩臺新服務器無數據時的配置

注意: 請將防火請關閉或設置防火牆規則測試

由於是新的服務器因此兩邊的數據必定相同,因此跳過了第一步同步主從的數據

需求:搭建兩臺MySQL服務器,一臺做爲主服務器(master164),一臺做爲從服務器(slaver163),主服務器進行寫操做,從服務器進行讀操做

環境說明:

數據庫角色 IP 應用與系統版本 有無數據
主數據庫 192.168.112.164 centos7/redhat7
mysql-5.7
無數據
從數據庫 192.168.112.163 centos7/redhat7
mysql-5.7
無數據

3.1.1 mysql安裝

請使用二進制mysql包安裝

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

[root@master164 ~]# mysql -uroot -pcwh123!
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 3
Server version: 5.7.22 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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> grant replication slave on *.* to 'repl'@'192.168.112.163' identified by 'cwh123!';
Query OK, 0 rows affected, 1 warning (0.00 sec)

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

3.1.3 配置主數據庫

[root@master164 ~]# vim /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
log-bin = mysql-master-bin
server-id = 1
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve

//重啓mysql服務
[root@master164 ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS! 
[root@master164 ~]# 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      80                :::3306                          :::*                  
LISTEN      0      32                :::21                            :::*                  
LISTEN      0      128               :::22                            :::*                  
LISTEN      0      100              ::1:25                            :::*    

//查看主庫的狀態
mysql> show master status;
+-------------------------+----------+--------------+------------------+-------------------+
| File                    | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------------+----------+--------------+------------------+-------------------+
| mysql-master-bin.000002 |      154 |              |                  |                   |
+-------------------------+----------+--------------+------------------+-------------------+
  //注意其中的file下的文件是主服務器的log-bin日誌
  //position下的數字是後面從數據庫須要用到的起始位置

3.1.4 配置從數據庫

symbolic-links=0
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
server-id = 6
relay-log = mysql-relay-bin

//重啓從庫的mysql服務
[root@slaver163 ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS! 
[root@slaver163 ~]# 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      80                :::3306                          :::*                  
LISTEN      0      128               :::22                            :::*                  
LISTEN      0      100              ::1:25                            :::*                  

//配置並啓動主從複製
mysql> change master to master_host='192.168.112.164',master_user='repl',master_password='cwh123!',master_log_file='mysql-master-bin.000002',master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.01 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.112.164
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-master-bin.000002
          Read_Master_Log_Pos: 154
               Relay_Log_File: mysql-relay-bin.000002
                Relay_Log_Pos: 327
        Relay_Master_Log_File: mysql-master-bin.000002
             Slave_IO_Running: Yes //slave的io線程yes 爲成功啓動
            Slave_SQL_Running: Yes //slave的SQL線程yes爲成功啓動

3.1.5 測試驗證

//在主服務器新建cwh庫並新建aaa表向aaa表中插入數據:
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql> create database cwh;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| cwh                |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> use cwh;
Database changed
mysql> create table aaa(name varchar(100) not null,age tinyint);
Query OK, 0 rows affected (0.05 sec)

mysql> show tables;
+---------------+
| Tables_in_cwh |
+---------------+
| aaa           |
+---------------+
1 row in set (0.00 sec)

mysql> insert into aaa values('tom',20),('jerry',30);
Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from aaa;
+-------+------+
| name  | age  |
+-------+------+
| tom   |   20 |
| jerry |   30 |
+-------+------+
2 rows in set (0.00 sec)

//在從數據庫中查看數據是否同步:
mysql> select * from cwh.aaa;
+-------+------+
| name  | age  |
+-------+------+
| tom   |   20 |
| jerry |   30 |
+-------+------+
2 rows in set (0.00 sec)
  //能夠看出同步成功了

3.1.6 從服務器IO線程和SQL線程變化

//主服務器每次重啓後log-bin日誌文件的編號會發生改變
mysql> show master status;
+-------------------------+----------+--------------+------------------+-------------------+
| File                    | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------------+----------+--------------+------------------+-------------------+
| mysql-master-bin.000003 |      154 |              |                  |                   |
+-------------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql> exit
Bye
[root@master164 ~]# /etc/init.d/mysqld restart
Shutting down MySQL............ SUCCESS! 
Starting MySQL. SUCCESS! 

[root@master164 ~]# mysql -uroot -pcwh123!
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 2
Server version: 5.7.22-log MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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-master-bin.000004 |      154 |              |                  |                   |
+-------------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
   //能夠看出已經改變變爲00004


mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Reconnecting after a failed master event read
                  Master_Host: 192.168.112.164
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-master-bin.000004
          Read_Master_Log_Pos: 154
               Relay_Log_File: mysql-relay-bin.000006
                Relay_Log_Pos: 381
        Relay_Master_Log_File: mysql-master-bin.000004
             Slave_IO_Running: Connecting
            Slave_SQL_Running: Yes
    //能夠看出IO線程狀態從yes變爲connecting

//等待一段時間後在查看從服務器
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.112.164
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-master-bin.000005
          Read_Master_Log_Pos: 154
               Relay_Log_File: mysql-relay-bin.000008
                Relay_Log_Pos: 381
        Relay_Master_Log_File: mysql-master-bin.000005
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
    //能夠看出IO線程變爲yes,也就說明此時同步完成

3.2主從複製兩臺主服務器有數據時的配置

主從複製配置步驟:

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

需求:搭建兩臺MySQL服務器,一臺做爲主服務器,一臺做爲從服務器,主服務器進行寫操做,從服務器進行讀操做

環境說明:

數據庫角色 IP 應用與系統版本 有無數據
主數據庫 192.168.112.164 centos7/redhat7
mysql-5.7
有數據
從數據庫 192.168.112.165 centos7/redhat7
mysql-5.7
無數據

3.2.1 mysql安裝

安裝MySQL二進制包

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

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

//先查看主庫有哪些庫
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| cwh                |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 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)

mysql> 

    //此時在原來終端上建立一個庫試試讀鎖是否生效
    //此鎖表的終端必須在全備配置完成之後才能退出

//全備主庫並將備份文件傳送到從庫
[root@master164 ~]# mysqldump -uroot -pcwh123! --all-databases > /opt/master_all_backup.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@master164 ~]# ls /opt/
data  master_all_backup.sql  yumbak
[root@master164 ~]# scp /opt/master_all_backup.sql root@192.168.112.165:/opt/
root@192.168.112.165's password: 
master_al 100%  203   199.6KB/s   00:00 

//解除主庫的鎖表狀態,直接退出交互式界面便可

//在從庫上恢復主庫的備份並查看從庫有哪些庫,確保與主庫一致
[root@slave165 ~]# mysql -uroot -pcwh123! < /opt/master_all_backup.sql 
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@slave165 ~]# mysql -uroot -pcwh123! -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| cwh                |
| mysql              |
| performance_schema |
| sys                |
+--------------------+

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

mysql> grant replication slave on *.* to 'repl'@'192.168.112.165' identified by 'cwh123!';
Query OK, 0 rows affected, 1 warning (0.00 sec)

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

3.2.4 配置主數據庫

[root@master164 ~]# vim /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
log-bin = mysql-master-bin
server-id = 1
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve

//重啓mysql服務
[root@master164 ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS! 

//查看主庫的狀態
mysql> show master status;
+-------------------------+----------+--------------+------------------+-------------------+
| File                    | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------------+----------+--------------+------------------+-------------------+
| mysql-master-bin.000007 |     603 |              |                  |                   |
+-------------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

3.2.5 配置從數據庫

[root@slave165 ~]# vim /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
server-id = 8
relay-log = mysql-relay-bin

//重啓從庫的mysql服務
[root@slave165 ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS! 

//配置並啓動主從複製
mysql> change master to master_host='192.168.112.164',master_user='repl',master_password='cwh123!',master_log_file='mysql-master-bin.000007',master_log_pos=603;
Query OK, 0 rows affected, 2 warnings (0.00 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.112.164
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-master-bin.000007
          Read_Master_Log_Pos: 603
               Relay_Log_File: mysql-relay-bin.000002
                Relay_Log_Pos: 327
        Relay_Master_Log_File: mysql-master-bin.000007
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

3.2.6 測試驗證

//在主服務器的建立庫:
mysql> create database ddl;
Query OK, 1 row affected (0.01 sec)
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| cwh                |
| ddl                |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
6 rows in set (0.00 sec)

//在從數據庫中查看數據是否同步:
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| cwh                |
| ddl                |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
6 rows in set (0.00 sec)

  //能夠看出數據同步成功
相關文章
相關標籤/搜索