AWS RDS MySQL with Unicode - mysql

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.

Related

Save emojis to MYSQL DB

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.

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.

MySQL Workbench - Collation reverts to Schema Default

This seems like I'm just missing something trivial, but I'm not able to change the collation of a table from Schema Default. Selecting anything from the dropdown just reverts to Schema Default.
I have utf8 - utf8_general_ci set as the schema collation, which I can change without issue.
However, even though the schema default is set to utf8_general_ci and the tables supposedly use the schema default, when I export the SQL CREATE script and import it in phpMyAdmin, the collation is set to latin1_swedish_ci.
The script itself contains a correct CREATE SCHEMA statement:
CREATE SCHEMA IF NOT EXISTS 'my_table' DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
But the CREATE TABLE statements only include the engine assignment.
I'm using MySQL Workbench 6.0, and the server I'm using is running MySQL 5.5.34 and phpyMyAdmin 3.4.11.1. The server default collation is also utf8_general_ci.
EDIT: As I suspected, something stupid. I created the database via the cPanel beforehand rather than through the SQL script, and the default collation was set to latin1_swedish_ci.
However, that doesn't explain why I couldn't set specific collations on the tables in Workbench.
Collations when not given the way they're supposed to be given will certainly revert to Schema Default.
Where did you set the collation in MySQL Workbench actually? There are 2 places, one in modeling and one for live objects (existing db objects in a server).
If the latter, did you apply your changes?

default database collation not respected while importing

In my database, the collation was originally utf8_general_ci. However, I noticed that utf8_unicode_ci is necessary because of better sorting accuracy.
So I exported all database using phpmyadmin and checked that the word "COLLATION" does not appear in the exported sql file (except for only 2 times in one table where it is set to binary) so generally this script is collation agnostic and should not imply any specific collation when importing but use database default.
After dropping all tables, the database collation was changed to utf8_unicode_ci and then the import script was run from phpmyadmin. But as a result, all tables and all columns are shown again with utf8_general_ci collation (and sorting is incorrect). Why?? And what to do to change it?
P.S. The export/import script contains commented line at the beginning:
/*!40101 SET #OLD_COLLATION_CONNECTION=##COLLATION_CONNECTION */;
I don't know if it has any impact while importing, but after opening mysql console, the command show variables like 'collation_connection'shows COLLATION_CONNECTION as cp852_general_ci.
However, in phpmyadmin->variables the variable 'collation_connection' is set to utf8_general_ci. But there is no way to change it.
That happens because the database export is setting the character set on every table, and such a clause comes with a default collation that depends on the character set, not on the collation of your connection. utf8_general_ci is the default collation for utf8.
You'll have to convert your tables with something like ALTER TABLE tablename CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci; or edit your database export if this is affordable.
As for the MySQL console: the command-line client is pretty much broken on Windows. It'll never support, display or read Unicode, and you're getting a per-connection collation for that client that matches your Windows so-called OEM character set for your locale. This is a Windows misfeature that's difficult to workaround in portable software. PHPMyAdmin uses a web server and doesn't suffer from this problem. I advise you to use a UNIX-like operating system like GNU/Linux for any serious work in any case, not just for this reason. As an added benefit, MySQL, Apache and your whole application stack perform better on Linux.

How to set encoding of database in Microsoft Server Managment Studio?

How to set encoding of database in Microsoft Server Managment Studio?
I need to set utf8 to my database and I can't find option in settings of database.
You cannot set UTF8 encoding to SQL Server database.
You can use char/varchar data types to save characters- one character = one byte- but that also means you can save only limited characters (see collations).
You can use nchar/nvarchar data types to save characters- one character = two bytes (its UCS-2. for sorting, searching.. see collations).
Finally in varbinary column you can store UTF8 or whatever you need :).
It is taken from here How do I set database default Encoding?
Assuming by encoding you mean collation, you can change the default for new databases like:
alter database model collate SQL_Latin1_General_CP1_CI_AS
The change the collation of an existing database:
alter database YourDbName collate SQL_Latin1_General_CP1_CI_AS
The list of available collations is returned by a system function:
select * from fn_helpcollations()