<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>資料庫同步 &#8211; 記下來</title>
	<atom:link href="https://noter.tw/tag/%E8%B3%87%E6%96%99%E5%BA%AB%E5%90%8C%E6%AD%A5/feed/" rel="self" type="application/rss+xml" />
	<link>https://noter.tw</link>
	<description>一路上踩到的坑、遇到的問題，一點一滴記下來，希望能幫助到需要的人~</description>
	<lastBuildDate>Sat, 07 Nov 2020 15:05:28 +0000</lastBuildDate>
	<language>zh-TW</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.6.3</generator>

<image>
	<url>https://noter.tw/wp-content/uploads/cropped-old-1130742_1920-1-32x32.jpg</url>
	<title>資料庫同步 &#8211; 記下來</title>
	<link>https://noter.tw</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>MariaDB Replication 設定</title>
		<link>https://noter.tw/4508/mariadb-replication-%e8%a8%ad%e5%ae%9a/</link>
					<comments>https://noter.tw/4508/mariadb-replication-%e8%a8%ad%e5%ae%9a/#respond</comments>
		
		<dc:creator><![CDATA[黃小蛙]]></dc:creator>
		<pubDate>Sat, 09 Mar 2019 07:50:44 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[資料庫]]></category>
		<category><![CDATA[replication]]></category>
		<category><![CDATA[master]]></category>
		<category><![CDATA[slave]]></category>
		<category><![CDATA[資料庫同步]]></category>
		<category><![CDATA[2019]]></category>
		<category><![CDATA[Ubuntu]]></category>
		<category><![CDATA[mariadb]]></category>
		<guid isPermaLink="false">https://noter.tw/?p=4508</guid>

					<description><![CDATA[<p>從上次買了 KVM 的虛擬主機之後，一直到現在都還沒時間處理好，這幾天終於撥出一點時間處理，這篇記錄下設定的經過。 本篇內容主要參考 MariaDB : Replication Setting 加上自&#46;&#46;&#46;</p>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/4508/mariadb-replication-%e8%a8%ad%e5%ae%9a/" data-wpel-link="internal">MariaDB Replication 設定</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></description>
										<content:encoded><![CDATA[
<p>從上次買了 KVM 的虛擬主機之後，一直到現在都還沒時間處理好，這幾天終於撥出一點時間處理，這篇記錄下設定的經過。</p>



<span id="more-4508"></span>



<p>本篇內容主要參考 <a href="https://www.server-world.info/en/note?os=Ubuntu_18.04&amp;p=mariadb&amp;f=4" target="_blank" rel="noopener nofollow external noreferrer" data-wpel-link="external" class="wpel-icon-right">MariaDB : Replication Setting<span class="wpel-icon wpel-image wpel-icon-6"></span></a> 加上自己的註解，一方面備份文章及過程，如果英文不錯或需要更詳細內容的，可以直接到那邊看！</p>



<h2 class="para wp-block-heading">緣由</h2>



<p>小蛙的機器都是三更半夜進行一次備份，並且自動把備份 sync 到 Google Drive 上，可參考 <a rel="noreferrer noopener" aria-label="一個腳本每天自動備份 (在新分頁中開啟)" href="https://noter.tw/436/%E4%B8%80%E5%80%8B%E8%85%B3%E6%9C%AC%E6%AF%8F%E5%A4%A9%E8%87%AA%E5%8B%95%E5%82%99%E4%BB%BD/" target="_blank" data-wpel-link="internal">一個腳本每天自動備份</a>、<a rel="noreferrer noopener" aria-label="gdirve 讓你在 Linux 文字介面也能好好使用 Google Drive (在新分頁中開啟)" href="https://noter.tw/4267/gdirve-%E8%AE%93%E4%BD%A0%E5%9C%A8-linux-%E6%96%87%E5%AD%97%E4%BB%8B%E9%9D%A2%E4%B9%9F%E8%83%BD%E5%A5%BD%E5%A5%BD%E4%BD%BF%E7%94%A8-google-drive/" target="_blank" data-wpel-link="internal">gdirve 讓你在 Linux 文字介面也能好好使用 Google Drive</a> 這兩篇。前陣子多買了一台 KVM 虛擬主機想要來玩 docker，一直遲遲沒有進展，這一兩天有一點時間，決定讓 MariaDB 同步寫入兩台資料庫中，這樣如果 Master DB 掛掉或出問題的話，馬上切到 Slave DB 上，跟之前備份的差別在於，備份的時間是在每天深夜，如果壞掉還原的資料時間點也是在該時刻，換成 Replication 的方式的話，就可以回到掛掉前的時間點。(小蛙的內容也沒有常常更新拉，只是想玩看看 MariaDB Replication 而已 &#8230;)</p>



<h2 class="para wp-block-heading">開始之前</h2>



<p>開始之前先確認一些概念</p>



<ul class="my-li bg-darkblue wp-block-list"><li>會有一台主要的資料庫 master (廢話)</li><li>會有一台次要的資料庫 slave (廢話 again)</li><li>主要的寫入都是在 master 裡面</li><li>slave 不提供資料寫入</li><li>當 master 有資料寫入時，會自動觸發寫入 slave</li></ul>



<p>這樣看起來應該沒問題吧！接著會進行的步驟大概是：</p>



<ul class="my-li bg-darkblue wp-block-list"><li>編輯 master 設定檔</li><li>在 master 建立一個供 slave 連線的使用者</li><li>記下 master 的連線及同步設定</li><li>匯出 master 的所有資料</li><li>編輯 slave 設定檔</li><li>匯入資料到 slave</li><li>在 slave 上設定 master 的連線資訊</li><li>檢查是否正常運作</li></ul>



<h2 class="para wp-block-heading">設定 Master </h2>



<p>修改 MariaDB 設定</p>



<pre class="wp-block-preformatted wp-block-syntaxhighlighter-code"># 使用文字編輯器修改設定檔
vim /etc/mysql/mariadb.conf.d/50-server.cnf
# 搜尋 bind-address 並修改成自己連線的 IP
bind-address = 10.0.0.1
# 搜尋 server-id，修改成自己想要設定的 ID
# 可以隨便設定，只要每一台都不同即可
server-id = 101
# 搜尋 log_bin，取消註解
log_bin = /var/log/mysql/mysql-bin.log
# 離開文字編輯器
:wq!
# 重啟 mariadb
systemctl restart mariadb</pre>



<p>進入資料庫建立一個供 slave 連線的使用者</p>



<pre class="wp-block-preformatted wp-block-syntaxhighlighter-code"># 登入 mariadb
mysql -u root -p
# 進入 mariadb 之後建立 slave 使用者
# 這邊要注意 
# a. '%' 表示任意主機，也可限定主機名稱或 IP 較安全
# b. 'your password' 改成從 slave 要連線過來的密碼          
MariaDB [(none)]&gt; grant replication slave on *.* to replica@'%' identified by 'your password'; 
# 讓上述設定生效
MariaDB [(none)]&gt; flush privileges;</pre>



<p>把 master lock 住，取得 master 的資訊</p>



<pre class="wp-block-preformatted wp-block-syntaxhighlighter-code"># 把資料表鎖住，避免匯出的當下有資料變動
MariaDB [(none)]&gt; flush tables with read lock; 
# 查看 master 的狀態，把 File 跟 Position 記下來
MariaDB [(none)]&gt; show master status; 
# 離開 mariadb
MariaDB [(none)]&gt; exit;</pre>



<p>開始匯出 master 所有資料</p>



<pre class="wp-block-preformatted wp-block-syntaxhighlighter-code"># 匯出所有資料
mysqldump -u root -p --all-databases --lock-all-tables --events &gt; mysql_dump.sql 
# 再進入 mariadb 把 lock 狀態解除
mysql -u root -p
MariaDB [(none)]&gt; unlock tables; 
MariaDB [(none)]&gt; exit; </pre>



<p>到這邊 master 的設定差不多就完成了。(這邊記錄的順序跟原文不太一樣，不過達成的目的都相同)</p>



<h2 class="wp-block-heading" id="mce_31">設定 Slave</h2>



<p>進入 slave 後，一樣先設定 mariadb 設定檔</p>



<pre class="wp-block-preformatted wp-block-syntaxhighlighter-code"># 使用文字編輯器修改設定檔
vim /etc/mysql/mariadb.conf.d/50-server.cnf
# 搜尋 bind-address 並修改成自己連線的 IP
bind-address = 10.0.0.2
# 搜尋 server-id，修改成自己想要設定的 ID
# 可以隨便設定，只要每一台都不同即可
server-id = 102
# 搜尋 log_bin，取消註解
log_bin = /var/log/mysql/mysql-bin.log
# 加入惟獨的屬性
read_only=1
# 設定 hostname
report-host=xxxx.abc.net
# 離開文字編輯器
:wq!
# 重啟 mariadb
systemctl restart mariadb</pre>



<p>把剛剛 master 匯出的資料抓回來</p>



<pre class="wp-block-preformatted wp-block-syntaxhighlighter-code"># 用法 rsync -avzh 來源 目的地
# 我們從 slave 把檔案從 master 拉過來，
# 因此 rsync -avzh master的連線資訊+檔案 slave的位置
rsync -avzh root@test:/root/mysql_dump.sql ./</pre>



<p>把所有資料匯入到資料庫中</p>



<pre class="wp-block-preformatted wp-block-syntaxhighlighter-code"># 將 master dump 出來的資料匯入到資料庫中
mysql -u root -p &lt; /root/mysql_dump.sql</pre>



<p>設定 master 連線資訊</p>



<pre class="wp-block-preformatted wp-block-syntaxhighlighter-code"># 進入 mariadb
mysql -u root -p 
# 設定 master 連線資訊
MariaDB [(none)]&gt; change master to 
    -&gt; master_host='10.0.0.1',               # master IP
    -&gt; master_user='replica',                # 剛剛設定的帳號
    -&gt; master_password='yourpassword',       # 剛剛設定的密碼
    -&gt; master_log_file='mysql-bin.000001',   # 上面得到的 File
    -&gt; master_log_pos=311;                   # 上面得到的 Position</pre>



<p>啟動 slave</p>



<pre class="wp-block-preformatted wp-block-syntaxhighlighter-code"># 啟動 slave
MariaDB [(none)]&gt; start slave;
# 查看 slave 狀態
MariaDB [(none)]&gt; show slave status\G 
# Slave_IO_State: Waiting for master to send event
# 如果顯示這樣表示連線成功，正在等待資料更新
# 有可能會有其他錯誤，連線錯誤、無法登入錯誤 ... 等</pre>



<h2 class="para wp-block-heading">後記</h2>



<p>show slave status\G 可以看到目前同步的狀態，Read_Master_Log_Pos 基本上會跟 show master status; 的 Position 一樣，也可以從 Last_Error 看到目前同步是否發生什麼錯誤。</p>



<p><strong>MySQL / MariaDB / Oracle 相關文章：</strong></p>



<ul class="my-li bg-darkblue wp-block-list">
<li><a href="https://noter.tw/12503/mariadb-json_extract-%e5%be%9e-json-%e6%ac%84%e4%bd%8d%e5%8f%96%e5%80%bc/" data-wpel-link="internal">MariaDB JSON_EXTRACT: 從 JSON 欄位取值</a></li>



<li><a href="https://noter.tw/12015/oracle-%e5%88%86%e7%b5%84%e7%b7%a8%e8%99%9f%e8%88%87%e5%85%a8%e6%8e%92%e5%ba%8f%e7%b7%a8%e8%99%9f-row_number-over-partition-by/" data-wpel-link="internal">Oracle, MySQL 分組編號與全排序編號 row_number, over, partition by</a></li>



<li><a href="https://noter.tw/3929/mysql-error-2003-hy000-%E7%84%A1%E6%B3%95%E9%81%A0%E7%AB%AF%E9%80%A3%E7%B7%9A/" data-wpel-link="internal">MySQL ERROR 2003 (HY000) 無法遠端連線</a></li>



<li><a href="https://noter.tw/2889/jsp-%E9%80%A3%E7%B5%90-mysql/" data-wpel-link="internal">JSP 連結 MySQL</a></li>



<li><a href="https://noter.tw/2883/java-%E9%80%A3%E7%B5%90-mysql/" data-wpel-link="internal">Java 連結 MySQL</a></li>



<li><a href="https://noter.tw/2306/mysql%E6%8E%92%E7%A8%8Bevent-scheduler/" data-wpel-link="internal">MySQL 排程(Event Scheduler)</a></li>



<li><a href="https://noter.tw/1988/java%E9%80%A3%E7%B5%90%E9%81%A0%E7%AB%AFmysql/" data-wpel-link="internal">Java 連結遠端 MySQL</a></li>



<li><a href="https://noter.tw/4508/mariadb-replication-%E8%A8%AD%E5%AE%9A/" data-wpel-link="internal">MariaDB Replication 設定</a></li>



<li><a href="https://noter.tw/122/java-php-%E6%8F%92%E5%85%A5-emoji-%E8%B3%87%E6%96%99%E5%88%B0-mariadb/" data-wpel-link="internal">Java / PHP 插入 emoji 資料到 MariaDB</a></li>
</ul>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/4508/mariadb-replication-%e8%a8%ad%e5%ae%9a/" data-wpel-link="internal">MariaDB Replication 設定</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://noter.tw/4508/mariadb-replication-%e8%a8%ad%e5%ae%9a/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
