摘要:使用apt-get 命令安装的mysql默认不是utf8、在这里记录一下如何将编码修改成utf8。
在终端界面输入上面命令、显示如下:
mysql Ver 14.14 Distrib 5.5.35, fordebian-linux-gnu (x86_64) using readline 6.2
a) 登录mysql
mysql –uroot –ppassword
b) 输入如下命令:
status
#会有如下显示: -------------- mysql Ver 14.14 Distrib5.5.35, for debian-linux-gnu (x86_64) using readline 6.2 Connection id: 45 Current database: Current user: root@localhost SSL: Not inuse Current pager: stdout Using outfile: '' Using delimiter: ; Server version: 5.5.35-0ubuntu0.12.04.2 (Ubuntu) Protocol version: 10 Connection: Localhost via UNIX socket Server characterset: latin1 Db characterset: latin1 Client characterset: utf8 Conn. characterset: utf8 UNIX socket: /var/run/mysqld/mysqld.sock Uptime: 1 hour15 min 13 sec Threads: 1 Questions:609 Slow queries: 0 Opens: 421 Flush tables: 1 Open tables:41 Queries per second avg: 0.134 --------------
a) 登录mysql
mysql –uroot –ppassword
b) 输入如下命令:
select version(); #会有如下显示: +-------------------------+ | version() | +-------------------------+ | 5.5.35-0ubuntu0.12.04.2 | +-------------------------+ 1 row in set (0.00 sec)
a) 输入命令:
mysql –uroot –p
b) 输入密码:
password
show variables like '%character%'; #会有如下显示: +--------------------------+----------------------------+ | 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/ | +--------------------------+----------------------------+ 8 rows in set (0.00 sec)
上面步骤中可以看出红色的部分的编码——latin1
a) 打开mysql配置文件:
vim/etc/mysql/my.cnf
b) 在[client]下追加:
default-character-set=utf8
c) 在[mysqld]下追加:
character-set-server=utf8
d) 在[mysql]下追加:
default-character-set=utf8
e) 保存并退出
下面两个任何一个都可以:
servicemysql restart /etc/init.d/mysqlrestart
a) 登录mysql
b) 输入:
show variables like '%character%'; #会有如下显示: +--------------------------+----------------------------+ | 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/ | +--------------------------+----------------------------+
则修改成功!