How to change the connection collation of Mysql - 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?

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!)

Firstly, I think the error message is because of the collation of stored procedure (connection) doesn't match the table. But I was wrong, the reason for the error is because of the collation of the column doesn't match the collation of the table.
I want to change to collation of column to 'utf8_unicode_ci' in my case. So
I have run this statement:
alter table <YourTableName>
MODIFY <YourColumnName> VARCHAR(XXX) COLLATE 'utf8_unicode_ci';
Please be aware that change of collation may result in data loss. For me, General -> Unicode, with all English in varchar column. There is none.
Further reading:
http://dev.mysql.com/doc/refman/5.7/en/charset-column.html
http://dev.mysql.com/doc/refman/5.7/en/charset-connection.html
http://dev.mysql.com/doc/refman/5.7/en/charset-database.html

Related

Problem changing mysql database encoding to utf8mb4 in Gcloud

I have a mysql database in Gcloud using utf8 enconding and I want to change it to utf8mb4 to support storing emoticons.
I did this using my local version of mysql in my laptop and it worked fine using:
ALTER TABLE table_name CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
ALTER DATABASE db_name CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
But when I try to do the same in Gcloud, the commands are accepted and the encoding changes but I keep receiving this error when trying to insert the record:
"error": "could not execute statement; nested exception is org.hibernate.exception.GenericJDBCException: could not execute statement: Incorrect string value: '\xF0\x9F\xA6\x95' for column 'text' at row 1" }
I also changed character_set_server variable in gcloud to utf8mb4 and restarted the engine but it does not work. Does anybody know what else should I change?
Thanks!

databases disappeared from phpmyadmin after database collation change

After changing the collation in phpmyadmin for several databases, I can't see them anymore in chrome and explorer. If a use the command SHOW DATABASES in the command line, they are all displayed.
In Firefox, the following error is thrown:
SET CHARACTER SET 'utf8mb4';
1115 - Unknown character set: 'utf8mb4'
This is my first post, any help would be appreciated.
Edit:
I had already read Q&A in ERROR 1115 (42000): Unknown character set: 'utf8mb4', but it didn't solve my problem.
The mysql version I'm using is 5.6.17, and the character set is supported from version 5.3.3, so updating wouldn't help. I'm using wamp 2.5 with Mysql 5.6.17, Apache 2.4.9 and PHP 5.5.12.
And I get the error simply accessing http://localhost/phpmyadmin (not executing any sql) after my update, so the problem seems to be already commited. It's quite confusing that the error message is displayed only in Firefox while other browsers simply hide some databases and show the others.
Executing 'SELECT default_collation_name FROM information_schema.SCHEMATA;' in command line brought me these results:
utf8_general_ci
utf8_general_ci
utf8_spanish_ci
utf8_general_ci
utf8_general_ci
utf8_spanish_ci
utf8_general_ci
utf8_spanish_ci
utf8_general_ci
latin1_swedish_ci
utf8_spanish_ci
utf8_general_ci
utf8_general_ci
utf8_general_ci
utf8_general_ci
latin1_swedish_ci
utf8_general_ci
utf8_general_ci
utf8_general_ci
utf8_general_ci
utf8_general_ci
utf8_general_ci
In PhpMyAdmin, the SCHEMATA table only shows ¡three! results:
One for 'information_schema', another for 'mysql' and the last one for one of my databases. The others are not there.
After some research, I discovered there where two instances of mysql running on my server.
Phpmyadmin was using a 32 bits old version while the mysql command line was linked to the 64 bits one. I stopped the old one and now everything runs ok.

In MySQL, what do I put inside my.cnf so that all tables are UTF-8 that works with emojis by default?

I'd like every table and database (to be created) to be utf-8 that works with emojis. I understand that there are a few variables I need to define inside my.cnf:
init_connect='SET collation_connection = ??? '
init_connect='SET NAMES ???'
character-set-server = ???
collation-server = ???
However, I'm not sure what to put in the ???. What do I put inside my.cnf?
This article may help: https://mathiasbynens.be/notes/mysql-utf8mb4#utf8-to-utf8mb4
It explains in detail how to switch to utf8mb4 to support full unicode, thus allowing emojis using the following config:
[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
In addition to changing to utf8mb4 as #eol suggests, there may be more to do.
If you already have tables that have utf8 columns; do ALTER TABLE .. CONVERT TO CHARACTER SET utf8mb4.
When you connect, be sure to establish UTF-8 if the client has a non-mysql way of doing it, or use SET NAMES utf8mb4.
Keep in mind that root ignores init-connect. You should have a non-root 'user' for the application.
To support full Unicode in MySQL databases
For database: ALTER DATABASE DATABASE_NAME CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;
For table:
ALTER TABLE TABLE_NAME CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
For column: ALTER TABLE TABLE_NAME CHANGE COLUMN_NAME COLUMN_NAME VARCHAR(191) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

MySQL collation server variables user dependent?

Connecting with different SQL users with the same client (Sequel Pro) on the same MySQL server on the same database results in different collation server variables.
In my.cnf, I included the following:
[mysqld]
init_connect='SET collation_connection = utf8_unicode_ci, NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
Connecting with user A results in:
collation_connection = utf8_unicode_ci
collation_database = utf8_unicode_ci
collation_server = utf8_unicode_ci
init_connect = SET collation_connection = utf8_unicode_ci, NAMES utf8
Connecting with user B results in:
collation_database = utf8_unicode_ci
collation_server = utf8_unicode_ci
init_connect = SET collation_connection = utf8_unicode_ci, NAMES utf8
So the collation_connection variable is missing. Is it possible the init_connect is ignored / user specific for some reason?
Because the collation_connection is not set, a Illegal mix of collations error occurs.
Possible cause: The init_connect string seems to be wrong. Try:
init_connect='SET collation_connection = utf8_unicode_ci; SET NAMES utf8;'
and then restart the mysql service.
This does not explain the weird behaviour with the SUPER permission however..
Please be aware that init_connect is not run when a user with SUPER privileges logs in. This is a security feature, as documented:
The content of init_connect is not executed for users that have the SUPER privilege. This is done so that an erroneous value for init_connect does not prevent all clients from connecting.
This is to prevent from locking yourself out of the database completely:
For example, the value might contain a statement that has a syntax error, thus causing client connections to fail. Not executing init_connect for users that have the SUPER privilege enables them to open a connection and fix the init_connect value.
So it is expected behavior for these users not to have this variable set upon connecting.

MySQL: Set Default Collation Connection and MySQL Dump

I am running MySQL 5.5.20 on Windows Vista Business. I am having a bit of a problem with collation_connection. My default charset is utf8 and collation is ut8_unicode_ci. However, when I perform mysql dumps on the database, my functions and procedures have the collation_connection showing utf8_general_ci; for example,
/*!50003 SET collation_connection = utf8_general_ci*/ ;
Is it possible to specify MySQL to default to ut8_unicode_ci for collation_connection? I use MySQL Workbench to perform the mysql dumps.
Nowadays consider using utf8mb4 instead of utf8.
If you want the behavior you're describing, you first would execute:
SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci;
Then redefine affected functions, procedures and triggers.
Next time you execute mysqldump, generated file will have lines like this:
/*!50003 SET collation_connection = utf8mb4_unicode_ci*/ ;
More details about SET NAMES in MySQL Reference Manual.