Ubuntu apt 安装 MySQL 5.7.29字符集更改

相关环境

Ubuntu 18.04 x64

apt 1.6.12 (amd64)

MySQL 5.7.29

配置字符集

安装好 MySQL 之后的第一件事情就是配置字符集,因为 MySQL 默认是 lantin1 的字符集,不支持中文,因此需要配置为支持中文的 UTF-8 字符集。由于 apt-get 安装的 MySQL 5.7.28 的配置文件有部分变化。因此,配置上有些许变化。

刚安装好的 MySQL 的默认字符集如下:


mysql> show variables like '%char%';
+--------------------------+----------------------------+
| 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     | latin1                     |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)


1、配置 mysql.cnf

mysql.cnf 位于 /etc/mysql/conf.d 目录下,mysql.cnf 的内容默认只有 [mysql] 一行,使用 sudo vi mysql.cnf 编辑文件,将内容改为如下:


[client]
default-character-set=utf8

[mysql]
default-character-set=utf8

2、配置 mysqld.conf

mysqld.conf 位于 /etc/mysql/mysql.conf.d 目录下,在 [mysqld] 节点下添加如下两行:


character-set-server=utf8
collation-server=utf8_general_ci

重启 MySQL

使用如下命令重启 mysql Server 服务,正常情况无报错信息则表示重启成功。


jiangzl@ubuntu18:/data$ sudo service mysql restart

root@ubuntu18:/etc/mysql/conf.d# ps -ef | grep mysql
mysql     34439      1  0 03:47 ?        00:00:00 /usr/sbin/mysqld --daemonize --pid-file=/run/mysqld/mysqld.pid
root      34534  34323  0 03:54 pts/2    00:00:00 grep --color=auto mysql

校验字符集

使用 mysql 命令行客户端 mysql 命令连接到 mysql server,查看当前的字符集情况,如下所示:


jiangzl@ubuntu18:~$ sudo mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.29-0ubuntu0.18.04.1 (Ubuntu)

Copyright (c) 2000, 2020, 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> how variables like '%char%';
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'how variables like '%char%'' at line 1
mysql> 
mysql> 
mysql> 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/ |
+--------------------------+----------------------------+
8 rows in set (0.01 sec)

mysql> 

可以看到,mysql 的字符集已经全部都改为了 utf8,更改完成。

发表评论

电子邮件地址不会被公开。 必填项已用*标注