50

I am trying to fix a character encoding issue - previously we had the collation set for this column utf8_general_ci which caused issues because it is accent insensitive..

I'm trying to find all the entries in the database that could have been affected.

set names utf8;
select * from table1 t1 join table2 t2 on (t1.pid=t2.pid and t1.id != t2.id) collate utf8_general_ci;

However, this generates the error:

ERROR 1253 (42000): COLLATION 'utf8_general_ci' is not valid for CHARACTER SET 'latin1'
  1. The database is now defined with DEFAULT CHARACTER SET utf8
  2. The table is defined with CHARSET=utf8
  3. The "pid" column is defined with: CHARACTER SET utf8 COLLATE utf8_bin NOT NULL
  4. The server version is Server version: 5.5.37-MariaDB-0ubuntu0.14.04.1 (Ubuntu)

Question: Why am I getting an error about latin1 when latin1 doesn't seem to be present anywhere in the table / schema definition?

MariaDB [(none)]> 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)

MariaDB [(none)]> SHOW VARIABLES LIKE '%collation%';
+----------------------+-------------------+
| Variable_name        | Value             |
+----------------------+-------------------+
| collation_connection | utf8_general_ci   |
| collation_database   | latin1_swedish_ci |
| collation_server     | latin1_swedish_ci |
+----------------------+-------------------+
3
  • Please post the SHOW CREATE TABLE table1 and SHOW CREATE TABLE table2. What is your client connection charset? Post also SHOW VARIABLES LIKE '%char%' Sep 15, 2014 at 16:08
  • Oh you do have set names utf8 so hopefully character_set_client will show utf8 Sep 15, 2014 at 16:10
  • This may be very helpful in this regard: scottlinux.com/2017/03/04/…
    – MBT
    Sep 1, 2019 at 16:44

3 Answers 3

54

First, run this query:

SHOW VARIABLES LIKE '%char%';

You have character_set_server='latin1' shown in your post ...

So, go into your my.cnf and add or uncomment these lines:

character-set-server = utf8
collation-server = utf8_unicode_ci

Restart the server.

11
  • 4
    Should that be character-set-server or character_set_server?
    – user151841
    Jul 26, 2017 at 21:46
  • 2
    It should be character-set-server
    – ath j
    Dec 14, 2017 at 11:04
  • character_set_server, MySQL 5.5
    – KayKay
    Dec 14, 2017 at 17:06
  • 1
    where to locate my.cnf file? Dec 11, 2018 at 8:32
  • 1
    you can run mysql --help
    – Bogdan Le
    Feb 28, 2019 at 19:39
8

The same error is produced in MariaDB (10.1.36-MariaDB) by using the combination of parenthesis and the COLLATE statement. My SQL was different, the error was the same, I had:

SELECT *
FROM table1
WHERE (field = 'STRING') COLLATE utf8_bin;

Omitting the parenthesis was solving it for me.

SELECT *
FROM table1
WHERE field = 'STRING' COLLATE utf8_bin;
1
  • 3
    If you can't omit the parenthesis use BINARY instead: SELECT * FROM table1 WHERE BINARY field = BINARY 'STRING';
    – Mel_T
    Oct 22, 2019 at 9:10
3

In my case I created a database and gave the collation 'utf8_general_ci' but the required collation was 'latin1'. After changing my collation type to latin1_bin the error was gone.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.