How to Change MySQL Character Set to UTF-8 on Linux
This guide walks through locating MySQL's configuration file on Linux, copying a sample cnf, editing it to set default-character-set=utf8 for client and server, restarting MySQL, and verifying the change with SHOW VARIABLES, plus alternative methods via SQL commands and database options.
Locate the MySQL configuration file
Run a search to find any *.cnf files, for example: find / -iname "*.cnf" -print Typical locations include:
/usr/share/mysql/my-innodb-heavy-4G.cnf
/usr/share/mysql/my-large.cnf
/usr/share/mysql/my-medium.cnf
/usr/share/mysql/my-huge.cnf
/usr/share/doc/MySQL-server-community-5.1.22/my-medium.cnf
Copy a sample configuration
Choose one of the sample files (e.g., my-medium.cnf) and copy it to /etc/my.cnf:
cp /usr/share/mysql/my-medium.cnf /etc/my.cnfEdit my.cnf to set UTF‑8
Open the file with an editor: vi /etc/my.cnf Add the following lines:
[client]
default-character-set=utf8
[mysqld]
default-character-set=utf8Restart MySQL
Execute the service restart command: /etc/rc.d/init.d/mysql restart Then log in to verify the server is running:
mysql -u root -pVerify the character set
Run the following queries:
show variables like 'collation_%'; show variables like 'character_set_%';Typical output should show utf8_general_ci for collations and utf8 for all character_set variables.
Other ways to change the charset
Direct SQL commands:
use mydb;
alter database mydb character set utf8;
create database mydb character set utf8;Modify the database option file ( /var/lib/mysql/mydb/db.opt) to replace default-character-set=latin1 with default-character-set=utf8 and restart MySQL.
Set variables at runtime:
set character_set_client=utf8;
set character_set_connection=utf8;
set character_set_database=utf8;
set character_set_results=utf8;
set character_set_server=utf8;
set collation_connection=utf8_general_ci;
set collation_database=utf8_general_ci;
set collation_server=utf8_general_ci;After setting, verify again with the SHOW VARIABLES queries.
Background on MySQL character set defaults
MySQL determines the effective character set through a hierarchy: server defaults (compiled or set in my.cnf), character_set_server, character_set_database, table defaults, and column defaults. If none are overridden, the legacy default is latin1, but most installations configure utf8 during setup.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
