Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the connection collation of Mysql

How can I change connection collation of mysql database?

I am using Mysql workbench 5.5 and mysql 5.5 in ubuntu 14.

When I execute a stored procedure, an error occurs:

Error Code: 1267. Illegal mix of collations (utf8_general_ci,IMPLICIT) and (utf8_unicode_ci,IMPLICIT) for operation '='

I have search though the internet, which has a temp solution that is to amend

COLLATE utf8_unicode_ci;

in the stored procedure.

But I want to fix this problem for all stored procedures in the future. I have found

SHOW VARIABLES LIKE 'collation%';

which return this.

collation_connection    utf8_general_ci
collation_database  utf8_unicode_ci
collation_server    latin1_swedish_ci

how can I change utf8_general_ci to utf8_unicode_ci?

like image 573
martin Avatar asked Aug 15 '16 03:08

martin


People also ask

Does MySQL 5.7 support utf8mb4_0900_ai_ci?

utf8mb4_0900_ai_ci is implemented only as of MySQL 8.0, so the 5.7 server does not recognize it.

What is the default collation for MySQL?

The default MySQL server character set and collation are latin1 and latin1_swedish_ci , but you can specify character sets at the server, database, table, column, and string literal levels.

What is collation in MySQL server?

A collation is a set of rules that defines how to compare and sort character strings. Each collation in MySQL belongs to a single character set. Every character set has at least one collation, and most have two or more collations. A collation orders characters based on weights.

How do I find MySQL database collation?

To see the default character set and collation for a given database, use these statements: USE db_name; SELECT @@character_set_database, @@collation_database; Alternatively, to display the values without changing the default database: SELECT DEFAULT_CHARACTER_SET_NAME, DEFAULT_COLLATION_NAME FROM INFORMATION_SCHEMA.


1 Answers

Look into your my.cnf, find the contents below near collation_server:

[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

Then change your collation variables to:

collation_connection    utf8_unicode_ci
collation_server        latin1_swedish_ci

Remember to restart MySQL server service.

For DB collation, you can use the following SQL:

ALTER DATABASE <database_name> CHARACTER SET utf8 COLLATE utf8_unicode_ci;

or you can do it at Alter database screen in MySQL Workbench (always update this to the latest version!)

like image 114
Raptor Avatar answered Oct 01 '22 05:10

Raptor