Operations 6 min read

Sed Command Hangs During MySQL MHA Installation Due to Charset Mismatch and Comment Formatting

The article analyzes why a sed command freezes while modifying a MySQL MHA installation script on a CentOS 6 system, identifies the charset conversion issue caused by GBK locale and missing spaces after comment symbols, and provides a step‑by‑step debugging and fix procedure.

Aikesheng Open Source Community
Aikesheng Open Source Community
Aikesheng Open Source Community
Sed Command Hangs During MySQL MHA Installation Due to Charset Mismatch and Comment Formatting

The author, responsible for Oracle, MySQL, MongoDB and Redis maintenance, reports that running sed -i "s/.*vip.*ping valid.*/#&/g" mha_install.sh on a MySQL server installation script hangs, with the sed process consuming 100% CPU.

Using top and pstack , the stack trace shows the process stuck in the gconv() function, which performs character‑set conversion. The script file is UTF‑8, while the current session locale is zh_CN.gbk , causing the conversion to fail.

file -i mha_install.sh
mha_install.sh: text/x-shellscript; charset=utf-8
locale
LANG=zh_CN.gbk
...

Setting LANG=en_US disables the conversion and the sed command returns instantly.

export LANG=en_US
sed -i "s/.*vip.*ping valid.*/#&/g" mha_install.sh

To locate the problematic line, a binary search pinpoints line 325, which contains a comment without a space before Chinese characters ( #生成密钥对 ). This format triggers the bug in older sed versions.

head -n 325 mha_install.sh|tail -1
#生成密钥对

Testing with a file that has a space after the comment symbol ( # 生成密钥对 ) shows sed works correctly, confirming the issue.

cat fxtest.txt
# 生成密钥对
#生成密钥对
head -n 1 fxtest.txt |sed -n '1,$p'
# 生成密钥对

The problem appears on CentOS 6 with GNU sed 4.2.1, while CentOS 7 ships sed 4.2.2, which does not exhibit the bug. Replacing the older sed binary with the newer version resolves the hang.

sed --version
GNU sed 4.2.1
# on CentOS 7
sed --version
sed (GNU sed) 4.2.2
# copy newer sed and run
chmod +x sed && head -n 2 fxtest.txt |./sed -n '1,$p'
# 生成密钥对
#生成密钥对

Summary

Always insert a space between comment symbols and Chinese characters in shell scripts.

Older versions of tools like sed may contain charset‑related bugs; upgrading or adjusting the environment can eliminate such issues.

debuggingLinuxMHAsedCharset
Aikesheng Open Source Community
Written by

Aikesheng Open Source Community

The Aikesheng Open Source Community provides stable, enterprise‑grade MySQL open‑source tools and services, releases a premium open‑source component each year (1024), and continuously operates and maintains them.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.