Save emojis to MYSQL DB - mysql

I am trying to save emojis to my MYSQL database, I have followed these steps
3 STEPS
ALTER TABLE TABLE CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
ALTER TABLE COLUMN modify Comment text charset utf8mb4;
SET NAMES utf8mb4;
From the above steps I am able save emojis to DB, but after some time I am not able to see those again, I have to execute the following command
SET NAMES utf8mb4;
Then it works
Is it nessary to use the following statement?
SET NAMES utf8mb4;
Can't we have a permanent fix or way to this?

The reason that you have to execute the SET NAMES again sometimes, is that the command only modifies configuration for the current session.
According to the documentation:
13.7.5.3 SET NAMES Syntax
This statement sets the three session system variables character_set_client, character_set_connection, and character_set_results to the given character set.
If you want to permanently set it you need to set define it when the mysql service starts, or add it to your MySQL conf file.
Check out https://dev.mysql.com/doc/refman/5.7/en/charset-connection.html
Alternatively (depending on which MySQL client you use for node) you might be able to define the connection character set directly from your client.

Related

why mysql execute sql need convert character set several times

I mean that when the Mysql excute a sql,the fist step is to convert character_set_client to character_set_connection, then character_set_connection will be converted to a specific character_set(beside on table character set)。
mysql execute sql need convert character set several times, why it is designed like this.

How to have a column specific character encoding in Ruby on Rails and MySQL?

I have a new table I am adding to a large Ruby on Rails project. Our database currently uses the UTF8 character encoding. However, I need to support emoji's in one column of this new table.
In order to do that, that column needs to be in UTF8mb4 encoding, since the base UTF8 encoding in MySQL doesn't support emoji's.
I can change things on the mysql side by executing this SQL:
execute "ALTER TABLE table_name CHANGE column_name VARCHAR(191) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin"
The problem is this doesn't fix the issue, I actually have to change the 'encoding' option in the database.yml file from utf8 to utf8mb4.
The problem with this is when my test run loads the database schema, suddenly it can't because characters in utf8mb4 use more bytes than they do in utf8, and we have existing indexes that are too long to fit into the new utf8mb4 byte requirement.
I don't really want to edit all of our existing indexes that break this, our database is pretty large.
Is there no way in Rails to use a character encoding on a table or column level? Do I have to update the database.yml file and therefore force my entire database to use utf8mb4?
Change VARCHAR(255) to VARCHAR(191), for any column being indexed, to get around index limit, or
Update to MySQL 5.7, which does not have the issue.
If you need to discuss more, please provide SHOW CREATE TABLE and the specific connection parameters, perhaps found in application_controller.rb.

What happens when SET NAMES does not match a table's character set?

I have a database with tables set as CHARACTER SET=utf8mb4, however I've discovered that the mysql connection (SET NAMES) has been latin1.
My questions are:
What happens to that data? Has MySQL been converting it to utf8mb4 to store it, or has it stored the data as latin1?
If I need to convert it, should I connect as SET NAMES latin1 to take the data out, convert it, and put it back in, or connect as utf8mb4 to pull the data out?
Data is stored in the character set defined in the table, so in your case all the data will be stored in utf8mb4.
Every connection to the database uses a character set for sending and receiving data. That character set might be different, so mysql converts between the storage character set and the connection character set.
So, to answer your questions:
1) mysql converts it.
2) you don't need to convert it. But you need to make sure that if you use set names, you actually are sending data using the character set you claim.
More here: http://dev.mysql.com/doc/refman/5.7/en/charset-connection.html

AWS RDS MySQL with Unicode

I'm running MySQL on AWS RDS, does anyone know how to set the character set there to UTF-8?
You can modify the database parameters through the AWS GUI/CLI or you can just create your tables with the required character set. (The Table character set does not need to match your database character set, nor match your column's character set)
E.g.
CREATE TABLE t1
(
c1 CHAR(10) CHARACTER SET utf8mb4
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
Here I've used utf8mb4, which supports the full Unicode set, including Emojis, which MySQL's utf8 does not.
I actually wanted to change the settings for the whole database. There's no way to do it after the DB is created, however, during the DB creation via AWS RDS console, it's just a matter of selecting the right Parameter Group. If you only have the default Parameter Group, just go to Parameter Groups section to create your specific one setting the right "character set" and then use that group.
So, this basically solves my problem.

import utf-8 mysqldump to latin1 database

I have a dump file of a phpnuke site somehow in utf8. I'm trying to reopen this site on a new server. But nuke uses latin1.
I need a way to create a latin1 database using this utf-8 dump file.
I tried everything I could think of. iconv, mysql replace, php replace...
Add the SET NAMES 'utf8'; statement at the beginning of your dump.
This will indicate to MySQL that the commands it is about to receive are in UTF8.
It will store the data in whatever character set your tables are currently set in; in this case if your database is in latin1, data will be stored in latin1.
From http://dev.mysql.com/doc/refman/5.0/en/charset-connection.html:
SET NAMES indicates what character set the client will use to send SQL statements to the server. Thus, SET NAMES 'cp1251' tells the server, “future incoming messages from this client are in character set cp1251.” It also specifies the character set that the server should use for sending results back to the client. (For example, it indicates what character set to use for column values if you use a SELECT statement.)
One last thing is that latin1 has much less characters available than utf8. For anything other than the western European languages, you will lose data.
For instance, assuming column test is latin1. The first entry will appear correctly (the French accent is within latin1); however the second entry in Korean will show as question marks.
SET NAMES 'utf8';
INSERT INTO TESTME(test) VALUES ('Bienvenue sur Wikipédia');
INSERT INTO TESTME(test) VALUES ('한국어 위키백과에 오신 것을 환영합니다!');