我的MySQL是5.6版本。用python抓取了一些东西放进去的时候,中文乱码,全是问号。于是开始了漫漫的修正之路。现在记录如下,非常的简单。
首先挂掉MySQL,以管理员的方式打开cmd。
输入 net stop mysql。
ok,现在打开MySQL的路径:
如果你的文件不是my.ini不要惊慌,不要着急,忧郁的日子总会过去。只要把那个my-default.ini拷贝一份变成my.ini就可以了呀~
好的,现在打开my.ini,进行如下修改:
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.
[client]
default-character-set=utf8
[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
# These are commonly set, remove the # and set as required.
# basedir = D:\MySQL\mysql-5.6.24
# datadir = D:\MySQL\mysql-5.6.24\data
# port = 3306
# server_id = .....
# default-character-set=utf8
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
ok,保存吧兄dei。现在重新打开MySQL。
输入net start mysql。
好了,接下来进入mysql,就是mysql -u root -p,然后输入你的密码。
输入show variables like 'char%'
第一张图是更改ini之前的,第二张图是更改之后的。我们要的就是第二张图的效果
图一
图二
如果你的数据库还没有建立过啥表的什么的,就不用管了。开开心心的新建你的表去吧。氮素如果你还有些表已经是中文乱码了,还不舍得删除。那就往下看。
因为在你更改MySQL配置之前,你建的表并不是utf-8,你改完之后再新建的表才是utf-8。所以同志,你要手动更改老表的编码
ALTER DATABASE ‘数据库名’DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci
ALTER TABLE ‘数据库名.表名’ DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci
举个栗子,现在我的数据库是taobao,我的表名是meishi。我做了上面两个操作以后,成了这个样子:
然鹅,悲催的是,我以为我可以了,结果我看了一下,我的表表还是乱码,为什么呢?于是乎我把表删除了,然后重新建立了meishi数据表。然后查看参数,是这样的:
这时候就可以显示中文了。如下:
我也不造为什么。所以就没有深究。其中的一些设置,意思如下:
– character_set_client:客户端来源数据使用的字符集
– character_set_connection:连接层字符集
– character_set_database:当前选中数据库的默认字符集
– character_set_results:查询结果字符集
– character_set_server:默认的内部操作字符集
– character_set_system:系统元数据(字段名等)字符集