Mysql2::Error: Incorrect string value - mysql

I have a rails application running on production mode, but all of the sudden this error came up today when a user tried to save a record.
Mysql2::Error: Incorrect string value
More details (from production log):
Parameters: {"utf8"=>"â<9c><93>" ...
Mysql2::Error: Incorrect string value: '\xC5\x99\xC3\xA1k
Mysql2::Error: Incorrect string value: '\xC5\x99\xC3\xA1k
Now I saw some solutions that required dropping the databases and recreating it, but I cannot do that.
Now mysql shows this:
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.04 sec)
What is wrong and how can I change it so I do not have any problems with any characters?
Also: Is this problem solvable with javascript? Convert it before sending it ?
Thanks

the problem is caused by charset of your mysql server side. You can config manually like:
ALTER TABLE your_database_name.your_table CONVERT TO CHARACTER SET utf8
or drop the table and recreate it like:
rake db:drop
rake db:create
rake db:migrate
references:
https://stackoverflow.com/a/18498210/2034097
https://stackoverflow.com/a/16934647/2034097
UPDATE
the first command only affect specified table, if you want to change all the tables in a database, you can do like
ALTER DATABASE databasename CHARACTER SET utf8 COLLATE utf8_general_ci;
reference:
https://stackoverflow.com/a/6115705/2034097

I managed to store emojis (which take up 4 bytes) by following this blog post:
Rails 4, MySQL, and Emoji (Mysql2::Error: Incorrect string value error.)
You might think that you’re safe inserting most utf8 data in
to mysql when you’ve specified that the charset is utf-8. Sadly,
however, you’d be wrong. The problem is that the utf8 character set
takes up 3 bytes when stored in a VARCHAR column. Emoji characters, on
the other hand, take up 4 bytes.
The solution is in 2 parts:
Change the encoding of your table and fields:
ALTER TABLE `[table]`
CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_bin,
MODIFY [column] VARCHAR(250)
CHARACTER SET utf8mb4 COLLATE utf8mb4_bin
Tell the mysql2 adapter about it:
development:
adapter: mysql2
database: db
username:
password:
encoding: utf8mb4
collation: utf8mb4_unicode_ci
Hope this helps someone!
Then I had to restart my app and it worked.
Please note that some emojis will work without this fix, while some won't:
➡️ Did work
🔵 Did not work until I applied the fix described above.

You can use a migration like this to convert your tables to utf8:
class ConvertTablesToUtf8 < ActiveRecord::Migration
def change_encoding(encoding,collation)
connection = ActiveRecord::Base.connection
tables = connection.tables
dbname =connection.current_database
execute <<-SQL
ALTER DATABASE #{dbname} CHARACTER SET #{encoding} COLLATE #{collation};
SQL
tables.each do |tablename|
execute <<-SQL
ALTER TABLE #{dbname}.#{tablename} CONVERT TO CHARACTER SET #{encoding} COLLATE #{collation};
SQL
end
end
def change
reversible do |dir|
dir.up do
change_encoding('utf8','utf8_general_ci')
end
dir.down do
change_encoding('latin1','latin1_swedish_ci')
end
end
end
end

