linux下mysql数据库中文乱码解决

往linux下的mysql(mariaDB)数据库插入中文数据时,显示乱码,中文出现问号,网上的解决方法没有提示修改配置后原来的数据库的编码格式是不变的,所以原来的数据库还是乱码,暂时只知道修改配置后重建数据库才能解决

一、问题

mysql数据库往表格插入中文数据,中文显示乱码’?’

如下,在数据库log中创建表dm_pvs_region,并插入一条含中文的数据,查看显示乱码:
linux下mysql数据库中文乱码解决_第1张图片

二、解决

乱码一般是默认字符集设置问题,修改配置即可(但这里有点小坑)

1、查看数据库默认编码格式(字符集)

linux下mysql数据库中文乱码解决_第2张图片
如上,character_set_database和character_set_server都默认是latin1.

2、修改配置文件/etc/my.cnf

在[mysqld]下添加:character_set_server=utf8

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
character_set_server=utf8
# 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
default-character-set = utf8
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

3、重启数据库服务并登陆查看

(一)重启并查看

[root@master etc]# systemctl restart mariadb.service
[root@master etc]# mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.60-MariaDB MariaDB Server

Copyright (c) 2000, 2018, 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 'character_set_%';
+--------------------------+----------------------------+
| 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)

如上显示,修改成功。但注意:修改配置后只对修改配置后新建的数据库起效,之前的不起效,所以最好是配置好后,再建数据库,建表。

(二)、进入log数据库,查看编码

MariaDB [(none)]> use log
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [log]> SHOW VARIABLES LIKE 'character_set_%';
+--------------------------+----------------------------+
| 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.00 sec)

如上,character_set_database还是latin1.
查看,还是乱码。说明修改配置对之前的数据库不起效。

MariaDB [log]> select * from dw_pvs_region;
+----+----------+------+-------+------+------+
| id | province | year | month | day  | pvs  |
+----+----------+------+-------+------+------+
|  1 | ??       | 2012 | 01    | 04   | 1000 |
+----+----------+------+-------+------+------+
1 row in set (0.00 sec)

(三)、新建数据库test测试查看成功

MariaDB [test]> SHOW VARIABLES LIKE 'character_set_%';
+--------------------------+----------------------------+
| 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 [test]> show tables;
+----------------+
| Tables_in_test |
+----------------+
| dw_pvs_region  |
+----------------+
1 row in set (0.00 sec)

MariaDB [test]> select * from dw_pvs_region;
+----+----------+------+-------+------+------+
| id | province | year | month | day  | pvs  |
+----+----------+------+-------+------+------+
|  1 | 广东     | 2012 | 01    | 04   | 1000 |
+----+----------+------+-------+------+------+
1 row in set (0.00 sec)

你可能感兴趣的:(hadoop)