CentOS解决中文乱码

CentOS解决中文乱码问题

场景:将本地数据保存到Linux虚拟机(远程服务器)中的数据库(Redis)时出现中文乱码。

解决步骤如下:

  1. root账号登录mysql(Linux系统不需要账号密码)
[root@CentOs ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 228
Server version: 5.1.73 Source distribution

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  1. 通过指令查看mysql的编码格式(show variables like ‘charac%’?
mysql> show variables like 'charac%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | latin1                     |
| character_set_connection | latin1                     |
| character_set_database   | latin1                     |
| character_set_filesystem | binary                     |
| character_set_results    | latin1                     |
| character_set_server     | latin1                     |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
  • 需要将编码格式为latin1设置为utf8
  1. 停止mysql服务
[root@CentOs ~]# service mysqld stop
停止 mysqld: 
  1. 编辑配置文件(vim /etc/my.cnf)
[root@CentOs ~]# vim /etc/my.cnf
[mysqld]
default-character-set = utf8 #添加 2018-07-16
character_set_server=utf8 #添加 2018-07-16
init_connect='SET NAMES utf8' #添加 2018-07-16
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

[mysqld_safe]
default-character-set = utf8 #添加 2018-07-16
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

[client]
default-character-set = utf8 #添加 2018-07-16

[mysql.server]
default-character-set = utf8 #添加 2018-07-16

[mysql]
default-character-set = utf8 #添加 2018-07-16
  • 注意:需要保存退出
  1. 重新启动mysql服务(service mysqld start)
[root@CentOs /]# service mysqld start
正在启动 mysqld:                                          [确定]
[root@CentOs /]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.1.73 Source distribution

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  1. 登录mysql
[root@CentOs /]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.1.73 Source distribution

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  1. 再次查看mysql的编码格式
mysql> show variables like 'charac%';
+--------------------------+----------------------------+
| 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)
  • 至此,mysql中的编码格式已经设置完毕,当我们再次往CentOS中存数据时就不会出现中文乱码问题了。赶紧去试试吧!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值