apt-get install 安裝的,不是源碼包安裝的mysql
1 修改mysql的配置文件
/etc/mysql/conf.d/mysql.cnf
在[mysql]的下方加入如下語句:(注:這個文件下沒有配置,只有【mysql】)
no-auto-rehash
default-character-set=utf8
/etc/mysql/mysql.conf.d/mysqld.cnf
在[mysqld]下加入
socket = /var/run/mysqld/mysqld.sock
port = 3306
character-set-server=utf8 (這裡是server,之前有的版本是set)
重啟mysql: sudo /etc/init.d/mysql restart
查看字符集
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/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
設置之後如果之前的數據庫已經創建了,只能刪除重新創建了。