查看修改MySQL字符集以及乱码解决

2019-02-16 09:15:47   MySQL
  MySQL  

查看和修改字符集

  • Terminal下连接MySQL
  • 输入以下语句
    
    mysql> show variables like 'collation_%';
    +----------------------+-----------------+
    | Variable_name        | Value           |
    +----------------------+-----------------+
    | collation_connection | utf8_general_ci |
    | collation_database   | utf8_general_ci |
    | collation_server     | utf8_general_ci |
    +----------------------+-----------------+
    3 rows in set (0.00 sec)

mysql> show variables like 'characterset%'; +--------------------------+------------------------------------------------------+ | 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/local/Cellar/mysql/5.7.18/share/mysql/charsets/ | +--------------------------+------------------------------------------------------+ 8 rows in set (0.00 sec)

可以看到你的数据库当前各个字符集变量的值

- 命令行修改字符集编码

mysql> set character_set_client=utf8; Query OK, 0 rows affected (0.00 sec) mysql> set character_set_database=utf8; Query OK, 0 rows affected (0.00 sec) mysql> set character_set_server=utf8; Query OK, 0 rows affected (0.00 sec)

不一一赘述了,其他的修改方式是一样的

- 配置文件修改字符集

vim /etc/my.cnf 在[client]下添加 default-character-set=utf8mb4 在[mysql]下添加 default-character-set=utf8mb4 在[mysqld]下添加 character-set-server=utf8mb4 collation-server = utf8mb4_unicode_ci init_connect='SET NAMES utf8mb4'

重启mysql服务

service mysql restart [Mac下为mysql.server restart]


随后配置就生效了,重新连接MySQL,可以利用前面提到的查询语句再去看一下

**ps:Mac下的navicat出现乱码问题,在确认数据库编码没有问题,而且命令行下查询不是乱码的情况下,修改connection的encoding为auto,可能会解决问题。**
vien.tech版权所有,允许转载,但转载请注明出处和原文链接: https://vien.tech/article/63
欢迎小伙伴们在下方评论区留言 ~ O(∩_∩)O
文章对我有帮助, 点此请博主吃包辣条 ~ O(∩_∩)O

猜你喜欢


评论

There are no comments yet.
未登录

登录后即可发表评论

登录或注册