前言
mysql版本 mysql 8.0.27
一、Mysql如何支持中文
# 查看镜像
[root@iZ2ze6kil8ulyuyvk3ef91 ~] docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c79316da0bda mysql "docker-entrypoint.s…" 1 weeks ago Up 18 hours 33060/tcp, 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp mydb1
# 复制数据库配置文件到本地当前路径
[root@iZ2ze6kil8ulyuyvk3ef91 ~] docker cp mydb1:/etc/mysql .
# 修改数据库配置
[root@iZ2ze6kil8ulyuyvk3ef91 ~] vi mysql/my.cnf
# Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# The MySQL Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
secure-file-priv= NULL
#################复制开始######################
# 服务端编码配置
character-set-server=utf8
[mysql]
# 客户端编码配置
default-character-set=utf8
#################复制结束######################
# Custom config should go here
!includedir /etc/mysql/conf.d/
#
# 拷贝配置文件至容器
[root@iZ2ze6kil8ulyuyvk3ef91 ~] docker cp ./mysql/my.cnf mydb1:/etc/mysql
# 进入容器查看编码格式
[root@iZ2ze6kil8ulyuyvk3ef91 ~] docker exec -it mydb1 mysql -uroot -p
Enter password:
mysql> show variables like '%character%';
+--------------------------+--------------------------------+
| Variable_name | Value |
+--------------------------+--------------------------------+
| character_set_client | utf8mb3 |
| character_set_connection | utf8mb3 |
| character_set_database | utf8mb3 |
| character_set_filesystem | binary |
| character_set_results | utf8mb3 |
| character_set_server | utf8mb3 |
| character_set_system | utf8mb3 |
| character_sets_dir | /usr/share/mysql-8.0/charsets/ |
+--------------------------+--------------------------------+
8 rows in set (0.00 sec)
配置成功
二、Navicat无法连接数据库(1251)
1.更改加密规则
将加密方式改为mysql_native_password
mysql> use mysql;
Database changed
mysql> SELECT Host, User, plugin from user;
+-----------+------------------+-----------------------+
| Host | User | plugin |
+-----------+------------------+-----------------------+
| % | root | caching_sha2_password |
+-----------+------------------+-----------------------+
mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'root';
mysql> SELECT Host, User, plugin from user;
+-----------+------------------+-----------------------+
| Host | User | plugin |
+-----------+------------------+-----------------------+
| % | root | mysql_native_password |
+-----------+------------------+-----------------------+
# 修改成功
出现这个原因是mysql8 之前的版本中加密规则是mysql_native_password,而在mysql8之后,加密规则是caching_sha2_password