<?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>utf8 &#8211; richliu&#039;s blog</title>
	<atom:link href="https://blog.richliu.com/tag/utf8/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.richliu.com</link>
	<description>Linux, 工作, 生活, 家人</description>
	<lastBuildDate>Fri, 21 Mar 2025 09:07:36 +0000</lastBuildDate>
	<language>zh-TW</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.6.2</generator>
	<item>
		<title>WordPress utf8mb3_general_ci 改utf8mb4_unicode_ci</title>
		<link>https://blog.richliu.com/2025/03/21/6220/wordpress-utf8mb3_general_ci-%e6%94%b9utf8mb4_unicode_ci/</link>
					<comments>https://blog.richliu.com/2025/03/21/6220/wordpress-utf8mb3_general_ci-%e6%94%b9utf8mb4_unicode_ci/#respond</comments>
		
		<dc:creator><![CDATA[richliu]]></dc:creator>
		<pubDate>Fri, 21 Mar 2025 04:07:30 +0000</pubDate>
				<category><![CDATA[Blog/wiki]]></category>
		<category><![CDATA[utf8]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[中文]]></category>
		<guid isPermaLink="false">https://blog.richliu.com/?p=6220</guid>

					<description><![CDATA[<p>這個最近在寫文章插入 emoji 😁的時候才發現的問題，想說怎麼不能存檔了，因為錯誤訊息只出現 Update  [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://blog.richliu.com/2025/03/21/6220/wordpress-utf8mb3_general_ci-%e6%94%b9utf8mb4_unicode_ci/">WordPress utf8mb3_general_ci 改utf8mb4_unicode_ci</a> appeared first on <a rel="nofollow" href="https://blog.richliu.com">richliu&#039;s blog</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>這個最近在寫文章插入 emoji <img src="https://s.w.org/images/core/emoji/15.0.3/72x72/1f601.png" alt="😁" class="wp-smiley" style="height: 1em; max-height: 1em;" />的時候才發現的問題，想說怎麼不能存檔了，因為錯誤訊息只出現 Update database error ， apache server 也沒有明顯的 LOG 指出這件事，錯了幾次才抓出是 emoji 的問題</p>



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



<p>在查找文章的時候有<a href="https://www.mrkwp.com/2025/03/fixing-emoji-saving-issues-chatgpt-wordpress/" target="_blank" rel="noopener">文章</a>就提到</p>



<pre class="wp-block-preformatted">Check Your Current Collation: Look at your database tables. If you see a collation like utf8mb3_unicode_ci, this is likely the cause of the problem. WordPress tables should ideally use utf8mb4_unicode_520</pre>



<p>才想到查一下我的 DB 是不是 utf8mb3 ，一查之下果然是，老的 WordPress 用的資料庫果然夠老，轉換資料庫也碰到一些問題，主要是 wp_comments 這個資料庫的問題，可以先去 WordPress comments 刪除所有垃圾留言</p>



<p>在 wp-config.php 內設定成 utf8mb4</p>



<pre class="wp-block-preformatted">define('DB_CHARSET', 'utf8mb4');
define('DB_COLLATE', 'utf8mb4_unicode_ci');</pre>



<p>導出 wp_comments 的資料庫</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#2f363c">Bash</span><span role="button" tabindex="0" data-code="mysqldump -u [用戶名] -p --skip-set-charset --default-character-set=latin1 [資料庫名] wp_comments &gt; wp_comments.sql" style="color:#24292e;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki github-light" style="background-color: #fff" tabindex="0"><code><span class="line"><span style="color: #6F42C1">mysqldump</span><span style="color: #24292E"> </span><span style="color: #005CC5">-u</span><span style="color: #24292E"> [用戶名] -p --skip-set-charset --default-character-set=latin1 [資料庫名] wp_comments </span><span style="color: #D73A49">&gt;</span><span style="color: #24292E"> wp_comments.sql</span></span></code></pre></div>



<p>導回 wp_comments 的資料庫</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#2f363c">Bash</span><span role="button" tabindex="0" data-code="mysql -u [用戶名] -p --default-character-set=utf8mb4 [資料庫名] &lt; wp_comments.sql" style="color:#24292e;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki github-light" style="background-color: #fff" tabindex="0"><code><span class="line"><span style="color: #6F42C1">mysql</span><span style="color: #24292E"> </span><span style="color: #005CC5">-u</span><span style="color: #24292E"> [用戶名] -p --default-character-set=utf8mb4 [資料庫名] </span><span style="color: #D73A49">&lt;</span><span style="color: #24292E"> wp_comments.sql</span></span></code></pre></div>



<p>轉換整個資料庫</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#2f363c">Bash</span><span role="button" tabindex="0" data-code="#!/bin/bash
DB_NAME=&quot;your_database&quot;
USER=&quot;your_user&quot;
PASS=&quot;your_password&quot;

TABLES=$(mysql -u $USER -p$PASS -Nse &quot;SHOW TABLES;&quot; $DB_NAME)
for TABLE in $TABLES; do
    mysql -u $USER -p$PASS -e &quot;ALTER TABLE $TABLE CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;&quot; $DB_NAME
done" style="color:#24292e;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki github-light" style="background-color: #fff" tabindex="0"><code><span class="line"><span style="color: #6A737D">#!/bin/bash</span></span>
<span class="line"><span style="color: #24292E">DB_NAME</span><span style="color: #D73A49">=</span><span style="color: #032F62">&quot;your_database&quot;</span></span>
<span class="line"><span style="color: #24292E">USER</span><span style="color: #D73A49">=</span><span style="color: #032F62">&quot;your_user&quot;</span></span>
<span class="line"><span style="color: #24292E">PASS</span><span style="color: #D73A49">=</span><span style="color: #032F62">&quot;your_password&quot;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #24292E">TABLES</span><span style="color: #D73A49">=</span><span style="color: #032F62">$(</span><span style="color: #6F42C1">mysql</span><span style="color: #032F62"> </span><span style="color: #005CC5">-u</span><span style="color: #032F62"> </span><span style="color: #24292E">$USER</span><span style="color: #032F62"> </span><span style="color: #005CC5">-p</span><span style="color: #24292E">$PASS</span><span style="color: #032F62"> </span><span style="color: #005CC5">-Nse</span><span style="color: #032F62"> &quot;SHOW TABLES;&quot; </span><span style="color: #24292E">$DB_NAME</span><span style="color: #032F62">)</span></span>
<span class="line"><span style="color: #D73A49">for</span><span style="color: #24292E"> TABLE </span><span style="color: #D73A49">in</span><span style="color: #24292E"> $TABLES; </span><span style="color: #D73A49">do</span></span>
<span class="line"><span style="color: #24292E">    </span><span style="color: #6F42C1">mysql</span><span style="color: #24292E"> </span><span style="color: #005CC5">-u</span><span style="color: #24292E"> $USER </span><span style="color: #005CC5">-p</span><span style="color: #24292E">$PASS</span><span style="color: #24292E"> </span><span style="color: #005CC5">-e</span><span style="color: #24292E"> </span><span style="color: #032F62">&quot;ALTER TABLE </span><span style="color: #24292E">$TABLE</span><span style="color: #032F62"> CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;&quot;</span><span style="color: #24292E"> $DB_NAME</span></span>
<span class="line"><span style="color: #D73A49">done</span></span></code></pre></div>



<p>DeepSeek 這邊做的非常好，直接給 shell script 幫助完成，Grok 就沒給出這麼方便的 Script. </p>
<p>The post <a rel="nofollow" href="https://blog.richliu.com/2025/03/21/6220/wordpress-utf8mb3_general_ci-%e6%94%b9utf8mb4_unicode_ci/">WordPress utf8mb3_general_ci 改utf8mb4_unicode_ci</a> appeared first on <a rel="nofollow" href="https://blog.richliu.com">richliu&#039;s blog</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.richliu.com/2025/03/21/6220/wordpress-utf8mb3_general_ci-%e6%94%b9utf8mb4_unicode_ci/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
