Databases 8 min read

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.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
How to Change MySQL Character Set to UTF-8 on Linux

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.cnf

Edit 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=utf8

Restart 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 -p

Verify 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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxmysqlUTF-8Character Set
MaGe Linux Operations
Written by

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.

0 followers
Reader feedback

How this landed with the community

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.