Partial configuration of utf8 collations and charsets on MySQL 5.6 - mysql

I've been trying various configurations to force charset and collations to utf8. However, everything I tried has failed and some settings reflect the latin charset or collation.
This is MySQL 5.6 on CentOS 6 from IUS repository.
My config on /usr/share/mysql/my.cnf
# The MySQL server
[mysqld]
port = 3306
socket = /var/lib/mysql/mysql.sock
skip-external-locking
key_buffer_size = 16K
max_allowed_packet = 1M
table_open_cache = 4
sort_buffer_size = 64K
read_buffer_size = 256K
read_rnd_buffer_size = 256K
net_buffer_length = 2K
thread_stack = 128K
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
Settings through Heidi
From the MySQL client
mysql> show variables like 'char%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.01 sec)
mysql> show variables like 'coll%';
+----------------------+-------------------+
| Variable_name | Value |
+----------------------+-------------------+
| collation_connection | utf8_general_ci |
| collation_database | latin1_swedish_ci |
| collation_server | latin1_swedish_ci |
+----------------------+-------------------+
3 rows in set (0.00 sec)
I did not have any issues in 5.1 though.
Update #1
I've put default-character-set = utf8, in client, mysqld and mysql section of my.cnf and rebooted the MySQL service. Still the same thing, a mix of latin and utf8.
mysql> show variables like 'char%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
mysql> show variables like 'coll%';
+----------------------+-------------------+
| Variable_name | Value |
+----------------------+-------------------+
| collation_connection | utf8_general_ci |
| collation_database | latin1_swedish_ci |
| collation_server | latin1_swedish_ci |
+----------------------+-------------------+
3 rows in set (0.00 sec)
Update #2
My issues related to migrating a database using Migration Wizard through MySQL Workbench. The resulting database has collations and charsets that I don't want. If I run the produced script through Heidi, I get the same result as the migration result.

