在我们使用MySQL数据库时,字符乱码,对我们来说是一个很头疼的问题。今天笔者就来教大家如何彻底解决更改默认字符集以及字符乱码问题。
当我们使用压缩包进行MySQL安装后,系统会使用默认的字符集,这时就会产生乱码。
方案:
来到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. [mysqld] # 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) datadir = 此处为你的安装路径(例:D:\mysql\data) port = 3306 # server_id = mysql character_set_server = 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 [client] port = 3306 default-character-set = utf8
其中路径一定不能错,不然会无法启动MySQL服务。
此配置启用需要重启MySQL服务。
此时通过管理员运行的CMD,停止MySQL服务:net stop mysql。
接着输入:net start mysql
此时,用root进入MySQL数据库(mysql -u root -p),输入show variables like 'char%'; 查看字符集设置。
至此,MySQL的字符集修改以及乱码问题就解决了。。。