以下的文章主要介绍的是MySQL字符集的实际应用,其中包括如何正确配置MySQL数据库的默认字符集,以及查看数据库当前所使用MySQL数据库字符集有哪些?以下就是文章的主要内容描述, 1. 配置MySQL默认字符集 [client] default-character-set = utf8 客户端连接
以下的文章主要介绍的是MySQL字符集的实际应用,其中包括如何正确配置MySQL数据库的默认字符集,以及查看数据库当前所使用MySQL数据库字符集有哪些?以下就是文章的主要内容描述,
1. 配置MySQL默认字符集
<ol class="dp-xml"> <li class="alt"><span><span>[client] </span></span></li> <li> <span class="attribute">default-character-set</span><span>=</span><span class="attribute-value">utf8</span><span> </span> </li> </ol>
客户端连接默认MySQL字符集
<ol class="dp-xml"> <li class="alt"><span>[MySQLd] </span></li> <li> <span class="attribute">default-character-set</span><span>=</span><span class="attribute-value">utf8</span><span> </span> </li> </ol>
MySQLd默认字符集
2. 查看MySQL当前使用MySQL字符集
<ol class="dp-xml"> <li class="alt">MySQL<span><span class="tag">></span><span> show variables like 'character%'; </span></span> </li> <li><span>+--------------------------+----------------------------------------+ </span></li> <li class="alt"><span>| Variable_name | Value | </span></li> <li><span>+--------------------------+----------------------------------------+ </span></li> <li class="alt"><span>| character_set_client | utf8 | </span></li> <li><span>| character_set_connection | utf8 | </span></li> <li class="alt"><span>| character_set_database | utf8 | </span></li> <li><span>| character_set_filesystem | binary | </span></li> <li class="alt"><span>| character_set_results | utf8 | </span></li> <li><span>| character_set_server | utf8 | </span></li> <li class="alt"><span>| character_set_system | utf8 | </span></li> <li> <span>| character_sets_dir | /usr/local/</span>MySQL<span>/share/</span>MySQL<span>/charsets/ | </span> </li> <li class="alt"><span>+--------------------------+----------------------------------------+ </span></li> </ol>
使用MySQL命令修改MySQL字符集
<ol class="dp-xml"> <li class="alt">MySQL<span><span class="tag">></span><span> SET </span><span class="attribute">character_set_client</span><span> = </span><span class="attribute-value">utf8</span><span> ; </span></span> </li> <li>MySQL<span class="tag">></span><span> SET </span><span class="attribute">character_set_connection</span><span> = </span><span class="attribute-value">utf8</span><span> ; </span> </li> <li class="alt">MySQL<span class="tag">></span><span> SET </span><span class="attribute">character_set_database</span><span> = </span><span class="attribute-value">utf8</span><span> ; </span> </li> <li>MySQL<span class="tag">></span><span> SET </span><span class="attribute">character_set_results</span><span> = </span><span class="attribute-value">utf8</span><span> ; </span> </li> <li class="alt">MySQL<span class="tag">></span><span> SET </span><span class="attribute">character_set_server</span><span> = </span><span class="attribute-value">utf8</span><span> ; </span> </li> </ol>
3. C api
<ol class="dp-xml"> <li class="alt">MySQL<span>_options(</span>MySQL<span>, </span>MySQL<span>_SET_CHARSET_NAME, 'utf8'); </span> </li> <li>MySQL<span>_real_connect(</span>MySQL<span>, ...); </span> </li> </ol>
大部分情况下,使用MySQL默认编码latin1可以解决MySQL乱码问题。同时在插入数据和读取数据时,不需要通过MySQL_options来指定编码类型。配置文件my.cnf也不需要指定编码类型。