Plesk

How to change the MySQL/MariaDB default charset from “utf8_unicode_ci” to “utf8mb4_unicode_ci” in MySQL in Plesk for Linux?

Question

How to change the MySQL/MariaDB default charset from "utf8_unicode_ci" to "utf8mb4_unicode_ci" in MySQL in Plesk for Linux?

Answer

This feature is not yet implemented in Plesk.

If you would like to see this feature in Plesk, please vote for it on Plesk UserVoice:

By default, Plesk databases are created with the following command:

CREATE DATABASE
databasename
/*!40101 default charset=utf8 */

As a workaround, apply the following solution:

Create an event which will change the charset upon creation of a new database
  1. Connect to a Plesk server via SSH.

  2. Create the file /root/dbscript.sh with the following content:

    #!/bin/sh

    db=${NEW_DATABASE_NAME}

    plesk db "ALTER DATABASE $db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci"

  3. Set the correct permissions:

    # chmod 0775 /root/dbscript.sh

  4. In Plesk, go to Tools & Settings > Event Manager and click Add Event Handler.
  5. Choose the event type to be Database created, put the following in the command section and press OK:

    /root/dbscript.sh

Additionally, you could also set the UTF8mb4 charset directly within the configuration file of your database server by using the information below:

Change the charset directly in MySQL/MariaDB configuration (via SSH)

Warning: These changes affect in database creation that is done directly in MySQL/MariaDB and is not done via Plesk interaction.

  1. Log into the server via SSH.

  2. Open the /etc/my.cnf file with the vi text editor and add the following lines under the corresponding sections:

    Note: for example if the default-character-set line already specified replace its value with utf8mb4.

    [client]
    default-character-set = utf8mb4
    [mysql]
    default-character-set = utf8mb4
    [mysqld]
    character-set-server = utf8mb4
    collation-server = utf8mb4_unicode_ci

  3. Restart the MariaDB service to apply the changes:

    # systemctl restart mariadb

Additional information

Best way to make sure a MySQL database is fully in UTF8 - Server Fault

sql - How to convert a big MySQL Database from utf8 to utf8mb4? - Stack Overflow

mysql - Trouble with UTF-8 characters; what I see is not what I stored - Stack Overflow

Exit mobile version