日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

MySQL主从复制原理应用基础

發(fā)布時間:2024/4/14 数据库 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 MySQL主从复制原理应用基础 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

mysql主從復制

mysql支持單向 雙向 鏈式級聯(lián) 實時 異步復制,在復制過程中,一臺服務器充當主服務器(Master),而一個或多個其他服務器充當從服務器(Slave)


mysql主從復制的應用場景

1、主從服務器互為備份

2、主從服務器讀寫分離分擔網(wǎng)站壓力


讀寫分離

中大型公司:通過程序(php,java)

測試環(huán)境:代理軟件(mysql-proxy,amoeba)

門戶網(wǎng)站:分布式dbproxy(讀寫分離,hash負載均衡,健康檢查)

主從同步實踐操作(多實例環(huán)境)1、主庫上面設置server-id值并開啟binlog參數(shù) [root@centos03?~]#?egrep?"log-bin|server-id"?/data/3306/my.cnf? log-bin?=?/data/3306/mysql-bin server-id?=?1檢查實際配置效果 [root@centos03?~]#?mysql?-uroot?-p123456?-S?/data/3306/mysql.sock?-e?"show?variables?like?'log_bin';"? +---------------+-------+ |?Variable_name?|?Value?| +---------------+-------+ |?log_bin???????|?ON????| +---------------+-------+2、建立用于同步的賬號 mysql>??grant?replication?slave??on?*.*?to?rep@'172.16.80.%'?identified?by?'123456'; 說明:replication?slave?是mysql同步的必須權(quán)限,此處不要授權(quán)allmysql>?flush?privileges;查看授權(quán)后的結(jié)果 mysql>?show?grants?for?rep@'172.16.80.%'; +--------------------------------------------------------------------------------------------------------------------------+ |?Grants?for?rep@172.16.80.%???????????????????????????????????????????????????????????????????????????????????????????????| +--------------------------------------------------------------------------------------------------------------------------+ |?GRANT?REPLICATION?SLAVE?ON?*.*?TO?'rep'@'172.16.80.%'?IDENTIFIED?BY?PASSWORD?'*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9'?| +--------------------------------------------------------------------------------------------------------------------------+ 1?row?in?set?(0.00?sec)3、鎖表,導出數(shù)據(jù)庫 mysql>?flush?table?with?read?lock;????#該窗口不能斷,新開一個窗口做數(shù)據(jù)庫導出操作 Query?OK,?0?rows?affected?(0.00?sec)mysql>?show?master?status; +------------------+----------+--------------+------------------+ |?File?????????????|?Position?|?Binlog_Do_DB?|?Binlog_Ignore_DB?| +------------------+----------+--------------+------------------+ |?mysql-bin.000002?|??????332?|??????????????|??????????????????| +------------------+----------+--------------+------------------+ 1?row?in?set?(0.00?sec)[root@centos03?~]#?mysqldump?-uroot?-p123456?-S?/data/3306/mysql.sock?-A?-B?--events?--master-data=2?>?/opt/rep.sql?#導出所有數(shù)據(jù)庫 [root@centos03?~]#?vim?/opt/rep.sql --?CHANGE?MASTER?TO?MASTER_LOG_FILE='mysql-bin.000002',?MASTER_LOG_POS=332;??#可以看到該語句的記錄位置和上面show?master?status是一樣的,注釋狀態(tài)4、數(shù)據(jù)庫導出后,解鎖 mysql>?show?master?status;???#再次查看位置點,以驗證上面的鎖表操作是否有效 +------------------+----------+--------------+------------------+ |?File?????????????|?Position?|?Binlog_Do_DB?|?Binlog_Ignore_DB?| +------------------+----------+--------------+------------------+ |?mysql-bin.000002?|??????332?|??????????????|??????????????????| +------------------+----------+--------------+------------------+ 1?row?in?set?(0.00?sec)mysql>?unlock?tables; Query?OK,?0?rows?affected?(0.00?sec)5、從庫上面 [root@centos03?~]#?egrep?"log-bin|server-id"?/data/3307/my.cnf????? #log-bin?=?/data/3307/mysql-bin????#log-bin無需開啟 server-id?=?3???#server-id的值不能和主庫上面的值一樣[root@centos03?~]#?mysql?-uroot?-p123456?-S?/data/3307/mysql.sock?<?/opt/rep.sql???#導入從主庫備份的數(shù)據(jù)庫[root@centos03?~]#?mysql?-uroot?-phello123?-S?/data/3307/mysql.sock????????????? mysql>?change?master?to?\->?master_host='172.16.80.118',\->?master_user='rep',\->?master_password='123456',\->?master_log_file='mysql-bin.000002',\->?master_log_pos=332; Query?OK,?0?rows?affected?(0.03?sec)驗證一下 [root@centos03?~]#?cat?/data/3307/data/master.info? 18 mysql-bin.000002 332 172.16.80.118 rep 123456 3306 60 0mysql>?start?slave;?????? Query?OK,?0?rows?affected?(0.01?sec)mysql>?show?slave?status\G;????#觀察Slave_IO和Slave_SQL?這兩個線程的狀態(tài)是否是yes ***************************?1.?row?***************************Slave_IO_State:?Waiting?for?master?to?send?eventMaster_Host:?172.16.80.118Master_User:?repMaster_Port:?3306Connect_Retry:?60Master_Log_File:?mysql-bin.000002Read_Master_Log_Pos:?332Relay_Log_File:?relay-bin.000002Relay_Log_Pos:?253Relay_Master_Log_File:?mysql-bin.000002Slave_IO_Running:?YesSlave_SQL_Running:?YesReplicate_Do_DB:?Replicate_Ignore_DB:?mysqlReplicate_Do_Table:?Replicate_Ignore_Table:?Replicate_Wild_Do_Table:?Replicate_Wild_Ignore_Table:?Last_Errno:?0Last_Error:?Skip_Counter:?0Exec_Master_Log_Pos:?332Relay_Log_Space:?403Until_Condition:?NoneUntil_Log_File:?Until_Log_Pos:?0Master_SSL_Allowed:?NoMaster_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:?NoLast_IO_Errno:?0Last_IO_Error:?Last_SQL_Errno:?0Last_SQL_Error:?Replicate_Ignore_Server_Ids:?Master_Server_Id:?1 1?row?in?set?(0.00?sec)

