最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

MySQL數(shù)據(jù)庫主從復(fù)制原理及作用分析

 更新時間:2021年09月06日 10:43:00   作者:神慕蔡蔡  
這篇文章主要介紹了MySQL數(shù)據(jù)庫主從復(fù)制原理并分析了主從復(fù)制的作用和使用方法,有需要的的朋友可以借鑒參考下,希望可以有所幫助,感謝閱讀

1.數(shù)據(jù)庫主從分類:

主從分為倆種:傳統(tǒng)主從/GTID主從

2.mysql主從介紹由來

現(xiàn)實生活中,數(shù)據(jù)極其重要,存儲數(shù)據(jù)庫的方式很多,但是數(shù)據(jù)庫存在著一種隱患。
隱患:

用一臺數(shù)據(jù)庫存放數(shù)據(jù),若數(shù)據(jù)庫服務(wù)器宕機(jī)了導(dǎo)致數(shù)據(jù)丟失數(shù)據(jù)多了,訪問量大了,一臺服務(wù)器無法保證服務(wù)質(zhì)量

因此數(shù)據(jù)庫主從誕生

3.主從作用

故障切換,實現(xiàn)預(yù)備讀寫分離,提供查詢服務(wù)數(shù)據(jù)庫管理系統(tǒng)備份(DBSM),避免影響業(yè)務(wù)

4.主從復(fù)制原理

bin log:二進(jìn)制日志,記錄寫操作(增刪改查)

Relay log:中繼日志

  1. 主庫會將所有的寫操作記錄到binlog日志下生成一個log dump線程,將binlog日志傳給從庫的I/O線程。
  2. 從庫有倆個線程:
    I/O線程
    sql線程
  3. 從庫的I/O線程會請求主庫得到binlog日志寫到relay log(中繼日志)中
  4. sql線程,會讀取relay log日志文件中的日志,并解析具體操作,來實現(xiàn)主從的操作一樣,達(dá)到數(shù)據(jù)一致

5.主從復(fù)制配置(數(shù)據(jù)一致時)

步驟:

  • 確保主數(shù)據(jù)庫與從數(shù)據(jù)的數(shù)據(jù)一樣
  • 主數(shù)據(jù)庫里創(chuàng)建一個同步賬號授權(quán)給從數(shù)據(jù)庫使用
  • 配置主數(shù)據(jù)庫(修改配置文件)
  • 配置從數(shù)據(jù)庫(修改配置文件)

環(huán)境需求:

倆臺mysql服務(wù)器,一臺主服務(wù)器(寫功能),一臺從服務(wù)器(讀功能)

主數(shù)據(jù)庫(centos8)  ip地址:192.168.136.145  centos8.0/mysql5.7  相同數(shù)據(jù)
                   第六節(jié):數(shù)據(jù)不相同 (可能在公司之前有數(shù)據(jù)的情況)
從數(shù)據(jù)庫(centos8)  ip地址:192.168.136.191  centos7.0/mysql5.7  相同數(shù)據(jù)

5.1主從服務(wù)器分別安裝mysql5.7

可看相關(guān)教程教程(超詳細(xì)):http://m.fzitv.net/article/221946.htm

#二進(jìn)制安裝:https://blog.csdn.net/qq_47945825/article/details/116848970?spm=1001.2014.3001.5501
#或者網(wǎng)絡(luò)倉庫安裝:(一般二進(jìn)制安裝)
https://blog.csdn.net/qq_47945825/article/details/116245442?spm=1001.2014.3001.5501

5.2主數(shù)據(jù)庫與從數(shù)據(jù)庫數(shù)據(jù)一致

[root@mysql01 ~]# mysql -uroot -e 'show databases;'
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
[root@mysql02 ~]# mysql -uroot -e 'show databases;'
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+

5.3在主數(shù)據(jù)庫里創(chuàng)建一個同步賬號授權(quán)給從數(shù)據(jù)庫使用

replication:復(fù)制 slave:從 192.168.136.191:從數(shù)據(jù)庫ip地址

mysql> create user 'vvv'@'192.168.136.191' identified by 'vvv0917';
Query OK, 0 rows affected (0.00 sec)
mysql> grant replication slave on *.*to 'vvv'@'192.168.136.191';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

5.4在從庫上測試連接

[root@mysql02 ~]# mysql -uvvv -vvv0917 -h192.168.136.145
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
+--------------------+
1 row in set (0.00 sec)

5.5配置主數(shù)據(jù)庫

[root@mysql01 ~]# cat /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
log-bin=mysql_bin #啟動binlog日志
server-id=10   #數(shù)據(jù)庫服務(wù)器唯一標(biāo)識,id必須比從數(shù)據(jù)庫小
#重啟服務(wù) (此重啟方式,前提已配置mysqld.service文件)
[root@mysql01 ~]# systemctl restart mysqld
觀察主數(shù)據(jù)庫狀態(tài):
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql_bin.000004 |      962 |              |                  |                   |
+------------------+----------+--------------+------------------+---

