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

<channel>
	<title>資料庫 &#8211; 記下來</title>
	<atom:link href="https://noter.tw/tag/%E8%B3%87%E6%96%99%E5%BA%AB/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[搜尋 JSON]]></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>
		<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>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[slave]]></category>
		<category><![CDATA[資料庫同步]]></category>
		<category><![CDATA[2019]]></category>
		<category><![CDATA[Ubuntu]]></category>
		<category><![CDATA[mariadb]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[資料庫]]></category>
		<category><![CDATA[replication]]></category>
		<category><![CDATA[master]]></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>JSP 連結 MySQL</title>
		<link>https://noter.tw/2889/jsp-%e9%80%a3%e7%b5%90-mysql/</link>
					<comments>https://noter.tw/2889/jsp-%e9%80%a3%e7%b5%90-mysql/#respond</comments>
		
		<dc:creator><![CDATA[黃小蛙]]></dc:creator>
		<pubDate>Mon, 03 Jun 2013 15:18:16 +0000</pubDate>
				<category><![CDATA[資料庫]]></category>
		<category><![CDATA[網頁後端]]></category>
		<category><![CDATA[JSP]]></category>
		<category><![CDATA[連結資料庫]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[connector]]></category>
		<guid isPermaLink="false">http://wazai.net/?p=2889</guid>

					<description><![CDATA[<p>Java 連結 MySQL 的方法 記錄用Java連結MySQL資料庫的方法，這篇要介紹JSP如何連結MySQL。這篇文章是從舊部落格移到這邊記錄留存。步驟：安裝MySQL =&#62; 安裝ODBC &#46;&#46;&#46;</p>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/2889/jsp-%e9%80%a3%e7%b5%90-mysql/" data-wpel-link="internal">JSP 連結 MySQL</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></description>
										<content:encoded><![CDATA[<p><a href="https://noter.tw/2883" target="_blank" rel="noopener noreferrer" data-wpel-link="internal">Java 連結 MySQL 的方法</a> 記錄用Java連結MySQL資料庫的方法，這篇要介紹JSP如何連結MySQL。這篇文章是從舊部落格移到這邊記錄留存。<br /><span id="more-2889"></span><br />步驟：安裝MySQL =&gt; 安裝ODBC =&gt; 安裝JDBC。</p>
<ul>
<li>
<h2>安裝 MySQL</h2>
<ul>
<li>進入 MySQL 下載頁面，http://dev.mysql.com/downloads/mysql/5.0.html。</li>
<li>依自己的平台選擇下載類型，若是 Win 環境，可下載 Windows Essentials 安裝檔或是不包含安裝檔的 Without installer 這項。</li>
<li>下載完之後，擊點兩下開始安裝。(期間會有些 MySQL Server 的設定)</li>
</ul>
</li>
<li>
<h2>安裝 ODBC</h2>
<ul>
<li>下載 ODBC，http://dev.mysql.com/downloads/connector/odbc/5.1.html。</li>
<li>安裝 ODBC。</li>
<li>安裝完成後，開啟 &#8220;控制台&#8221; -&gt; &#8220;系統管理工具&#8221; -&gt; &#8220;資料來源(ODBC)&#8221;，之後切換到 &#8220;系統資料來源名稱&#8221; 頁面下，看是否有 &#8220;MySQL&#8221;，有的話表示安裝成功。</li>
<li>沒有的話，選擇右邊的 &#8220;新增&#8221; -&gt; &#8220;MySQL ODBC 5.1 Driver&#8221; -&gt; &#8220;完成&#8221;，到這裡會跳出一個 MySQL 設定的視窗，&#8221;Data Source Name&#8221; 隨便輸入，下面包括 &#8220;Server/Port/User/Password&#8221; 都依照安裝 MySQL 時的設定輸入，之後按下 &#8220;Test&#8221;，出現 &#8220;Connection Successful&#8221; 表示整個安裝已經完成了。</li>
</ul>
</li>
<li>
<h2>安裝 JDBC</h2>
<ul>
<li>下載 JDBC，http://dev.mysql.com/downloads/connector/j/5.1.html。</li>
<li>下載完成後解壓縮，找到裡面有個檔名是 &#8220;mysql-connector-java-5.1.6-bin.jar&#8221; 的資料，複製到 Tomcat 安裝目錄下的 &#8220;\shared\lib&#8221; 下。</li>
<li>設計 JSP 文件時，用以下指令就可以連結資料庫。
<pre class="brush: java; gutter: true">String JDBCDriver = "com.mysql.jdbc.Driver";
String strUrl = "jdbc:mysql://localhost:3306/test?user=root&amp;characterEncoding=big5"
Class.forName(JDBCDriver);
Connection con = DriverManager.getConnection(strUrl);</pre>
</li>
</ul>
</li>
</ul>
<p><strong>參考資料：</strong></p>
<ul>
<li style="list-style-type: none;">
<ol>
<li><a href="https://noter.tw/2883" data-wpel-link="internal">Java 連結 MySQL</a></li>
<li><a href="https://noter.tw/1988" data-wpel-link="internal">Java 連結遠端 MySQL</a></li>
</ol>
</li>
</ul>


<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/2889/jsp-%e9%80%a3%e7%b5%90-mysql/" data-wpel-link="internal">JSP 連結 MySQL</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://noter.tw/2889/jsp-%e9%80%a3%e7%b5%90-mysql/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
