水煮鱼 发表于 2023-3-2 08:51:10

将 discuz 数据库编码从 utf8 转为 utf8mb4

使用 emoji 特殊字符表情,需要将 discuz 的数据库编码转换为utf8mb4

1. 确认数据库版本支持 utf8mb4 和 utf8mb4_0900_ai_ci
2. 由于用户表中 username 是不重复索引,且区分大小写和声调,字符集需设置为 utf8mb4_general_ci
3. 手动执行下面的 SELECT CONCAT 语句,然后使用执行结果替换该语句,保存为一个 sql 文件
4. 为避免 php 运行超时,请使用 cli 工具导入保存的 sql 文件
5. 转换前先备份数据库
6. 将 config/config_global.php 与 config/config_ucenter.php 中的 dbcharset 的值由 utf8 改为 utf8mb4
7. 将 uc_server/data/config.inc.php 中的 dbcharset 的值由 utf8 改为 utf8mb4

USE `discuz_databese`;

-- 更改主库字符集

ALTER DATABASE `discuz_databese` COLLATE utf8mb4_0900_ai_ci;

-- 请使用下列语句的执行结果替换该语句

SELECT CONCAT(
'ALTER TABLE ', `TABLE_NAME`, ' CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci;'
) FROM `information_schema`.`TABLES`
WHERE `TABLE_SCHEMA` = 'discuz_databese'
    AND `TABLE_NAME` <> 'pre_common_member'
    AND `TABLE_NAME` <> 'pre_common_member_archive'
    AND `TABLE_NAME` <> 'pre_ucenter_members';

-- 单独修改用户表(`username`字符集为`utf8mb4_general_ci`)

ALTER TABLE `pre_common_member` COLLATE utf8mb4_0900_ai_ci,
MODIFY `email` char(40) NOT NULL DEFAULT '',
MODIFY `username` char(15) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '',
MODIFY `password` char(32) NOT NULL DEFAULT '',
MODIFY `extgroupids` char(20) NOT NULL DEFAULT '',
MODIFY `timeoffset` char(4) NOT NULL DEFAULT '';

ALTER TABLE `pre_common_member_archive` COLLATE utf8mb4_0900_ai_ci,
MODIFY `email` char(40) NOT NULL DEFAULT '',
MODIFY `username` char(15) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '',
MODIFY `password` char(32) NOT NULL DEFAULT '',
MODIFY `extgroupids` char(20) NOT NULL DEFAULT '',
MODIFY `timeoffset` char(4) NOT NULL DEFAULT '';

ALTER TABLE `pre_ucenter_members` COLLATE utf8mb4_0900_ai_ci,
MODIFY `username` char(15) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '',
MODIFY `password` char(32) NOT NULL DEFAULT '',
MODIFY `email` char(32) NOT NULL DEFAULT '',
MODIFY `myid` char(30) NOT NULL DEFAULT '',
MODIFY `myidkey` char(16) NOT NULL DEFAULT '',
MODIFY `regip` char(15) NOT NULL DEFAULT '',
MODIFY `salt` char(6) NOT NULL,
MODIFY `secques` char(8) NOT NULL DEFAULT '';

补充 (只给有需要的使用):
-- 创建临时库

CREATE DATABASE `tmp123`;

-- 从原始数据库复制表(会丢弃索引)

USE `discuz_database`;

CREATE TABLE `tmp123`.`pre_common_member` AS SELECT * FROM `pre_common_member`;
CREATE TABLE `tmp123`.`pre_common_member_archive` AS SELECT * FROM `pre_common_member_archive`;

CREATE TABLE `tmp123`.`pre_ucenter_members` AS SELECT * FROM `pre_ucenter_members`;

-- 进入临时库

USE `tmp123`;

-- 转换数据库编码和字符集(已无索引,可以转换)

ALTER TABLE `pre_common_member` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci;
ALTER TABLE `pre_common_member_archive` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci;

ALTER TABLE `pre_ucenter_members` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci;

-- 获取重复的用户统计到临时表

CREATE TABLE `pre_ucenter_members_having_count` AS
SELECT `username`, count(username) as `count` FROM `pre_ucenter_members`
    GROUP BY `username` HAVING `count` > 1;

-- 获取重复的用户详情到临时表

CREATE TABLE `pre_ucenter_members_having` AS
SELECT * FROM `pre_ucenter_members` WHERE `username` IN (
      SELECT `username` FROM `pre_ucenter_members_having_count`
);


页: [1]
查看完整版本: 将 discuz 数据库编码从 utf8 转为 utf8mb4