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

歡迎訪問 生活随笔!

生活随笔

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

数据库

Mysql内存表主从复制【译】

發布時間:2025/3/15 数据库 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Mysql内存表主从复制【译】 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

Some Applications need to store some transient data which is frequently regenerated and MEMORY table look like a very good match for this sort of tasks. Unfortunately this will bite when you will be looking to add Replication to your environment as MEMORY tables do not play well with replication.

有些應用程序需要頻繁存儲短期數據,mysql內存表看起來很適合承擔這類任務。然而當你在內存表上應用主從復制的時候,會很受傷~

The reason is very simple – both STATEMENT and ROW replication contain the changes to the data in binary logs. This requires the data to be same on Master and Slave. When you restart the slave you will lose contents of your MEMORY tables and replication will break. STATEMENT replication will often continue to run, with contents of the table just being different as there is a little checks whenever statements produce the same results on the slave. ROW replication will complain about ROW not exist for UPDATE or DELETE operation.

原因很簡單——語句(STATEMENT)和行(ROW)復制包含數據的改變都記錄在binlog中,主從庫上的數據必須保持一致。當你出于某種原因重啟從庫后,將丟失內存表中的數據進而導致復制失敗。語句復制將持續執行,表中內容不同時會進行一些檢查并在從庫上產生同樣的結果。行復制在UPDATEDELETE操作時會報錯說行不存在。

So what you can do ? 如此,咋整?

Use Innodb Table Instead Innodb is quite fast when it fits in memory so for most applications this performance will be enough and it will save you from all complexity of different workarounds.

使用Innodb表替代內存表,Innodb對于絕大多數應用來說性能是足夠的,且可以將你從各種復雜的場景中解脫出來。

Do not replicate MEMORY tables If you do not really need MEMORY table on the slaves you can skip replicating it specifying replicate-ignore-table=db.memory_table. Note you should not be using STATEMENT level replication with INSERT … SELECT into this memory table for this to work. Be careful using data on the Slave in this case as table will be empty. Another nice trick sometimes is to make slave to generate its own copy of the table, for example by running the same cron jobs MASTER runs to refresh this table periodically.

不復制內存表,如果你的從庫上確實不需要內存表,你可以通過設置 replicate-ignore-table=db.memory_table 參數來忽略內存表的主從復制。注意,使用此參數時,不要在內存表上進行語句級的復制——INSERT ... SELECT,這樣做從庫上的表會是空的。另一個小技巧是在從庫上周期性的執行一個計劃任務,將主庫上的數據刷新到從庫中。

Restart Slaves Carefully

I would not use this as long term solution as there are going to be the reasons when SLAVE will not restart normally – power goes down MySQL crashes etc.

If you however are using MEMORY table in replication and just want to do a restart without replication breaking you can do the following:

Add skip-slave-start in your my.cnf; run SLAVE STOP; dump all your memory tables using MySQLDump; Restart the MySQL As planned; Load Dumped tables; run SLAVE START; Remove skip-slave-start from config file. Be careful using it with MASTER-MASTER or CHAIN/TREE replication. In this case you will need to disable binary logging while loading data from mysqldump as you may not want these changes to be replicated.

謹慎的重啟從庫

長遠看,還是不要使用內存表,因為從庫可能因各種原因宕掉——掉電、mysql進程崩潰等等。

如果你正在使用內存表并且想在主從復制時不會中斷可以這樣做:

在my.cnf中添加?skip-slave-start, 執行SALVE?STOP; 用mysqldump導出所有內存表; 重啟mysql; 導入剛才的表格; 執行SLAVE?START; 在my.cnf中去掉?skip-slave-start。

使用MASTER-MASTERCHAIN/TREE復制時要小心,這種情況下你需要禁用二進制日志,來確保導入數據時不復制某些改變。

What could have done better ? 有更好的招兒沒?

MySQL could have features to make it more convenient. It would be great to have MEMORY table option which would save table to on disk file on shutdown and load it back on startup. Of course you would lose the data on unclear start, but it is still handy for a lot of cases.

MySQL具備許多特性使其用來更方便。要是能在數據庫(正常)關閉時自動將內存表保存到磁盤上,在啟動時自動恢復到內存里就太好了。當然非正常關閉后啟動依然會丟失數據,但在多數情況下這特性還是很有用好使的。

We could have the option similar to skip-slave-errors but specified on per-table basics. This would allow me to simply allow to avoid all replication errors for MEMORY table which would make things more robust if table is regenerated periodically. It can be helpful in many other cases too.

我們可以選擇性的設置 skip-slave-errors 而其他表使用基本設置。這樣我能簡單的忽略內存表的復制錯誤,使得表格在周期性的再生數據時程序運行得更加健壯。這種方法對于許多其他案例也適用。

轉載于:https://my.oschina.net/newlooper/blog/507507

總結

以上是生活随笔為你收集整理的Mysql内存表主从复制【译】的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。