MySQL Chinese garbled Windows system

Although built on Windows Server Web applications rarely use MySQL, but I am poor and cash-strapped technology, MySQL free because of the lower cost and learning have become my first choice.
1. The operating environment
has, since graduation need to build a website ASP.net course of study, which used MySQL database, but also requires the mentor deployment server, but can only rent a Windows server Tencent cloud deployment.
Version of the database is MySQL 5.7 Windows version of the application is of ASP.net MVC 5, run in IIS8 above.
2. Chinese garbled
deploy all goes well, run access no problem, but once it becomes inserted into the Chinese character '???'. Intuition told me I should be a coding problem. For ease of operation, I am here to make presentations using MySQL Workbench, should actually use the command line.
1. Double-click the selected dump.
2. Execute the query show variables like 'char%';
Here Insert Picture Description

Can be seen in the figure character_set_connection and character_set_server is lantin1, perhaps this is the reason Chinese garbled appeared in it, then we should figure into all lantin1 utf8.

3. Modify encoding
the Internet just a search can be found to many solutions, roughly divided into two types:
by set character_set_server = utf8; statement to set up, but this method is to set the character set encoding of the database, MySQL encoding other databases still lantin1. Even if modify global set global character_set_database = utf8; after restart or change back lantin1.
So, we need another way to change the character set encoding by modifying the MySQL configuration file. First, in Windows, we need to find a my.ini file. Many online tutorials say it in the MySQL folder, but it is actually very misleading, because when installing MySQL, program files and data files are stored separately, many people can not find the folder in the MySQL my.ini because it is as a data file in the data folder inside. And this data in the Windows folder is hidden below.
To find it, first just open a folder, enter% ProgramData% Enter the path bar inside. Into the hidden folder, find the MySQL folder, find the MySQL Server 5.7 into the folder, enter you can see the my.ini.
Here Insert Picture Description

这里只需要用记事本打开,然后修改一些东西就好了。如果对ini文件不熟悉的同学可以先百度补充一波ini的知识再操作。
1.在 [client] 节下面增加character-set-server=utf8
2.在 [mysqld] 节下面增加character-set-server=utf8
3.在 [mysql] 节下增加default-character-set=utf8
4.这里要注意的是,这个ini文件不是以’;‘标记注释的,是以’#‘标记注释的,所以在文件中可以看到很多#。在 [mysqld] 和 [mysql] 节下面本来就有character-set-server和default-character-set 配置,只不过用’#‘注释了,要把’#'去掉。
5.至于在哪一行加都无所谓,ini文件中哪行没有关系,只要在规定的节里面就好了。

4.重启MySQL服务
如果有用MySQL Notifier的话,直接在那里点restart就好了,如果没有的话,就用命令行吧。Win+R -> cmd -> net stop mysql -> net start mysql。

5.设置数据库编码
现在我们已经设置好了MySQL的默认字符集编码,接下来为了安全起见,我们还要继续检查一下数据库的字符集编码,用 show variables like ‘char%’; 查看选中数据库的字符集编码,看看 character_set_connection 和 character_set_server是否变成了utf8。如果没变的话,检查一下数据库的Collation是不是utf8。如果不是则需要设置成utf8。

ALTER SCHEMA DB_Name DEFAULT CHARACTER SET utf8 ;
set character_set_server = utf8;
set character_set_connection = utf8;

6.命令行操作
其实我个人是比较鼓励用命令行对MySQL进行设置的,不过考虑到效率和正确性,还是需要装上WorkBench进行一些辅助。
1.进入MySQL命令行模式
Win+R -> cmd -> cd 你安装MySQL文件夹里面的bin文件夹
mysql -u root -p
输入密码
2.查看数据库字符集编码
use 数据库名
show variables like ‘char%’;
注意每句都要有’;’
3.设置字符集编码
set character_set_server=utf8;
set character_set_connection=utf8;

4.退出
exit

7.参考文章
https://blog.csdn.net/u012410733/article/details/61619656/
https://www.cnblogs.com/houqi/p/5713176.html
https://blog.csdn.net/zkzk96871/article/details/78307774

Published 15 original articles · won praise 2 · Views 4869

Guess you like

Origin blog.csdn.net/weixin_38138153/article/details/82958875