5.6配置從數(shù)據(jù)庫

[root@mysql02 ~]# cat /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql 
datadir = /opt/data 
socket = /tmp/mysql.sock 
port = 3307
user = mysql
pid-file = /opt/data/mysql.pid
skip-name-resolve
#skip-grant-tables 
server-id=20               #服務(wù)器id,大于主數(shù)據(jù)庫id
relay-log=mysql_relay_log  #啟動中繼日志
#log-bin=mysql-bin 
#重啟服務(wù):
[root@mysql02 ~]# systemctl restart mysqld

5.7配置并啟動主從復(fù)制的功能(mysql02從數(shù)據(jù)庫上)

[root@slave02 ~]# mysql -uroot -p
mysql> change master to
    -> master_host='192.168.136.145',
    -> master_user='vvv',
    -> master_password='vvv0917',
    -> master_log_file='mysql_bin.000004',
    -> master_log_pos=962;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
mysql> start slave;   #stop slave為關(guān)閉
Query OK, 0 rows affected (0.01 sec)
#查看配置狀態(tài):
mysql> show slave status\G; 
   Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.136.145
                  Master_User: vvv
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql_bin.000004
          Read_Master_Log_Pos: 962
               Relay_Log_File: mysql_relay_log.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql_bin.000004
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
            #此處必須倆個都是yes,就是配置成功,否則失敗

5.8測試:

主庫:

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+

從庫:

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+


主庫創(chuàng)建數(shù)據(jù)庫clq并且加入數(shù)據(jù):

mysql> create database clq;
Query OK, 1 row affected (0.00 sec)
mysql> create table clq01(id int(11)not null primary key auto_increment,name varchar(100)not null,age tinyint(4)); 
mysql> insert clq01(name,age) values('A',20),('B',21),('C',22);
Query OK, 3 rows affected (0.00 sec)

從庫中查看:

mysql> select * from clq01;
+----+------+------+s
| id | name | age  |
+----+------+------s+
|  1 | A    |   20 |
|  2 | B    |   21 |
|  3 | C    |   22 |
+----+------+------+
                              #主從復(fù)制完成!


6.主從配置(數(shù)據(jù)不一致時)

6.1一般全備主庫需要另開一個終端,給數(shù)據(jù)庫加上讀鎖(只讀不寫)

避免其他人在寫入數(shù)據(jù)導(dǎo)致不一樣

flush tables with read lock:
quit:退出即可為解鎖(備份完之后才能解鎖)

6.2確保主主數(shù)據(jù)庫與從數(shù)據(jù)庫的數(shù)據(jù)一樣

#先對主庫進(jìn)行全備
[root@mysql01 ~]# mysqldump -uroot -A > all-databases.sql 
#拷貝數(shù)據(jù)到從數(shù)據(jù)庫上
[root@mysql01 ~]# ls /clq
all-databases.sql
[root@mysql01 ~]# scp /clq/all-databases.sql root@192.168.136.193:/clq/
The authenticity of host '192.168.136.193 (192.168.136.193)' can't be established.
ECDSA key fingerprint is SHA256:XIAQEoJ+M0vOHmCwQvhUdw12u5s2nvkN0A4TMKLaFiY.
Are you sure you want to continue connecting (yes/no/[fingerprint])yes
root@192.168.136.193's password: 
all-databases.sql                                                 100%  853KB 115.4MB/s   00:00  
[root@mysql02 clq]# ll
總用量 896                       #從庫上查看
-rw-r--r--. 1 root root 873266 5月  17 19:36 all-databases.sql

6.3在從庫上查看主庫有哪些庫,確保一致

[root@mysql02 clq]# mysql -uroot -pHuawei0917@ < all-databases.sql 
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@mysql02 clq]# mysql -uroot -pHuawei0917@ -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| clq                |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
主庫:
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| clq                |
| mysql              |
| performance_schema |
| sys                |
+--------------------+


6.4確保倆庫的配置文件已經(jīng)配置了相應(yīng)的文件

[root@mysql01 ~]# cat /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
log-bin=mysql_bin     #日志文件
server-id=10          #唯一標(biāo)識服務(wù)id
[root@mysql02 ~]# cat /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql 
datadir = /opt/data 
socket = /tmp/mysql.sock 
port = 3307
user = mysql
pid-file = /opt/data/mysql.pid
skip-name-resolve
#skip-grant-tables 
server-id=20                #唯一標(biāo)識服務(wù)id(大于主庫)
relay-log=mysql_relay_log     #中繼日志
#log-bin=mysql-bin 


此后步驟和5.5之后一模一樣!

小結(jié):

主庫修改數(shù)據(jù),從庫的數(shù)據(jù)隨之改變!
反之,從庫修改數(shù)據(jù),主庫的數(shù)據(jù)不會發(fā)生改變

