mysql中文乱码解决方法

时间:2022-09-22 19:04:15

以下方法只适用于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]>