博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mysql中文编码问题
阅读量:5827 次
发布时间:2019-06-18

本文共 3920 字,大约阅读时间需要 13 分钟。

我比较推荐的方法是在创建数据库时便设置中文编码

create database bp default character set utf8;          #注意是utf8不是utf-8

以下方法只适用于mysql5.5以上版本的(其实我的是mariadb5.5版本的)

编辑mysql配置文件

[root@localhost ~]# cat /etc/my.cnf[mysqld]#log-bin=mysql-bin                      #这行是增量备份那个实验的character_set_server=utf8               #添加这行collation-server=utf8_general_ci        #添加这行init_connect='SET NAMES utf8'           #添加这行#binlog_format=row#skip-grantdatadir=/var/lib/mysqlsocket=/var/lib/mysql/mysql.sock# Disabling symbolic-links is recommended to prevent assorted security riskssymbolic-links=0# Settings user and group are ignored when systemd is used.# If you need to run mysqld under a different user or group,# customize your systemd unit file for mariadb according to the# instructions in http://fedoraproject.org/wiki/Systemd[mysqld_safe]log-error=/var/log/mariadb/mariadb.logpid-file=/var/run/mariadb/mariadb.pid## include all files from the config directory#!includedir /etc/my.cnf.d[root@localhost ~]#

接下来重启服务器

[root@localhost ~]# mysql -uroot -pEnter password: Welcome to the MariaDB monitor.  Commands end with ; or \g.Your MariaDB connection id is 3Server version: 5.5.52-MariaDB MariaDB ServerCopyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.MariaDB [(none)]> 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)MariaDB [bp]> create table zhao(id int ,name varchar(20));              #创建表时没有设置中文编码,导致中文还算是乱码Query OK, 0 rows affected (0.01 sec)MariaDB [bp]> insert into zhao values(3,'速度飞快将');Query OK, 1 row affected, 1 warning (0.01 sec)MariaDB [bp]> select * from zhao;+------+-------+| id   | name  |+------+-------+|    3 | ????? |+------+-------+1 row in set (0.00 sec)MariaDB [bp]> create table ff(id int ,name varchar(20)) character set utf8;             #创建表时设置中文编码Query OK, 0 rows affected (0.07 sec)MariaDB [bp]> insert into ff values(3,'速度飞快将');Query OK, 1 row affected (0.01 sec)MariaDB [bp]> select * from ff;                     #显示正常+------+-----------------+| id   | name            |+------+-----------------+|    3 | 速度飞快将      |+------+-----------------+1 row in set (0.00 sec)MariaDB [bp]> show variables like '%char%';                 #utf8变回latin1,暂时还不知道原因,但似乎这个没有什么大影响+--------------------------+----------------------------+| 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     | utf8                       || character_set_system     | utf8                       || character_sets_dir       | /usr/share/mysql/charsets/ |+--------------------------+----------------------------+8 rows in set (0.01 sec)MariaDB [bp]> select * from ff;+------+-----------------+| id   | name            |+------+-----------------+|    3 | 速度飞快将      |+------+-----------------+1 row in set (0.01 sec)MariaDB [bp]>

转载于:https://www.cnblogs.com/biaopei/p/7730506.html

你可能感兴趣的文章
递归法----整数划分问题
查看>>
【小梅哥FPGA进阶教程】第十三章 四通道数字电压表
查看>>
安卓ListView的性能优化
查看>>
利用WM_COPYDATA进行进程间通信
查看>>
jQuery学习笔记之jQuery的Ajax(3)
查看>>
谷歌阅读器将于2013年7月1日停止服务,博客订阅转移到邮箱
查看>>
HTML5 自动聚焦 属性
查看>>
解决FormClosing事件点击关闭2次的问题
查看>>
python的字符串连接操作符+
查看>>
类属性赋值
查看>>
node、npm的安装和环境变量的配置
查看>>
spring的几种注入方式
查看>>
1.环境搭建
查看>>
计算几何摸黑(3)-圆及有关计算(上)
查看>>
NYOJ 737 石子合并(一)
查看>>
后缀树
查看>>
Python 3 面向对象进阶
查看>>
MAVEN for mac 安装
查看>>
9、StringBuffer和StringBuilder
查看>>
转 白话经典算法系列之六 快速排序 快速搞定
查看>>