查看數(shù)據(jù)庫運(yùn)行的命令進(jìn)程

mysql> show processlist;
+----+------+-----------------------+------+-------------+------+---------------------------------------------------------------+------------------+
| Id | User | Host                  | db   | Command     | Time | State                                                         | Info             |
+----+------+-----------------------+------+-------------+------+---------------------------------------------------------------+------------------+
|  5 | repl | 192.168.136.219:39788 | NULL | Binlog Dump | 1575 | Master has sent all binlog to slave; waiting for more updates | NULL             |
|  7 | root | localhost             | NULL | Query       |    0 | starting                                                      | show processlist |
+----+------+-----------------------+------+-------------+------+---------------------------------------------------------------+------------------+
2 rows in set (0.00 sec)

以上就是MySQL數(shù)據(jù)庫主從復(fù)制原理及作用分析的詳細(xì)內(nèi)容,更多關(guān)于MySQL數(shù)據(jù)庫主從復(fù)制的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 詳解java調(diào)用ffmpeg轉(zhuǎn)換視頻格式為flv

    詳解java調(diào)用ffmpeg轉(zhuǎn)換視頻格式為flv

    這篇文章主要介紹了 詳解java調(diào)用ffmpeg轉(zhuǎn)換視頻格式為flv的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下
    2017-09-09
  • 最新mysql 5.7.23安裝配置圖文教程

    最新mysql 5.7.23安裝配置圖文教程

    這篇文章主要為大家詳細(xì)介紹了最新 mysql 5.7.23 安裝配置圖文教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-11-11
  • mysql8.0?my.ini?如何永久修改時區(qū)

    mysql8.0?my.ini?如何永久修改時區(qū)

    這篇文章主要介紹了mysql8.0?my.ini?如何永久修改時區(qū),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • InnoDB存儲引擎中的表空間詳解

    InnoDB存儲引擎中的表空間詳解

    這篇文章主要介紹了InnoDB存儲引擎中的表空間詳解,表空間內(nèi)部,所有頁按照區(qū)extent為物理單元進(jìn)行劃分和管理,extent由64個物理連續(xù)的頁組成,表空間可以理解為由一個個物理相鄰的extent組成,需要的朋友可以參考下
    2023-09-09
  • 淺談mysql數(shù)據(jù)庫事物隔離級別

    淺談mysql數(shù)據(jù)庫事物隔離級別

    本文主要介紹了淺談mysql數(shù)據(jù)庫事物隔離級別,數(shù)據(jù)庫事務(wù)的隔離級別有4個,這四個級別可以逐個解決臟讀 、不可重復(fù)讀 、幻讀這幾類問題,本文就詳細(xì)的介紹一下,感興趣的可以了解一下
    2023-05-05
  • MySQL中TO_DAYS()函數(shù)詳解與實際應(yīng)用舉例

    MySQL中TO_DAYS()函數(shù)詳解與實際應(yīng)用舉例

    TO_DAYS函數(shù)是指從零開始到函數(shù)內(nèi)時間的天數(shù),下面這篇文章主要給大家介紹了關(guān)于MySQL中TO_DAYS()函數(shù)詳解與實際應(yīng)用的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-04-04
  • mysql如何顯示longblob解決方案

    mysql如何顯示longblob解決方案

    這篇文章主要為大家介紹了mysql如何顯示longblob解決方案,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-10-10
  • MySQL排序原理和案例詳析

    MySQL排序原理和案例詳析

    這篇文章主要給大家介紹了關(guān)于MySQL排序原理和案例詳析的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • MySQL使用Replace操作時造成數(shù)據(jù)丟失的問題解決

    MySQL使用Replace操作時造成數(shù)據(jù)丟失的問題解決

    這篇文章主要給大家介紹了關(guān)于MySQL使用Replace操作時造成數(shù)據(jù)丟失問題的解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用MySQL具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • Mysql快速列出來所有列信息實現(xiàn)思路

    Mysql快速列出來所有列信息實現(xiàn)思路

    本文介紹了如何使用MySQL查詢系統(tǒng)表和內(nèi)置函數(shù),將指定表的所有字段信息(包括字段名和注釋)以指定格式展示出來,本文給大家分享實現(xiàn)思路,感興趣的朋友一起看看吧
    2025-01-01

最新評論

津市市| 福鼎市| 耿马| 青海省| 安庆市| 应城市| 开封县| 蕲春县| 城步| 夏津县| 铁岭县| 博兴县| 四会市| 靖边县| 宜君县| 刚察县| 内江市| 宁海县| 科技| 京山县| 华池县| 九龙县| 金沙县| 辽阳县| 峨眉山市| 贵南县| 义乌市| 阳泉市| 杭锦后旗| 遵义县| 岚皋县| 正定县| 林州市| 濉溪县| 黄浦区| 东阿县| 沾化县| 嫩江县| 武汉市| 潢川县| 宜城市|