If you want to the store emoji, you need to do the following:
Create a migration (thanks #mfazekas)
class ConvertTablesToUtf8 < ActiveRecord::Migration
def change_encoding(encoding,collation)
connection = ActiveRecord::Base.connection
tables = connection.tables
dbname =connection.current_database
execute <<-SQL
ALTER DATABASE #{dbname} CHARACTER SET #{encoding} COLLATE #{collation};
SQL
tables.each do |tablename|
execute <<-SQL
ALTER TABLE #{dbname}.#{tablename} CONVERT TO CHARACTER SET #{encoding} COLLATE #{collation};
SQL
end
end
def change
reversible do |dir|
dir.up do
change_encoding('utf8mb4','utf8mb4_bin')
end
dir.down do
change_encoding('latin1','latin1_swedish_ci')
end
end
end
end
Change rails charset to utf8mb4 (thanks #selvamani-p)
production:
encoding: utf8mb4
References:
https://stackoverflow.com/a/39465494/1058096
https://stackoverflow.com/a/26273185/1058096

Need to change CHARACTER SET and COLLATE for already created database:
ALTER DATABASE databasename CHARACTER SET utf8 COLLATE utf8_unicode_ci;
Or it was necessary to create a database with pre-set parameters:
CREATE DATABASE databasename CHARACTER SET utf8 COLLATE utf8_general_ci;

It seems like an encoding problem while getting data from database. Try adding the below to your database.yml file
encoding: utf8
Hope this solves your issue

Also, if you don't want to do changes in your database structure, you could opt by serializing the field in question.
class MyModel < ActiveRecord::Base
serialize :content
attr_accessible :content, :title
end

Related

How to Properly Set Variables (character_set_%) for Proper Cyrillic Leters Reading from MySQL in R

I am experiencing the issue of unproper reading of cyrillic letters from MySQL table.
I use the following code:
library(RMySQL)
library(keyring)
mydb = dbConnect(MySQL(), ...)
dbReadTable(mydb, 'tregions2')
The table is read but cyrillic letters are substituted with question marks:
id regionname iSOID administrativeCenter
1 1 ????????? ???? RU-ALT ???????
I started investigating into the issue.
The result of the query show variables like 'character_set_%'; in MySQL Workbench for the same user logged in on the same PC returns:
character_set_client utf8mb4
character_set_connection utf8mb4
character_set_database utf8
character_set_filesystem binary
character_set_results utf8mb4
character_set_server utf8mb4
character_set_system utf8
character_sets_dir C:\Program Files\MySQL\MySQL Server 8.0\share\charsets\
But result of the query returned by R is different:
> dbGetQuery(mydb, "show variables like 'character_set_%'")
Variable_name Value
1 character_set_client latin1
2 character_set_connection latin1
3 character_set_database utf8
4 character_set_filesystem binary
5 character_set_results latin1
6 character_set_server utf8mb4
7 character_set_system utf8
8 character_sets_dir C:\\Program Files\\MySQL\\MySQL Server 8.0\\share\\charsets\\
The locale variables of R are the following:
> Sys.getlocale()
[1] "LC_COLLATE=Russian_Russia.1251;LC_CTYPE=Russian_Russia.1251;LC_MONETARY=Russian_Russia.1251;LC_NUMERIC=C;LC_TIME=Russian_Russia.1251
I tried to change character set and collation of the table in DB. Earlier setting cp1251 character set helped me to properly write the data into the database. But not now. I tried utf8/koi8r/cp1251 without any effect.
Attempt to execute Sys.setlocale(,"ru_RU") aborted with an error that it could not be executed.
I am stuck. Could anyone give me an advise what else I should do?
After several hours of investigation I finaly figured out the solution. Hope it will help someone encountering the same problem:
> dbExecute(mydb, "SET NAMES cp1251")
[1] 0
> dbGetQuery(mydb, "show variables like 'character_set_%'")
Variable_name Value
1 character_set_client cp1251
2 character_set_connection cp1251
3 character_set_database utf8
4 character_set_filesystem binary
5 character_set_results cp1251
6 character_set_server utf8mb4
7 character_set_system utf8
8 character_sets_dir C:\\Program Files\\MySQL\\MySQL Server 8.0\\share\\charsets\\
>
> TrTMP <- dbReadTable(mydb, 'tregions')
> TrTMP[1,c(1,2,6,14)]
id regionname iSOID administrativeCenter
1 1 Алтайский край RU-ALT Барнаул
Tool -> Global Options -> Code -> Saving and put UTF-8
rs <- dbSendQuery(con, 'set character set "utf8"')
rs <- dbSendQuery(con, 'SET NAMES utf8')
options(encoding = "UTF-8") at the top of my main script from which I call my package seems to fix the issue with having non-ascii characters in my package code.
read_chunk(lines = readLines("TestSpanishText.R", encoding = "UTF-8")) (also file())
For more flexibility, you should use utf8mb4 instead of cp1251. If you have data coming into the client in cp1251, then you probably have to stick with that charset.

Unable to update character set of mysql from utf8mb4 to utf8

I am running mysql Ver 8.0.20.
Currently the collation and character sets are set to utf8mb4_0900_ai_ci and
I have been trying to update these to UTF8 by running these commands. But everytime I close the client and log back in the values have reverted to utf8mb4.
SET character_set_client = 'utf8';
SET character_set_connection = 'utf8';
SET character_set_database = 'latin1';
SET character_set_filesystem = 'binary';
SET character_set_results = 'utf8';
SET character_set_server = 'latin1';
SET character_set_system = 'utf8';
set collation_connection = 'utf8_general_ci';
set collation_database = 'latin1_swedish_ci';
set collation_database = 'latin1_swedish_ci'
commit;
ALTER DATABASE sanskvrcpu_db2 CHARACTER SET utf8 COLLATE utf8_general_ci;
The output of these statmentnts is something like this:
mysql> ALTER DATABASE sanskvrcpu_db2 CHARACTER SET utf8 COLLATE utf8_general_ci;
Query OK, 1 row affected, 2 warnings (0.00 sec)
Warning (Code 3719): 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous.
Warning (Code 3778): 'utf8_general_ci' is a collation of the deprecated character set UTF8MB3. Please consider using UTF8MB4 with an appropriate collation instead.
SET ... applies to the current "session" (aka "connection"). So the information is lost when you disconnect.
SET GLOBAL ... applies to the global variables, but not the current session. It applies only to new connections. But they are lost when the server goes down. When doing item 3, also do item 2.
Changing the config file (my.cnf or whatever) applies after the next restart. When doing item 2, also do item 3.
MySQL 8.0 has SET PERSIST, a way of "persisting" global settings via item 2 without resorting to also doing item 3. Ref: https://dev.mysql.com/doc/refman/8.0/en/set-variable.html
(You did item 1.)
Exception: Some settings are both 'session' and 'global'; hence the above restrictions are not quite followed.

MySQL 1300 Invalid big5 character string: ' \xC3\x97 '

I have no clue what's going on here. One of my DB servers is giving me that error when trying to create a function (that works on all the other servers):
My function is
delimiter $$
CREATE DEFINER=`root`#`%` FUNCTION `getreadablesize`(`Width` DECIMAL(13,4),`Height` DECIMAL(13,4),`Type` VARCHAR(64)) RETURNS varchar(64) CHARSET utf8mb4 COLLATE utf8mb4_unicode_ci
BEGIN
RETURN concat(trim(trailing'.'
from trim(trailing'0'
from`Width`)),'\"',
if(`Height`>0,concat(' × ',trim(trailing'.'
from trim(trailing'0'
from`Height`)),'\"'),''),
if(`Type`>'',concat(' ',`Type`),''));
END$$
And the exact error message is
0 row(s) affected, 2 warning(s): 1300 Invalid big5 character string: '
\xC3\x97 ' 1300 Invalid big5 character string: 'C39720'
None of my DB is in Chinese, or ever uses the Big5 character set?
If I copy my schema create code, I get this:
CREATE DATABASE `sterling` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci */;
EDIT: It works if I change the times symbol to something else, but that still doesn't make any sense as to why it's being treated as Big5 and not uft8
EDIT: Something is apparently quite wrong. If I run the following query, this is what I get:
SHOW VARIABLES LIKE 'character_set%';
+-----------------------------+------------------------------+
| 'character_set_client', | 'big5' |
+-----------------------------+------------------------------+
| 'character_set_connection', | 'big5' |
+-----------------------------+------------------------------+
| 'character_set_database', | 'utf8mb4' |
+-----------------------------+------------------------------+
| 'character_set_filesystem', | 'binary' |
+-----------------------------+------------------------------+
| 'character_set_results', | 'big5' |
+-----------------------------+------------------------------+
| 'character_set_server', | 'utf8mb4' |
+-----------------------------+------------------------------+
| 'character_set_system', | 'utf8' |
+-----------------------------+------------------------------+
| 'character_sets_dir', | '/usr/share/mysql/charsets/' |
+-----------------------------+------------------------------+
But my my.cnf clearly has default-character-set = utf8mb4 and all the variants of it under each applicable section... I will restart my MySQL server, because something is most definitely a foot.
Welp. I restarted the server and it never came back. I tried just about every MySQL server repair steps that existed and just nothing worked. Eventually I got it to start in safe mode with the innodb force flag set to 6, and I was then able to use HeidiSQL to pull all the data out (mysqlddump just hung and never started, but HeidiSQL implements their own export.). Even then though I still had 3 of my 165 tables that I couldn't read at all.

How to connect to MySQL using UTF8 within a perl script?

In a nutshell:
Within a Perl-Script: How do I connect to MySQL in a way that allows to transmit the four-byte unicode character U+1F61C ("😜") from the perl script to a MySQL-Table where this character should be stored?
Using {mysql_enable_utf8 => 1} doesn't solve the problem.
In detail:
I have exactly the same problem as described in the Question ERROR 1366 (HY000): Incorrect string value: '\xF0\x9F\x98\x9C' for column 'comment' at row 1 and even with the same Unicode character (😜 = U+1F61C = FACE WITH STUCK-OUT TONGUE AND WINKING EYE) which produces the error message
DBD::mysql::st execute failed: Incorrect string value: '\xF0\x9F\x98\x9C' for column ...
But I don't use PHP, I use Perl.
The accepted answer in the other question says:
Run MySQL 5.5 or later.
I check the version:
mysql> select version();
+-------------------------+
| version() |
+-------------------------+
| 5.7.13-0ubuntu0.16.04.2 |
+-------------------------+
So it is 5.7 which is later than 5.5.
✅checked
Set table's character to utf8mb4.
I check the character set of my database, my table and even of the reported column:
mysql> SELECT default_character_set_name FROM information_schema.SCHEMATA
-> WHERE schema_name = "myDatabase";
+----------------------------+
| default_character_set_name |
+----------------------------+
| utf8mb4 |
+----------------------------+
mysql> SELECT CCSA.character_set_name FROM information_schema.`TABLES` T,
-> information_schema.`COLLATION_CHARACTER_SET_APPLICABILITY` CCSA
-> WHERE CCSA.collation_name = T.table_collation
-> AND T.table_schema = "myDatabase"
-> AND T.table_name = "myTable";
+--------------------+
| character_set_name |
+--------------------+
| utf8mb4 |
+--------------------+
mysql> SELECT character_set_name FROM information_schema.`COLUMNS`
-> WHERE table_schema = "myDatabase"
-> AND table_name = "myTable"
-> AND column_name = "myColumn";
+--------------------+
| character_set_name |
+--------------------+
| utf8mb4 |
+--------------------+
So my database, my table and the reported column all use the character set utf8mb4.
✅checked
Enable UTF8 on your MySQL connection.
This seems to be the problem. The answer to the other question says:
SET NAMES utf8, or use an option when connecting that similarly enables it.
I don't know how to SET NAMES utf8 within a perl script, so I did it how I did it over the last years. I think that this is "an option when connecting that similarly enables it".
It's at the end of the long line that begins with my $dbh = DBI->connect:
#!/usr/bin/perl -w
use strict;
use warnings;
use utf8;
use Encode;
use DBI;
binmode STDOUT, ":utf8";
#Here I connect using the parameter mysql_enable_utf8 (create database handle):
my $dbh = DBI->connect('DBI:mysql:database=myDatabase;host=localhost','aUser','aPassword',{mysql_enable_utf8 => 1});
#Prepare the statement (create statement handle):
my $sth = $dbh->prepare('INSERT INTO `myTable` (`myColumn`) VALUES(?);');
#This doesn't work:
$sth->execute('😜');
#This doesn't work either:
$sth->execute(encode_utf8('😜'));
#end processing:
$dbh->disconnect();
exit(0);
Both executes throw the same error (only the line number at the end changes):
DBD::mysql::st execute failed: Incorrect string value: '\xF0\x9F\x98\x9C' for column 'myColumn' at row 1 at myTestScript.pl line 16.
What am I doing wrong?
How can I do it better?
The problem is with the SET NAMES utf8 command. In MySQL the utf8 character set is not truly utf8, it supports characters up 3 bytes only and the character in question has 4 bytes:
The utf8 character set in MySQL has these characteristics:
• No support for supplementary characters (BMP characters only).
• A maximum of three bytes per multibyte character.
The true utf8 is the utf8mb4 that you use as character set in the field itself. So, use SET NAMES utf8mb4
So from Perl you should use {mysql_enable_utf8mb4 => 1} instead of {mysql_enable_utf8 => 1}.
I tried so many times, in so many different ways, to cgi script works correctly to read input from STDIN, read html file, print it to STDOUT and search the inputed text on mysql correctly. The attribute mysql_enable_utf8mb4 and "SET NAMES utf8mb4" after the mysql connection works correctly with "meta charset='UTF-8'".
#!/usr/bin/perl
print "Content-type: text/html; charset=UTF-8\n\n";
#use utf8;
#use open ':utf8';
#binmode STDOUT, ":utf8";
#binmode STDIN , ":utf8";
#use encoding 'utf8';
our $dbh = DBI->connect("DBI:mysql:database=$database;host=$servername;port=$port",$username,$password, {PrintWarn => 0, PrintError => 0, mysql_enable_utf8mb4 => 1}) || die;
$dbh->do("SET NAMES utf8mb4");

MySQL utf8mb4, Errors when saving Emojis

I try to save names from users from a service in my MySQL database. Those names can contain emojis like 🙈😂😱🍰 (just for examples)
After searching a little bit I found this stackoverflow linking to this tutorial. I followed the steps and it looks like everything is configured properly.
I have a Database (charset and collation set to utf8mb4 (_unicode_ci)), a Table called TestTable, also configured this way, as well as a "Text" column, configured this way (VARCHAR(191) utf8mb4_unicode_ci).
When I try to save emojis I get an error:
Example of error for shortcake (🍰):
Warning: #1300 Invalid utf8 character string: 'F09F8D'
Warning: #1366 Incorrect string value: '\xF0\x9F\x8D\xB0' for column 'Text' at row 1
The only Emoji that I was able to save properly was the sun ☀️
Though I didn't try all of them to be honest.
Is there something I'm missing in the configuration?
Please note: All tests of saving didn't involve a client side. I use phpmyadmin to manually change the values and save the data. So the proper configuration of the client side is something that I will take care of after the server properly saves emojis.
Another Sidenote: Currently, when saving emojis I either get the error like above, or get no error and the data of Username 🍰 will be stored as Username ????. Error or no error depends on the way I save. When creating/saving via SQL Statement I save with question marks, when editing inline I save with question marks, when editing using the edit button I get the error.
thank you
EDIT 1:
Alright so I think I found out the problem, but not the solution.
It looks like the Database specific variables didn't change properly.
When I'm logged in as root on my server and read out the variables (global):
Query used: SHOW VARIABLES WHERE Variable_name LIKE 'character\_set\_%' OR Variable_name 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 |
| collation_connection | utf8mb4_unicode_ci |
| collation_database | utf8mb4_unicode_ci |
| collation_server | utf8mb4_unicode_ci |
+--------------------------+--------------------+
10 rows in set (0.00 sec)
For my Database (in phpmyadmin, the same query) it looks like the following:
+--------------------------+--------------------+
| Variable_name | Value |
+--------------------------+--------------------+
| character_set_client | utf8 |
| character_set_connection | utf8mb4 |
| character_set_database | utf8mb4 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| collation_connection | utf8mb4_unicode_ci |
| collation_database | utf8mb4_unicode_ci |
| collation_server | utf8mb4_unicode_ci |
+--------------------------+--------------------+
How can I adjust these settings on the specific database?
Also even though I have the first shown settings as default, when creating a new database I get the second one as settings.
Edit 2:
Here is my my.cnf file:
[client]
port=3306
socket=/var/run/mysqld/mysqld.sock
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4
[mysqld_safe]
socket=/var/run/mysqld/mysqld.sock
[mysqld]
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
log_error=/var/log/mysql/error.log
max_connections=200
max_user_connections=30
wait_timeout=30
interactive_timeout=50
long_query_time=5
innodb_file_per_table
character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
!includedir /etc/mysql/conf.d/
character_set_client, _connection, and _results must all be utf8mb4 for that shortcake to be eatable.
Something, somewhere, is setting a subset of those individually. Rummage through my.cnf and phpmyadmin's settings -- something is not setting all three.
If SET NAMES utf8mb4 is executed, all three set correctly.
The sun shone because it is only 3-bytes - E2 98 80; utf8 is sufficient for 3-byte utf8 encodings of Unicode characters.
For me, it turned out that the problem lied in mysql client.
mysql client updates my.cnf's char setting on a server, and resulted in unintended character setting.
So, What I needed to do is just to add character-set-client-handshake = FALSE.
It disables client setting from disturbing my char setting.
my.cnf would be like this.
[mysqld]
character-set-client-handshake = FALSE
character-set-server = utf8mb4
...
Hope it helps.
It is likely that your service/application is connecting with "utf8" instead of "utf8mb4" for the client character set. That's up to the client application.
For a PHP application see http://php.net/manual/en/function.mysql-set-charset.php or http://php.net/manual/en/mysqli.set-charset.php
For a Python application see https://github.com/PyMySQL/PyMySQL#example or http://docs.sqlalchemy.org/en/latest/dialects/mysql.html#mysql-unicode
Also, check that your columns really are utf8mb4. One direct way is like this:
mysql> SELECT character_set_name FROM information_schema.`COLUMNS` WHERE table_name = "user" AND column_name = "displayname";
+--------------------+
| character_set_name |
+--------------------+
| utf8mb4 |
+--------------------+
1 row in set (0.00 sec)
Symfony 5 answer
Although this is not what was asked, people can land up here after searching the web for the same problem in Symfony.
1. Configure MySQL properly
☝️ See (and upvote if helpful) top answers here.
2. Change your Doctrine configuration
/config/packages/doctrine.yaml
doctrine:
dbal:
...
charset: utf8mb4
I'm not proud of this answer, because it uses brute-force to clean the input. It's brutal, but it works
function cleanWord($string, $debug = false) {
$new_string = "";
for ($i=0;$i<strlen($string);$i++) {
$letter = substr($string, $i, 1);
if ($debug) {
echo "Letter: " . $letter . "<BR>";
echo "Code: " . ord($letter) . "<BR><BR>";
}
$blnSkip = false;
if (ord($letter)=="146") {
$letter = "´";
$blnSkip = true;
}
if (ord($letter)=="233") {
$letter = "é";
$blnSkip = true;
}
if (ord($letter)=="147" || ord($letter)=="148") {
$letter = """;
$blnSkip = true;
}
if (ord($letter)=="151") {
$letter = "–";
$blnSkip = true;
}
if ($blnSkip) {
$new_string .= $letter;
break;
}
if (ord($letter) > 127) {
$letter = "&#0" . ord($letter) . ";";
}
$new_string .= $letter;
}
if ($new_string!="") {
$string = $new_string;
}
//optional
$string = str_replace("\r\n", "<BR>", $string);
return $string;
}
//clean up the input
$message = cleanWord($message);
//now you can insert it as part of SQL statement
$sql = "INSERT INTO tbl_message (`message`)
VALUES ('" . addslashes($message) . "')";
ALTER TABLE table_name CHANGE column_name column_name
VARCHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL
DEFAULT NULL;
example query :
ALTER TABLE `reactions` CHANGE `emoji` `emoji` VARCHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL;
after that , successful able to store emoji in table :
Consider adding
init_connect = 'SET NAMES utf8mb4'
to all of your your db-servers' my.cnf-s.
(still, clients can (so will) overrule it)
I was importing data via command:
LOAD DATA LOCAL INFILE 'abc.csv' INTO TABLE abc
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES
(col1, col2, col3, col4, col5...);
This didnt work for me:
SET NAMES utf8mb4;
I had to add the CHARACTER SET to make it working:
LOAD DATA LOCAL INFILE
'E:\\wamp\\tmp\\customer.csv' INTO TABLE `customer`
CHARACTER SET 'utf8mb4'
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES;
Note, the target column must be also utf8mb4 not utf8, or the import will save (without errors thought) the question marks like "?????".
For codeigniter user, ensure your character set and collate setting in database.php is set properly, which is worked for me.
$db['default']['char_set'] = 'utf8mb4';
$db['default']['dbcollat'] = 'utf8mb4_unicode_ci';