1、查看你编码
SHOW VARIABLES WHERE Variable_name LIKE 'character_set_%' OR Variable_name LIKE 'collation%';(如果不是下图则继续)
2、修改默认参数 /etc/my.cnf
[mysqld]
datadir=/usr/local/mysql/data
basedir=/usr/local/mysql
socket=/usr/local/mysql/data/mysql.sock
user=mysql
port=3306
character-set-server=utf8mb4
symbolic-links=0
lower_case_table_names=1
default-storage-engine=INNODB
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
[client]
default-character-set=utf8mb4
port=3306
socket=/usr/local/mysql/data/mysql.sock
标红的没有则添加
3、修改编码语句
更改数据库编码:
ALTER DATABASE DATABASE_NAME CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
更改表编码:
ALTER TABLE TABLE_NAME CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
4、如果报如下错
5、解决Specified key wai too long; max key length is 767 bytes
查看:
show variables like 'innodb_large_prefix';
show variables like 'innodb_file_format';
修改参数
SET GLOBAL innodb_large_prefix = ON;
SET GLOBAL innodb_file_format=BARRACUDA;
将这两参数设置到/etc/my.cnf 下,否则重启会恢复默认
innodb_file_format=BARRACUDA
innodb_large_prefix=ON
6、修改表格式
将innodb_large_prefix参数值修改为ON后,对于Dynamic和Compressed格式的InnoDB引擎表,其支持的最大索引字段长度为3072字节。
只支持DYNAMIC和COMPRESSED格式的表,compact格式的表不支持。
创建表时配置:指定表的Row Format为Dynamic或Compressed,示例如下。
create table [$Table_Name] (id int auto_increment primary key, name varchar(255)) ROW_FORMAT=DYNAMIC default charset utf8mb4;
1
说明 [$Table_Name]为表名。
修改已有表配置:
执行以下SQL语句,查看Row_format取值,如果取值不是DYNAMIC和COMPRESSED(任意一种格式即可),则执行后续步骤修改取值。
alter table [$Table_Name] Row_format=dynamic;
alter table [$Table_Name] Row_format=compressed;
ALTER TABLE [$Table_Name] ENGINE = InnoDB ROW_FORMAT = compressed;
查看
show table status like 'act_hi_actinst'\G;
7、修改完成后再执行第三步修改表编码