欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

Ubuntu更改MySQL字符集UTF-8,正确显示中文_MySQL

程序员文章站 2022-05-24 08:27:00
...
Ubuntu

首先停mysql服务,编辑配置文件my.cnf

$ sudo stop mysql$sudo vim /etc/mysql/my.cnf

[client] default-character-set=utf8 #增加这一行,指定字符集port            = 3306 socket          = /var/run/mysqld/mysqld.sock .............[mysqld] character_set_server=utf8 #增加这一行,指定字符集user            = mysql pid-file        = /var/run/mysqld/mysqld.pid

重启服务,字符集就成了UTF-8

$ sudo start mysql$ sudo mysql -uroot -pmysql> use rootmysql> show variables like '%char%' ;#更改前存在的用户字符集不会改变,以后新建的用户字符集都变成了UTF-8+--------------------------+----------------------------+| 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/ |+--------------------------+----------------------------+mysql> create database mydb;mysql> use mydbmysql> 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/ |+--------------------------+----------------------------+