refencen https://www.cnblogs.com/lin3615/p/5684891.html
我們提供的服務(wù)有:網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站設(shè)計(jì)、微信公眾號(hào)開(kāi)發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、鹽亭ssl等。為1000+企事業(yè)單位解決了網(wǎng)站和推廣的問(wèn)題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的鹽亭網(wǎng)站制作公司
這里介紹兩種方式,一種是用MySQL-proxy,一種用Amoeba
amoeba
優(yōu)點(diǎn):直接實(shí)現(xiàn)讀寫(xiě)分離和負(fù)載均衡,不用修改代碼,有很靈活的數(shù)據(jù)解決方案
缺點(diǎn):自己分配賬戶,和后端數(shù)據(jù)庫(kù)權(quán)限管理獨(dú)立,權(quán)限處理不夠靈活
mysql-proxy
優(yōu)點(diǎn):直接實(shí)現(xiàn)讀寫(xiě)分離和負(fù)載均衡,不用修改代碼,master和slave用一樣的帳號(hào)
缺點(diǎn):字符集問(wèn)題,lua語(yǔ)言編程,還只是alpha版本,時(shí)間消耗有點(diǎn)高
在slave服務(wù)器上執(zhí)行 show slave status 查看同步情況
Master_Log_File:slave中的I/O線程當(dāng)前正在讀取的master服務(wù)器二進(jìn)制式日志文件名.
Read_Master_Log_Pos:在當(dāng)前的 master服務(wù)器二進(jìn)制日志中,slave中的I/O線程已經(jīng)讀取的位置
Relay_Log_File:SQL線程當(dāng)前正在讀取與執(zhí)行中繼日志文件的名稱
Relay_Log_Pos:在當(dāng)前的中繼日志中,SQL線程已讀取和執(zhí)行的位置
Relay_Master_Log_File:由SQL線程執(zhí)行的包含多數(shù)近期事件的master二進(jìn)制日志文件的名稱
Slave_IO_Running:I/O線程是否被啟動(dòng)并成功連接到master
Slave_SQL_Running:SQL線程是否被啟動(dòng)
Seconds_Behind_Master:slave服務(wù)器SQL線程和從服務(wù)器I/O線程之間的差距,單位為秒計(jì)
slave同步延遲情況出現(xiàn):
1.Seconds_Behind_Master不為0,這個(gè)值可能會(huì)很大
2.Relay_Master_Log_File和Master_Log_File顯示bin-log的編號(hào)相差很大,說(shuō)明bin-log在slave上沒(méi)有及時(shí)同步,所以近期執(zhí)行的 bin-log和當(dāng)前I/O線程所讀的 bin-log相差很大
3.mysql的 slave數(shù)據(jù)庫(kù)目錄下存在大量的 mysql-relay-log日志,該日志同步完成之后就會(huì)被系統(tǒng)自動(dòng)刪除,存在大量日志,說(shuō)明主從同步延遲很厲害
mysql主從同步原理
主庫(kù)針對(duì)讀寫(xiě)操作,順序?qū)?binlog,從庫(kù)單線程去主庫(kù)讀"寫(xiě)操作的binlog",從庫(kù)取到 binlog在本地原樣執(zhí)行(隨機(jī)寫(xiě)),來(lái)保證主從數(shù)據(jù)邏輯上一致.
mysql的主從復(fù)制都是單線程的操作,主庫(kù)對(duì)所有DDL和DML產(chǎn)生 binlog,binlog是順序?qū)懀孕屎芨?,slave的Slave_IO_Running線程到主庫(kù)取日志,效率比較高,下一步問(wèn)題來(lái)了,slave的 slave_sql_running線程將主庫(kù)的 DDL和DML操作在 slave實(shí)施。DML,DDL的IO操作是隨即的,不能順序的,成本高很多,還有可能slave上的其他查詢產(chǎn)生 lock,由于 slave_sql_running也是單線程的,所以 一個(gè) DDL卡住了,需要執(zhí)行一段時(shí)間,那么所有之后的DDL會(huì)等待這個(gè) DDL執(zhí)行完才會(huì)繼續(xù)執(zhí)行,這就導(dǎo)致了延遲.由于master可以并發(fā),Slave_sql_running線程卻不可以,所以主庫(kù)執(zhí)行 DDL需求一段時(shí)間,在slave執(zhí)行相同的DDL時(shí),就產(chǎn)生了延遲.
主從同步延遲產(chǎn)生原因
當(dāng)主庫(kù)的TPS并發(fā)較高時(shí),產(chǎn)生的DDL數(shù)量超過(guò)Slave一個(gè) sql線程所能承受的范圍,那么延遲就產(chǎn)生了,當(dāng)然還有就是可能與 slave的大型 query語(yǔ)句產(chǎn)生了鎖等待
首要原因:數(shù)據(jù)庫(kù)在業(yè)務(wù)上讀寫(xiě)壓力太大,CPU計(jì)算負(fù)荷大,網(wǎng)卡負(fù)荷大,硬盤(pán)隨機(jī)IO太高
次要原因:讀寫(xiě) binlog帶來(lái)的性能影響,網(wǎng)絡(luò)傳輸延遲
架構(gòu)方面
1.業(yè)務(wù)的持久化層的實(shí)現(xiàn)采用分庫(kù)架構(gòu),mysql服務(wù)可平行擴(kuò)展分散壓力
2.單個(gè)庫(kù)讀寫(xiě)分離,一主多從,主寫(xiě)從讀,分散壓力。
3.服務(wù)的基礎(chǔ)架構(gòu)在業(yè)務(wù)和mysql之間加放 cache層
4.不同業(yè)務(wù)的mysql放在不同的機(jī)器
5.使用比主加更了的硬件設(shè)備作slave
反正就是mysql壓力變小,延遲自然會(huì)變小
硬件方面:
采用好的服務(wù)器
1、sync_binlog在slave端設(shè)置為0
2、–logs-slave-updates從服務(wù)器從主服務(wù)器接收到的更新不記入它的二進(jìn)制日志。
3、直接禁用slave端的binlog
4、slave端,如果使用的存儲(chǔ)引擎是innodb,innodb_flush_log_at_trx_commit =2
從文件系統(tǒng)本身屬性角度優(yōu)化
master端
修改linux、Unix文件系統(tǒng)中文件的etime屬性, 由于每當(dāng)讀文件時(shí)OS都會(huì)將讀取操作發(fā)生的時(shí)間回寫(xiě)到磁盤(pán)上,對(duì)于讀操作頻繁的數(shù)據(jù)庫(kù)文件來(lái)說(shuō)這是沒(méi)必要的,只會(huì)增加磁盤(pán)系統(tǒng)的負(fù)擔(dān)影響I/O性能??梢酝ㄟ^(guò)設(shè)置文件系統(tǒng)的mount屬性,組織操作系統(tǒng)寫(xiě)atime信息,在linux上的操作為:
打開(kāi)/etc/fstab,加上noatime參數(shù)
/dev/sdb1 /data reiserfs noatime 1 2
然后重新mount文件系統(tǒng)
#mount -oremount /data
主庫(kù)是寫(xiě),對(duì)數(shù)據(jù)安全性較高,比如sync_binlog=1,innodb_flush_log_at_trx_commit = 1 之類的設(shè)置是需要的
而slave則不需要這么高的數(shù)據(jù)安全,完全可以將sync_binlog設(shè)置為0或者關(guān)閉binlog,innodb_flushlog也可以設(shè)置為0來(lái)提高sql的執(zhí)行效率
1、sync_binlog=1
MySQL提供一個(gè)sync_binlog參數(shù)來(lái)控制數(shù)據(jù)庫(kù)的binlog刷到磁盤(pán)上去。
默認(rèn),sync_binlog=0,表示MySQL不控制binlog的刷新,由文件系統(tǒng)自己控制它的緩存的刷新。這時(shí)候的性能是最好的,但是風(fēng)險(xiǎn)也是最大的。一旦系統(tǒng)Crash,在binlog_cache中的所有binlog信息都會(huì)被丟失。
如果sync_binlog>0,表示每sync_binlog次事務(wù)提交,MySQL調(diào)用文件系統(tǒng)的刷新操作將緩存刷下去。最安全的就是sync_binlog=1了,表示每次事務(wù)提交,MySQL都會(huì)把binlog刷下去,是最安全但是性能損耗最大的設(shè)置。這樣的話,在數(shù)據(jù)庫(kù)所在的主機(jī)操作系統(tǒng)損壞或者突然掉電的情況下,系統(tǒng)才有可能丟失1個(gè)事務(wù)的數(shù)據(jù)。
但是binlog雖然是順序IO,但是設(shè)置sync_binlog=1,多個(gè)事務(wù)同時(shí)提交,同樣很大的影響MySQL和IO性能。
雖然可以通過(guò)group commit的補(bǔ)丁緩解,但是刷新的頻率過(guò)高對(duì)IO的影響也非常大。對(duì)于高并發(fā)事務(wù)的系統(tǒng)來(lái)說(shuō),“sync_binlog”設(shè)置為0和設(shè)置為1的系統(tǒng)寫(xiě)入性能差距可能高達(dá)5倍甚至更多。
所以很多MySQL DBA設(shè)置的sync_binlog并不是最安全的1,而是2或者是0。這樣犧牲一定的一致性,可以獲得更高的并發(fā)和性能。
默認(rèn)情況下,并不是每次寫(xiě)入時(shí)都將binlog與硬盤(pán)同步。因此如果操作系統(tǒng)或機(jī)器(不僅僅是MySQL服務(wù)器)崩潰,有可能binlog中最后的語(yǔ)句丟失了。要想防止這種情況,你可以使用sync_binlog全局變量(1是最安全的值,但也是最慢的),使binlog在每N次binlog寫(xiě)入后與硬盤(pán)同步。即使sync_binlog設(shè)置為1,出現(xiàn)崩潰時(shí),也有可能表內(nèi)容和binlog內(nèi)容之間存在不一致性。
2、innodb_flush_log_at_trx_commit(這個(gè)很管用)
抱怨Innodb比MyISAM慢 100倍?那么你大概是忘了調(diào)整這個(gè)值。默認(rèn)值1的意思是每一次事務(wù)提交或事務(wù)外的指令都需要把日志寫(xiě)入(flush)硬盤(pán),這是很費(fèi)時(shí)的。特別是使用電池供電緩存(Battery backed up cache)時(shí)。設(shè)成2對(duì)于很多運(yùn)用,特別是從MyISAM表轉(zhuǎn)過(guò)來(lái)的是可以的,它的意思是不寫(xiě)入硬盤(pán)而是寫(xiě)入系統(tǒng)緩存。
日志仍然會(huì)每秒flush到硬盤(pán),所以你一般不會(huì)丟失超過(guò)1-2秒的更新。設(shè)成0會(huì)更快一點(diǎn),但安全方面比較差,即使MySQL掛了也可能會(huì)丟失事務(wù)的數(shù)據(jù)。而值2只會(huì)在整個(gè)操作系統(tǒng) 掛了時(shí)才可能丟數(shù)據(jù)。
3、ls命令可用來(lái)列出文件的 atime、ctime和 mtime。
atime文件的access time在讀取文件或者執(zhí)行文件時(shí)更改的
ctime文件的create time在寫(xiě)入文件,更改所有者,權(quán)限或鏈接設(shè)置時(shí)隨inode的內(nèi)容更改而更改
mtime文件的modified time在寫(xiě)入文件時(shí)隨文件內(nèi)容的更改而更改
ls -lc filename列出文件的 ctime
ls -lu filename列出文件的 atime
ls -l filename列出文件的 mtime
stat filename列出atime,mtime,ctime
atime不一定在訪問(wèn)文件之后被修改
因?yàn)椋菏褂胑xt3文件系統(tǒng)的時(shí)候,如果在mount的時(shí)候使用了noatime參數(shù)那么就不會(huì)更新atime信息。
這三個(gè)time stamp都放在 inode中.如果mtime,atime修改,inode就一定會(huì)改,既然 inode改了,那ctime也就跟著改了.
之所以在 mount option中使用 noatime,就是不想file system做太多的修改,而改善讀取效能
4.進(jìn)行分庫(kù)分表處理,這樣減少數(shù)據(jù)量的復(fù)制同步操作
主庫(kù) 192.168.12.56 3306
從庫(kù)1 192.168.12.56 3307
從庫(kù)2 192.168.12.55 3306
====》192.168.12.56
# vi /etc/my.cnf
[mysqld]
server-id = 1
log-bin=mysql-bin
------重啟mysql主
# /etc/init.d/mysqld restart
# netstat -nltpd |grep mysql
------檢查參數(shù)
# ls -lrth /app/mysql/data/|grep mysql-bin
-rw-rw---- 1 mysql mysql 107 Oct 29 22:35 mysql-bin.000001
-rw-rw---- 1 mysql mysql 19 Oct 29 22:35 mysql-bin.index
# mysql -uroot -p111111 -e 'show variables;'|egrep "log_bin|server_id"
log_bin ON
server_id 1
====》192.168.12.56
# vi /mysqldata/3307/my3307.cnf
[mysqld]
server-id = 2
# mysqladmin -uroot -p1234567 -S /mysqldata/3307/mysql3307.sock shutdown
# mysqld_safe --defaults-file=/mysqldata/3307/my3307.cnf 2>&1 >/dev/null &
# mysql -uroot -p1234567 -S /mysqldata/3307/mysql3307.sock -e 'show variables like "server%"'
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 2 |
|
|
====》192.168.12.55
# vi /etc/my.cnf
[mysqld]
server-id = 3
# /etc/init.d/mysqld restart
# netstat -nltpd |grep mysql
# mysql -uroot -p111111 -e 'show variables like "server%"'
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 3 |
+---------------+-------+
# mysql -uroot -p111111
mysql> grant replication slave on *.* to 'rep'@'192.168.12.%' identified by '123456';
mysql> flush privileges;
----replication slave為mysql同步的必須權(quán)限,此處不要授予all
----*.*表示所有庫(kù)所有表,也可以指定具體庫(kù)和表進(jìn)行復(fù)制。shaw_gbk_db.test_tb
----'rep'@'192.168.12.%' rep為同步帳號(hào),192.168.12.%為授權(quán)主機(jī)網(wǎng)段
mysql> select user,host from mysql.user where user='rep';
+------+--------------+
| user | host |
+------+--------------+
| rep | 192.168.12.% |
+------+--------------+
1 row in set (0.04 sec)
mysql> show grants for rep@'192.168.12.%'\G
*************************** 1. row ***************************
Grants for rep@192.168.12.%: GRANT REPLICATION SLAVE ON *.* TO 'rep'@'192.168.12.%' IDENTIFIED BY PASSWORD '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9'
------主庫(kù)加鎖。加鎖后,該窗口不能退出,并且受以下參數(shù)影響
mysql> show variables like '%timeout';
| interactive_timeout | 28800 |
| wait_timeout | 28800 |
mysql> flush table with read lock;
注意:mysql 5.1及mysql 5.5版本鎖表方式不一樣:
5.1版本: flush tables with read lock
5.5版本: flush table with read lock
------查看當(dāng)前binlog
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 334 | | |
+------------------+----------+--------------+------------------+
------新開(kāi)窗口導(dǎo)出數(shù)據(jù)
# mkdir /bak
# mysqldump -uroot -p1111111 -A -B --events|gzip >/bak/mysql_bak_$(date +%F).sql.gz
# ll -lrht /bak/
total 144K
-rw-r--r-- 1 root root 141K Jan 10 07:58 mysql_bak_2018-01-10.sql.gz
------導(dǎo)完數(shù)據(jù)之后,查看binlog狀態(tài)是否和之前一致,無(wú)誤后解鎖
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 334 | | |
+------------------+----------+--------------+------------------+
mysql> unlock tables;
-------創(chuàng)建一個(gè)數(shù)據(jù)庫(kù),帶回從庫(kù)搭建后,看是否能自動(dòng)同步過(guò)去
mysql> create database shaw_db;
Query OK, 1 row affected (0.02 sec)
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 423 | | |
+------------------+----------+--------------+------------------+
mysql> show processlist;
+----+------+-------------------------------------------------------------------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-------------------------------------------------------------------+------------------+
| 4 | root | localhost | shaw_db | Query | 0 | NULL | show processlist |
| 10 | rep | xuan2:37165 | NULL | Binlog Dump | 406 | Master has sent all binlog to slave; waiting for binlog to be updated | NULL |
+----+------+---------------------------------------------------------------------+------------------+
# gzip -d /bak/mysql_bak_2018-01-10.sql.gz
# ls -lrht /bak/
total 516K
-rw-r--r-- 1 root root 516K Jan 10 07:58 mysql_bak_2018-01-10.sql
# mysql -uroot -p1234567 -S /mysqldata/3307/mysql3307.sock
mysql> show slave status\G
Empty set (0.00 sec)
# mysql -uroot -p1234567 -S /mysqldata/3307/mysql3307.sock< change master to master_host='192.168.12.56', master_port=3306, master_user='rep', master_password='123456', master_log_file='mysql-bin.000001', master_log_pos=334; EOF ====>從庫(kù)1生成master.info # ls -lrht /mysqldata/3307/data/ total 29M drwx------ 2 mysql mysql 4.0K Oct 29 21:45
performance_schema -rw-rw---- 1 mysql mysql 5.0M Oct 29 21:47
ib_logfile1 -rw-rw---- 1 mysql mysql 18M Oct 29 22:42 ibdata1 -rw-rw---- 1 mysql mysql 5.0M Oct 29 22:43
ib_logfile0 drwx------ 2 mysql root 4.0K Oct 29 23:42 mysql -rw-rw---- 1 mysql mysql 78 Oct 29 23:45 master.info -rw-rw---- 1 mysql mysql 107 Oct 29 23:45 mysql3307-relay-bin.000001 -rw-rw---- 1 mysql mysql 29 Oct 29 23:45 mysql3307-relay-bin.index -rw-r----- 1 mysql root 6.0K Oct 29 23:45 xuan2.err -rw-rw---- 1 mysql mysql 52 Oct 29 23:45 relay-log.info # cat master.info 18 mysql-bin.000001 334 192.168.12.56 rep 123456 3306 60 0 mysql> start slave; mysql> show slave status\G
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Seconds_Behind_Master: 0 落后主庫(kù)的秒數(shù) *************************** 1. row
*************************** Slave_IO_State: Waiting for
master to send event
Master_Host: 192.168.12.56 Master_User: rep Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 423 Relay_Log_File:
orcl-relay-bin.000002 Relay_Log_Pos: 342
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: 423 Relay_Log_Space: 497 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 1 row in set (0.00 sec) mysql> show processlist; +----+-------------+-----------+----------------------------------+------------------+ | Id | User | Host | db
| Command | Time | State
| Info | +----+-------------+-----------+---------------------------------------------------------------+------------------+ | 9
| root | localhost | NULL |
Query | 0 | NULL
| show processlist | | 10 | system user | | NULL | Connect | 347 | Waiting for master to send event |
NULL | | 11 | system user | | NULL | Connect | 289 | Slave has read all relay log; waiting
for the slave I/O thread to update it | NULL
| +----+-------------+---------------------------------------------------------------------+------------------+ 3 rows in set (0.00 sec) ----檢查,發(fā)現(xiàn)之前創(chuàng)建的數(shù)據(jù) mysql> show databases like 'shaw_db'; +--------------------+ | Database (shaw_db) | +--------------------+ | shaw_db | +--------------------+ 1 row in set (0.00 sec) ----主庫(kù) mysql> use shaw_db; Database changed mysql> create table t_zhong as select * from mysql.user; Query OK, 3 rows affected (0.02 sec) Records: 3
Duplicates: 0 Warnings: 0 mysql> show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB |
Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000001 | 537 | | | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec) ----從庫(kù)1 mysql> select count(*) from shaw_db.t_zhong; +----------+ | count(*) | +----------+ |
3 | +----------+ 1 row in set (0.00 sec) ----主庫(kù)開(kāi)啟binlog后產(chǎn)生binlog日志 # ls -lrt |grep bin -rw-rw---- 1 mysql mysql 33 Jan 10 07:13 mysql-bin.index -rw-rw---- 1 mysql mysql 827 Jan 10 10:57 mysql-bin.000001 # cat mysql-bin.index /mysqldata/3309/mysql-bin.000001 # mysqlbinlog mysql-bin.000001|more /*!50530 SET
@@SESSION.PSEUDO_SLAVE_MODE=1*/; /*!40019 SET
@@session.max_insert_delayed_threads=0*/; /*!50003 SET
@OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/; DELIMITER /*!*/; # at 4 #180110
7:13:05 server id 1 end_log_pos
107 Start: binlog v 4, server v
5.5.32-log created 1801 10
7:13:05 at startup # Warning: this binlog is either in use or
was not closed properly. ROLLBACK/*!*/; BINLOG ' 。。。。。。。。。。。。。。。。。。 ----從庫(kù)開(kāi)啟未開(kāi)啟binlog,但是有relaylog,并且有master.info記錄信息 # ls -lrt |grep relay -rw-rw---- 1 mysql mysql 155 Jan 10 08:20 orcl-relay-bin.000001 -rw-rw---- 1 mysql mysql 48 Jan 10 08:20 orcl-relay-bin.index -rw-rw---- 1 mysql mysql 746 Jan 10 10:57 orcl-relay-bin.000002 -rw-rw---- 1 mysql mysql 49 Jan 10 10:57 relay-log.info # cat orcl-relay-bin.index ##relaylog索引文件 ./orcl-relay-bin.000001 ./orcl-relay-bin.000002 # cat relay-log.info relaylog是SQL線程 ./orcl-relay-bin.000002 746
## Relay_Log_Pos: 746表示從庫(kù)sql應(yīng)用日志的relaylog位置。 mysql-bin.000001 ##這個(gè)表示從庫(kù)從主庫(kù)取數(shù)據(jù)回來(lái)的binlog位置 827 ##Exec_Master_Log_Pos: 827 這個(gè)是從庫(kù)從主庫(kù)取數(shù)據(jù)回來(lái)的binlog日志中pos位置 # mysqlbinlog orcl-relay-bin.000002 |more /*!50530 SET
@@SESSION.PSEUDO_SLAVE_MODE=1*/; /*!40019 SET @@session.max_insert_delayed_threads=0*/; /*!50003 SET
@OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/; DELIMITER /*!*/; # at 4 #180110
8:20:25 server id 2 end_log_pos
107 Start: binlog v 4, server v 5.5.32
created 180110 8:20:25 。。。。。。。。。 # cat master.info ##master.info是IO線程 18
## mysql-bin.000001 ##主庫(kù)binlog位置 827
##主庫(kù)pos位置 192.168.12.55 ##主庫(kù)地址 rep
##主庫(kù)連接帳號(hào) 123456
##主庫(kù)連接密碼 3309
##主庫(kù)端口號(hào) 60
##表示主從出現(xiàn)問(wèn)題后,從庫(kù)重試時(shí)間 0 0 1800.000 0 ====》192.168.12.56 ----導(dǎo)出時(shí)加入master-data=1 主庫(kù)導(dǎo)出備份加參數(shù) master-data=1后。導(dǎo)入從庫(kù)后,change master to時(shí)
不需要加以下參數(shù),如果是master-data=2則需要加以下參數(shù) master_log_file='mysql-bin.000001', master_log_pos=334; ##導(dǎo)出數(shù)據(jù) # mysqldump -uroot -p111111 -A -B -F --master-data=1 --events --single-transaction|gzip
> /bak/mysqld_$(date +%F).sql.gz =============================================================== ##參數(shù)說(shuō)明: -B指定多個(gè)庫(kù),增加建庫(kù)及use語(yǔ)句 --compact去掉注釋,適合調(diào)試輸出,生產(chǎn)不用 -A備份所有庫(kù) -F刷新binlog日志 --master-data增加binlog日志文件名及對(duì)應(yīng)的位置點(diǎn) -x, --lock-all-tables
Locks all tables across all databases.This is archieved by taking a
global read lock for the duration of the whole dump. Automatically turns
–single-transaction and –lock-tables off -l, --lock-tables Lock all tables for read -d只備份表結(jié)構(gòu) -t只備份數(shù)據(jù) --single-transaction 適合InnoDB事務(wù)數(shù)據(jù)庫(kù)備份 InnoDB表在備份時(shí),通常啟用選項(xiàng)—single-transaction來(lái)保證備份的一致性,實(shí)際上它的工作原理是設(shè)定本次會(huì)話的隔離級(jí)別為:repeatable read以確保本次會(huì)話dump時(shí)不會(huì)看到其他會(huì)話已經(jīng)提交的數(shù)據(jù)。 myisam備份命令 mysqldump –uroot –p111111 –A –B –F
–master-data=2 –x –events|gzip > /opt/all.sql.gz innodb備份命令:推薦 mysqldump –uroot –p111111 –A –B –F
–master-data=2 –events –single-transaction |gzip >opt/all.sql.gz =============================================================== ====》192.168.12.55 # scp /bak/mysqld_2016-10-30.sql.gz
root@192.168.12.55:/root # gunzip mysqld_2016-10-30.sql.gz # ls -lrht |grep mysqld -rw-r--r--
1 root root 520K Feb 27 14:12
mysqld_2016-10-30.sql # mysql -uroot -p111111 # mysql -uroot -p111111 mysql> show slave status\G Empty set (0.00 sec) # mysql -uroot -p1111111 < change master to master_host='192.168.12.56', master_port=3306, master_user='rep', master_password='123456'; EOF mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Master_Host: 192.168.12.55 Master_User: rep Master_Port: 3309 Connect_Retry: 60 Master_Log_File:
Read_Master_Log_Pos: 4 Relay_Log_File:
mysql3308-relay-bin.000001 Relay_Log_Pos: 4
Relay_Master_Log_File:
Slave_IO_Running: No Slave_SQL_Running: No 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: 0 Relay_Log_Space: 107 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:
NULL 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: 0 1 row in set (0.00 sec) mysql> start slave; mysql> show slave status\G *************************** 1. row
*************************** Slave_IO_State: Waiting for
master to send event Master_Host: 192.168.12.55 Master_User: rep Master_Port: 3309 Connect_Retry: 60 Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 107 Relay_Log_File:
mysql3308-relay-bin.000002 Relay_Log_Pos: 480
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: No Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table: Last_Errno: 1007 Last_Error: Error 'Can't create database 'shaw_db';
database exists' on query. Default database: 'shaw_db'. Query: 'create database
shaw_db' Skip_Counter: 0
Exec_Master_Log_Pos: 334 Relay_Log_Space: 1642 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: NULL Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 1007 Last_SQL_Error: Error 'Can't create database 'shaw_db';
database exists' on query. Default database: 'shaw_db'. Query: 'create database
shaw_db'
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1 1 row in set (0.00 sec) 報(bào)錯(cuò)原因: 在從庫(kù)先創(chuàng)建個(gè)對(duì)象,數(shù)據(jù)庫(kù)、表….然后在主庫(kù)創(chuàng)建一個(gè)同名的對(duì)象就會(huì)出現(xiàn)以上報(bào)錯(cuò) ----解決方法兩種,一種如下: stop slave; set global sql_slave_skip_counter = 1; start slave ----另一種,如下:需要重啟mysql 根據(jù)錯(cuò)誤號(hào)跳過(guò)指定的錯(cuò)誤。 slave-skip-errors = 1032,1062,1007 1032:記錄不存在 1062:字段值重復(fù),入庫(kù)失敗 1007:數(shù)據(jù)庫(kù)已存在,創(chuàng)建數(shù)據(jù)庫(kù)失敗 1050:數(shù)據(jù)表已存在 一般由于入庫(kù)重復(fù)導(dǎo)致的失敗,可以忽略。也可以使用all值忽略所有錯(cuò)誤消息如下,但不建議。slave-skip-errors = all ===處理之后: mysql> show slave status\G *************************** 1. row
*************************** Slave_IO_State: Waiting for
master to send event Master_Host: 192.168.12.56 Master_User: rep Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 107 Relay_Log_File: orcl-relay-bin.000007 Relay_Log_Pos: 253
Relay_Master_Log_File: mysql-bin.000002
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: 107 Relay_Log_Space: 554 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 1 row in set (0.00 sec) ----主庫(kù)操作 mysql> use shaw_db; Database changed mysql> create table t_user as select *
from mysql.user; ----從庫(kù)1檢查 mysql> show slave status\G mysql> select count(*) from
shaw_db.t_user; +----------+ | count(*) | +----------+ |
3 | +----------+ 1 row in set (0.01 sec) ----從庫(kù)2檢查 mysql> show slave status\G mysql> select count(*) from
shaw_db.t_user; +----------+ | count(*) | +----------+ |
3 | +----------+ 1 row in set (0.01 sec) mysql> grant all privileges on *.* to user01@'192.168.12.%'
identified by "111111"; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) mysql> show grants for user01@'192.168.12.%'\G *************************** 1. row
*************************** Grants for user01@192.168.12.%: GRANT ALL
PRIVILEGES ON *.* TO 'user01'@'192.168.12.%' IDENTIFIED BY PASSWORD
'*FD571203974BA9AFE270FE62151AE967ECA5E0AA' 1 row in set (0.00 sec) ###### 注意: ========》在登錄的時(shí)候,如果是本機(jī)登錄,默認(rèn)的認(rèn)證方式是local,因此默認(rèn)登錄會(huì)報(bào)錯(cuò),需要指定ip登錄。如下: # mysql -uuser01 -p111111 ERROR 1045 (28000):
Access denied for user 'user01'@'localhost' (using password: YES) # mysql -uuser01 -p111111 -h 192.168.12.56 -P3306 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 20 Server version: 5.5.32-log Source
distribution Copyright (c) 2000, 2013, 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> exit Bye =====》這里處理方法是再創(chuàng)建local的用戶 mysql> grant all privileges on *.* to user01@'localhost'
identified by "111111"; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; # mysql -uuser01 -p111111 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 23 Server version: 5.5.32-log Source
distribution Copyright (c) 2000, 2013, 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> mysql> select user,host from mysql.user; +--------+--------------+ | user
| host | +--------+--------------+ | root
| 127.0.0.1 | | rep
| 192.168.12.% | | user01 | 192.168.12.% |
5.1.4 從庫(kù)1啟動(dòng)從庫(kù)進(jìn)程
5.1.5 再次測(cè)試同步情況
--5.1.6 主從庫(kù)產(chǎn)生的日志文件信息
5.2 ### 從庫(kù)2搭建 ###(方式二) 推薦
5.2.1 主庫(kù)導(dǎo)出數(shù)據(jù)
5.2.2 從庫(kù)2導(dǎo)入數(shù)據(jù)
5.2.3 從庫(kù)2設(shè)置change master
5.2.4 從庫(kù)2啟動(dòng)從庫(kù)進(jìn)程
5.2.5 處理同步故障
5.2.6 測(cè)試同步
5.3 創(chuàng)建用戶用于應(yīng)用連接數(shù)據(jù)庫(kù)
本文名稱:MySQL讀寫(xiě)分離amoeba&mysql-proxy
本文路徑:http://weahome.cn/article/iidhco.html