<?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>mariadb &#8211; 記下來</title>
	<atom:link href="https://noter.tw/tag/mariadb/feed/" rel="self" type="application/rss+xml" />
	<link>https://noter.tw</link>
	<description>一路上踩到的坑、遇到的問題，一點一滴記下來，希望能幫助到需要的人~</description>
	<lastBuildDate>Tue, 23 Jan 2024 05:07:30 +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>mariadb &#8211; 記下來</title>
	<link>https://noter.tw</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>MariaDB JSON_EXTRACT: 從 JSON 欄位取值</title>
		<link>https://noter.tw/12503/mariadb-json_extract-%e5%be%9e-json-%e6%ac%84%e4%bd%8d%e5%8f%96%e5%80%bc/</link>
					<comments>https://noter.tw/12503/mariadb-json_extract-%e5%be%9e-json-%e6%ac%84%e4%bd%8d%e5%8f%96%e5%80%bc/#respond</comments>
		
		<dc:creator><![CDATA[黃小蛙]]></dc:creator>
		<pubDate>Sun, 14 Jan 2024 10:56:23 +0000</pubDate>
				<category><![CDATA[資料庫]]></category>
		<category><![CDATA[JSON KEY]]></category>
		<category><![CDATA[查詢條件]]></category>
		<category><![CDATA[排序條件]]></category>
		<category><![CDATA[mariadb]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[讀取 json 欄位]]></category>
		<category><![CDATA[JSON_EXTRACT]]></category>
		<category><![CDATA[JSON 欄位排序]]></category>
		<category><![CDATA[搜尋 JSON]]></category>
		<guid isPermaLink="false">https://noter.tw/?p=12503</guid>

					<description><![CDATA[<p>根據 JSON 路徑取得儲存在 MariaDB 資料庫中的 JSON 欄位的值，JSON_EXTRACT 可以用在 SELECT 欄位、WHERE 條件及 ORDER BY 排序 &#8230; 等。&#46;&#46;&#46;</p>
<p>這篇文章 <a rel="nofollow" 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> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></description>
										<content:encoded><![CDATA[
<p>根據 JSON 路徑取得儲存在 MariaDB 資料庫中的 JSON 欄位的值，JSON_EXTRACT 可以用在 SELECT 欄位、WHERE 條件及 ORDER BY 排序 &#8230; 等。</p>



<span id="more-12503"></span>



<h2 class="wp-block-heading para">JSON_EXTRACT</h2>



<h3 class="wp-block-heading para">語法</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="sql" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">JSON_EXTRACT(json_doc, path[, path] ...)</pre>



<h3 class="wp-block-heading para">說明</h3>



<p>從 JSON <span style="text-decoration: underline;">欄位</span>中讀取資料，讀取的依據是<span style="text-decoration: underline;">路徑參數</span>相符的部分，函數將回傳所有符合的值；如果回傳值為多個值，則結果會按符合的順序自動包裝為陣列，如果沒有路徑匹配或任何參數為 NULL，則傳回 NULL。</p>



<p>如果<span style="text-decoration: underline;">路徑參數</span>不是有效路徑，或者 json_doc 參數不是有效的 JSON 文檔，則會發生錯誤。路徑表達式是 MariaDB 支援的 <a href="https://mariadb.com/kb/en/jsonpath-expressions/" target="_blank" rel="noreferrer noopener nofollow external" data-wpel-link="external" class="wpel-icon-right">JSONPath 表達式<span class="wpel-icon wpel-image wpel-icon-6"></span></a>。 (<a href="https://mariadb.com/kb/en/json_extract/" target="_blank" rel="noreferrer noopener nofollow external" data-wpel-link="external" class="wpel-icon-right">參考資料<span class="wpel-icon wpel-image wpel-icon-6"></span></a>)</p>



<h3 class="wp-block-heading para">範例</h3>



<p>用在 ORDER BY 排序</p>



<pre class="EnlighterJSRAW" data-enlighter-language="sql" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">SELECT * FROM `tender` ORDER BY JSON_EXTRACT(`json`, '$.create_date') DESC;</pre>



<p>用在搜尋欄位</p>



<pre class="EnlighterJSRAW" data-enlighter-language="sql" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">SELECT * FROM `tender` WHERE JSON_EXTRACT(`json`, '$.title') = '公告';</pre>



<p>用在欄位取得</p>



<pre class="EnlighterJSRAW" data-enlighter-language="sql" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">SELECT `id`, JSON_EXTRACT(`json`, '$.title') AS `title` FROM `tender`;</pre>



<p>延伸閱讀：<br></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/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> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://noter.tw/12503/mariadb-json_extract-%e5%be%9e-json-%e6%ac%84%e4%bd%8d%e5%8f%96%e5%80%bc/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Oracle, MySQL 分組編號與全排序編號 row_number, over, partition by</title>
		<link>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/</link>
					<comments>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/#respond</comments>
		
		<dc:creator><![CDATA[黃小蛙]]></dc:creator>
		<pubDate>Fri, 17 Nov 2023 04:02:11 +0000</pubDate>
				<category><![CDATA[資料庫]]></category>
		<category><![CDATA[mariadb]]></category>
		<category><![CDATA[MySQL 連續編號]]></category>
		<category><![CDATA[Oracle]]></category>
		<category><![CDATA[MariaDB 分組編號]]></category>
		<category><![CDATA[Oracle 編號]]></category>
		<category><![CDATA[Oracle 分組編號]]></category>
		<category><![CDATA[Oracle 序號]]></category>
		<category><![CDATA[Oracle 連續編號]]></category>
		<category><![CDATA[Oracle ROWNUM]]></category>
		<category><![CDATA[row_number()]]></category>
		<category><![CDATA[partition by]]></category>
		<category><![CDATA[over]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[MySQL 分組編號]]></category>
		<guid isPermaLink="false">https://noter.tw/?p=12015</guid>

					<description><![CDATA[<p>留給自己的一 memo，使用 Oracle 匯出資料的時候常常需要加上分組編號，這邊簡單記錄一下 Oracle 分組編號的使用方法。</p>
<p>這篇文章 <a rel="nofollow" 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> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></description>
										<content:encoded><![CDATA[
<p>留給自己的一 memo，使用 Oracle 匯出資料的時候常常需要加上分組編號，這邊簡單記錄一下使用方法。</p>



<span id="more-12015"></span>



<h2 class="wp-block-heading para">使用 ROWNUM 編號 (Oracle)</h2>



<p>首先第一種情況是要從頭到尾做連續編號，如果使用 ROWNUM 做的話，會發現只要加了 ORDER BY，序號不會跟著順序重新編號，例如</p>



<pre class="EnlighterJSRAW" data-enlighter-language="sql" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">SELECT rownum, t.* FROM TEST t</pre>



<p>如果只是要簡單編號不考慮資料排序 (order by) 直接使用 ROWNUM 即可(下表左)，若加上排序後如下表右，順序並不會跟著變動。</p>



<div class="wp-block-columns is-layout-flex wp-container-core-columns-is-layout-1 wp-block-columns-is-layout-flex">
<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow">
<figure class="wp-block-table"><table><tbody><tr><td class="has-text-align-center" data-align="center">1</td><td>B000636</td><td>無</td></tr><tr><td class="has-text-align-center" data-align="center">2</td><td>B000637</td><td>無</td></tr><tr><td class="has-text-align-center" data-align="center">3</td><td>F00001</td><td>原始</td></tr><tr><td class="has-text-align-center" data-align="center">4</td><td>F00001</td><td>分類</td></tr><tr><td class="has-text-align-center" data-align="center">5</td><td>F00002</td><td>原始</td></tr><tr><td class="has-text-align-center" data-align="center">6</td><td>F00003</td><td>分類</td></tr></tbody></table><figcaption class="wp-element-caption">使用 ROWNUM 加上編號</figcaption></figure>
</div>



<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow">
<figure class="wp-block-table"><table><tbody><tr><td class="has-text-align-center" data-align="center">6</td><td>F00003</td><td>分類</td></tr><tr><td class="has-text-align-center" data-align="center">5</td><td>F00002</td><td>原始</td></tr><tr><td class="has-text-align-center" data-align="center">4</td><td>F00001</td><td>原始</td></tr><tr><td class="has-text-align-center" data-align="center">3</td><td>F00001</td><td>分類</td></tr><tr><td class="has-text-align-center" data-align="center">2</td><td>B000637</td><td>無</td></tr><tr><td class="has-text-align-center" data-align="center">1</td><td>B000636</td><td>無</td></tr></tbody></table><figcaption class="wp-element-caption">加上 ORDER BY 後</figcaption></figure>
</div>
</div>



<h2 class="wp-block-heading para">使用 row_number(), over() 全排序編號</h2>



<p>這種時候借助 row_number() 與 over() 的幫忙就可以完成</p>



<pre class="EnlighterJSRAW" data-enlighter-language="sql" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">SELECT row_number() over (order by no desc) seq, t.* FROM TEST t</pre>



<figure class="wp-block-table"><table><tbody><tr><td class="has-text-align-center" data-align="center">1</td><td>F00003</td><td>分類</td></tr><tr><td class="has-text-align-center" data-align="center">2</td><td>F00002</td><td>原始</td></tr><tr><td class="has-text-align-center" data-align="center">3</td><td>F00001</td><td>原始</td></tr><tr><td class="has-text-align-center" data-align="center">4</td><td>F00001</td><td>分類</td></tr><tr><td class="has-text-align-center" data-align="center">5</td><td>B000637</td><td>無</td></tr><tr><td class="has-text-align-center" data-align="center">6</td><td>B000636</td><td>無</td></tr></tbody></table><figcaption class="wp-element-caption">使用 row_number() over () 後</figcaption></figure>



<h2 class="wp-block-heading para">使用 row_number(), over(partition by) 做 Oracle 分組編號</h2>



<p>還有一種常見的需求是做分組編號，就是遇到同類型的時候，可以標註這是該類型的編號幾，只要在 over 中加入 partition by 要用來當作分組依據的欄位即可</p>



<pre class="EnlighterJSRAW" data-enlighter-language="sql" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">SELECT t.no, row_number() over (partition by t.no order by t.no asc) seq, t.type FROM TEST t</pre>



<div class="wp-block-columns is-layout-flex wp-container-core-columns-is-layout-2 wp-block-columns-is-layout-flex">
<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow">
<figure class="wp-block-table"><table><tbody><tr><td>B000636</td><td>1</td><td>無</td></tr><tr><td>B000637</td><td>1</td><td>無</td></tr><tr><td>F00001</td><td>1</td><td>原始</td></tr><tr><td>F00001</td><td>2</td><td>分類</td></tr><tr><td>F00002</td><td>1</td><td>原始</td></tr><tr><td>F00003</td><td>1</td><td>分類</td></tr></tbody></table><figcaption class="wp-element-caption">加上分組編號</figcaption></figure>



<p>類似需求備忘：<a href="https://tomkuo139.blogspot.com/2011/11/oracle-rownum-rownumber-rank-denserank.html" target="_blank" rel="noreferrer noopener nofollow external" data-wpel-link="external" class="wpel-icon-right">Oracle rownum / row_number / rank / dense_rank 這四個的差異<span class="wpel-icon wpel-image wpel-icon-6"></span></a></p>
</div>
</div>



<p>延伸閱讀：<br><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></p>


<h2 class="para">與 Oracle 相關的文章</h2><ul><li><span style="font-size: 1em;"><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></span></li><li><span style="font-size: 1em;"><a href="https://noter.tw/9109/oracle-sqlplus-export-results-to-file/" data-wpel-link="internal">使用 Oracle sqlplus 將查詢結果匯出到檔案</a></span></li><li><span style="font-size: 1em;"><a href="https://noter.tw/8540/database/" data-wpel-link="internal">資料庫問題彙整</a></span></li><li><span style="font-size: 1em;"><a href="https://noter.tw/2711/oracle-%e5%8c%af%e5%85%a5%e5%8c%af%e5%87%ba-excel-%e9%80%8f%e9%81%8e-sql-developer/" data-wpel-link="internal">Oracle 匯入/匯出 Excel (透過 SQL Developer)</a></span></li><li><span style="font-size: 1em;"><a href="https://noter.tw/2714/%e5%ae%98%e6%96%b9-client-oracle-sql-developer-%e5%ae%89%e8%a3%9d%e6%95%99%e5%ad%b8/" data-wpel-link="internal">官方 Client &#8211; Oracle SQL Developer 安裝教學</a></span></li><li><span style="font-size: 1em;"><a href="https://noter.tw/2371/oracle-sqlplus-import-sql-file/" data-wpel-link="internal">Oracle 使用 sqlplus 匯入 SQL 檔案</a></span></li></ul><p>這篇文章 <a rel="nofollow" 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> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>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/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<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[mariadb]]></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>
		<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>
		<item>
		<title>Java / PHP 插入 emoji 資料到 MariaDB</title>
		<link>https://noter.tw/122/java-php-%e6%8f%92%e5%85%a5-emoji-%e8%b3%87%e6%96%99%e5%88%b0-mariadb/</link>
					<comments>https://noter.tw/122/java-php-%e6%8f%92%e5%85%a5-emoji-%e8%b3%87%e6%96%99%e5%88%b0-mariadb/#respond</comments>
		
		<dc:creator><![CDATA[黃小蛙]]></dc:creator>
		<pubDate>Thu, 28 Jun 2018 14:57:34 +0000</pubDate>
				<category><![CDATA[資料庫]]></category>
		<category><![CDATA[網頁後端]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[emoji]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[mariadb]]></category>
		<category><![CDATA[utf8mb4]]></category>
		<category><![CDATA[useUnicode]]></category>
		<category><![CDATA[characterEncoding]]></category>
		<category><![CDATA[錯誤]]></category>
		<category><![CDATA[Java]]></category>
		<guid isPermaLink="false">https://noter.tw/?p=122</guid>

					<description><![CDATA[<p>Java 跟 PHP 新增 emoji 字串進去 MySQL / MariaDB 資料庫的時候發生錯誤？這篇可以幫助你喔！</p>
<p>這篇文章 <a rel="nofollow" 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> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></description>
										<content:encoded><![CDATA[<p>這件事是這樣的，小蛙需要接收一個從 App 過來的表單資料，就在接收後要塞進 MariaDB 的時候，問題就出現了 &#8230;</p>
<p><span id="more-122"></span></p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic">Error Code: 1366. Incorrect string value: '\xF0\x9F\x92\xA2' for column 'SOURCE_CONTEXT' at row 1</pre>
<p>一再檢查要塞入的字串感覺沒有什麼問題，試了好幾次之後才發現 &#8230; 原來裡面包含了 emoji 啦！！！</p>
<p>之前跟同事聊天的時候得知，如果要存 emoji 的話要把資料庫的欄位改成 utf8mb4，但是呢 &#8230; 小蛙實在不想動太多，參考資料裡有很多解法，有興趣可以去看看。</p>
<p>小蛙這邊只是把要存 emoji 的欄位改成 utf8mb4，然後 &#8230; php 一執行，還是噴一樣的錯誤出來，嗯 ~ 這個案情不單純，來慢慢 try ~</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic">jdbc:mysql://ip:port/db_name?useUnicode=true&amp;characterEncoding=utf8mb4</pre>
<p>試了一些方法之後發現，在 php 的部份連線後，在做任何操作之前先執行</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic">mysqli_query( $conn, "SET NAMES 'utf8mb4'");</pre>
<p>ㄟ ~ 竟然好像貌似就可以了！另一個專案需要用 Java 去剖析並塞入有 emoji 的需求，立馬試試</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic">ps.executeQuery("SET NAMES 'utf8mb4'");</pre>
<p>哦哦哦哦哦哦哦哦哦哦哦哦哦 ~</p>
<p>有很多文章的教學是根本的解決方法，像是把資料庫各項設定都直接改成 utf8mb4，小蛙不想動到太多，試出這方法，最後總結一下：</p>
<ol class="my-li bg-darkblue">
<li>mysql connector 更新到新版</li>
<li>jdbc:mysql://ip:port/db_name?useUnicode=true&amp;characterEncoding=utf-8&amp;character_set_server=utf8mb4</li>
<li>建立 Statement 或 PreparedStatement 後就立馬執行 SET NAMES &#8216;utf8mb4&#8217;</li>
</ol>
<p>以上，希望幫助到有需要的人！</p>
<h2 class="para">參考資料</h2>
<ol class="my-li bg-darkblue">
<li><a href="https://stackoverflow.com/questions/44591895/utf8mb4-in-mysql-workbench-and-jdbc" target="_blank" rel="noopener noreferrer nofollow external" data-wpel-link="external" class="wpel-icon-right">utf8mb4 in MySQL Workbench and JDBC &#8211; Stack Overflow<span class="wpel-icon wpel-image wpel-icon-6"></span></a></li>
<li><a href="https://stackoverflow.com/questions/46140583/caused-by-java-sql-sqlexception-unsupported-character-encoding-utf8mb4" target="_blank" rel="noopener noreferrer nofollow external" data-wpel-link="external" class="wpel-icon-right">Caused by: java.sql.SQLException: Unsupported character encoding &#8216;utf8mb4&#8217; &#8211; Stack Overflow<span class="wpel-icon wpel-image wpel-icon-6"></span></a></li>
<li><a href="https://stackoverflow.com/questions/47060827/jdbc-and-utf8mb4-encoding-the-emoji-symbol" target="_blank" rel="noopener noreferrer nofollow external" data-wpel-link="external" class="wpel-icon-right">JDBC and utf8mb4 encoding the emoji symbol ? &#8211; Stack Overflow<span class="wpel-icon wpel-image wpel-icon-6"></span></a></li>
<li><a href="https://blog.csdn.net/sunny05296/article/details/78488366" target="_blank" rel="noopener noreferrer nofollow external" data-wpel-link="external" class="wpel-icon-right">mysql/Java服务端对emoji(utf8mb4编码)的支持有关的问题 &#8211; CSDN 博客<span class="wpel-icon wpel-image wpel-icon-6"></span></a></li>
</ol>


<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/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> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://noter.tw/122/java-php-%e6%8f%92%e5%85%a5-emoji-%e8%b3%87%e6%96%99%e5%88%b0-mariadb/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>MySQL ERROR 2003 (HY000) 無法遠端連線</title>
		<link>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/</link>
					<comments>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/#respond</comments>
		
		<dc:creator><![CDATA[黃小蛙]]></dc:creator>
		<pubDate>Wed, 06 Jun 2018 12:01:04 +0000</pubDate>
				<category><![CDATA[資料庫]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[mariadb]]></category>
		<category><![CDATA[error]]></category>
		<category><![CDATA[2003]]></category>
		<category><![CDATA[遠端連線]]></category>
		<guid isPermaLink="false">https://wazai.net/?p=3929</guid>

					<description><![CDATA[<p>前面兩篇：Java 連結 MySQL、JSP 連結 MySQL 有提到遠端連線設定的方式，但自己回頭看才發現很不清楚啊！因最近朋友遇到類似問題，在這邊完整記錄一下。 先來了解一下 MySQL 遠端登入&#46;&#46;&#46;</p>
<p>這篇文章 <a rel="nofollow" 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> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></description>
										<content:encoded><![CDATA[
<p>前面兩篇：<a rel="noreferrer noopener" href="https://noter.tw/2883/java-%e9%80%a3%e7%b5%90-mysql/" target="_blank" data-wpel-link="internal">Java 連結 MySQL</a>、<a rel="noreferrer noopener" href="https://noter.tw/2889/jsp-%e9%80%a3%e7%b5%90-mysql/" target="_blank" data-wpel-link="internal">JSP 連結 MySQL</a> 有提到遠端連線設定的方式，但自己回頭看才發現很不清楚啊！因最近朋友遇到類似問題，在這邊完整記錄一下。</p>



<span id="more-3929"></span>



<p>先來了解一下 MySQL 遠端登入需要有哪些條件：</p>



<ul class="my-li bg-darkblue wp-block-list"><li>帳號要對</li><li>密碼要對</li><li>IP 要對</li></ul>



<p>除了上面這些還有呢？第一次遇到這個錯誤還真的很難處理，上面三個都對了，卻還是一直噴錯誤出來，死活都不讓連。</p>



<p><br>這邊小蛙大概提一下自己了解的，需要詳細說明的，請自行 Google 囉！以下說明皆以小蛙使用的 Ubuntu 16.04 為主，Ubuntu 下的 MySQL 預設只允許本機連線，其他版本的小蛙不清楚，例如：CentOS 可能還要額外設定打開防火牆 port &#8230; 這些暫時不討論，根據上面的敘述我們可以得知，會有一個設定檔的某個設定可以打開禁忌，沒錯，開始設定吧！</p>



<h2 class="para wp-block-heading">設定 mysqld.cnf</h2>



<p>這個可能根據不同的版本會叫不同的名字，也可能會放在不同的地方，例如：mysql.conf, mysql.cnf, mysqld.cnf, my.ini &#8230; anyway 自己拼湊看看，下面是小蛙的範例：</p>



<pre class="wp-block-preformatted wp-block-syntaxhighlighter-code"># 編輯 mysql 設定檔
vim /etc/mysql/mysql.conf.d/mysqld.cnf

# 把這行註解掉，預設只允許 127.0.0.1 連線
#bind-address = 127.0.0.1</pre>



<p>好了，重新啟動 MySQL 後，大功告成！興奮的跑去連線，這時候你會罵一聲 X！最好是這樣就可以了，沒錯喔！到這邊其實就可以允許外部連線了（扣除上面提到有額外設定防火牆的情況外），為什麼還是無法連線呢？這又要回到 MySQL 的連線機制了，上面提到除了 帳號、密碼、IP 之外，MySQL 自己有一個使用者權限表，在這權限表裡面詳細記錄了 OO 使用者，密碼是 XX，允許連線的位置是 ZZ，連進來之後有的權限是 AABBCCDD &#8230; 等資訊，到這邊懂了嗎？如果上述設定都好了，卻還是無法連線，很大的機率是「允許連線位置是 ZZ」這邊造成的啊！<br>什麼？我們來前往一探究竟，先用本機登入自己的 MySQL </p>



<pre class="wp-block-preformatted wp-block-syntaxhighlighter-code">mysql -u root -p
# 輸入密碼
use mysql;
select host, user from user;
+----------------+------------------+
| host           | user             |
+----------------+------------------+
| 123.3.3.3      | root             |
| localhost      | phpmyadmin       |
| localhost      | root             |
+----------------+------------------+</pre>



<p>這邊可以看到允許從 localhost 及 123.3.3.3 的 root 使用者登入，locahost 的 phpmyadmin 使用者登入，讓我們來設定可以遠端登入吧！</p>



<pre class="wp-block-preformatted wp-block-syntaxhighlighter-code">(這個步驟如果有 phpmyadmin 的話，可以直接透過 phpmyadmin 來設定更方便)</pre>



<h2 class="para wp-block-heading">創建可遠端登入使用者</h2>



<pre class="wp-block-preformatted wp-block-syntaxhighlighter-code">mysql -u root -p 
# 輸入密碼 

use mysql; 
# 新建一個使用者並允許所有權限 
GRANT ALL ON *.* TO 'user_name'@'from_ip' identified by 'password'; 

# 更新權限，這行一定要輸入 
FLUSH PRIVILEGES; </pre>



<p>這樣一來就會建立一個使用者叫做 user_name 密碼是 password，並且允許從 from_ip 遠端登入，如果希望該使用者可以從「任意IP」登入的話，可以把 from_ip 換成「%」，但是要注意喔！如果是開放 root 可以從任意地方登入，又擁有所有權限 &#8230; 那 &#8230; 風險應該不用小蛙提醒，就記錄到這邊了！希望能幫助到有需要的人。 </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/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> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>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/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Solr 教學 (5) – 建立新的 Core</title>
		<link>https://noter.tw/3170/solr-%e6%95%99%e5%ad%b8-5-%e5%bb%ba%e7%ab%8b%e6%96%b0%e7%9a%84-core/</link>
					<comments>https://noter.tw/3170/solr-%e6%95%99%e5%ad%b8-5-%e5%bb%ba%e7%ab%8b%e6%96%b0%e7%9a%84-core/#respond</comments>
		
		<dc:creator><![CDATA[黃小蛙]]></dc:creator>
		<pubDate>Wed, 08 Jan 2014 06:19:30 +0000</pubDate>
				<category><![CDATA[資料庫]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Web Server]]></category>
		<category><![CDATA[java.sql.SQLException: Illegal value for setFetchSize()]]></category>
		<category><![CDATA[schema.xml]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[mariadb]]></category>
		<category><![CDATA[solr]]></category>
		<category><![CDATA[solrconfig.xml]]></category>
		<category><![CDATA[DataImportHandler]]></category>
		<category><![CDATA[batchSize]]></category>
		<category><![CDATA[db-data-config.xml]]></category>
		<guid isPermaLink="false">http://wazai.net/?p=3170</guid>

					<description><![CDATA[<p>這篇文章小蛙要記錄怎麼樣新建一個 Core 並且也加入 DataImportHandler 的方法。其他相關 Solr 系列文章請參考：Solr 教學 (1) – 安裝篇Solr 教學 (2) – t&#46;&#46;&#46;</p>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/3170/solr-%e6%95%99%e5%ad%b8-5-%e5%bb%ba%e7%ab%8b%e6%96%b0%e7%9a%84-core/" data-wpel-link="internal">Solr 教學 (5) – 建立新的 Core</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></description>
										<content:encoded><![CDATA[<p>這篇文章小蛙要記錄怎麼樣新建一個 Core 並且也加入 DataImportHandler 的方法。<br><span id="more-3170"></span>其他相關 Solr 系列文章請參考：<br><a href="https://noter.tw/3084/solr-%e6%95%99%e5%ad%b8-1-%e5%ae%89%e8%a3%9d%e7%af%87/" target="_blank" rel="noopener noreferrer" data-wpel-link="internal">Solr 教學 (1) – 安裝篇</a><br><a href="https://noter.tw/3082/solr-%e6%95%99%e5%ad%b8-2-tomcat-7-%e4%bb%a5%e5%b8%b3%e8%99%9f%e5%af%86%e7%a2%bc%e9%99%90%e5%88%b6%e5%ad%98%e5%8f%96%e9%a0%81%e9%9d%a2/" target="_blank" rel="noopener noreferrer" data-wpel-link="internal">Solr 教學 (2) – tomcat 7 以帳號密碼限制存取頁面</a><br><a href="https://noter.tw/3094/solr-%e6%95%99%e5%ad%b8-3-%e4%bb%a5-remote-address-filter-%e9%99%90%e5%88%b6%e5%ad%98%e5%8f%96/" target="_blank" rel="noopener noreferrer" data-wpel-link="internal">Solr 教學 (3) – 以 Remote Address Filter 限制存取</a><br><a href="https://noter.tw/3160/solr-%e6%95%99%e5%ad%b8-4-%e4%bd%bf%e7%94%a8-dataimport-%e5%8c%af%e5%85%a5%e8%b3%87%e6%96%99%e5%ba%ab%e8%b3%87%e6%96%99/" target="_blank" rel="noopener noreferrer" data-wpel-link="internal">Solr 教學 (4) – 使用 DataImport 匯入資料庫資料</a></p>
<p>上一篇記錄了怎麼透過 DataImportHandler 來將 MySQL 的資料匯入到 Solr 中，剛詢問了同事，一個 Core 可以大概看成資料庫中的一個 table，所以要匯入兩個不相關的 table 要用兩個 Core，雖然網路上好像有人提到可以將兩個不相干的 table 匯入到一個 Core 中，不過小蛙的 table 不多，就用簡單的做法就好。</p>
<p>小蛙的 index 環境是在 /opt/solr/example，最快的方法是直接把 example 資料夾複製一個新的並且改名字，這邊小蛙想要有一個 news 的 core， 使用自己建立必要資料夾的方法，建立以下資料夾</p>
<pre>/opt/solr/news/
/opt/solr/news/conf
/opt/solr/news/data</pre>
<p>再將原本 /opt/solr/example/conf/ 下的&nbsp;db-data-config.xml,&nbsp;schema.xml,&nbsp;solrconfig.xml 這三個檔案複製到&nbsp;/opt/solr/news/conf/ 目錄中。</p>
<p>接著修改 /opt/solr/solr.xml，加入新的 core</p>
<div>
<div id="highlighter_990520" class="syntaxhighlighter  xml">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td class="gutter">
<div class="line number1 index0 alt2">1</div>
<div class="line number2 index1 alt1">2</div>
<div class="line number3 index2 alt2">3</div>
<div class="line number4 index3 alt1">4</div>
<div class="line number5 index4 alt2">5</div>
<div class="line number6 index5 alt1">6</div>
<div class="line number7 index6 alt2">7</div>
<div class="line number8 index7 alt1">8</div>
</td>
<td class="code">
<div class="container">
<div class="line number1 index0 alt2"><code class="xml plain">&lt;?</code><code class="xml keyword">xml</code> <code class="xml color1">version</code><code class="xml plain">=</code><code class="xml string">"1.0"</code> <code class="xml color1">encoding</code><code class="xml plain">=</code><code class="xml string">"UTF-8"</code> <code class="xml plain">?&gt;</code></div>
<div class="line number2 index1 alt1"><code class="xml plain">&lt;</code><code class="xml keyword">solr</code> <code class="xml color1">persistent</code><code class="xml plain">=</code><code class="xml string">"true"</code><code class="xml plain">&gt;</code></div>
<div class="line number3 index2 alt2"><code class="xml spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="xml plain">&lt;</code><code class="xml keyword">cores</code> <code class="xml color1">adminPath</code><code class="xml plain">=</code><code class="xml string">"/admin/cores"</code><code class="xml plain">&gt;</code></div>
<div class="line number4 index3 alt1"><code class="xml spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="xml plain">&lt;</code><code class="xml keyword">core</code> <code class="xml color1">name</code><code class="xml plain">=</code><code class="xml string">"example"</code> <code class="xml color1">instanceDir</code><code class="xml plain">=</code><code class="xml string">"example"</code> <code class="xml plain">/&gt;</code></div>
<div class="line number5 index4 alt2"><code class="xml spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="xml plain">&lt;</code><code class="xml keyword">core</code> <code class="xml color1">name</code><code class="xml plain">=</code><code class="xml string">"News"</code> <code class="xml color1">instanceDir</code><code class="xml plain">=</code><code class="xml string">"news"</code> <code class="xml plain">/&gt;</code></div>
<div class="line number6 index5 alt1"><code class="xml spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="xml plain">...</code></div>
<div class="line number7 index6 alt2"><code class="xml spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="xml plain">&lt;/</code><code class="xml keyword">cores</code><code class="xml plain">&gt;</code></div>
<div class="line number8 index7 alt1"><code class="xml plain">&lt;/</code><code class="xml keyword">solr</code><code class="xml plain">&gt;</code></div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>重新啟動 tomcat。小蛙在這邊就遇到很多錯誤，因為資料夾是用 root 權限建立的，而 solr 並沒有這樣的權限可以存取，所以這邊要注意把 /opt/solr/news 下的檔案擁有權都改成 tomcat7 (看 tomcat user 叫什麼就改成那個)。</p>
<p>第二個要注意的事情是一切正常運作後，點選到 DataImport 頁面開始運行時，卻出現下面的錯誤</p>
<div>
<div id="highlighter_887532" class="syntaxhighlighter  text">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td class="gutter">
<div class="line number1 index0 alt2">1</div>
<div class="line number2 index1 alt1">2</div>
<div class="line number3 index2 alt2">3</div>
<div class="line number4 index3 alt1">4</div>
</td>
<td class="code">
<div class="container">
<div class="line number1 index0 alt2"><code class="text plain">org.apache.solr.handler.dataimport.DataImportHandlerException: Unable to execute query: select * from news Processing Document # 1</code></div>
<div class="line number2 index1 alt1"><code class="text plain">...</code></div>
<div class="line number3 index2 alt2"><code class="text plain">Caused by: java.sql.SQLException: Illegal value for setFetchSize().</code></div>
<div class="line number4 index3 alt1"><code class="text plain">...</code></div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>參考資料 1,2 中提到在 db-data-config.xml 中的 &lt;dataSource 加上&nbsp;batchSize=&#8221;-1&#8243; 就可以解決這個問題囉！</p>


<p><strong>Solr 系列文章：</strong></p>



<ul class="my-li bg-darkblue wp-block-list"><li><a href="https://noter.tw/3084/solr-%e6%95%99%e5%ad%b8-1-%e5%ae%89%e8%a3%9d%e7%af%87/" data-wpel-link="internal">Solr 教學 (1) &#8211; 安裝篇</a></li><li><a href="https://noter.tw/3082/solr-%e6%95%99%e5%ad%b8-2-tomcat-7-%e4%bb%a5%e5%b8%b3%e8%99%9f%e5%af%86%e7%a2%bc%e9%99%90%e5%88%b6%e5%ad%98%e5%8f%96%e9%a0%81%e9%9d%a2/" data-wpel-link="internal">Solr 教學 (2) – tomcat 7 以帳號密碼限制存取頁面</a></li><li><a href="https://noter.tw/3094/solr-%e6%95%99%e5%ad%b8-3-%e4%bb%a5-remote-address-filter-%e9%99%90%e5%88%b6%e5%ad%98%e5%8f%96/" data-wpel-link="internal">Solr 教學 (3) – 以 Remote Address Filter 限制存取</a></li><li><a href="https://noter.tw/3160/solr-%e6%95%99%e5%ad%b8-4-%e4%bd%bf%e7%94%a8-dataimport-%e5%8c%af%e5%85%a5%e8%b3%87%e6%96%99%e5%ba%ab%e8%b3%87%e6%96%99/" data-wpel-link="internal">Solr 教學 (4) – 使用 DataImport 匯入資料庫資料</a></li><li><a href="https://noter.tw/3170/solr-%e6%95%99%e5%ad%b8-5-%e5%bb%ba%e7%ab%8b%e6%96%b0%e7%9a%84-core/" data-wpel-link="internal">Solr 教學 (5) – 建立新的 Core</a></li></ul>



<h2 class="para wp-block-heading">參考資料</h2>



<ul class="wp-block-list"><li><a href="http://lucene.472066.n3.nabble.com/problem-on-running-fullimport-td1707206.html" target="_blank" rel="noreferrer noopener nofollow external" aria-label=" (在新分頁中開啟)" data-wpel-link="external" class="wpel-icon-right">Solr &#8211; User &#8211; problem on running fullimport<span class="wpel-icon wpel-image wpel-icon-6"></span></a></li><li><a rel="noreferrer noopener nofollow external" aria-label=" (在新分頁中開啟)" href="http://wiki.apache.org/solr/DataImportHandler#Configuring_JdbcDataSource" target="_blank" data-wpel-link="external" class="wpel-icon-right">DataImportHandler &#8211; Solr Wiki<span class="wpel-icon wpel-image wpel-icon-6"></span></a></li></ul>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/3170/solr-%e6%95%99%e5%ad%b8-5-%e5%bb%ba%e7%ab%8b%e6%96%b0%e7%9a%84-core/" data-wpel-link="internal">Solr 教學 (5) – 建立新的 Core</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://noter.tw/3170/solr-%e6%95%99%e5%ad%b8-5-%e5%bb%ba%e7%ab%8b%e6%96%b0%e7%9a%84-core/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Solr 教學 (4) – 使用 DataImport 匯入資料庫資料</title>
		<link>https://noter.tw/3160/solr-%e6%95%99%e5%ad%b8-4-%e4%bd%bf%e7%94%a8-dataimport-%e5%8c%af%e5%85%a5%e8%b3%87%e6%96%99%e5%ba%ab%e8%b3%87%e6%96%99/</link>
					<comments>https://noter.tw/3160/solr-%e6%95%99%e5%ad%b8-4-%e4%bd%bf%e7%94%a8-dataimport-%e5%8c%af%e5%85%a5%e8%b3%87%e6%96%99%e5%ba%ab%e8%b3%87%e6%96%99/#respond</comments>
		
		<dc:creator><![CDATA[黃小蛙]]></dc:creator>
		<pubDate>Wed, 08 Jan 2014 04:00:17 +0000</pubDate>
				<category><![CDATA[資料庫]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Web Server]]></category>
		<category><![CDATA[匯入資料]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[mariadb]]></category>
		<category><![CDATA[solr]]></category>
		<category><![CDATA[solrconfig.xml]]></category>
		<category><![CDATA[DataImportHandler]]></category>
		<category><![CDATA[Error loading class 'org.apache.solr.handler.dataimport.DataImportHandler']]></category>
		<guid isPermaLink="false">http://wazai.net/?p=3160</guid>

					<description><![CDATA[<p>好一陣子沒有用 Solr，接續前幾篇Solr 教學 (1) – 安裝篇Solr 教學 (2) – tomcat 7 以帳號密碼限制存取頁面Solr 教學 (3) – 以 Remote Address &#46;&#46;&#46;</p>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/3160/solr-%e6%95%99%e5%ad%b8-4-%e4%bd%bf%e7%94%a8-dataimport-%e5%8c%af%e5%85%a5%e8%b3%87%e6%96%99%e5%ba%ab%e8%b3%87%e6%96%99/" data-wpel-link="internal">Solr 教學 (4) – 使用 DataImport 匯入資料庫資料</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></description>
										<content:encoded><![CDATA[<p>好一陣子沒有用 Solr，接續前幾篇<br><a href="https://noter.tw/3084/solr-%e6%95%99%e5%ad%b8-1-%e5%ae%89%e8%a3%9d%e7%af%87/" target="_blank" rel="noopener noreferrer" data-wpel-link="internal">Solr 教學 (1) – 安裝篇</a><br><a href="https://noter.tw/3082/solr-%e6%95%99%e5%ad%b8-2-tomcat-7-%e4%bb%a5%e5%b8%b3%e8%99%9f%e5%af%86%e7%a2%bc%e9%99%90%e5%88%b6%e5%ad%98%e5%8f%96%e9%a0%81%e9%9d%a2/" target="_blank" rel="noopener noreferrer" data-wpel-link="internal">Solr 教學 (2) – tomcat 7 以帳號密碼限制存取頁面</a><br><a href="https://noter.tw/3094/solr-%e6%95%99%e5%ad%b8-3-%e4%bb%a5-remote-address-filter-%e9%99%90%e5%88%b6%e5%ad%98%e5%8f%96/" target="_blank" rel="noopener noreferrer" data-wpel-link="internal">Solr 教學 (3) – 以 Remote Address Filter 限制存取</a><br>這篇文章要記錄一下怎麼把 MySQL 裡面的資料匯入成 Solr 的索引檔。<br><span id="more-3160"></span>小蛙從參考資料1中發現匯入的動作好像不困難，就照著做下來，這邊也順便記錄一下可能會遇到的問題。</p>
<ol>
<li>修改 solrconfig.xml，這個是前幾篇提到 core 下面的，小蛙的目錄是 <span style="color: #ff0000;"><strong>/opt/solr/example/conf</strong></span>，這邊 db-data-config.xml 可以改成自己想要的名字，也可以直接使用相對路徑，考慮可能會有移機的狀況，還是盡可能使用相對路徑。
<div>
<div id="highlighter_747430" class="syntaxhighlighter  xml">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td class="gutter">
<div class="line number1 index0 alt2">1</div>
<div class="line number2 index1 alt1">2</div>
<div class="line number3 index2 alt2">3</div>
<div class="line number4 index3 alt1">4</div>
<div class="line number5 index4 alt2">5</div>
</td>
<td class="code">
<div class="container">
<div class="line number1 index0 alt2"><code class="xml plain">&lt;</code><code class="xml keyword">requestHandler</code> <code class="xml color1">name</code><code class="xml plain">=</code><code class="xml string">"/dataimport"</code> <code class="xml color1">class</code><code class="xml plain">=</code><code class="xml string">"org.apache.solr.handler.dataimport.DataImportHandler"</code><code class="xml plain">&gt;</code></div>
<div class="line number2 index1 alt1"><code class="xml spaces">&nbsp;&nbsp;</code><code class="xml plain">&lt;</code><code class="xml keyword">lst</code> <code class="xml color1">name</code><code class="xml plain">=</code><code class="xml string">"defaults"</code><code class="xml plain">&gt;</code></div>
<div class="line number3 index2 alt2"><code class="xml spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="xml plain">&lt;</code><code class="xml keyword">str</code> <code class="xml color1">name</code><code class="xml plain">=</code><code class="xml string">"config"</code><code class="xml plain">&gt;db-data-config.xml&lt;/</code><code class="xml keyword">str</code><code class="xml plain">&gt;</code></div>
<div class="line number4 index3 alt1"><code class="xml spaces">&nbsp;&nbsp;</code><code class="xml plain">&lt;/</code><code class="xml keyword">lst</code><code class="xml plain">&gt;</code></div>
<div class="line number5 index4 alt2"><code class="xml plain">&lt;/</code><code class="xml keyword">requestHandler</code><code class="xml plain">&gt;</code></div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</li>
<li>新增資料庫連線檔 db-data-config.xml，這個檔名是上面自己設定的，跟 solrconfig.xml 放在同一個目錄下即可。依自己的環境修改 url, user, password，以及 SQL 語法及欄位對應，這邊的 field name 是資料庫的欄位。
<div>
<div id="highlighter_291252" class="syntaxhighlighter  xml">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td class="gutter">
<div class="line number1 index0 alt2">01</div>
<div class="line number2 index1 alt1">02</div>
<div class="line number3 index2 alt2">03</div>
<div class="line number4 index3 alt1">04</div>
<div class="line number5 index4 alt2">05</div>
<div class="line number6 index5 alt1">06</div>
<div class="line number7 index6 alt2">07</div>
<div class="line number8 index7 alt1">08</div>
<div class="line number9 index8 alt2">09</div>
<div class="line number10 index9 alt1">10</div>
<div class="line number11 index10 alt2">11</div>
</td>
<td class="code">
<div class="container">
<div class="line number1 index0 alt2"><code class="xml plain">&lt;</code><code class="xml keyword">dataConfig</code><code class="xml plain">&gt;</code></div>
<div class="line number2 index1 alt1"><code class="xml spaces">&nbsp;&nbsp;</code><code class="xml plain">&lt;</code><code class="xml keyword">dataSource</code> <code class="xml color1">type</code><code class="xml plain">=</code><code class="xml string">"JdbcDataSource"</code> <code class="xml color1">driver</code><code class="xml plain">=</code><code class="xml string">"com.mysql.jdbc.Driver"</code> <code class="xml color1">url</code><code class="xml plain">=</code><code class="xml string">"jdbc:<a href="mysql://localhost:3306/test" target="_blank" rel="noopener" data-wpel-link="internal">mysql://localhost:3306/test</a>"</code> <code class="xml color1">user</code><code class="xml plain">=</code><code class="xml string">"myusername"</code> <code class="xml color1">password</code><code class="xml plain">=</code><code class="xml string">"mypassword"</code><code class="xml plain">/&gt;</code></div>
<div class="line number3 index2 alt2"><code class="xml spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="xml plain">&lt;</code><code class="xml keyword">document</code> <code class="xml color1">name</code><code class="xml plain">=</code><code class="xml string">"mydocument"</code><code class="xml plain">&gt;</code></div>
<div class="line number4 index3 alt1"><code class="xml spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="xml plain">&lt;</code><code class="xml keyword">entity</code> <code class="xml color1">name</code><code class="xml plain">=</code><code class="xml string">"item"</code> <code class="xml color1">query</code><code class="xml plain">=</code><code class="xml string">"SELECT * FROM stockTable"</code><code class="xml plain">&gt;</code></div>
<div class="line number5 index4 alt2"><code class="xml spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="xml plain">&lt;</code><code class="xml keyword">field</code> <code class="xml color1">column</code><code class="xml plain">=</code><code class="xml string">"ID"</code> <code class="xml color1">name</code><code class="xml plain">=</code><code class="xml string">"id"</code> <code class="xml plain">/&gt;</code></div>
<div class="line number6 index5 alt1"><code class="xml spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="xml plain">&lt;</code><code class="xml keyword">field</code> <code class="xml color1">column</code><code class="xml plain">=</code><code class="xml string">"SNO"</code> <code class="xml color1">name</code><code class="xml plain">=</code><code class="xml string">"no"</code> <code class="xml plain">/&gt;</code></div>
<div class="line number7 index6 alt2"><code class="xml spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="xml plain">&lt;</code><code class="xml keyword">field</code> <code class="xml color1">column</code><code class="xml plain">=</code><code class="xml string">"SNAME"</code> <code class="xml color1">name</code><code class="xml plain">=</code><code class="xml string">"name"</code> <code class="xml plain">/&gt;</code></div>
<div class="line number8 index7 alt1"><code class="xml spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="xml plain">&lt;</code><code class="xml keyword">field</code> <code class="xml color1">column</code><code class="xml plain">=</code><code class="xml string">"STYPE"</code> <code class="xml color1">name</code><code class="xml plain">=</code><code class="xml string">"type"</code> <code class="xml plain">/&gt;</code></div>
<div class="line number9 index8 alt2"><code class="xml spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="xml plain">&lt;/</code><code class="xml keyword">entity</code><code class="xml plain">&gt;</code></div>
<div class="line number10 index9 alt1"><code class="xml spaces">&nbsp;&nbsp;</code><code class="xml plain">&lt;/</code><code class="xml keyword">document</code><code class="xml plain">&gt;</code></div>
<div class="line number11 index10 alt2"><code class="xml plain">&lt;/</code><code class="xml keyword">dataConfig</code><code class="xml plain">&gt;</code></div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</li>
<li>修改 schema.xml，改成自己的設定
<div>
<div id="highlighter_199988" class="syntaxhighlighter  xml">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td class="gutter">
<div class="line number1 index0 alt2">01</div>
<div class="line number2 index1 alt1">02</div>
<div class="line number3 index2 alt2">03</div>
<div class="line number4 index3 alt1">04</div>
<div class="line number5 index4 alt2">05</div>
<div class="line number6 index5 alt1">06</div>
<div class="line number7 index6 alt2">07</div>
<div class="line number8 index7 alt1">08</div>
<div class="line number9 index8 alt2">09</div>
<div class="line number10 index9 alt1">10</div>
<div class="line number11 index10 alt2">11</div>
<div class="line number12 index11 alt1">12</div>
<div class="line number13 index12 alt2">13</div>
<div class="line number14 index13 alt1">14</div>
<div class="line number15 index14 alt2">15</div>
<div class="line number16 index15 alt1">16</div>
<div class="line number17 index16 alt2">17</div>
<div class="line number18 index17 alt1">18</div>
<div class="line number19 index18 alt2">19</div>
<div class="line number20 index19 alt1">20</div>
<div class="line number21 index20 alt2">21</div>
<div class="line number22 index21 alt1">22</div>
<div class="line number23 index22 alt2">23</div>
<div class="line number24 index23 alt1">24</div>
<div class="line number25 index24 alt2">25</div>
<div class="line number26 index25 alt1">26</div>
<div class="line number27 index26 alt2">27</div>
<div class="line number28 index27 alt1">28</div>
<div class="line number29 index28 alt2">29</div>
<div class="line number30 index29 alt1">30</div>
<div class="line number31 index30 alt2">31</div>
<div class="line number32 index31 alt1">32</div>
<div class="line number33 index32 alt2">33</div>
<div class="line number34 index33 alt1">34</div>
<div class="line number35 index34 alt2">35</div>
<div class="line number36 index35 alt1">36</div>
</td>
<td class="code">
<div class="container">
<div class="line number1 index0 alt2"><code class="xml plain">&lt;?</code><code class="xml keyword">xml</code> <code class="xml color1">version</code><code class="xml plain">=</code><code class="xml string">"1.0"</code> <code class="xml plain">?&gt;</code></div>
<div class="line number2 index1 alt1"><code class="xml comments">&lt;!--</code></div>
<div class="line number3 index2 alt2"><code class="xml spaces">&nbsp;</code><code class="xml comments">Licensed to the Apache Software Foundation (ASF) under one or more</code></div>
<div class="line number4 index3 alt1"><code class="xml spaces">&nbsp;</code><code class="xml comments">contributor license agreements. See the NOTICE file distributed with</code></div>
<div class="line number5 index4 alt2"><code class="xml spaces">&nbsp;</code><code class="xml comments">this work for additional information regarding copyright ownership.</code></div>
<div class="line number6 index5 alt1"><code class="xml spaces">&nbsp;</code><code class="xml comments">The ASF licenses this file to You under the Apache License, Version 2.0</code></div>
<div class="line number7 index6 alt2"><code class="xml spaces">&nbsp;</code><code class="xml comments">(the "License"); you may not use this file except in compliance with</code></div>
<div class="line number8 index7 alt1"><code class="xml spaces">&nbsp;</code><code class="xml comments">the License. You may obtain a copy of the License at</code></div>
<div class="line number9 index8 alt2"><code class="xml comments"><a href="http://www.apache.org/licenses/LICENSE-2.0" target="_blank" rel="noopener nofollow external noreferrer" data-wpel-link="external" class="wpel-icon-right">http://www.apache.org/licenses/LICENSE-2.0<span class="wpel-icon wpel-image wpel-icon-6"></span></a></code></div>
<div class="line number10 index9 alt1"><code class="xml comments">Unless required by applicable law or agreed to in writing, software</code></div>
<div class="line number11 index10 alt2"><code class="xml spaces">&nbsp;</code><code class="xml comments">distributed under the License is distributed on an "AS IS" BASIS,</code></div>
<div class="line number12 index11 alt1"><code class="xml spaces">&nbsp;</code><code class="xml comments">WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</code></div>
<div class="line number13 index12 alt2"><code class="xml spaces">&nbsp;</code><code class="xml comments">See the License for the specific language governing permissions and</code></div>
<div class="line number14 index13 alt1"><code class="xml spaces">&nbsp;</code><code class="xml comments">limitations under the License.</code></div>
<div class="line number15 index14 alt2"><code class="xml comments">--&gt;</code></div>
<div class="line number16 index15 alt1"><code class="xml plain">&lt;</code><code class="xml keyword">schema</code> <code class="xml color1">name</code><code class="xml plain">=</code><code class="xml string">"example core zero"</code> <code class="xml color1">version</code><code class="xml plain">=</code><code class="xml string">"1.1"</code><code class="xml plain">&gt;</code></div>
<div class="line number17 index16 alt2"><code class="xml spaces">&nbsp;&nbsp;</code><code class="xml plain">&lt;</code><code class="xml keyword">types</code><code class="xml plain">&gt;</code></div>
<div class="line number18 index17 alt1"><code class="xml spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="xml plain">&lt;</code><code class="xml keyword">fieldtype</code> <code class="xml color1">name</code><code class="xml plain">=</code><code class="xml string">"string"</code> <code class="xml color1">class</code><code class="xml plain">=</code><code class="xml string">"solr.StrField"</code> <code class="xml color1">sortMissingLast</code><code class="xml plain">=</code><code class="xml string">"true"</code> <code class="xml color1">omitNorms</code><code class="xml plain">=</code><code class="xml string">"true"</code><code class="xml plain">/&gt;</code></div>
<div class="line number19 index18 alt2"><code class="xml spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="xml plain">&lt;</code><code class="xml keyword">fieldType</code> <code class="xml color1">name</code><code class="xml plain">=</code><code class="xml string">"long"</code> <code class="xml color1">class</code><code class="xml plain">=</code><code class="xml string">"solr.TrieLongField"</code> <code class="xml color1">precisionStep</code><code class="xml plain">=</code><code class="xml string">"0"</code> <code class="xml color1">positionIncrementGap</code><code class="xml plain">=</code><code class="xml string">"0"</code><code class="xml plain">/&gt;</code></div>
<div class="line number20 index19 alt1"><code class="xml spaces">&nbsp;&nbsp;</code><code class="xml plain">&lt;/</code><code class="xml keyword">types</code><code class="xml plain">&gt;</code></div>
<div class="line number21 index20 alt2"><code class="xml spaces">&nbsp;&nbsp;</code><code class="xml plain">&lt;</code><code class="xml keyword">fields</code><code class="xml plain">&gt;</code></div>
<div class="line number22 index21 alt1"><code class="xml spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="xml comments">&lt;!-- general --&gt;</code></div>
<div class="line number23 index22 alt2"><code class="xml spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="xml plain">&lt;</code><code class="xml keyword">field</code> <code class="xml color1">name</code><code class="xml plain">=</code><code class="xml string">"id"</code> <code class="xml color1">type</code><code class="xml plain">=</code><code class="xml string">"string"</code> <code class="xml color1">indexed</code><code class="xml plain">=</code><code class="xml string">"true"</code> <code class="xml color1">stored</code><code class="xml plain">=</code><code class="xml string">"true"</code> <code class="xml color1">multiValued</code><code class="xml plain">=</code><code class="xml string">"false"</code> <code class="xml color1">required</code><code class="xml plain">=</code><code class="xml string">"true"</code><code class="xml plain">/&gt;</code></div>
<div class="line number24 index23 alt1"><code class="xml spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="xml plain">&lt;</code><code class="xml keyword">field</code> <code class="xml color1">name</code><code class="xml plain">=</code><code class="xml string">"no"</code> <code class="xml color1">type</code><code class="xml plain">=</code><code class="xml string">"string"</code> <code class="xml color1">indexed</code><code class="xml plain">=</code><code class="xml string">"true"</code> <code class="xml color1">stored</code><code class="xml plain">=</code><code class="xml string">"true"</code> <code class="xml color1">multiValued</code><code class="xml plain">=</code><code class="xml string">"false"</code> <code class="xml plain">/&gt;</code></div>
<div class="line number25 index24 alt2"><code class="xml spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="xml plain">&lt;</code><code class="xml keyword">field</code> <code class="xml color1">name</code><code class="xml plain">=</code><code class="xml string">"name"</code> <code class="xml color1">type</code><code class="xml plain">=</code><code class="xml string">"string"</code> <code class="xml color1">indexed</code><code class="xml plain">=</code><code class="xml string">"true"</code> <code class="xml color1">stored</code><code class="xml plain">=</code><code class="xml string">"true"</code> <code class="xml color1">multiValued</code><code class="xml plain">=</code><code class="xml string">"false"</code> <code class="xml plain">/&gt;</code></div>
<div class="line number26 index25 alt1"><code class="xml spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="xml plain">&lt;</code><code class="xml keyword">field</code> <code class="xml color1">name</code><code class="xml plain">=</code><code class="xml string">"type"</code> <code class="xml color1">type</code><code class="xml plain">=</code><code class="xml string">"string"</code> <code class="xml color1">indexed</code><code class="xml plain">=</code><code class="xml string">"true"</code> <code class="xml color1">stored</code><code class="xml plain">=</code><code class="xml string">"true"</code> <code class="xml color1">multiValued</code><code class="xml plain">=</code><code class="xml string">"false"</code> <code class="xml plain">/&gt;</code></div>
<div class="line number27 index26 alt2"><code class="xml spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="xml comments">&lt;!-- 這個 _version_ 如果拿掉會報錯 --&gt;</code></div>
<div class="line number28 index27 alt1"><code class="xml spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="xml plain">&lt;</code><code class="xml keyword">field</code> <code class="xml color1">name</code><code class="xml plain">=</code><code class="xml string">"_version_"</code> <code class="xml color1">type</code><code class="xml plain">=</code><code class="xml string">"long"</code> <code class="xml color1">indexed</code><code class="xml plain">=</code><code class="xml string">"true"</code> <code class="xml color1">stored</code><code class="xml plain">=</code><code class="xml string">"true"</code><code class="xml plain">/&gt;</code></div>
<div class="line number29 index28 alt2"><code class="xml spaces">&nbsp;&nbsp;</code><code class="xml plain">&lt;/</code><code class="xml keyword">fields</code><code class="xml plain">&gt;</code></div>
<div class="line number30 index29 alt1"><code class="xml spaces">&nbsp;&nbsp;</code><code class="xml comments">&lt;!-- 主鍵 --&gt;</code></div>
<div class="line number31 index30 alt2"><code class="xml spaces">&nbsp;&nbsp;</code><code class="xml plain">&lt;</code><code class="xml keyword">uniqueKey</code><code class="xml plain">&gt;id&lt;/</code><code class="xml keyword">uniqueKey</code><code class="xml plain">&gt;</code></div>
<div class="line number32 index31 alt1"><code class="xml spaces">&nbsp;&nbsp;</code><code class="xml comments">&lt;!-- 沒有設定欄位時的預設搜尋 --&gt;</code></div>
<div class="line number33 index32 alt2"><code class="xml spaces">&nbsp;&nbsp;</code><code class="xml plain">&lt;</code><code class="xml keyword">defaultSearchField</code><code class="xml plain">&gt;name&lt;/</code><code class="xml keyword">defaultSearchField</code><code class="xml plain">&gt;</code></div>
<div class="line number34 index33 alt1"><code class="xml spaces">&nbsp;&nbsp;</code><code class="xml comments">&lt;!-- 空白串接關鍵字時預設使用 OR 或是 AND --&gt;</code></div>
<div class="line number35 index34 alt2"><code class="xml spaces">&nbsp;&nbsp;</code><code class="xml plain">&lt;</code><code class="xml keyword">solrQueryParser</code> <code class="xml color1">defaultOperator</code><code class="xml plain">=</code><code class="xml string">"OR"</code><code class="xml plain">/&gt;</code></div>
<div class="line number36 index35 alt1"><code class="xml plain">&lt;/</code><code class="xml keyword">schema</code><code class="xml plain">&gt;</code></div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</li>
<li>這時候因為連結的是 MySQL，所以需要 mysql connector，假設使用的是 oracle 就要下載 ojdbc 來使用，MySQL Connector/J 到 <a href="http://dev.mysql.com/downloads/connector/j/" target="_blank" rel="noopener noreferrer nofollow external" data-wpel-link="external" class="wpel-icon-right">MySQL 官網<span class="wpel-icon wpel-image wpel-icon-6"></span></a>下載。選擇&nbsp;<span style="color: #0000ff;"><strong>Platform Independent</strong> </span>後下載壓縮檔，解壓縮後只需要裡面的&nbsp;<span style="color: #ff0000;"><strong>mysql-connector-java-x.x.xx-bin.jar</strong></span> 這個檔案，把這個檔案放在 solr lib 目錄下，以小蛙的環境為例，是直接放在<span style="color: #0000ff;"><strong> /var/lib/tomcat7/webapps/solr/WEB-INF/lib</strong> </span>目錄下，當 tomcat 啟動 solr 專案時就會自動被載入。</li>
<li>到這邊似乎都沒問題，在小蛙準備要重新啟動 tomcat 讓設定生效的時候，卻一直出現這樣子的錯誤
<div>
<div id="highlighter_65283" class="syntaxhighlighter  bash">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td class="gutter">
<div class="line number1 index0 alt2">1</div>
<div class="line number2 index1 alt1">2</div>
<div class="line number3 index2 alt2">3</div>
<div class="line number4 index3 alt1">4</div>
<div class="line number5 index4 alt2">5</div>
<div class="line number6 index5 alt1">6</div>
<div class="line number7 index6 alt2">7</div>
<div class="line number8 index7 alt1">8</div>
</td>
<td class="code">
<div class="container">
<div class="line number1 index0 alt2"><code class="bash plain">org.apache.solr.common.SolrException: RequestHandler init failure</code></div>
<div class="line number2 index1 alt1"><code class="bash plain">...</code></div>
<div class="line number3 index2 alt2"><code class="bash plain">Caused by: org.apache.solr.common.SolrException: RequestHandler init failure</code></div>
<div class="line number4 index3 alt1"><code class="bash plain">...</code></div>
<div class="line number5 index4 alt2"><code class="bash plain">Caused by: org.apache.solr.common.SolrException: Error loading class </code><code class="bash string">'org.apache.solr.handler.dataimport.DataImportHandler'</code></div>
<div class="line number6 index5 alt1"><code class="bash plain">...</code></div>
<div class="line number7 index6 alt2"><code class="bash plain">Caused by: java.lang.ClassNotFoundException: org.apache.solr.handler.dataimport.DataImportHandler</code></div>
<div class="line number8 index7 alt1"><code class="bash plain">...</code></div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>錯誤真的要看清楚，小蛙當時只有看到前三個，一直以為是設定檔哪裡出錯，一個一個找了好久，才發現第四個錯誤 java.lang.ClassNotFoundException，上面這些錯誤都是因為根本找不到 org.apache.solr.handler.dataimport.DataImportHandler 引起的，在網路上搜尋了一下，有一些網友分享這個檔案存在於下載回來 solr 的 dist 目錄中，可以透過在 solrconfig.xml 中設定 lib 的讀取路徑，小蛙則是把&nbsp;solr-dataimporthandler-4.5.0.jar,&nbsp;solr-dataimporthandler-extras-4.5.0.jar 這兩個 jar 直接複製到&nbsp;<span style="color: #0000ff;"><strong>/var/lib/tomcat7/webapps/solr/WEB-INF/lib </strong></span>目錄中。</p>
</li>
<li>重新啟動 tomcat 後，solr 畫面可以正常進入，左邊 Core Selector 下拉選單選到自己的 Core 後再選擇 DataImport，
<div>
<div id="highlighter_543646" class="syntaxhighlighter  text">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td class="gutter">
<div class="line number1 index0 alt2">1</div>
<div class="line number2 index1 alt1">2</div>
<div class="line number3 index2 alt2">3</div>
<div class="line number4 index3 alt1">4</div>
<div class="line number5 index4 alt2">5</div>
<div class="line number6 index5 alt1">6</div>
<div class="line number7 index6 alt2">7</div>
</td>
<td class="code">
<div class="container">
<div class="line number1 index0 alt2"><code class="text plain">// full-import : 全部匯入；delta-import : 只匯入變更部分</code></div>
<div class="line number2 index1 alt1"><code class="text plain">Command -&gt; full-import</code></div>
<div class="line number3 index2 alt2"><code class="text plain">// verbose 打勾可以看到詳細訊息</code></div>
<div class="line number4 index3 alt1"><code class="text plain">v Verbose</code></div>
<div class="line number5 index4 alt2"><code class="text plain">// 選擇剛剛設定檔新增的名稱</code></div>
<div class="line number6 index5 alt1"><code class="text plain">Entity -&gt; item</code></div>
<div class="line number7 index6 alt2"><code class="text plain">執行 Execute</code></div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>完成後可以看到「<strong><span style="color: #008000;">Indexing completed. Added/Updated: 12701 documents. Deleted 0 documents. (Duration: 14s)</span></strong>」</p>
</li>
<li>大功告成！檢查看看是否正常匯入，點選左邊的 Query，測試 Execute Query 看看資料正不正確。</li>
</ol>
<p><strong>參考資料：</strong></p>
<ol>
<li>[Solr] Solr 學習筆記(五) 設定 Data Import Handler 直接從 MySQL 把資料匯入 @&nbsp;長島冰茶的工程師筆記<br>
<address><a href="http://wbkuo.pixnet.net/blog/post/156125771-%5Bsolr%5D-solr-%E5%AD%B8%E7%BF%92%E7%AD%86%E8%A8%98(%E4%BA%94)-%E8%A8%AD%E5%AE%9A-data-import-handler-%E7%9B%B4" target="_blank" rel="noopener noreferrer nofollow external" data-wpel-link="external" class="wpel-icon-right">http://wbkuo.pixnet.net/blog/post/156125771-%5Bsolr%5D-solr-%E5%AD%B8%E7%BF%92%E7%AD%86%E8%A8%98(%E4%BA%94)-%E8%A8%AD%E5%AE%9A-data-import-handler-%E7%9B%B4<span class="wpel-icon wpel-image wpel-icon-6"></span></a></address>
</li>
</ol>


<p><strong>Solr 系列文章：</strong></p>



<ul class="my-li bg-darkblue wp-block-list"><li><a href="https://noter.tw/3084/solr-%e6%95%99%e5%ad%b8-1-%e5%ae%89%e8%a3%9d%e7%af%87/" data-wpel-link="internal">Solr 教學 (1) &#8211; 安裝篇</a></li><li><a href="https://noter.tw/3082/solr-%e6%95%99%e5%ad%b8-2-tomcat-7-%e4%bb%a5%e5%b8%b3%e8%99%9f%e5%af%86%e7%a2%bc%e9%99%90%e5%88%b6%e5%ad%98%e5%8f%96%e9%a0%81%e9%9d%a2/" data-wpel-link="internal">Solr 教學 (2) – tomcat 7 以帳號密碼限制存取頁面</a></li><li><a href="https://noter.tw/3094/solr-%e6%95%99%e5%ad%b8-3-%e4%bb%a5-remote-address-filter-%e9%99%90%e5%88%b6%e5%ad%98%e5%8f%96/" data-wpel-link="internal">Solr 教學 (3) – 以 Remote Address Filter 限制存取</a></li><li><a href="https://noter.tw/3160/solr-%e6%95%99%e5%ad%b8-4-%e4%bd%bf%e7%94%a8-dataimport-%e5%8c%af%e5%85%a5%e8%b3%87%e6%96%99%e5%ba%ab%e8%b3%87%e6%96%99/" data-wpel-link="internal">Solr 教學 (4) – 使用 DataImport 匯入資料庫資料</a></li><li><a href="https://noter.tw/3170/solr-%e6%95%99%e5%ad%b8-5-%e5%bb%ba%e7%ab%8b%e6%96%b0%e7%9a%84-core/" data-wpel-link="internal">Solr 教學 (5) – 建立新的 Core</a></li></ul>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/3160/solr-%e6%95%99%e5%ad%b8-4-%e4%bd%bf%e7%94%a8-dataimport-%e5%8c%af%e5%85%a5%e8%b3%87%e6%96%99%e5%ba%ab%e8%b3%87%e6%96%99/" data-wpel-link="internal">Solr 教學 (4) – 使用 DataImport 匯入資料庫資料</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://noter.tw/3160/solr-%e6%95%99%e5%ad%b8-4-%e4%bd%bf%e7%94%a8-dataimport-%e5%8c%af%e5%85%a5%e8%b3%87%e6%96%99%e5%ba%ab%e8%b3%87%e6%96%99/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Java 連結 MySQL</title>
		<link>https://noter.tw/2883/java-%e9%80%a3%e7%b5%90-mysql/</link>
					<comments>https://noter.tw/2883/java-%e9%80%a3%e7%b5%90-mysql/#respond</comments>
		
		<dc:creator><![CDATA[黃小蛙]]></dc:creator>
		<pubDate>Mon, 03 Jun 2013 15:17:32 +0000</pubDate>
				<category><![CDATA[資料庫]]></category>
		<category><![CDATA[一般程式]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[mariadb]]></category>
		<category><![CDATA[connector]]></category>
		<guid isPermaLink="false">http://wazai.net/?p=2883</guid>

					<description><![CDATA[<p>在寫程式需要連結到資料庫的時候，程式中必須要先有 Connector 及 Driver，MySQL 叫做 Connector/J，Oracle 則叫做 OJDBC。這篇文章是從舊部落格轉發過來，純粹當&#46;&#46;&#46;</p>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/2883/java-%e9%80%a3%e7%b5%90-mysql/" data-wpel-link="internal">Java 連結 MySQL</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></description>
										<content:encoded><![CDATA[<p>在寫程式需要連結到資料庫的時候，程式中必須要先有 Connector 及 Driver，MySQL 叫做 Connector/J，Oracle 則叫做 OJDBC。這篇文章是從舊部落格轉發過來，純粹當做文件記錄。<br>
<span id="more-2883"></span><br>
1. 到<a href="http://dev.mysql.com/downloads/connector/j/5.0.html" target="_blank" rel="noopener nofollow external noreferrer" data-wpel-link="external" class="wpel-icon-right">http://dev.mysql.com/downloads/connector/j/5.0.html<span class="wpel-icon wpel-image wpel-icon-6"></span></a>下載connector。 (看是用哪一版本自行更換)<br>
2. 解壓縮後，將 mysql-connector-java-5.0.4-bin.jar 複製到 jre 安裝目錄下的 \lib\ext<br>
例如：C:\Program Files\Java\jre1.5.0_09\lib\ext<br>
<span style="color: #ff0000;"><span style="color: #000000;">3.&nbsp;</span><strong>set classpath=.;C:\Program Files\Java\jre1.5.0_09\lib\ext\mysql-connector-java-5.0.4-bin.jar&nbsp;</strong><span style="color: #000000;">(這一步應該是不需要，只要在 IDE 中把 jar import 進專案就可以了，例如：eclipse 裡面設定 Java Build Path)</span></span><br>
4. 程式碼部分
</p>
<pre class="brush: java; gutter: true">String jdbc = "com.mysql.jdbc.Driver";               // 使用的 Driver
String url = "jdbc:mysql://localhost/資料庫名稱";     // 連結URL
String user = "帳號";
String pass = "密碼";
Class.forName(jdbc);
Connection con = DriverManager.getConnection(url,user,pass);</pre>
<p><span style="color: #ff00ff;">有件事要注意：電腦中同時有 jdk 及 jre，set classpath 及 connector 複製的路徑，當時設定 jdk 中的 jre 路徑， 仍沒辦法使用，改成 jre 才可使用。</span></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/2883/java-%e9%80%a3%e7%b5%90-mysql/" data-wpel-link="internal">Java 連結 MySQL</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://noter.tw/2883/java-%e9%80%a3%e7%b5%90-mysql/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>MySQL 排程(Event Scheduler)</title>
		<link>https://noter.tw/2306/mysql%e6%8e%92%e7%a8%8bevent-scheduler/</link>
					<comments>https://noter.tw/2306/mysql%e6%8e%92%e7%a8%8bevent-scheduler/#comments</comments>
		
		<dc:creator><![CDATA[黃小蛙]]></dc:creator>
		<pubDate>Sat, 29 Sep 2012 06:12:19 +0000</pubDate>
				<category><![CDATA[資料庫]]></category>
		<category><![CDATA[alter]]></category>
		<category><![CDATA[create]]></category>
		<category><![CDATA[delete]]></category>
		<category><![CDATA[event_scheduler]]></category>
		<category><![CDATA[general_log]]></category>
		<category><![CDATA[排程]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[mariadb]]></category>
		<guid isPermaLink="false">http://wazai.net/?p=2306</guid>

					<description><![CDATA[<p>一段陣子沒清vps上的東西，同步到Dropbox的檔案瞬間爆增，每隔一段時間都要定期手動清一下，今天一有時間索性加入crontab -e排程一勞永逸，還有另外一個問題，由於我的股票精算師只留七日內的文&#46;&#46;&#46;</p>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/2306/mysql%e6%8e%92%e7%a8%8bevent-scheduler/" data-wpel-link="internal">MySQL 排程(Event Scheduler)</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></description>
										<content:encoded><![CDATA[
<p>一段陣子沒清vps上的東西，<strong><a rel="noreferrer noopener nofollow external" href="http://wazai.net/2166" target="_blank" data-wpel-link="external" class="wpel-icon-right">同步到Dropbox<span class="wpel-icon wpel-image wpel-icon-6"></span></a></strong>的檔案瞬間爆增，每隔一段時間都要定期手動清一下，今天一有時間索性<strong><a rel="noreferrer noopener nofollow external" href="http://wazai.net/2000" target="_blank" data-wpel-link="external" class="wpel-icon-right">加入crontab -e排程<span class="wpel-icon wpel-image wpel-icon-6"></span></a></strong>一勞永逸，還有另外一個問題，由於我的股票精算師只留七日內的文章，也面臨相同的問題，為了不想讓資料庫文章量太大，造成效能低落，因此希望可以把定期清理資料庫的動作改為自動化，要自動化有很多種做法，也可以用上面提到的 crontab -e 加入排程，再透過 php 或其他方式執行清理的 SQL，Google一下發現 MySQL 有 <strong><a rel="noreferrer noopener nofollow external" href="http://dev.mysql.com/doc/refman/5.1/en/create-event.html" target="_blank" data-wpel-link="external" class="wpel-icon-right">Event<span class="wpel-icon wpel-image wpel-icon-6"></span></a></strong> 這個排程的東東，就來試用看看囉！ </p>



<span id="more-2306"></span>



<p>小蛙就直接記錄自己做了哪些事情，從<strong><a rel="noreferrer noopener nofollow external" href="http://dev.mysql.com/doc/refman/5.1/en/create-event.html" target="_blank" data-wpel-link="external" class="wpel-icon-right">官網<span class="wpel-icon wpel-image wpel-icon-6"></span></a></strong>上面以及參考資料中查到建立 event 的基本方法 </p>



<pre class="wp-block-preformatted wp-block-syntaxhighlighter-code">CREATE
    [DEFINER = { user | CURRENT_USER }]
    EVENT
    [IF NOT EXISTS]
    event_name
    ON SCHEDULE schedule
    [ON COMPLETION [NOT] PRESERVE]
    [ENABLE | DISABLE | DISABLE ON SLAVE]
    [COMMENT 'comment']
    DO event_body;
schedule:
    AT timestamp [+ INTERVAL interval] ...
    | EVERY interval
    [STARTS timestamp [+ INTERVAL interval] ...]
    [ENDS timestamp [+ INTERVAL interval] ...]
interval:
    quantity {YEAR | QUARTER | MONTH | DAY | HOUR | MINUTE |
    WEEK | SECOND | YEAR_MONTH | DAY_HOUR | DAY_MINUTE |
    DAY_SECOND | HOUR_MINUTE | HOUR_SECOND | MINUTE_SECOND}</pre>



<h2 class="para wp-block-heading">建立 MySQL Event</h2>



<p>看起來有點複雜，用下面的簡單例子直接套用(<strong><a rel="noreferrer noopener nofollow external" href="http://blog.sina.com.cn/s/blog_548668f1010007pu.html" target="_blank" data-wpel-link="external" class="wpel-icon-right">這個部落格<span class="wpel-icon wpel-image wpel-icon-6"></span></a></strong>有非常詳細的說明)，小蛙要的功能是「<strong>馬上把7天以前的文章刪除，並且每天執行一次刪除動作</strong>」，這樣就簡單多了，不需要用到上面複雜的說明。 </p>



<pre class="wp-block-preformatted wp-block-syntaxhighlighter-code">CREATE EVENT my_event
    ON SCHEDULE EVERY 1 DAY STARTS CURRENT_TIMESTAMP
&nbsp;&nbsp;&nbsp;&nbsp;DO&nbsp;
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DELETE FROM `mytable` WHERE `publish_date` &lt; DATE_SUB(now(), INTERVAL 7 DAY)</pre>



<p>Line 1: 建立一個叫做 my_event 的事件<br>Line 2: 從現在開始執行一次後，每天都會執行一次<br>Line 3: 要做的動作是刪除 mytable 中，小於現在時間減掉七天(也就是七天前)的資料 </p>



<p>到這邊看起來似乎很簡單，但小蛙為了測試把時間設定成 5 分鐘，遲遲不見 my_event 去做相對應的工作 &gt;&lt;。 </p>



<h2 class="para wp-block-heading">開啟 MySQL log (非必需) </h2>



<p>這邊順便紀錄一下把 MySQL log 打開的過程(參考自<strong><a rel="noreferrer noopener nofollow external" href="http://blog.permastyle.com/2010/12/mysql-%E5%95%9F%E7%94%A8query-log-%E8%88%87-slow-query-log%E5%8A%9F%E8%83%BD.html" target="_blank" data-wpel-link="external" class="wpel-icon-right">這裡<span class="wpel-icon wpel-image wpel-icon-6"></span></a></strong>)</p>



<p>在 /etc/mysql/my.cnf 中的 [mysqld] 區塊內加入 </p>



<pre class="wp-block-preformatted wp-block-syntaxhighlighter-code">general_log_file = /var/log/mysql.log
general_log = 0</pre>



<p>原本被註解掉的話，把他打開，把general_log = 0 改成 1，到上述路徑檢查是否有該 log 檔案，如果沒有的話，建立一個 </p>



<pre class="wp-block-preformatted wp-block-syntaxhighlighter-code">touch /var/log/mysqld.log
chown mysql.mysql /var/log/mysqld.log</pre>



<p>為了避免 mysql 沒有權限寫入該檔案，順便把群組跟所有者改成 mysql，重新啟動 mysql 就可以看到 log 了。這邊要注意的是如果一天的流量很高的話，log 成長的速度會非常快速(會記錄每一條執行的SQL)。 </p>



<h2 class="para wp-block-heading">設定 MySQL Event Scheduler</h2>



<p>很多部落格包括官網提到開啟 event_scheduler 的方法，先利用以下指令檢查是否已開啟 </p>



<pre class="wp-block-preformatted wp-block-syntaxhighlighter-code">SELECT @@event_scheduler;</pre>



<p>如果是 OFF 的話，就用以下指令來開啟 </p>



<pre class="wp-block-preformatted wp-block-syntaxhighlighter-code">SET GLOBAL event_scheduler := 1;</pre>



<p>小蛙開啟了，但是這邊有一個很大的問題，下次去看的時候卻又是 OFF &#8230; 後來發現應該是因為重新啟動後，又會變回 OFF，這篇文章提到可以直接在 /etc/mysql/my.cnf 中直接設定。 </p>



<pre class="wp-block-preformatted wp-block-syntaxhighlighter-code">event_scheduler = ON</pre>



<p>一定要放在 [mysqld] 區塊中。小蛙一開始放在最底下，結果完全毫無動靜 &gt;&lt;，停止 Event Scheduler </p>



<pre class="wp-block-preformatted wp-block-syntaxhighlighter-code">ALTER EVENT my_event DISABLE;</pre>



<p>修改 Scheduler </p>



<pre class="wp-block-preformatted wp-block-syntaxhighlighter-code">ALTER
    [DEFINER = { user | CURRENT_USER }]
    EVENT event_name
    [ON SCHEDULE schedule]
    [ON COMPLETION [NOT] PRESERVE]
    [RENAME TO new_event_name]
    [ENABLE | DISABLE | DISABLE ON SLAVE]
    [COMMENT 'comment']
    [DO event_body]</pre>



<p>顯示所有 Events </p>



<pre class="wp-block-preformatted wp-block-syntaxhighlighter-code">show events;</pre>



<p>顯示詳細 Events 內容 </p>



<pre class="wp-block-preformatted wp-block-syntaxhighlighter-code">SELECT * FROM INFORMATION_SCHEMA.EVENTS&nbsp;WHERE EVENT_NAME='mytable';</pre>



<p>刪除 Events </p>



<pre class="wp-block-preformatted wp-block-syntaxhighlighter-code">DELETE FROM mysql.event&nbsp;WHERE db = 'mydb' AND name = 'mytable';</pre>



<h2 class="para wp-block-heading">參考資料</h2>



<ol class="my-li bg-darkblue wp-block-list"><li>修改&nbsp;Event Scheduler @&nbsp;MySQL官網<br><a href="http://dev.mysql.com/doc/refman/5.1/en/alter-event.html" target="_blank" rel="noopener nofollow external noreferrer" data-wpel-link="external" class="wpel-icon-right">http://dev.mysql.com/doc/refman/5.1/en/alter-event.html<span class="wpel-icon wpel-image wpel-icon-6"></span></a></li><li>建立 Event Scheduler @&nbsp;MySQL官網<br><a href="http://dev.mysql.com/doc/refman/5.1/en/create-event.html" target="_blank" rel="noopener nofollow external noreferrer" data-wpel-link="external" class="wpel-icon-right">http://dev.mysql.com/doc/refman/5.1/en/create-event.html<span class="wpel-icon wpel-image wpel-icon-6"></span></a></li><li>MySQL: 事件排程器 (Event Scheduler) @&nbsp;Wax Way<br><a href="http://waxway.blogspot.tw/2011/12/mysql-event-schedule.html" target="_blank" rel="noreferrer noopener nofollow external" data-wpel-link="external" class="wpel-icon-right">http://waxway.blogspot.tw/2011/12/mysql-event-schedule.html<span class="wpel-icon wpel-image wpel-icon-6"></span></a></li><li>Enabling the MySQL Event Scheduler @&nbsp;LOGZILLA<br><a href="http://forum.logzilla.pro/index.php?topic=71.0" target="_blank" rel="noreferrer noopener nofollow external" data-wpel-link="external" class="wpel-icon-right">http://forum.logzilla.pro/index.php?topic=71.0<span class="wpel-icon wpel-image wpel-icon-6"></span></a></li><li>mysql event&#8212;-语法之CREATE EVENT @&nbsp;星期八的博客<br><a href="http://blog.sina.com.cn/s/blog_548668f1010007pu.html" target="_blank" rel="noreferrer noopener nofollow external" data-wpel-link="external" class="wpel-icon-right">http://blog.sina.com.cn/s/blog_548668f1010007pu.html<span class="wpel-icon wpel-image wpel-icon-6"></span></a></li><li>MySQL日期函數應用 &#8211; @&nbsp;No Money No Honey<br>h<a href="http://blog.infinity.idv.tw/index.php/2009/12/16/mysql%E6%97%A5%E6%9C%9F%E5%87%BD%E6%95%B8%E6%87%89%E7%94%A8" target="_blank" rel="noreferrer noopener nofollow external" data-wpel-link="external" class="wpel-icon-right">ttp://blog.infinity.idv.tw/index.php/2009/12/16/mysql%E6%97%A5%E6%9C%9F%E5%87%BD%E6%95%B8%E6%87%89%E7%94%A8<span class="wpel-icon wpel-image wpel-icon-6"></span></a></li><li>[MySQL] 啟用query log 與 slow query log功能 @&nbsp;Allen開放源碼研究室<br><a href="http://blog.permastyle.com/2010/12/mysql-%E5%95%9F%E7%94%A8query-log-%E8%88%87-slow-query-log%E5%8A%9F%E8%83%BD.html" target="_blank" rel="noopener nofollow external noreferrer" data-wpel-link="external" class="wpel-icon-right">http://blog.permastyle.com/2010/12/mysql-%E5%95%9F%E7%94%A8query-log-%E8%88%87-slow-query-log%E5%8A%9F%E8%83%BD.html<span class="wpel-icon wpel-image wpel-icon-6"></span></a></li></ol>



<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/2306/mysql%e6%8e%92%e7%a8%8bevent-scheduler/" data-wpel-link="internal">MySQL 排程(Event Scheduler)</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://noter.tw/2306/mysql%e6%8e%92%e7%a8%8bevent-scheduler/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
