標籤:
重裝系統後,要裝個mysql伺服器,發現添加資料時,一直錯誤,查其原因,原來時編碼問題。Google了一下,可都不太一樣,可能時資料庫版本問題吧。
查看資料庫版本:
select version();
顯示:‘5.7.15-0ubuntu0.16.04.1‘
官方文檔:Mysql Document
1、查看資料庫編碼
show variables like ‘character%‘;
顯示結果:
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_client為用戶端編碼方式;character_set_connection為建立串連使用的編碼;character_set_database資料庫的編碼;character_set_results結果集的編碼;character_set_server資料庫伺服器的編碼;
只要保證以上四個採用的編碼方式一樣,就不會出現亂碼問題。
2、設定資料庫編碼
分為兩部分:設定資料庫伺服器編碼和設定資料庫編碼
設定資料庫編碼比較簡單,在建立資料庫時,可以指定編碼;
設定伺服器編碼的話,需要修改設定檔。設定檔路徑為:/etc/mysql/mysql.conf.d
找到[mysqld],在後面添加如下代碼後,重啟伺服器即可。
character-set-server=utf8collation-server=utf8_general_ci
3、重啟資料庫
service mysql restart
Linux下設定Mysql資料庫編碼