You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Webmin中修改MySQL服务器字符集遇问题求助

问题排查与解决方案

一、特殊字符读取乱码问题

1. 强制后端连接时指定UTF16字符集

后端代码连接MySQL时,必须显式设置连接字符集为utf16,否则默认字符集(通常是utf8mb4或utf8)会导致编码转换错误。以PHP为例:

  • mysqli扩展:
$conn = mysqli_connect('localhost', 'user', 'pass', 'db');
mysqli_set_charset($conn, 'utf16'); // 关键设置
  • PDO扩展:
$dsn = 'mysql:host=localhost;dbname=your_db;charset=utf16';
$pdo = new PDO($dsn, 'user', 'pass');

phpMyAdmin会自动匹配数据库字符集,所以测试正常,后端遗漏此设置是核心原因之一。

2. 统一MySQL服务器全局字符集配置

即使库、表、字段已设为utf16_bin,MySQL服务器的全局字符集变量可能未同步,导致连接阶段的编码转换异常。执行以下SQL查看当前配置:

SHOW VARIABLES LIKE 'character_set%';
SHOW VARIABLES LIKE 'collation%';

确保以下变量值为utf16或utf16_bin:

  • character_set_server
  • character_set_client
  • character_set_connection
  • collation_server

修改MySQL配置文件(通常是/etc/my.cnf或/etc/mysql/my.cnf),添加或更新:

[mysqld]
character_set_server=utf16
character_set_client=utf16
character_set_connection=utf16
collation_server=utf16_bin

保存后重启MySQL服务:

systemctl restart mysql

3. 确认后端输出的页面编码

如果是Web后端,需确保页面响应头或HTML meta标签指定UTF-16编码:

<meta charset="UTF-16">

或通过后端代码设置响应头:

header('Content-Type: text/html; charset=UTF-16');

否则浏览器会用默认编码解析,导致显示乱码。

二、Webmin切换UTF16/UTF32时的报错问题

报错信息:

The MySQL client program /usr/bin/mysql does not accept passwords passed using the MYSQL_PWD environment variable. To ensure that Webmin is able to fully communicate with MySQL, this option should be turned off on the module configuration page. Alternately, you can remove any password set in the root user's .my.cnf file.

解决步骤:

  1. 登录Webmin,进入MySQL Database Server模块,点击左侧菜单的Module Configuration。
  2. 找到Use MYSQL_PWD environment variable for password选项,取消勾选,保存配置。
  3. 如果上述操作无效,检查root用户的主目录下的.my.cnf文件(路径通常是/root/.my.cnf),如果存在类似以下内容,删除password行:
[client]
user=root
password=your_password

保存后重新登录Webmin即可解决。

内容的提问来源于stack exchange,提问作者StackProg

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 06:27:21