解決資料庫亂碼問題的倚天屠龍劍
現象:開啟資料庫表,發現裡面輸入的資料就是亂碼;
查看錶字元編碼
mysql> show create table user \G;
*************************** 1. row ***************************
Table: user
Create Table: CREATE TABLE `user` (
`name` varchar(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
我們可以看到表的預設字符集是latin1.
所以我們在建立表的時候就需要指定表的字符集:
create table user(name varchar(11)) default charset=utf8;
那麼有沒有終極的徹底解決資料庫裡面資料亂碼的辦法呢?答案是有
參考這篇博文:
https://blog.csdn.net/u012410733/article/details/61619656
即可得到徹底解決,不要怕,下面就教你終極大招:
修改mysql配置檔案/etc/my.cnf。
[mysqld]
character-set-server=utf8
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
請注意這幾個引數配置的位置,不然可能會啟動不起來mysql服務:
OK。這下如果你重啟mysql服務也會發現它的字符集是utf8.
到此,我們建立表的時候不需要指定字元編碼,它預設就是utf8。你說好不好!
按下列操作看看吧。
drop database test;
create database test;
use test;
create table user(name varchar(11));
show create table user \G;
編碼問題已經全部統一起來了!
回到我這裡,就是按照上述操作,刪除資料庫,重新見表,重新insert資料。重新訪問伺服器專案,各個模組測試一遍,所有亂碼問題徹底解決掉了