shift-org / shift-docs

Shift2Bikes: website and calendar for shift and pedalpalooza
https://shift2bikes.org
Other
22 stars 17 forks source link

Set MySQL environment/session variables to UTF-8 #796

Open carrythebanner opened 3 months ago

carrythebanner commented 3 months ago

Following #332, the database itself is now UTF-8. However, MySQL session's terminal output still shows ? in place of non-ASCII characters. This is because MySQL itself has some character set and collation settings which are analogous to the database/table ones. Show the values by running this statement:

SHOW VARIABLES WHERE Variable_name LIKE 'character\_set\_%' OR Variable_name LIKE 'collation%';

and you'll get output like this:

+--------------------------+--------------------+
| Variable_name            | Value              |
+--------------------------+--------------------+
| character_set_client     | latin1             |
| character_set_connection | latin1             |
| character_set_database   | utf8mb4            |
| character_set_filesystem | binary             |
| character_set_results    | latin1             |
| character_set_server     | latin1             |
| character_set_system     | utf8               |
| collation_connection     | latin1_swedish_ci  |
| collation_database       | utf8mb4_general_ci |
| collation_server         | latin1_swedish_ci  |
+--------------------------+--------------------+
10 rows in set (0.01 sec)

These statements will set those variables to UTF-8:

SET NAMES utf8mb4;
set session character_set_server=utf8mb4;
set session collation_server=utf8mb4_general_ci;

Re-running the above SHOW VARIABLES command now shows:

+--------------------------+--------------------+
| 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_general_ci |
| collation_database       | utf8mb4_general_ci |
| collation_server         | utf8mb4_general_ci |
+--------------------------+--------------------+
10 rows in set (0.01 sec)

That works, and UTF-8 characters now show correctly in the terminal.

However, these settings don't persist across sessions. There are ways to set these for each session, or even better permanently as a config for all sessions, but I wasn't yet able to sniff out exactly how to do that.