mysql> show variables like 'character%';
(查看字符集,发现为latin1: character_set_server | latin1
)
2.[root@linux2018 ~]# cd /usr/share/mysql/
(进入mysql安装目录)
3. [root@linux2018 mysql]# ll
(列出mysql安装目录)
4. [root@linux2018 mysql]# cp my-huge.cnf /etc/my.cnf
(拷贝my-huge.cnf
文件到/etc/my.cnf
)
5. [root@linux2018 mysql]# cd /etc
(进入etc目录)
6. [root@linux2018 etc]# vim my.cnf
(进入my.cnf文件,编辑)
-
[client]
(客户端项配置)
default-character-set=utf8
#The MySQL server
-
[mysqld]
(服务端项配置)
character_set_server=utf8
character_set_client=utf8
collation-server=utf8_general_ci
-
[mysql]
(通用项配置)
default-character-set=utf8
-
[root@linux2018 etc]# service mysql restart
(重启mysql) -
mysql> show create database mydb;
(查看建库信息,发现为Latin1)
+----------+-----------------------------------------------------------------+
| mydb | CREATE DATABASE `mydb` /*!40100 DEFAULT CHARACTER SET latin1 */
-
mysql> alter database mydb character set 'utf8';
(修改库的字符集) -
mysql> alter table mytbl convert to character set 'utf8';
(修改表的字符集)
总结思路:
- 修改配置文件,支持utf8字符集
- 修改已经存在的库和表的字符集,改为utf8;
- 已经变为乱码的数据,重新灌入或update;