3 Ways to Get the Server Collation in MariaDB

Here are three ways to show the server collation in MariaDB.

The @@collation_server Variable

The @@collation_server variable contains the default collation used by the server. This is automatically set to the default collation for a given character set when character_set_server is changed, but it can also be set manually.

Here’s how to select it:

SELECT @@collation_server;

Example result:

+--------------------+
| @@collation_server |
+--------------------+
| utf8mb4_general_ci |
+--------------------+

That’s the collation in my test environment. The result you get may be different, depending on your environment.

The SHOW VARIABLES Statement

The SHOW VARIABLES statement shows the values of MariaDB system variables. This can return a lot of data, so it’s usually best to use either a WHERE or LIKE clause to narrow it down to only those variables that you’re interested in.

Example:

SHOW VARIABLES WHERE Variable_name = 'collation_server';

Example result:

+------------------+--------------------+
| Variable_name    | Value              |
+------------------+--------------------+
| collation_server | utf8mb4_general_ci |
+------------------+--------------------+

Alternatively, you can use the LIKE clause to return variables that begin with collation:

SHOW VARIABLES LIKE 'collation%';

Example result:

+----------------------+--------------------+
| Variable_name        | Value              |
+----------------------+--------------------+
| collation_connection | utf8_general_ci    |
| collation_database   | utf8mb4_general_ci |
| collation_server     | utf8mb4_general_ci |
+----------------------+--------------------+

By default, SHOW VARIABLES shows the SESSION variables. In this case, it returns the values that are in effect for the current connection.

Therefore, the previous example could be rewritten as follows:

SHOW SESSION VARIABLES LIKE 'collation%';

Example result:

+----------------------+--------------------+
| Variable_name        | Value              |
+----------------------+--------------------+
| collation_connection | utf8_general_ci    |
| collation_database   | utf8mb4_general_ci |
| collation_server     | utf8mb4_general_ci |
+----------------------+--------------------+

Alternatively, you can replace SESSION with LOCAL (which is a synonym for SESSION):

SHOW LOCAL VARIABLES LIKE 'collation%';

You can also use the GLOBAL modifier to display the values that are used for new connections to MariaDB.

Example:

SHOW GLOBAL VARIABLES LIKE 'collation%';

Example result:

+----------------------+--------------------+
| Variable_name        | Value              |
+----------------------+--------------------+
| collation_connection | utf8mb4_general_ci |
| collation_database   | utf8mb4_general_ci |
| collation_server     | utf8mb4_general_ci |
+----------------------+--------------------+

The mariadb-admin Utility (aka mysqladmin)

It’s also possible to get server collation information with the mariadb-admin utility.

You can use this utility to return all variables. And you can narrow those variables down to just the ones you’re interested in – in this case, the collation_server variable.

To return just the collation_server variable, open a new terminal and run the following command:

mariadb-admin variables | grep collation_server

Result:

| collation_server     | utf8mb4_general_ci

Another way to do it is replace mariadb-admin with mysqladmin.

Like this:

mysqladmin variables | grep collation_server

You will need to use mysqladmin if you use an earlier version of MariaDB (before 10.4.6). From MariaDB 10.4.6, mariadb-admin is a symlink to mysqladmin. From MariaDB 10.5.2, mariadb-admin is the name of the script, with mysqladmin a symlink. See the MariaDB documentation for mysqladmin for more information.