1. 程式人生 > >mysql5.7設定預設編碼

mysql5.7設定預設編碼

1、通過 show variables like '%char%';檢視MySQL字符集情況

mysql> show variables like '%char%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| 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/ |
+--------------------------+----------------------------+

2、修改mysql配置檔案
#備份配置檔案
sudo cp  /etc/mysql/mysql.conf.d/mysqld.cnf /etc/mysql/mysql.conf.d/mysqld.cnf_2
sudo vim  /etc/mysql/mysql.conf.d/mysqld.cnf
# 修改處1:新增以下2行
[client]
default-character-set=utf8

[mysqld]
# 修改處2:新增以下3行
default-storage-engine=INNODB
character-set-server=utf8
collation-server=utf8_general_ci

3、重啟mysql
service mysql restart

4、檢視字符集
mysql -uroot -p

mysql> show variables like "%char%";
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+