问题:html
向mysql 数据库插入数据是,出现中文乱码(中文均显示为‘??’)java
缘由:mysql
mysql 默认的字符集是latin1,因此我么须要改成ut8编码才能够sql
解决:数据库
一、以管理员权限运行cmd窗口服务器
二、查看当前字符集app
>net start mysqlide
启动mysql服务post
>mysqlui
进入mysql运行环境
>show variables like 'character%';
查看当前字符集编码状况
其中,character_set_client为客户端编码方式;
character_set_connection为创建链接使用的编码;
character_set_database数据库的编码;
character_set_results结果集的编码;
character_set_server数据库服务器的编码;
只要保证以上四个采用的编码方式同样,就不会出现乱码问题。
三、修改数据库编码格式
>select @@basedir;
在mysql环境中查看mysql的安装位置
找到这个安装位置,下图为安装位置下的文件
本机的mysql是经过解压安装的,开始时只有my-default.ini文件,无my.ini文件
这时复制一份my-default.init并重命名为my.ini
打开my.ini文件
在[mysqld] 下添加character-set-server=utf8
在[client] 下添加:default-character-set=utf8
# For advice on how to change settings please see # http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html # *** DO NOT EDIT THIS FILE. It's a template which will be copied to the # *** default location during install, and will be replaced if you # *** upgrade to a newer version of MySQL. [mysqld] character-set-server=utf8 # 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. # basedir = ..... # datadir = ..... # port = ..... # 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] port=3306 default-character-set=utf8
保存文件
四、从新在mysql环境中查看字符编码
发现这时的字符编码已经改成utf8
五、若是更改字符编码以前已经有建立的数据表,能够经过以下语句,更改已经建立数据表的字符格式
mysql> alter table testapp_article convert to character set utf8;
也能够在建立数据表的时候指定使用utf8编码,方法是在建立语句后加上character set = utf8
如:
create table test_table ( id int auto_increment, title text, content text, posted_on datetime, primary key (id) ) character set = utf8;
参考: