CentOS解决中文乱码问题
场景:将本地数据保存到Linux虚拟机(远程服务器)中的数据库(Redis)时出现中文乱码。
解决步骤如下:
- 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.
- 通过指令查看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
- 停止mysql服务
[root@CentOs ~]# service mysqld stop
停止 mysqld:
- 编辑配置文件(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
- 注意:需要保存退出
- 重新启动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.
- 登录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.
- 再次查看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中存数据时就不会出现中文乱码问题了。赶紧去试试吧!