做開發時很多時候會碰到亂碼的問題;
有的時候可能是從后台傳到數據庫時亂碼,這時候可能就是數據庫默認的字符集編碼有問題
默認的字符集為
mysql配置文件為my.ini(在linux下為my.cnf)
[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.
# 設置mysql的安裝目錄
basedir = D:/soft/mysql-5.6.36-winx64
# 設置mysql數據庫的數據的存放目錄,必須是data
datadir = D:/soft/mysql-5.6.36-winx64/data
port =3306
# server_id = .....
# 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
在doc下進入mysql
輸入show variables like 'character%';查看字符集
結果如下:
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | latin1 |
| character_set_system | latin1 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
解決方案:
修改配置文件
mysql配置文件為my.ini(在linux下為my.cnf)
找到[mysqld] 添加:
character-set-server=utf8
init_connect='SET NAMES utf8'
然后在最后面添加
[client]
default-character-set=utf8
修改后為:
[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.
# 設置mysql的安裝目錄
basedir = D:/soft/mysql-5.6.36-winx64
# 設置mysql數據庫的數據的存放目錄,必須是data
datadir = D:/soft/mysql-5.6.36-winx64/data
port =3306
character-set-server=utf8
init_connect='SET NAMES utf8'
# server_id = .....
# 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]
default-character-set=utf8
重啟mysql服務
然后在查看mysql編碼
+--------------------------+----------------------------+
| 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/ |
+--------------------------+----------------------------+
搞定。