zoukankan      html  css  js  c++  java
  • (原创)Linux下MySQL 5.5/5.6的修改字符集编码为UTF8(彻底解决中文乱码问题)

    PS:昨天一同事遇到mysql 5.5中文乱码问题,找我解决。解决了,有个细节问题网上没人说,我就总结一下。

    一、登录MySQL查看用SHOW VARIABLES LIKE ‘character%’;(正确命令为show variables like "%char%";)下字符集,显示如下:

    +--------------------------+----------------------------+
    | Variable_name | Value |
    +--------------------------+----------------------------+
    | character_set_client | utf8 |
    | character_set_connection | utf8 |
    | character_set_database | latin1 |
    | character_set_filesystem | binary |
    | character_set_results | utf8 |
    | character_set_server | latin1 |
    | character_set_system | utf8 |
    | character_sets_dir | /usr/share/mysql/charsets/ |
    +--------------------------+----------------------------+

    character_set_database和character_set_server的默认字符集还是latin1。

    二、最简单的完美修改方法,修改mysql的my.cnf文件中的字符集键值(注意配置的字段细节):

    1、在[client]字段里加入default-character-set=utf8,如下:

    [client]
    port = 3306
    socket = /var/lib/mysql/mysql.sock
    default-character-set=utf8

    2、在[mysqld]字段里加入character-set-server=utf8,如下:

    [mysqld]
    port = 3306
    socket = /var/lib/mysql/mysql.sock
    character-set-server=utf8

    3、在[mysql]字段里加入default-character-set=utf8,如下:

    [mysql]
    no-auto-rehash
    default-character-set=utf8

    修改完成后,service mysql restart重启mysql服务就生效。注意:[mysqld]字段与[mysql]字段是有区别的。这点在网上没人反馈过。

    使用SHOW VARIABLES LIKE ‘character%’;查看,发现数据库编码全已改成utf8。

    +--------------------------+----------------------------+
    | Variable_name | Value |
    +--------------------------+----------------------------+
    | character_set_client | utf8 |
    | character_set_connection | utf8 |
    | character_set_database | utf8 |
    | character_set_filesystem | binary |
    | character_set_results | utf8 |
    | character_set_server | utf8 |
    | character_set_system | utf8 |
    | character_sets_dir | /usr/share/mysql/charsets/ |
    +--------------------------+----------------------------+


    4、如果上面的都修改了还乱码,那剩下问题就一定在connection连接层上。解决方法是在发送查询前执行一下下面这句(直接写在SQL文件的最前面):
    SET NAMES 'utf8';

    它相当于下面的三句指令:

    SET character_set_client = utf8;
    SET character_set_results = utf8;
    SET character_set_connection = utf8;


    网上很多其他方法不能彻底解决这个问题,这个可以完美解决!

    		<p class="postendmeta">
    		<a href="http://www.ha97.com/5359.html" rel="bookmark" title="(原创)Linux下MySQL 5.5/5.6的修改字符集编码为UTF8(彻底解决中文乱码问题)" class="permalink">永久链接</a> : http://www.ha97.com/5359.html			</p>
    	</div>
    
    本文相关评论 - 才 11 条评论
    一号站长
    			</div>
    			<div class="body">
    				<div class="commmeta">
    					2013-02-02 13:37:07 						
    				</div>					
    				<div class="content clearfix">
    						<img alt="" src="http://2.gravatar.com/avatar/8713a94f107dc37bacb383e3f298bcd4?s=32&amp;d=identicon&amp;r=g" srcset="http://2.gravatar.com/avatar/8713a94f107dc37bacb383e3f298bcd4?s=64&amp;d=identicon&amp;r=g 2x" class="avatar avatar-32 photo" height="32" width="32">						<div class="wp-useragent"> <img src="http://www.ha97.com/wp-content/plugins/wp-useragent/img/24/net/firefox.png" title="Firefox 18.0" style="border:0px;vertical-align:middle;" alt="Firefox 18.0" height="24" width="24"> Firefox 18.0  <img src="http://www.ha97.com/wp-content/plugins/wp-useragent/img/24/os/win-4.png" title="Windows 7 x64 Edition" style="border:0px;vertical-align:middle;" alt="Windows 7 x64 Edition" height="24" width="24"> Windows 7 x64 Edition</div><p>没遇到过这个问题,用的5.5.25版本,一切正常,估计是默认配置好的</p>
    				</div>
    				<div class="reply">
    					 
    					<a href="#" onclick="moveAddCommentBelow(&quot;div-comment-107175&quot;, 107175, true); return false;">回复他(她)</a>
    										</div>
    			</div>
    				
    
    		</div>
    		<div id="div-comment-121366" class="clearfix comment">
    			<a name="comment-121366" id="comment-121366"></a>
    			<div class="title clearfix">
    				<div class="collapseicon" style="float:left" id="ment-121366">
    				</div>
    				<cite>Shylock<a href="#comment-121366"></a></cite> 
    								
    			</div>
    			<div class="body">
    				<div class="commmeta">
    					2013-03-24 11:43:30 						
    				</div>					
    				<div class="content clearfix">
    						<img alt="" src="http://2.gravatar.com/avatar/8cffd49456779d84b5c4d832c634155a?s=32&amp;d=identicon&amp;r=g" srcset="http://2.gravatar.com/avatar/8cffd49456779d84b5c4d832c634155a?s=64&amp;d=identicon&amp;r=g 2x" class="avatar avatar-32 photo" height="32" width="32">						<div class="wp-useragent"> <img src="http://www.ha97.com/wp-content/plugins/wp-useragent/img/24/net/firefox.png" title="Firefox 19.0" style="border:0px;vertical-align:middle;" alt="Firefox 19.0" height="24" width="24"> Firefox 19.0  <img src="http://www.ha97.com/wp-content/plugins/wp-useragent/img/24/os/ubuntu-1.png" title="Ubuntu x64" style="border:0px;vertical-align:middle;" alt="Ubuntu x64" height="24" width="24"> Ubuntu x64</div><p>确实是改好了,,,按照其他方式改后数据库都启动不了了。</p>
    				</div>
    				<div class="reply">
    					 
    					<a href="#" onclick="moveAddCommentBelow(&quot;div-comment-121366&quot;, 121366, true); return false;">回复他(她)</a>
    										</div>
    			</div>
    				
    
    		</div>
    		<div id="div-comment-128881" class="clearfix comment">
    			<a name="comment-128881" id="comment-128881"></a>
    			<div class="title clearfix">
    				<div class="collapseicon" style="float:left" id="ment-128881">
    				</div>
    				<cite><a href="http://bbs.chuangxin.biz" rel="external nofollow" class="url">成功论坛</a><a href="#comment-128881"></a></cite> 
    								
    			</div>
    			<div class="body">
    				<div class="commmeta">
    					2013-04-24 10:26:00 						
    				</div>					
    				<div class="content clearfix">
    						<img alt="" src="http://0.gravatar.com/avatar/f01ef126e86787aafaaf5a4120e807dd?s=32&amp;d=identicon&amp;r=g" srcset="http://0.gravatar.com/avatar/f01ef126e86787aafaaf5a4120e807dd?s=64&amp;d=identicon&amp;r=g 2x" class="avatar avatar-32 photo" height="32" width="32">						<div class="wp-useragent"> <img src="http://www.ha97.com/wp-content/plugins/wp-useragent/img/24/net/sogou.png" title="Sogou Explorer " style="border:0px;vertical-align:middle;" alt="Sogou Explorer " height="24" width="24"> Sogou Explorer   <img src="http://www.ha97.com/wp-content/plugins/wp-useragent/img/24/os/win-4.png" title="Windows 7 x64 Edition" style="border:0px;vertical-align:middle;" alt="Windows 7 x64 Edition" height="24" width="24"> Windows 7 x64 Edition</div><p>你的方法在最新版本的mysql里面有问题<br>
    

    给一个真正完美的解决方案!

    [client]
    default-character-set=utf8
    [mysql]
    default-character-set=utf8
    [mysqld]
    collation-server = utf8_unicode_ci
    init-connect=’SET NAMES utf8′
    character-set-server = utf8

    					<a href="#" onclick="moveAddCommentBelow(&quot;div-comment-128881&quot;, 128881, true); return false;">回复他(她)</a>
    										</div>
    			</div>
    				
    
    		</div>
    		<div id="div-comment-137217" class="clearfix comment">
    			<a name="comment-137217" id="comment-137217"></a>
    			<div class="title clearfix">
    				<div class="collapseicon" style="float:left" id="ment-137217">
    				</div>
    				<cite>郁蓝<a href="#comment-137217"></a></cite> 
    								
    			</div>
    			<div class="body">
    				<div class="commmeta">
    					2013-05-29 22:24:05 						
    				</div>					
    				<div class="content clearfix">
    						<img alt="" src="http://2.gravatar.com/avatar/513b1ae133d97916b7b8f81599f7cd3e?s=32&amp;d=identicon&amp;r=g" srcset="http://2.gravatar.com/avatar/513b1ae133d97916b7b8f81599f7cd3e?s=64&amp;d=identicon&amp;r=g 2x" class="avatar avatar-32 photo" height="32" width="32">						<div class="wp-useragent"> <img src="http://www.ha97.com/wp-content/plugins/wp-useragent/img/24/net/firefox.png" title="Firefox 21.0" style="border:0px;vertical-align:middle;" alt="Firefox 21.0" height="24" width="24"> Firefox 21.0  <img src="http://www.ha97.com/wp-content/plugins/wp-useragent/img/24/os/linux.png" title="GNU/Linux x64" style="border:0px;vertical-align:middle;" alt="GNU/Linux x64" height="24" width="24"> GNU/Linux x64</div><p>最好的解决方案就是不要在数据库里面放中文。。。。。可这怎么可能呢?</p>
    				</div>
    				<div class="reply">
    					 
    					<a href="#" onclick="moveAddCommentBelow(&quot;div-comment-137217&quot;, 137217, true); return false;">回复他(她)</a>
    										</div>
    			</div>
    				
    
    		</div>
    		<div id="div-comment-150299" class="clearfix comment">
    			<a name="comment-150299" id="comment-150299"></a>
    			<div class="title clearfix">
    				<div class="collapseicon" style="float:left" id="ment-150299">
    				</div>
    				<cite>ls<a href="#comment-150299"></a></cite> 
    								
    			</div>
    			<div class="body">
    				<div class="commmeta">
    					2013-08-28 02:04:51 						
    				</div>					
    				<div class="content clearfix">
    						<img alt="" src="http://1.gravatar.com/avatar/144e6d27cdbc76868bf2724b19ba8012?s=32&amp;d=identicon&amp;r=g" srcset="http://1.gravatar.com/avatar/144e6d27cdbc76868bf2724b19ba8012?s=64&amp;d=identicon&amp;r=g 2x" class="avatar avatar-32 photo" height="32" width="32">						<div class="wp-useragent"> <img src="http://www.ha97.com/wp-content/plugins/wp-useragent/img/24/net/chromium.png" title="Chromium 28.0.1500.71" style="border:0px;vertical-align:middle;" alt="Chromium 28.0.1500.71" height="24" width="24"> Chromium 28.0.1500.71  <img src="http://www.ha97.com/wp-content/plugins/wp-useragent/img/24/os/ubuntu-1.png" title="Ubuntu" style="border:0px;vertical-align:middle;" alt="Ubuntu" height="24" width="24"> Ubuntu</div><p>实在是非常感谢楼主,其他地方看到的都是直接在[clien] 和 [mysqld] 下加 defualt-character-set=utf-8,<br>
    

    楼主 给出了正确方法,要在[clien]下加defualt-character-set=utf-8,在[mysqld]加 character-set-service=utf8 ,还有在[mysql] 下也加defualt-character-set 完美解决了,找了几天了拜谢



    					<a href="#" onclick="moveAddCommentBelow(&quot;div-comment-150299&quot;, 150299, true); return false;">回复他(她)</a>
    										</div>
    			</div>
    				
    
    		</div>
    		<div id="div-comment-157604" class="clearfix comment">
    			<a name="comment-157604" id="comment-157604"></a>
    			<div class="title clearfix">
    				<div class="collapseicon" style="float:left" id="ment-157604">
    				</div>
    				<cite><a href="http://www.favolove.com" rel="external nofollow" class="url">favolove</a><a href="#comment-157604"></a></cite> 
    								
    			</div>
    			<div class="body">
    				<div class="commmeta">
    					2013-10-14 15:31:51 						
    				</div>					
    				<div class="content clearfix">
    						<img alt="" src="http://1.gravatar.com/avatar/11482e877352b8986067a4c9d449fa60?s=32&amp;d=identicon&amp;r=g" srcset="http://1.gravatar.com/avatar/11482e877352b8986067a4c9d449fa60?s=64&amp;d=identicon&amp;r=g 2x" class="avatar avatar-32 photo" height="32" width="32">						<div class="wp-useragent"> <img src="http://www.ha97.com/wp-content/plugins/wp-useragent/img/24/net/firefox.png" title="Firefox 24.0" style="border:0px;vertical-align:middle;" alt="Firefox 24.0" height="24" width="24"> Firefox 24.0  <img src="http://www.ha97.com/wp-content/plugins/wp-useragent/img/24/os/win-4.png" title="Windows 7" style="border:0px;vertical-align:middle;" alt="Windows 7" height="24" width="24"> Windows 7</div><p>[clien] 和 [mysql] 下加 defualt-character-set=utf-8 ? =&gt; 没有必要。<br>
    

    [mysqld]加 character-set-service=utf8 ? => 新版本的mysql会无法重启。

    最简单的解决办法如下:
    [mysqld]加 character-set-server = utf8

    可按需添加 init-connect=’SET NAMES utf8′

    					<a href="#" onclick="moveAddCommentBelow(&quot;div-comment-157604&quot;, 157604, true); return false;">回复他(她)</a>
    										</div>
    			</div>
    				
    
    		</div>
    		<div id="div-comment-158210" class="clearfix comment">
    			<a name="comment-158210" id="comment-158210"></a>
    			<div class="title clearfix">
    				<div class="collapseicon" style="float:left" id="ment-158210">
    				</div>
    				<cite>野人<a href="#comment-158210"></a></cite> 
    								
    			</div>
    			<div class="body">
    				<div class="commmeta">
    					2013-10-17 10:37:44 						
    				</div>					
    				<div class="content clearfix">
    						<img alt="" src="http://2.gravatar.com/avatar/8628bd63aed6833bb9e490c7c63c43a8?s=32&amp;d=identicon&amp;r=g" srcset="http://2.gravatar.com/avatar/8628bd63aed6833bb9e490c7c63c43a8?s=64&amp;d=identicon&amp;r=g 2x" class="avatar avatar-32 photo" height="32" width="32">						<div class="wp-useragent"> <img src="http://www.ha97.com/wp-content/plugins/wp-useragent/img/24/net/firefox.png" title="Firefox 24.0" style="border:0px;vertical-align:middle;" alt="Firefox 24.0" height="24" width="24"> Firefox 24.0  <img src="http://www.ha97.com/wp-content/plugins/wp-useragent/img/24/os/win-4.png" title="Windows 7 x64 Edition" style="border:0px;vertical-align:middle;" alt="Windows 7 x64 Edition" height="24" width="24"> Windows 7 x64 Edition</div><p>遇到一模一样的问题,按您的方法解决了,非常感谢!!</p>
    				</div>
    				<div class="reply">
    					 
    					<a href="#" onclick="moveAddCommentBelow(&quot;div-comment-158210&quot;, 158210, true); return false;">回复他(她)</a>
    										</div>
    			</div>
    				
    
    		</div>
    		<div id="div-comment-158211" class="clearfix comment">
    			<a name="comment-158211" id="comment-158211"></a>
    			<div class="title clearfix">
    				<div class="collapseicon" style="float:left" id="ment-158211">
    				</div>
    				<cite>野人<a href="#comment-158211"></a></cite> 
    								
    			</div>
    			<div class="body">
    				<div class="commmeta">
    					2013-10-17 10:39:41 						
    				</div>					
    				<div class="content clearfix">
    						<img alt="" src="http://2.gravatar.com/avatar/8628bd63aed6833bb9e490c7c63c43a8?s=32&amp;d=identicon&amp;r=g" srcset="http://2.gravatar.com/avatar/8628bd63aed6833bb9e490c7c63c43a8?s=64&amp;d=identicon&amp;r=g 2x" class="avatar avatar-32 photo" height="32" width="32">						<div class="wp-useragent"> <img src="http://www.ha97.com/wp-content/plugins/wp-useragent/img/24/net/firefox.png" title="Firefox 24.0" style="border:0px;vertical-align:middle;" alt="Firefox 24.0" height="24" width="24"> Firefox 24.0  <img src="http://www.ha97.com/wp-content/plugins/wp-useragent/img/24/os/win-4.png" title="Windows 7 x64 Edition" style="border:0px;vertical-align:middle;" alt="Windows 7 x64 Edition" height="24" width="24"> Windows 7 x64 Edition</div><p>遇到了一模一样的问题,用您的方法解决了,非常感谢!!!</p>
    				</div>
    				<div class="reply">
    					 
    					<a href="#" onclick="moveAddCommentBelow(&quot;div-comment-158211&quot;, 158211, true); return false;">回复他(她)</a>
    										</div>
    			</div>
    				
    
    		</div>
    		<div id="div-comment-173322" class="clearfix comment">
    			<a name="comment-173322" id="comment-173322"></a>
    			<div class="title clearfix">
    				<div class="collapseicon" style="float:left" id="ment-173322">
    				</div>
    				<cite>小罗<a href="#comment-173322"></a></cite> 
    								
    			</div>
    			<div class="body">
    				<div class="commmeta">
    					2014-04-28 10:15:16 						
    				</div>					
    				<div class="content clearfix">
    						<img alt="" src="http://2.gravatar.com/avatar/82b30fab8912918dc4e40d4d1610594a?s=32&amp;d=identicon&amp;r=g" srcset="http://2.gravatar.com/avatar/82b30fab8912918dc4e40d4d1610594a?s=64&amp;d=identicon&amp;r=g 2x" class="avatar avatar-32 photo" height="32" width="32">						<div class="wp-useragent"> <img src="http://www.ha97.com/wp-content/plugins/wp-useragent/img/24/net/chrome.png" title="Google Chrome 34.0.1847.116" style="border:0px;vertical-align:middle;" alt="Google Chrome 34.0.1847.116" height="24" width="24"> Google Chrome 34.0.1847.116  <img src="http://www.ha97.com/wp-content/plugins/wp-useragent/img/24/os/win-4.png" title="Windows 7 x64 Edition" style="border:0px;vertical-align:middle;" alt="Windows 7 x64 Edition" height="24" width="24"> Windows 7 x64 Edition</div><p>解决了  感谢</p>
    				</div>
    				<div class="reply">
    					 
    					<a href="#" onclick="moveAddCommentBelow(&quot;div-comment-173322&quot;, 173322, true); return false;">回复他(她)</a>
    										</div>
    			</div>
    				
    
    		</div>
    		<div id="div-comment-693702" class="clearfix comment">
    			<a name="comment-693702" id="comment-693702"></a>
    			<div class="title clearfix">
    				<div class="collapseicon" style="float:left" id="ment-693702">
    				</div>
    				<cite>ZZZ<a href="#comment-693702"></a></cite> 
    								
    			</div>
    			<div class="body">
    				<div class="commmeta">
    					2016-06-12 13:23:39 						
    				</div>					
    				<div class="content clearfix">
    						<img alt="" src="http://2.gravatar.com/avatar/b9e7e824e8d8614a3f3fb6ed847ea8c0?s=32&amp;d=identicon&amp;r=g" srcset="http://2.gravatar.com/avatar/b9e7e824e8d8614a3f3fb6ed847ea8c0?s=64&amp;d=identicon&amp;r=g 2x" class="avatar avatar-32 photo" height="32" width="32">						<div class="wp-useragent"> <img src="http://www.ha97.com/wp-content/plugins/wp-useragent/img/24/net/chrome.png" title="Google Chrome 42.0.2311.154" style="border:0px;vertical-align:middle;" alt="Google Chrome 42.0.2311.154" height="24" width="24"> Google Chrome 42.0.2311.154  <img src="http://www.ha97.com/wp-content/plugins/wp-useragent/img/24/os/win-5.png" title="Windows 8.1 x64 Edition" style="border:0px;vertical-align:middle;" alt="Windows 8.1 x64 Edition" height="24" width="24"> Windows 8.1 x64 Edition</div><p>为什么我都改了 WINDOWS的 SHOW VARIABLES LIKE ‘character%’  character set database 还是latin1  server也是latin1</p>
    				</div>
    				<div class="reply">
    					 
    					<a href="#" onclick="moveAddCommentBelow(&quot;div-comment-693702&quot;, 693702, true); return false;">回复他(她)</a>
    										</div>
    			</div>
    				
    
    		</div>
    		<div id="div-comment-703937" class="clearfix comment">
    			<a name="comment-703937" id="comment-703937"></a>
    			<div class="title clearfix">
    				<div class="collapseicon" style="float:left" id="ment-703937">
    				</div>
    				<cite><a href="http://blog.whsir.com/" rel="external nofollow" class="url">吴昊博客</a><a href="#comment-703937"></a></cite> 
    								
    			</div>
    			<div class="body">
    				<div class="commmeta">
    					2016-12-09 13:29:05 						
    				</div>					
    				<div class="content clearfix">
    						<img alt="" src="http://0.gravatar.com/avatar/35de170fc7836ea645e1a7d7b307ff6e?s=32&amp;d=identicon&amp;r=g" srcset="http://0.gravatar.com/avatar/35de170fc7836ea645e1a7d7b307ff6e?s=64&amp;d=identicon&amp;r=g 2x" class="avatar avatar-32 photo" height="32" width="32">						<div class="wp-useragent"> <img src="http://www.ha97.com/wp-content/plugins/wp-useragent/img/24/net/chrome.png" title="Google Chrome 54.0.2840.87" style="border:0px;vertical-align:middle;" alt="Google Chrome 54.0.2840.87" height="24" width="24"> Google Chrome 54.0.2840.87  <img src="http://www.ha97.com/wp-content/plugins/wp-useragent/img/24/os/win-4.png" title="Windows 7 x64 Edition" style="border:0px;vertical-align:middle;" alt="Windows 7 x64 Edition" height="24" width="24"> Windows 7 x64 Edition</div><p>[mysql]字段不用添加utf8,我添加后会报mysql启动错误</p>
    				</div>
    				<div class="reply">
    					 
    					<a href="#" onclick="moveAddCommentBelow(&quot;div-comment-703937&quot;, 703937, true); return false;">回复他(她)</a>
    										</div>
    			</div>
    				
    
    		</div>
    
    	<div class="clearfix formr">
    <label>
    	昵称 *
    </label>
    <div>
    	<input name="author" id="author" class="textarea" value="" size="28" tabindex="1" type="text">
    </div>
    </div>
    	
    <div class="clearfix formr">	
    <label>
    	电子邮件 *
    </label>
    <div>
    	<input name="email" id="email" value="" size="28" tabindex="2" type="text">
    </div>
    </div>		
    	
    <div class="clearfix formr">
    <label>
    	网站/博客
    </label>
    <div>
    	<input name="url" id="url" value="" size="28" tabindex="3" type="text">	
    </div>
    </div>
    	<textarea name="comment" id="comment" cols="60" rows="10" tabindex="4"></textarea>
    <div>
    	<input name="comment_post_ID" value="5359" type="hidden">
    	<input name="redirect_to" value="/5359.html" type="hidden">
    	<input name="submit" id="submit" class="p" tabindex="5" value="发表" type="submit">
    </div>
    

  • 相关阅读:
    andriod的数据传递方法
    Activity生命周期详解
    android 和主线程有关的小问题
    java解析Excel(xls、xlsx两种格式)
    菜鸟感想一
    springboot 填坑一 springboot java.sql.SQLException: Access denied for user ''@'localhost' (using password: NO)
    git 第一次提交至仓库
    andriod studio报错 Emulator: emulator: ERROR: x86 emulation currently requires hardware acceleration! Emulator: Process finished with exit code 1
    通过以太坊发行代币(token)
    nginx+tomcat 配置负载均衡集群 (转载)
  • 原文地址:https://www.cnblogs.com/jpfss/p/7881726.html
Copyright © 2011-2022 走看看