Search blog

Sunday, February 26, 2017

Enabling Multi-byte UTF-8 support in Drupal 7

This tutorial explains the various steps to enable Multi-byte UTF-8 support in Drupal 7. It's a very simple and straightforward process and if implemented rightly, you can easily convert your 'character set' & collation to

According to this post in drupal.org, multi-byte UTF-8 support for MySQL and other database drivers, allows emojis, Asian symbols, mathematical symbols, etc with your Drupal 7 installation. The only caveat to this is that your Drupal installation must be at least Version 7.50 or higher.


Requirements:
  •  Drupal: Version 7.50 or above
 Initially when you check your Drupal Site Report at: admin/reports/status, you will see a message similar to the image below:

Click on image to enlarge

 The steps below have been compiled from the following post at drupal.org: https://www.drupal.org/node/2754539

For MySQL

1. In order to be able to use this, the following requirements must be met:

In order to allow for large indexes, MySQL must be set up with the following my.cnf settings:

 [mysqld]  
 innodb_large_prefix=true  
 innodb_file_format=barracuda  
 innodb_file_per_table=true  

These settings are available as of MySQL 5.5.14, and are defaults in MySQL 5.7.7 and up.

After editing the my.cnf file (often located at /etc/mysql/my.cnf), don't forget to restart the mysql service (for example, with the command sudo service mysql restart) in order to load the new configuration.

2. The PHP MySQL driver must support the utf8mb4 charset (libmysqlclient 5.5.3 and up, as well as mysqlnd 5.0.9 and up).

3. The MySQL server must support the utf8mb4 charset (5.5.3 and up).

Run the command mysql --version to see your current MySQL version.

Steps to enable 

 

For existing Drupal installations
  • First, back up your database and convert all existing tables to utf8mb4, such as by using the drush command provided by the utf8mb4_convert contrib project.
     
  • Set the "charset" and "collation" keys on the database connection array in settings.php to "utf8mb4" and "utf8mb4_general_ci" respectively:

     $databases['default']['default'] = array(  
      'driver' => 'mysql',  
      'database' => 'databasename',  
      'username' => 'username',  
      'password' => 'password',  
      'host' => 'localhost',  
      'charset' => 'utf8mb4',  
      'collation' => 'utf8mb4_general_ci',  
     );  
    

 For new Drupal installations

  • Prior to running the installer, edit settings.php and manually add in the full database settings array code, with 'charset' as 'utf8mb4', and 'collation' as 'utf8mb4_general_ci', and with the actual values for 'database', 'username', 'password', 'host', as shown below:

     databases['default']['default'] = array(  
      'driver' => 'mysql',  
      'database' => 'databasename',  
      'username' => 'username',  
      'password' => 'password',  
      'host' => 'localhost',  
      'charset' => 'utf8mb4',  
      'collation' => 'utf8mb4_general_ci',  
     );  
    

  • Then run the installer specifically by accessing install.php, as in http://example.com/install.php (not just http://example.com/)

Other database systems


  • PostgreSQL and SQLite support 4-byte UTF-8 out-of-the-box, so no special changes are needed for these.
  • MariaDB and other MySQL equivalents should likely work with similar instructions as above for MySQL.
  • Database drivers provided by contributed modules may or may not support this feature. The best place to look for answers is the issue queue of the contributed module which provides the database driver you are using.
Now when you look at your Drupal Site Report, you should be getting a warning message as the image below, which is related to Database 4 byte UTF-8 support:

Click on image to enlarge 

Converting Characterset & Collation to utf8mb4:

Using SQL commands:

 ALTER DATABASE db_name CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;  
 ALTER TABLE tablename CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;  

Or if you're still on MySQL 5.5.2 or older which didn't support 4-byte UTF-8, use utf8 instead of utf8mb4:

 ALTER DATABASE databasename CHARACTER SET utf8 COLLATE utf8_unicode_ci;  
 ALTER TABLE tablename CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;  

 

Easier way using Drush:


Now to convert all your database tables to utf8mb4 characterset and utf8mb4_general_ci collation, you may make use of the following Drush command called UTF8MB4 Convert. Please find detailed instructions of this command here: https://www.drupal.org/project/utf8mb4_convert.

The conversion process in your terminal after executing the above commands should be as below:

Click image to enlarge
 Final Message After Conversion:


Click image to enlarge
Finally when all your tables are converted, you can view each of their's Collation by using any database management tool such as phpMyAdmin. 

 
Click image to enlarge

Finally your Drupal site should display a message like this, confirming successful enabling of  Multi-byte UTF-8 support.

Click image to enlarge
 That's all there is to enable multi-byte UTF-8 support in Drupal 7.

No comments:

Post a Comment

Thank you for your Feedback!
www.evagabond.me

Top 5 Posts (Weekly)