mysql设置数据库编码为utf-8

在使用mysql过程中,发现由于要用到中文,但是输入进去数据库中的中文无法通过web页面显示出来。排查定位到是编码问题。故修改数据库编码为utf-8。

1.使用root账号,打开mysql 的配置文件/etc/mysql/my.cnf.

添加如下代码:(一定要注意下划线和横杠的区别)

[client]
port = 3306
socket = /var/run/mysqld/mysqld.sock
default_character_set=utf8(此处为添加部分)

[mysqld]

* Basic Settings

user = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port = 3306
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
lc-messages-dir = /usr/share/mysql
character-set-server=utf8(此处为添加部分)
init_connect='SET NAMES utf8'(此处为添加部分)

2.然后重启mysql。

/etc/init.d/mysql restart

3.然后进入数据库中。

查看数据库编码配置:

show variables like 'character%';

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/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

此时数据库就正常了。

作者:Troublemaker201407
来源:CSDN
原文:https://blog.csdn.net/suhanjiao4897/article/details/53893686
版权声明:本文为博主原创文章,转载请附上博文链接!

你可能感兴趣的:(mysql设置数据库编码为utf-8)