- 通过set修改value字段
- 登录MySQL,使用 show variables like 'character%';
- 查看当前编码格式

- 使用 set character_set_client = 'utf8mb4';
- 直接修改variable_name的value

- Mariadb通过配置文件修改编码
-
1、登录MySQL,使用
SHOW VARIABLES LIKE 'character%';查看当前使用的字符集,应该有好几个不是UTF-8格式。
2、要修改的配置文件位于 /etc/my.cnf.d目录下:
client.cnf
在[client]字段里加入
default-character-set=utf8server.cnf
在[mysqld]字段里加入
character-set-server=utf8systemctl restart mariadb 配置生效,修改后的效果如下:
-
修改字段编码格式:
-
先查看下编码:
MariaDB [(none)]> SHOW VARIABLES WHERE Variable_name LIKE 'character\_set\_%' OR Variable_name LIKE 'collation%'; +--------------------------+-------------------+ | 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 | | collation_connection | utf8_general_ci | | collation_database | latin1_swedish_ci | | collation_server | latin1_swedish_ci | +--------------------------+-------------------+ 10 rows in set (0.00 sec)修改/etc/my.cnf.d/server.cnf
[mysqld][mysqld] character_set_server=utf8mb4 collation-server=utf8mb4_unicode_ci init_connect='SET NAMES utf8mb4' skip-character-set-client-handshake=true重启DB:
systemctl restart mariadb再次查看编码:
MariaDB [(none)]> SHOW VARIABLES WHERE Variable_name LIKE 'character\_set\_%' OR Variable_name LIKE 'collation%'; +--------------------------+--------------------+ | Variable_name | Value | +--------------------------+--------------------+ | character_set_client | utf8mb4 | | character_set_connection | utf8mb4 | | character_set_database | utf8mb4 | | character_set_filesystem | binary | | character_set_results | utf8mb4 | | character_set_server | utf8mb4 | | character_set_system | utf8 | | collation_connection | utf8mb4_unicode_ci | | collation_database | utf8mb4_unicode_ci | | collation_server | utf8mb4_unicode_ci | +--------------------------+--------------------+ 10 rows in set (0.00 sec)修改DB的默认编码:
ALTER DATABASE XX_db CHARACTER SET utf8mb4;1.查看数据库编码格式
1
mysql> show variableslike'character_set_database';2.查看数据表的编码格式
1
mysql> showcreatetable<表名>;3.创建数据库时指定数据库的字符集
mysql>create database <数据库名> character set utf8;4.创建数据表时指定数据表的编码格式
create table tb_books ( name varchar(45) not null, price double not null, bookCount int not null, author varchar(45) not null ) default charset = utf8;5.修改数据库的编码格式
mysql>alter database <数据库名> character set utf8;6.修改DB的默认编码:
ALTER DATABASE XX_db CHARACTER SET utf8mb4;7.修改表中字段编码
ALTER TABLE 表名 MODIFY `字段` TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;8.添加外键
mysql>alter table tb_product add constraint fk_1 foreign key(factoryid) references tb_factory(factoryid); mysql>alter table <表名> add constraint <外键名> foreign key<字段名> REFERENCES <外表表名><字段名>;9.删除外键
mysql>alter table tb_people drop foreign key fk_1; mysql>alter table <表名> drop foreign key <外键名>;
本文详细介绍了如何在MariaDB中修改字符编码格式,包括通过命令行临时设置和配置文件永久修改。步骤包括查看当前编码、编辑配置文件如client.cnf和server.cnf,重启服务,以及修改数据库、数据表和字段的编码格式,确保全面支持UTF-8。
7531