6、登錄主庫創(chuàng)建數(shù)據(jù)庫,看是否會同步到從庫上面 [root@centos03?~]#?mysql?-uroot?-p123456?-S?/data/3306/mysql.sock? mysql>?create?database?martin;? Query?OK,?1?row?affected?(0.01?sec)觀察從庫,可以看到已經(jīng)同步過來 mysql>?show?databases; +--------------------+ |?Database???????????| +--------------------+ |?information_schema?| |?martin?????????????| |?mysql??????????????| |?performance_schema?| |?test???????????????| +--------------------+ 5?rows?in?set?(0.00?sec)[root@centos03?~]#?cd?/data/3307 [root@centos03?3307]#?ls data??my.cnf??mysql??mysqld.pid??mysql_martin3307.err??mysql.sock??relay-bin.000001??relay-bin.000002??relay-bin.index??relay-log.info [root@centos03?3307]#?cat?data/master.info? 18 mysql-bin.000002 419 172.16.80.118 rep 123456 3306 60 0[root@centos03?3307]#?mysqlbinlog??relay-bin.000002 /*!\C?utf8?*//*!*/; SET?@@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=33/*!*/; SET?@@session.lc_time_names=0/*!*/; SET?@@session.collation_database=DEFAULT/*!*/; create?database?martin


mysql主從復制原理總結(jié):

1、異步同步方式

2、邏輯同步模式,多種模式,默認是通過sql語句執(zhí)行

3、主庫通過記錄bin-log實現(xiàn)對從庫的同步,bin-log記錄數(shù)據(jù)庫更新的語句

4、主庫一個IO線程,從庫一個IO線程和一個SQL線程

5、從庫關(guān)鍵文件master.info ?relay-log ?relay-info?

6、如果從庫還需要做級聯(lián)從庫,從庫需要打開log-bin和log-slave-updates參數(shù)


監(jiān)控mysql主從狀態(tài)(這里我們簡單監(jiān)控從庫上面io和sql線程yes總數(shù)為不是2就認為主從出現(xiàn)問題了)

在客戶端編寫腳本

[root@centos03 tools]# cat /tmp/mysql-replication.sh?

#!/bin/bash

/application/mysql/bin/mysql -uroot -p123456 -e 'show slave status\G' -S /data/3307/mysql.sock|grep -Ei "Slave_IO_Running|Slave_SQL_Running"|awk '{print $2}'|grep -c Yes


修改zabbix-agent配置文件

UnsafeUserParameters=1

UserParameter=mysql.replication,/tmp/mysql-replication.sh


在服務器端添加監(jiān)控項--觸發(fā)器--圖形




正常情況下的圖形如下

此時我們模擬主庫掛掉

[root@centos03 tools]# /data/3306/mysql stop


轉(zhuǎn)載于:https://blog.51cto.com/huaxin/1841312

超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術(shù)人生

總結(jié)

以上是生活随笔為你收集整理的MySQL主从复制原理应用基础的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。