mysql编码问题
我比较推荐的方法是在创建数据库时便设置中文编码
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-grant
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-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.log
pid-file=/var/run/mariadb/mariadb.pid
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
[root@localhost ~]#
接下来重启服务器
[root@localhost ~]# mysql -uroot -p Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 3 Server version: 5.5.52-MariaDB MariaDB Server Copyright (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]>
修改已存在数据库的编码:修改bp数据的编码为utf8
alter database bp character set utf8;
修改已存在的数据库表编码(如果不加convert to 的话,只能修改表格编码,但是不会自动修改表格字段编码,待验证)
alter table `tablename` convert to character set utf8;