init-connect is ignored when you connect as root (or other SUPER user).
In your my.cnf (my.ini) files, add this under [mysql] or [client]:
default-character-set = utf8
(I don't know if Heidi will override that -- find out how it does the "connect".)
Addenda
character_set_database and _server are not necessarily an issue. _client, connection and _results are used to say how the bytes in the client are encoded. You say what the table contains by adding CHARACTER SET utf8 in CREATE TABLE.
Aside from the settings being like you show them, what symptoms do you have of messed up text? Do this with some column with non-English text:
SELECT col, HEX(col) FROM ... WHERE ...
to show what is actually stored.

I ended up hard coding the collation and character set that I wanted in the generated scripts.
ie. when creating the database and adding columns, specifying the collation and character set where necessary, utf8_unicode_ci, utf8...

Related

Incorrect String Value in MySQL db

I am running a webapp on Ubuntu 16.04.4.
The stack is as follows
Python 3.5.2
MySQL 5.7.22
Flask
Flask-SQLAlchemy
The webapp has a feature for admins to upload some text using a xlsx. file which is read with openpyxl inside the webapp. However while saving I am getting errors like:
sqlalchemy.exc.OperationalError: (_mysql_exceptions.OperationalError) (1366, "Incorrect string value: '\\xC4\\x9B nep...'
In the beginning I was able to delete the characters which were making troubles (e.g. zero width whitespace). But now I am not able to do it anymore like this.
Reading a bit on the internet I think it could be that my db is not using utf8mb4. Could someone lead me to update my db and all its tables? Because I do not know anything about SQL and stuff.
As the webapp is used in production I do not like to try tutorials which are outdated.
Seems to work now. I did following steps:
Started the mysql cli with:
mysql -u root -p
Logged in using the root pw.
Checked the default parameters using
show variables like "%character%";
which gave me:
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
and
show variables like "%collation%";
which gave me
+----------------------+-------------------+
| Variable_name | Value |
+----------------------+-------------------+
| collation_connection | utf8_general_ci |
| collation_database | latin1_swedish_ci |
| collation_server | latin1_swedish_ci |
+----------------------+-------------------+
So I edited /etc/mysql/my.cnf
I added:
[client]
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4
[mysqld]
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
Restarting mysql (sudo service mysql restart) and running the same commands as above now gave me
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8mb4 |
| character_set_connection | utf8mb4 |
| character_set_database | utf8mb4 |
| character_set_filesystem | binary |
| character_set_results | utf8mb4 |
| character_set_server | utf8mb4 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
and
+----------------------+--------------------+
| Variable_name | Value |
+----------------------+--------------------+
| collation_connection | utf8mb4_general_ci |
| collation_database | utf8mb4_unicode_ci |
| collation_server | utf8mb4_unicode_ci |
+----------------------+--------------------+
So I looked up the table settings using
SHOW TABLE STATUS FROM databasename;
They still used stuff like latin1_swedish_ci
I used following to change the database setting:
ALTER DATABASE databasename CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
and following for each table:
use databasename;
ALTER TABLE assessments CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
Looking up again the table settings showed that latin1_swedish_ci was now changed to utf8mb4_unicode_ci
Then I changed the sqlalchemy connection url to use ?encoding=utf8mb4 at the end.
Restarted mysql again and the webapp. Since then it's working properly.

Can't change charset in MySQL 5.7 (from latin to utf8)

I installed MySQL 5.7.8 on my Ubuntu 14.04.
But I can't modify MySQL default charset. I want to change charset to utf8.
I've searched and view many articles on the internet, but every method that I tried don't apply for me.
I already add 'character-set-server=utf8' and 'collation-server=utf8_general_ci' in /etc/mysql/my.cnf. but it's not working...
How can I change my charset of MySQL? Please answer to me!
Status in MySQL
Connection: Localhost via UNIX socket
Server characterset: latin1
Db characterset: latin1
Client characterset: utf8
Conn. characterset: utf8
UNIX socket: /var/run/mysqld/mysqld.sock
Uptime: 1 min 24 sec
show variables like 'c%'
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
| check_proxy_users | OFF |
| collation_connection | utf8_general_ci |
| collation_database | latin1_swedish_ci |
| collation_server | latin1_swedish_ci |
| completion_type | NO_CHAIN |
| concurrent_insert | AUTO |
| connect_timeout | 10 |
| core_file | OFF |
my.cnf (in /etc/mysql)
[mysqld]
#
# * Basic Settings
#
character-set-server=utf8
collation-server=utf8_general_ci
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port = 3306
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
lc-messages-dir = /usr/share/mysql
skip-external-locking
character-set-server=utf8
collation-server=utf8_general_ci
init_connect=set collation_connection=utf8_general_ci
init_connect=set names utf8
character-set-client-handshake=FALSE
Setting UTF-8 character set and collation for the MySQL server:
sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf
Then press i and add the following:
...
[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci
...
Press Esc then :w then :q!
Then restart the server:
sudo /etc/init.d/mysql restart
Check your configuration:
mysql> show variables like "%character%";
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.01 sec)
mysql> show variables like "%collation%";
+----------------------+-----------------+
| Variable_name | Value |
+----------------------+-----------------+
| collation_connection | utf8_general_ci |
| collation_database | utf8_general_ci |
| collation_server | utf8_general_ci |
+----------------------+-----------------+
3 rows in set (0.00 sec)
Setting UTF-8 character set and collation for the database and tables:
Specify it explicitly when creating the database:
CREATE DATABASE some_database CHARACTER SET utf8 COLLATE utf8_general_ci;
Check your configuration:
mysql> SHOW CREATE DATABASE ...
mysql> SHOW CREATE TABLE ...

MySQL utf8mb4 on Amazon RDS: global variables set correctly but variables not set

I'm trying to convert my Amazon RDS server to use utf8mb4 encoding instead of utf8. I've followed the guide here and it has worked for the most part (global variables are set through my new parameter group in RDS), but my system variables are not setting correctly which means that I'm not able to utilize the new encoding.
When I run:
SHOW VARIABLES WHERE Variable_name LIKE 'character\_set\_%' OR Variable_name LIKE 'collation%';
I see:
+--------------------------+--------------------+
| Variable_name | Value |
+--------------------------+--------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8mb4 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8mb4 |
| character_set_system | utf8 |
| collation_connection | utf8_general_ci |
| collation_database | utf8mb4_unicode_ci |
| collation_server | utf8mb4_unicode_ci |
+--------------------------+--------------------+
Which is obviously incorrect, but when I run:
SHOW GLOBAL VARIABLES WHERE Variable_name LIKE 'character\_set\_%' OR Variable_name LIKE 'collation%';
I see:
+--------------------------+--------------------+
| Variable_name | Value |
+--------------------------+--------------------+
| character_set_client | utf8mb4 |
| character_set_connection | utf8mb4 |
| character_set_database | utf8mb4 |
| character_set_filesystem | binary |
| character_set_results | utf8mb4 |
| character_set_server | utf8mb4 |
| character_set_system | utf8 |
| collation_connection | utf8mb4_unicode_ci |
| collation_database | utf8mb4_unicode_ci |
| collation_server | utf8mb4_unicode_ci |
+--------------------------+--------------------+
This is correct, but for some reason these global values are not setting the server when I restart the server. I can correctly set the variables manually after restarts, but I don't understand why they aren't setting initially.
To clarify Aaron's answer, this was (at least for us) caused by the character set/collation of our connection to the database; the database itself is set up correctly. When you use any client to connect to the db -- whether it be SQLyog, MySQL Workbench, built in client or any other -- there is a character set and collation associated with that connection. Thus you need to need to change this connection charset/collation to utf8mb4 and utf8mb4_unicode_ci, and the values of
SHOW VARIABLES WHERE Variable_name LIKE 'character\_set\_%' OR Variable_name LIKE 'collation%';
will then all show correctly. If you take a close look at the "problem" values in the original question you'll notice they are _client, _connection etc. which should have given me an obvious clue that the problem was with my mysql client and not the database itself.
Fixed it. It was an issue with my local installation of MySQL - not the server. I had to change the default encoding that it sent and it worked great from there.
For me this was because I had skipped the instruction (in the original linked guide) to modify /etc/my.cnf:
[client]
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4
[mysqld]
character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci

MySQL 5.5 utf8 trouble

I've done all the things that worked on previous versions of MySQL (and new to MySQL 5.5) to set utf8 encoding.
Now I have output of
SHOW GLOBAL VARIABLES LIKE '%char%';
exactly what I wanted:
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
And that cmds in [mysqld]:
collation-server = utf8_unicode_ci
init-connect='SET NAMES utf8'
character-set-server = utf8
In [client] and [mysql]:
default-character-set= utf8
But default table encoding in creating is still latin1!
Am I missed some other things with MySQL 5.5 to make it work?
Thanks in advance!
Default table encoding equals to the current database encoding.
You can check it with
SHOW CREATE DATABASE dbname;
PS: it's a good practice to always specify encoding explicitly. That way you won't rely on server settings.

Change MySQL default character set to UTF-8 in my.cnf?

Currently we are using the following commands in PHP to set the character set to UTF-8 in our application.
Since this is a bit of overhead, we'd like to set this as the default setting in MySQL. Can we do this in /etc/my.cnf or in another location?
SET NAMES 'utf8'
SET CHARACTER SET utf8
I've looked for a default charset in /etc/my.cnf, but there's nothing there about charsets.
At this point, I did the following to set the MySQL charset and collation variables to UTF-8:
skip-character-set-client-handshake
character_set_client=utf8
character_set_server=utf8
Is that a correct way to handle this?
To set the default to UTF-8, you want to add the following to my.cnf/my.ini
[client]
default-character-set=utf8mb4
[mysql]
default-character-set=utf8mb4
[mysqld]
collation-server = utf8mb4_unicode_520_ci
init-connect='SET NAMES utf8mb4'
character-set-server = utf8mb4
If you want to change the character set for an existing DB, let me know... your question didn't specify it directly so I am not sure if that's what you want to do.
Edit: I replaced utf8 with utf8mb4 in the original answer due to utf8 only being a subset of UTF-8. MySQL and MariaDB both call UTF-8 utf8mb4.
For the recent version of MySQL,
default-character-set = utf8
causes a problem. It's deprecated I think.
As Justin Ball says in "Upgrade to MySQL 5.5.12 and now MySQL won’t start, you should:
Remove that directive and you should be good.
Then your configuration file ('/etc/my.cnf' for example) should look like that:
[mysqld]
collation-server = utf8_unicode_ci
init-connect='SET NAMES utf8'
character-set-server = utf8
Restart MySQL.
For making sure, your MySQL is UTF-8, run the following queries in your MySQL prompt:
First query:
mysql> show variables like 'char%';
The output should look like:
+--------------------------+---------------------------------+
| Variable_name | Value |
+--------------------------+---------------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/local/mysql/share/charsets/|
+--------------------------+---------------------------------+
Second query:
mysql> show variables like 'collation%';
And the query output is:
+----------------------+-----------------+
| Variable_name | Value |
+----------------------+-----------------+
| collation_connection | utf8_general_ci |
| collation_database | utf8_unicode_ci |
| collation_server | utf8_unicode_ci |
+----------------------+-----------------+
This question already has a lot of answers, but Mathias Bynens mentioned that 'utf8mb4' should be used instead of 'utf8' in order to have better UTF-8 support ('utf8' does not support 4 byte characters, fields are truncated on insert). I consider this to be an important difference. So here is yet another answer on how to set the default character set and collation. One that'll allow you to insert a pile of poo (💩).
This works on MySQL 5.5.35.
Note, that some of the settings may be optional. As I'm not entirely sure that I haven't forgotten anything, I'll make this answer a community wiki.
Old Settings
mysql> SHOW VARIABLES LIKE 'char%'; SHOW VARIABLES LIKE 'collation%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
+----------------------+-------------------+
| Variable_name | Value |
+----------------------+-------------------+
| collation_connection | utf8_general_ci |
| collation_database | latin1_swedish_ci |
| collation_server | latin1_swedish_ci |
+----------------------+-------------------+
3 rows in set (0.00 sec)
Config
# 💩 𝌆
# UTF-8 should be used instead of Latin1. Obviously.
# NOTE "utf8" in MySQL is NOT full UTF-8: http://mathiasbynens.be/notes/mysql-utf8mb4
[client]
default-character-set = utf8mb4
[mysqld]
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
[mysql]
default-character-set = utf8mb4
New Settings
mysql> SHOW VARIABLES LIKE 'char%'; SHOW VARIABLES LIKE 'collation%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8mb4 |
| character_set_connection | utf8mb4 |
| character_set_database | utf8mb4 |
| character_set_filesystem | binary |
| character_set_results | utf8mb4 |
| character_set_server | utf8mb4 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
+----------------------+--------------------+
| Variable_name | Value |
+----------------------+--------------------+
| collation_connection | utf8mb4_general_ci |
| collation_database | utf8mb4_unicode_ci |
| collation_server | utf8mb4_unicode_ci |
+----------------------+--------------------+
3 rows in set (0.00 sec)
character_set_system is always utf8.
This won't affect existing tables, it's just the default setting (used for new tables).
The following ALTER code can be used to convert an existing table (without the dump-restore workaround):
ALTER DATABASE databasename CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
ALTER TABLE tablename CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
Edit:
On a MySQL 5.0 server: character_set_client, character_set_connection, character_set_results, collation_connection remain at latin1. Issuing SET NAMES utf8 (utf8mb4 not available in that version) sets those to utf8 as well.
Caveat:
If you had a utf8 table with an index column of type VARCHAR(255), it can't be converted in some cases, because the maximum key length is exceeded (Specified key was too long; max key length is 767 bytes.). If possible, reduce the column size from 255 to 191 (because 191 * 4 = 764 < 767 < 192 * 4 = 768). After that, the table can be converted.
On MySQL 5.5 I have in my.cnf
[mysqld]
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
Result is
mysql> show variables like "%character%";show variables like "%collation%";
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
+----------------------+-----------------+
| Variable_name | Value |
+----------------------+-----------------+
| collation_connection | utf8_unicode_ci |
| collation_database | utf8_unicode_ci |
| collation_server | utf8_unicode_ci |
+----------------------+-----------------+
3 rows in set (0.00 sec)
Note:
my.cnf file is located at /etc/mysql/
After adding these lines:
[mysqld]
collation-server = utf8_unicode_ci
init-connect='SET NAMES utf8'
character-set-server = utf8
skip-character-set-client-handshake
[client]
default-character-set = utf8
[mysql]
default-character-set = utf8
Don't forget to restart server:
sudo service mysql restart
MySQL v5.5.3 and greater:
Just add three lines only in the [mysqld] section:
[mysqld]
character-set-server = utf8
collation-server = utf8_unicode_ci
skip-character-set-client-handshake
Note: Including skip-character-set-client-handshake here obviates the need to include both init-connect in [mysqld] and default-character-set in the [client] and [mysql] sections.
NijaCat was close, but specified overkill:
To set the default to UTF-8, you want to add the following to my.cnf
[client]
default-character-set=utf8
[mysqld]
default-character-set = utf8
Then, to verify:
mysql> show variables like "%character%";show variables like "%collation%";
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
+----------------------+-----------------+
| Variable_name | Value |
+----------------------+-----------------+
| collation_connection | utf8_general_ci |
| collation_database | utf8_general_ci |
| collation_server | utf8_general_ci |
+----------------------+-----------------+
3 rows in set (0.00 sec)
I also have found out that after setting default-character-set = utf8 under [mysqld] title, MySQL 5.5.x would not start under Ubuntu 12.04 (Precise Pangolin).
All settings listed here are correct, but here are the most optimal and sufficient solution:
[mysqld]
init_connect='SET collation_connection = utf8_unicode_ci'
character-set-server = utf8
collation-server = utf8_unicode_ci
[client]
default-character-set = utf8
Add these to /etc/mysql/my.cnf.
Please note, I choose utf8_unicode_ci type of collation due to the performance issue.
The result is:
mysql> SHOW VARIABLES LIKE 'character%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
mysql> SHOW VARIABLES LIKE 'collation%';
+----------------------+-----------------+
| Variable_name | Value |
+----------------------+-----------------+
| collation_connection | utf8_unicode_ci |
| collation_database | utf8_unicode_ci |
| collation_server | utf8_unicode_ci |
+----------------------+-----------------+
And this is when you connect as non-SUPER user!
For example, the difference between connection as SUPER and non-SUPER user (of course in case of utf8_unicode_ci collation):
user with SUPER priv.:
mysql> SHOW VARIABLES LIKE 'collation%';
+----------------------+-----------------+
| Variable_name | Value |
+----------------------+-----------------+
| collation_connection | utf8_general_ci | <---
| collation_database | utf8_unicode_ci |
| collation_server | utf8_unicode_ci |
+----------------------+-----------------+
user with non-SUPER priv.:
mysql> SHOW VARIABLES LIKE 'collation%';
+----------------------+-----------------+
| Variable_name | Value |
+----------------------+-----------------+
| collation_connection | utf8_unicode_ci |
| collation_database | utf8_unicode_ci |
| collation_server | utf8_unicode_ci |
+----------------------+-----------------+
I wrote a comprehensive article (rus) explaining in details why you should use one or the other option. All types of Character Sets and Collations are considered: for server, for database, for connection, for table and even for column.
I hope this and the article will help to clarify unclear moments.
Under Xubuntu 12.04 I simply added
[mysqld]
character_set_server = utf8
to /etc/mysql/my.cnf
And the result is
mysql> show variables like "%character%";show variables like "%collation%";
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
+----------------------+-----------------+
| Variable_name | Value |
+----------------------+-----------------+
| collation_connection | utf8_general_ci |
| collation_database | utf8_general_ci |
| collation_server | utf8_general_ci |
+----------------------+-----------------+
3 rows in set (0.00 sec)
Also take a look at http://dev.mysql.com/doc/refman/5.6/en/charset-server.html
On Fedora 21
$ vi /etc/my.cnf
Add follow:
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
Save and exit.
Final remember restart service mysqld with service mysqld restart.
The directive has changed to character-set-system=utf8
http://dev.mysql.com/doc/refman/5.6/en/charset-configuration.html
MySQL versions and Linux distributions may matter when making configurations.
However, the changes under [mysqld] section is encouraged.
I want to give a short explanation of tomazzlender's answer:
[mysqld]
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
[mysqld]
This will change collation_connection to utf8_unicode_ci
init_connect='SET collation_connection = utf8_unicode_ci'
Using SET NAMES:
init_connect='SET NAMES utf8'
The SET NAMES will influence three characters, that is:
character_set_client
character_set_results
character_set_connection
This will set character_set_database & character_set_server
character-set-server=utf8
This will only affect collation_database & collation_server
collation-server=utf8_unicode_ci
Sorry, I'm not so sure what is this for. I don't use it however:
skip-character-set-client-handshake
MySQL 5.5, all you need is:
[mysqld]
character_set_client=utf8
character_set_server=utf8
collation_server=utf8_unicode_ci
collation_server is optional.
mysql> show variables like 'char%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
If you're having trouble confirming the client's character-set support using MySQL Workbench, then keep the following note in mind:
Important
All connections opened by MySQL Workbench automatically set the client character
set to utf8. Manually changing the client character set, such as using SET NAMES
..., may cause MySQL Workbench to not correctly display the characters. For
additional information about client character sets, see Connection Character Sets
and Collations.
Thus I was unable to override MySQL Workbench's character sets with my.cnf changes. e.g. 'set names utf8mb4'
If you are confused by your setting for client and conn is reseted after restart mysql service. Try these steps (which worked for me):
vi /etc/my.cnf
add the contents blow and :wq
[client]
character-sets-dir=/usr/local/mysql/share/mysql/charsets
restart mysql and login mysql , use database, input command status;, you'll find the character-set for 'client' and 'conn' is set to 'utf8'.
Check the reference for more info.
For utf8mb4_general_ci
[client]
default-character-set=utf8mb4
[mysql]
default-character-set=utf8mb4
[mysqld]
collation-server = utf8mb4_general_ci
init-connect='SET NAMES utf8mb4'
character-set-server = utf8mb4
As per symfony framework Documentation at https://symfony.com/doc/2.6/book/doctrine.html#configuring-the-database
We recommend against MySQL’s utf8 character set, since it does not
support 4-byte unicode characters, and strings containing them will be
truncated. This is fixed by the newer utf8mb4 character set.
You can do it the way it does, and if it doesn't work, you need to restart mysql.
Change MySQL character:
Client
default-character-set=utf8
mysqld
character_set_server=utf8
We should not write default-character-set=utf8 in mysqld, because that could result in an error like:
start: Job failed to start
At last:
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+