<?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/database/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>資料庫 &#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[database]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[讀取 json 欄位]]></category>
		<category><![CDATA[JSON_EXTRACT]]></category>
		<category><![CDATA[JSON 欄位排序]]></category>
		<category><![CDATA[搜尋 JSON]]></category>
		<category><![CDATA[JSON KEY]]></category>
		<category><![CDATA[查詢條件]]></category>
		<category><![CDATA[排序條件]]></category>
		<category><![CDATA[mariadb]]></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[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>
		<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>
		<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>修改 Oracle Sequence</title>
		<link>https://noter.tw/11178/%e4%bf%ae%e6%94%b9-oracle-sequence/</link>
					<comments>https://noter.tw/11178/%e4%bf%ae%e6%94%b9-oracle-sequence/#respond</comments>
		
		<dc:creator><![CDATA[黃小蛙]]></dc:creator>
		<pubDate>Sun, 16 Apr 2023 09:35:07 +0000</pubDate>
				<category><![CDATA[資料庫]]></category>
		<category><![CDATA[Oracle 序列]]></category>
		<category><![CDATA[Oracle INCREMENT]]></category>
		<category><![CDATA[如何修改 Sequence]]></category>
		<category><![CDATA[修改 Sequence 的方法]]></category>
		<category><![CDATA[Oracle Sequence]]></category>
		<category><![CDATA[Oracle last_number]]></category>
		<category><![CDATA[修改 Oracle 序列]]></category>
		<category><![CDATA[modify oracle sequence last number]]></category>
		<category><![CDATA[.nextval]]></category>
		<category><![CDATA[last_number]]></category>
		<guid isPermaLink="false">https://noter.tw/?p=11178</guid>

					<description><![CDATA[<p>不同於 MySQL 非常方便的 Auto Increment，Oracle 中必須自己建立及管理 Sequence 來記錄目前 Table 的序號，這篇文章記錄如何透過指令修改 Oracle Sequence。</p>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/11178/%e4%bf%ae%e6%94%b9-oracle-sequence/" data-wpel-link="internal">修改 Oracle Sequence</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></description>
										<content:encoded><![CDATA[
<p>不同於 MySQL 非常方便的 Auto Increment，Oracle 中必須自己建立及管理 Sequence 來記錄目前 Table 的序號，這篇文章記錄如何透過指令修改 Oracle Sequence。</p>



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



<p>小蛙需要修改一個既有的 Sequence，之前差一些些的話，可以偷懶直接透過 .nextval 取得下一個 sequence 達到調整的效果</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 XXX_SEQ.nextval FROM DUAL;</pre>



<p>但這次要調整三萬多個序號就沒辦法這樣做了，透過 <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">Oracle SQL Developer</a> 修改的話又跳出嚇人的警告畫面，因此還是乖乖採取保守方式來調整。</p>



<h2 class="para wp-block-heading" id="修改-increment">修改 INCREMENT</h2>



<p>Oracle 的 Sequence 運作會有一個遞增量 (increment)，小蛙遇到的狀況需要一次調整 30000，以下指令可以先將該 sequence 的遞增量修改成 30000，也就是每一次觸發 nextval 就會增加 30000。</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="">ALTER SEQUENCE XXX_SEQ INCREMENT BY 30000;</pre>



<h2 class="para wp-block-heading" id="取得下一個號碼">取得下一個號碼</h2>



<p>透過 .nextval 取得下一個號碼，可以發現假設原本序號是 30，遞增量是 1，那 .nextval 就會是 31；我們把遞增量改成 30000 之後，原本序號是 31，取得 .nextval 時就會得到 30031。</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">SELECT XXX_SEQ.nextval FROM DUAL;</pre>



<h2 class="para wp-block-heading" id="恢復-increment">恢復 INCREMENT</h2>



<p>最後把遞增量改回 1 就完成了。</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="">ALTER SEQUENCE XXX_SEQ INCREMENT BY 1;</pre>



<p>可以透過以下語法看到目前 Sequence 的 last_number 是多少</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 last_number FROM user_sequences WHERE sequence_name = 'XXX_SEQ';</pre>



<p>參考資料：<a href="https://dotblogs.com.tw/falassion/2016/07/13/093912" target="_blank" rel="noreferrer noopener nofollow external" data-wpel-link="external" class="wpel-icon-right">修改Oracle Sequence的last number<span class="wpel-icon wpel-image wpel-icon-6"></span></a>、<a data-wpel-link="exclude" href="https://stackoverflow.com/questions/6099108/best-way-to-reset-an-oracle-sequence-to-the-next-value-in-an-existing-column" target="_blank" rel="noreferrer noopener follow" class="wpel-icon-right">Best way to reset an Oracle sequence to the next value in an existing column?<span class="wpel-icon wpel-image wpel-icon-6"></span></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/11178/%e4%bf%ae%e6%94%b9-oracle-sequence/" data-wpel-link="internal">修改 Oracle Sequence</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://noter.tw/11178/%e4%bf%ae%e6%94%b9-oracle-sequence/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>使用 Oracle sqlplus 將查詢結果匯出到檔案</title>
		<link>https://noter.tw/9109/oracle-sqlplus-export-results-to-file/</link>
					<comments>https://noter.tw/9109/oracle-sqlplus-export-results-to-file/#respond</comments>
		
		<dc:creator><![CDATA[黃小蛙]]></dc:creator>
		<pubDate>Wed, 11 Aug 2021 11:54:28 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[技術相關]]></category>
		<category><![CDATA[資料庫]]></category>
		<category><![CDATA[查詢結果]]></category>
		<category><![CDATA[colsep]]></category>
		<category><![CDATA[linesize]]></category>
		<category><![CDATA[另存檔案]]></category>
		<category><![CDATA[Oracle]]></category>
		<category><![CDATA[spool]]></category>
		<category><![CDATA[sqlplus]]></category>
		<category><![CDATA[匯出搜尋結果]]></category>
		<category><![CDATA[匯出檔案]]></category>
		<guid isPermaLink="false">https://noter.tw/?p=9109</guid>

					<description><![CDATA[<p>小蛙又遇到不能用 GUI 來管理 Oracle 的專案了，其實跟 2012 年的時候遇到的這個是相同的專案（Oracle 使用 sqlplus 匯入 SQL 檔案），該篇記錄了怎麼把一堆要執行的 SQ&#46;&#46;&#46;</p>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/9109/oracle-sqlplus-export-results-to-file/" data-wpel-link="internal">使用 Oracle sqlplus 將查詢結果匯出到檔案</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></description>
										<content:encoded><![CDATA[
<p>小蛙又遇到不能用 GUI 來管理 Oracle 的專案了，其實跟 2012 年的時候遇到的這個是相同的專案（<a href="https://noter.tw/2371/oracle-sqlplus-import-sql-file/" target="_blank" rel="noreferrer noopener" data-wpel-link="internal">Oracle 使用 sqlplus 匯入 SQL 檔案</a>），該篇記錄了怎麼把一堆要執行的 SQL 指令存入檔案中，並於 Oracle sqlplus 載入檔案執行，這篇文章為反向動作，將查詢的結果輸出到檔案內。</p>



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



<p>這個專案的環境很麻煩，在一個需要透過 VPN 連進去的內部環境，連上 VPN 之後就 &#8230; 斷網了，完全沒辦法對外，沒辦法 Google 查資料，對小蛙這種不專精的半調子來說存在一定難度，幸好有強大的同仁用 VM 的特殊大絕解決這問題，不過 VM 裡面連上 VPN 還是不能上網，但總算可以在本機 Google 資料，再手動把指令或語法手動輸入進去，對，沒錯，沒辦法複製貼上 Orz &#8230;</p>



<p>囉嗦一堆，進正題</p>



<pre class="wp-block-preformatted withcode">set colsep ,
set headsep off
set pagesize 0
set linesize 10000
set trimspool on
set echo off

spool /home/hans/output

select * from image;

spool off</pre>



<p class="withcode">上面那些指令是參考 <a href="https://stackoverflow.com/" target="_blank" rel="noreferrer noopener nofollow external" data-wpel-link="external" class="wpel-icon-right">StackOverflow<span class="wpel-icon wpel-image wpel-icon-6"></span></a> 多個答案來的，有些實際上在做什麼小蛙也不是很了解，但還是可以猜出大概是什麼，例如：<code>colset</code> 是設定欄位間的分隔符號(其他詳細設定請自行 Google)。小蛙遇到有問題的地方是 <code>linesize</code> 沒有設定，導致匯出的每一行都被切斷在莫名其妙的地方。其他 <code>spool 輸出檔案路徑</code>、<code>select * from image;</code> 要匯出結果的查詢 &#8230; 等等。</p>



<p>這指令已符合小蛙的需求，先備存，之後有其他需求再去查詳細的資料。以下是小蛙自己留存用的</p>



<pre class="wp-block-preformatted withcode">// 相當於 mysql show tables;
select table_name from user_tables;

// 查找排序過的前 10 筆 (Top 10)
select rownum, t.* from ( 實際要運作的 SQL ) t where rownum &lt; 10;

// 列出表格的所有欄位
select * from user_tab_columns A where A.table_name = 'IMAGE';
// 列出名稱、類型及長度
select column_name, data_type, default_length from user_tab_columns A where A.table_name = 'IMAGE';

// 複製 table 內所有資料
create table image_backup as select * from image;

// 表格建立的時間
select object_name, created, TIMESTAMP from user_objects where object_name = 'IMAGE';

// 修改表格
alter table IMAGE modify (file_name varchar2(300), index_term varchar2(1000));

// 格式變動完成
commit;</pre>



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



<ul class="my-li bg-darkblue wp-block-list"><li><a href="https://noter.tw/11178/%e4%bf%ae%e6%94%b9-oracle-sequence/" data-wpel-link="internal">修改 Oracle Sequence</a><a href="https://noter.tw/2371/oracle-%e5%8c%af%e5%85%a5-sql-%e6%aa%94%e6%a1%88/" data-wpel-link="internal"></a></li><li><a href="https://noter.tw/2371/oracle-sqlplus-import-sql-file/" data-wpel-link="internal">Oracle 使用 sqlplus 匯入 SQL 檔案</a></li><li><a href="https://noter.tw/9109/oracle-sqlplus-export-results-to-file/" data-wpel-link="internal">使用 Oracle sqlplus 將查詢結果匯出到檔案</a></li><li><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 – Oracle SQL Developer 安裝教學</a></li><li><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></li></ul>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/9109/oracle-sqlplus-export-results-to-file/" data-wpel-link="internal">使用 Oracle sqlplus 將查詢結果匯出到檔案</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://noter.tw/9109/oracle-sqlplus-export-results-to-file/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>資料庫問題彙整</title>
		<link>https://noter.tw/8540/database/</link>
					<comments>https://noter.tw/8540/database/#comments</comments>
		
		<dc:creator><![CDATA[黃小蛙]]></dc:creator>
		<pubDate>Fri, 04 Jun 2021 02:29:26 +0000</pubDate>
				<category><![CDATA[資料庫]]></category>
		<category><![CDATA[to_char]]></category>
		<category><![CDATA[java.sql.SQLException]]></category>
		<category><![CDATA[Oracle]]></category>
		<category><![CDATA[ORA-00932]]></category>
		<category><![CDATA[inconsistent datatypes]]></category>
		<category><![CDATA[CLOB]]></category>
		<guid isPermaLink="false">https://noter.tw/?p=8540</guid>

					<description><![CDATA[<p>記錄一些遇到過 DB 相關錯誤訊息。 這些問題都是消耗時間換來的經驗，希望能節省有相同問題的您的時間，現在開始陸續補上 (但也可能偷懶後來就沒更新了 Orz &#8230;) java.sql.SQL&#46;&#46;&#46;</p>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/8540/database/" data-wpel-link="internal">資料庫問題彙整</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></description>
										<content:encoded><![CDATA[
<p>記錄一些遇到過 DB 相關錯誤訊息。</p>



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



<p>這些問題都是消耗時間換來的經驗，希望能節省有相同問題的您的時間，現在開始陸續補上 (但也可能偷懶後來就沒更新了 Orz &#8230;)</p>



<h2 class="para wp-block-heading">java.sql.SQLException: ORA-00932: inconsistent datatypes: expected &#8211; got CLOB</h2>



<p>小蛙收到問題回報，在某個頁面的排序功能上會出現該錯誤，這個問題的解法很簡單，只是用來排序的欄位上出現 CLOB 格式，只要使用 to_char() 把 CLOB 轉成 CHAR 來做排序即可。例如：</p>



<p class="pre left green font-code">// 原始 SQL<br>SELECT * FROM EXHIBITION ORDER BY clob_field DESC<br>// 改成<br>SELECT * FROM EXHIBITION ORDER BY <span class="has-inline-color has-vivid-red-color"><strong>to_char(</strong></span>clob_field<strong><span class="has-inline-color has-vivid-red-color">)</span></strong> DESC</p>



<p>參考資料：<a href="https://blog.xuite.net/akf3dsmax8/wretch/192494934-oracle+sql+distinct+clob" target="_blank" rel="noreferrer noopener nofollow external" data-wpel-link="external" class="wpel-icon-right">來源1<span class="wpel-icon wpel-image wpel-icon-6"></span></a>、<a href="https://stackoverflow.com/questions/12980038/ora-00932-inconsistent-datatypes-expected-got-clob" target="_blank" rel="noreferrer noopener nofollow external" data-wpel-link="external" class="wpel-icon-right">來源2<span class="wpel-icon wpel-image wpel-icon-6"></span></a></p>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/8540/database/" data-wpel-link="internal">資料庫問題彙整</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://noter.tw/8540/database/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
		<item>
		<title>MySQL 在內網不能連？開 1 個洞吧！autossh Reverse Tunnel 教學</title>
		<link>https://noter.tw/6742/mysql-autossh-reverse-tunnel/</link>
					<comments>https://noter.tw/6742/mysql-autossh-reverse-tunnel/#respond</comments>
		
		<dc:creator><![CDATA[黃小蛙]]></dc:creator>
		<pubDate>Wed, 15 Jan 2020 15:03:10 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[資料庫]]></category>
		<category><![CDATA[穿透]]></category>
		<category><![CDATA[autossh]]></category>
		<category><![CDATA[內網]]></category>
		<category><![CDATA[SSH Reverse Tunnel 教學]]></category>
		<category><![CDATA[SSH Tunnel]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[SSH 穿牆]]></category>
		<category><![CDATA[ssh]]></category>
		<category><![CDATA[無法連線]]></category>
		<category><![CDATA[Reverse Tunnel]]></category>
		<category><![CDATA[反向通道]]></category>
		<category><![CDATA[防火牆]]></category>
		<guid isPermaLink="false">https://noter.tw/?p=6742</guid>

					<description><![CDATA[<p>這篇躺在草稿區好久了，小蛙一直偷懶沒有寫，今天朋友遇到這個問題，小蛙幫忙處理了一下，趁著還有印象，趕快來把 SSH Reverse Tunnel 教學記下來。</p>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/6742/mysql-autossh-reverse-tunnel/" data-wpel-link="internal">MySQL 在內網不能連？開 1 個洞吧！autossh Reverse Tunnel 教學</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></description>
										<content:encoded><![CDATA[
<p>這篇躺在草稿區好久了，小蛙一直偷懶沒有寫，今天朋友遇到這個問題，小蛙幫忙處理了一下，趁著還有印象，趕快來把 SSH Reverse Tunnel 教學記下來。</p>



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



<p>有接觸過 Linux 的人都知道 SSH，但應該也有很多人跟小蛙一樣不知道有一個東西叫做 ssh reverse tunnel，可以透過 SSH 打出一個外部可以連進來的「洞」。這樣講很難懂，小蛙每次看完、做完就忘記了 &#8230; >&lt; 這篇記錄一下流程。</p>



<h2 class="wp-block-heading para" id="p1">環境 &amp; 目的</h2>



<p>小蛙有以下兩台機器，兩台都是 Ubuntu</p>



<ul class="my-li bg-darkblue wp-block-list">
<li>A：必須要透過 FortiClient VPN 才能經由內網 IP 進入的主機</li>



<li>B：一台外部機器</li>
</ul>



<p>小蛙的需求是要讓 B 可以連線到 A 的 MySQL，小蛙平常要連上 VPN 之後透過內部 IP 才連到的 A，因此從外部機器 B 沒辦法直接連到 A，小蛙接著要透過 SSH 打出一個反向的通道，這樣就可以讓外部機器 B 經由這個打出的通道，連進去 A 的 MySQL 裡。這就是這篇最主要的目的～</p>



<h2 class="wp-block-heading para" id="p2">前處理</h2>



<p>打洞之前需要做一些前處理</p>



<ul class="my-li bg-darkblue wp-block-list">
<li>設定連線免輸入密碼（可參考<a href="https://noter.tw/278/%e7%99%bb%e5%85%a5-ssh-%e5%85%8d%e8%bc%b8%e5%85%a5%e5%b8%b3%e8%99%9f%e5%af%86%e7%a2%bc/" data-wpel-link="internal">這篇</a>，最下方「設置公鑰」章節）</li>



<li>安裝 autossh：apt-get install autossh </li>
</ul>



<p>前處理這樣就算結束了～</p>



<h2 class="wp-block-heading para" id="p3">開洞囉！SSH Reverse Tunnel 教學</h2>



<p>其實用 ssh 加一些設定就可以做到了，但是小蛙 survey 了一些文章後發現 <a href="https://linux.die.net/man/1/autossh" target="_blank" rel="noreferrer noopener nofollow external" data-wpel-link="external" class="wpel-icon-right">autossh<span class="wpel-icon wpel-image wpel-icon-6"></span></a> 可以很輕鬆做到這件事情，因此選擇了使用 autossh 來協助。</p>



<pre class="EnlighterJSRAW" data-enlighter-language="bash" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">autossh -M 12345 -NfR 33306:localhost:3306 account@server -p112233

參數介紹
-M 12345 : 用來監聽 ssh 有沒有斷線的 port
-NfR : N 不運行遠端指令、f 連線後背景執行、R 將遠端 port 對應到本地端 port
33306:localhost:3306 : 把遠端的 33306 對應到本機的 3306
account@server : 用來登入遠端 SSH 的帳號
-p112233 : 如果遠端 SSH 不是 22 port，可透過 -p 來指定 port</pre>



<p>這樣看完還是霧沙沙，簡單的說因為 B 沒辦法連到 A，小蛙要從 A 執行上述指令，這樣就會連到 B，並且讓 B 可以透過 33306 port 連過來，而且連過來的時候對應到 A 的 3306 port。</p>



<p>前處理的時候已經設定登入免密碼，讓 A 在打 reverse tunnel 的時候不用再輸入密碼，而 autossh 又會在 ssh 斷線後自動重新連接。</p>



<p>小蛙很容易弄錯的點是 -R 後面參數的設定，也就是上面 33306:localhost:3306 的部份（這裡常常記反），但其實只要記</p>



<ul class="my-li bg-darkblue wp-block-list">
<li>從內網主機 or 沒辦法透過外部連線、要打洞的主機下指令</li>



<li>-R 的參數，前面的是對方 port，後面才是 localhost 本機自己的</li>
</ul>



<h2 class="wp-block-heading para" id="p4">測試</h2>



<p>從 A 打反向通道到 B 之後，接著我們就要到 B 去測試到底能不能連上 A 啦～連上 B 後，小蛙要測試的是 MySQL，這邊可以直接用 mysql 的指令來做測試</p>



<pre class="EnlighterJSRAW" data-enlighter-language="bash" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">mysql -umysql_account -p -h127.0.0.1 -P33306</pre>



<p>噹啷～順利連上啦！</p>



<p>在 A 上面下 <code>netstat -utln</code> 可以看到多聆聽了 12345，在 B 上面下 <code>netstat -utln</code> 則會多聆聽 12345 及上面設定的 33306。</p>



<h2 class="wp-block-heading para" id="p5">注意事項</h2>



<p>ssh reverse tunnel 畢竟是在系統上面開洞，安全性上面多少會有一些影響，使用時要特別小心。</p>



<p>如果要關閉通道的話，只要下 ps -Al</p>



<pre class="EnlighterJSRAW" data-enlighter-language="bash" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">1 S     0 23025     1  0  80   0 -  1092 pause  ?        00:00:00 autossh</pre>



<p>接著 kill 23025 就可以刪掉 ssh reverse tunnel 囉！</p>


<h2 class="para">與 Ubuntu 相關的文章</h2><ul><li><span style="font-size: 1em;"><a href="https://noter.tw/10572/ubuntu-%e5%ae%89%e8%a3%9d%e5%8f%8a%e8%a8%ad%e5%ae%9a-vsftp/" data-wpel-link="internal">Ubuntu 安裝及設定 vsftp</a></span></li><li><span style="font-size: 1em;"><a href="https://noter.tw/10525/w-gpg-error-%e7%94%b1%e6%96%bc%e7%84%a1%e6%b3%95%e5%8f%96%e5%be%97%e5%ae%83%e5%80%91%e7%9a%84%e5%85%ac%e9%91%b0%ef%bc%8c%e4%bb%a5%e4%b8%8b%e7%b0%bd%e7%ab%a0%e7%84%a1%e6%b3%95%e9%80%b2%e8%a1%8c/" data-wpel-link="internal">W: GPG error: 由於無法取得它們的公鑰，以下簽章無法進行驗證： NO_PUBKEY 4EB27DB2A3B88B8B</a></span></li><li><span style="font-size: 1em;"><a href="https://noter.tw/9107/certbot-error-acmev1-is-deprecated/" data-wpel-link="internal">Certbot error: ACMEv1 is deprecated and you can &#8230;</a></span></li><li><span style="font-size: 1em;"><a href="https://noter.tw/4524/softether-vpn-%e6%9e%b6%e8%a8%ad%e8%a8%98%e9%8c%84/" data-wpel-link="internal">SoftEther VPN 架設記錄</a></span></li><li><span style="font-size: 1em;"><a href="https://noter.tw/4508/mariadb-replication-%e8%a8%ad%e5%ae%9a/" data-wpel-link="internal">MariaDB Replication 設定</a></span></li><li><span style="font-size: 1em;"><a href="https://noter.tw/50/ubuntu-%e4%bd%bf%e7%94%a8-ssmtp-%e9%80%8f%e9%81%8e-gmail-%e7%99%bc%e9%80%81-email/" data-wpel-link="internal">Ubuntu 使用 ssmtp 透過 Gmail 發送 email</a></span></li><li><span style="font-size: 1em;"><a href="https://noter.tw/115/curl-%e7%9a%84%e8%a9%ad%e7%95%b0%e5%95%8f%e9%a1%8c-vultr-dns-%e8%a8%ad%e5%ae%9a/" data-wpel-link="internal">curl 的詭異問題 &#8211; Vultr DNS 設定</a></span></li><li><span style="font-size: 1em;"><a href="https://noter.tw/3980/ubuntu-16-04-%e8%a8%ad%e5%ae%9a-apache-virtualhost-2/" data-wpel-link="internal">Ubuntu 16.04 設定 Apache VirtualHost</a></span></li><li><span style="font-size: 1em;"><a href="https://noter.tw/3946/ssh-%e9%81%a0%e7%ab%af%e7%99%bb%e5%87%ba%e5%be%8c%e7%b9%bc%e7%ba%8c%e5%9f%b7%e8%a1%8c/" data-wpel-link="internal">SSH 遠端登出後繼續執行</a></span></li><li><span style="font-size: 1em;"><a href="https://noter.tw/3913/ubuntu-16-04-phpmyadmin-deprecation-notice-with-php-7/" data-wpel-link="internal">Ubuntu 16.04 &#8211; phpmyadmin Deprecation Notice with PHP 7</a></span></li></ul><p>這篇文章 <a rel="nofollow" href="https://noter.tw/6742/mysql-autossh-reverse-tunnel/" data-wpel-link="internal">MySQL 在內網不能連？開 1 個洞吧！autossh Reverse Tunnel 教學</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://noter.tw/6742/mysql-autossh-reverse-tunnel/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[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>
		<category><![CDATA[php]]></category>
		<category><![CDATA[emoji]]></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[error]]></category>
		<category><![CDATA[2003]]></category>
		<category><![CDATA[遠端連線]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[mariadb]]></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[solrconfig.xml]]></category>
		<category><![CDATA[DataImportHandler]]></category>
		<category><![CDATA[batchSize]]></category>
		<category><![CDATA[db-data-config.xml]]></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>
		<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[solr]]></category>
		<category><![CDATA[solrconfig.xml]]></category>
		<category><![CDATA[DataImportHandler]]></category>
		<category><![CDATA[Error loading class 'org.apache.solr.handler.dataimport.DataImportHandler']]></category>
		<category><![CDATA[匯入資料]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[mariadb]]></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>
	</channel>
</rss>
