天天看点

MariaDB中文乱码字符集处理1.场景2.配置3.重启服务4.效果

MariaDB中文乱码字符集处理

标签(空格分隔): MySQL

1.场景

服务器的MariaDB字符集中文乱码,经查服务器端设置为latin1,可以使用下面的命令:

MariaDB [(none)]>  SHOW VARIABLES LIKE 'character%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | latin1                     |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | latin1                     |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
 rows in set ( sec)
           

需要修改为utf8

2.配置

目录与配置文件如下

/etc/my.cnf.d
-rw-r--r--   root root  Jun    server.cnf
-rw-r--r--.  root root  Jun    mysql-clients.cnf
-rw-r--r--   root root  Jun    client.cnf
           

其中,

client.cnf

server.cnf

建议修改。

2.1原始内容

client.cnf

cat client.cnf 
#
# These two groups are read by the client library
# Use it for options that affect all clients, but not the server
#


[client]

# This group is not read by mysql client library,
# If you use the same .cnf file for MySQL and MariaDB,
# use it for MariaDB-only client options
[client-mariadb]
           

server.cnf

cat server.cnf 
#
# These groups are read by MariaDB server.
# Use it for options that only the server (but not clients) should see
#
# See the examples of server my.cnf files in /usr/share/mysql/
#

# this is read by the standalone daemon and embedded servers
[server]

# this is only for the mysqld standalone daemon
[mysqld]

# this is only for embedded server
[embedded]

# This group is only read by MariaDB-5.5 servers.
# If you use the same .cnf file for MariaDB of different versions,
# use this group for options that older servers don't understand
[mysqld-]

# These two groups are only read by MariaDB servers, not by MySQL.
# If you use the same .cnf file for MySQL and MariaDB,
# you can put MariaDB-only options here
[mariadb]

[mariadb-]
           

2.2修改后

client.cnf

#
# These two groups are read by the client library
# Use it for options that affect all clients, but not the server
#


[client]
default-character-set=utf8
# This group is not read by mysql client library,
# If you use the same .cnf file for MySQL and MariaDB,
# use it for MariaDB-only client options
[client-mariadb]
           
说明:增加了default-character-set=utf8

server.cnf

#
# These groups are read by MariaDB server.
# Use it for options that only the server (but not clients) should see
#
# See the examples of server my.cnf files in /usr/share/mysql/
#

# this is read by the standalone daemon and embedded servers
[server]

# this is only for the mysqld standalone daemon
[mysqld]
init_connect = 'SET collation_connection = utf8_general_ci'
init_connect = 'SET NAMES utf8'
character_set_server = utf8
collation_server = utf8_general_ci

[mysqld_safe]
init_connect = 'SET collation_connection = utf8_general_ci'
init_connect = 'SET NAMES utf8'
character_set_server = utf8
collation_server = utf8_general_ci

# this is only for embedded server
[embedded]

# This group is only read by MariaDB-5.5 servers.
# If you use the same .cnf file for MariaDB of different versions,
# use this group for options that older servers don't understand
[mysqld-]

# These two groups are only read by MariaDB servers, not by MySQL.
# If you use the same .cnf file for MySQL and MariaDB,
# you can put MariaDB-only options here
[mariadb]

[mariadb-]
           

说明:增加了

[mysqld]

init_connect = ‘SET collation_connection = utf8_general_ci’

init_connect = ‘SET NAMES utf8’

character_set_server = utf8

collation_server = utf8_general_ci

[mysqld_safe]

init_connect = ‘SET collation_connection = utf8_general_ci’

init_connect = ‘SET NAMES utf8’

character_set_server = utf8

collation_server = utf8_general_ci

3.重启服务

4.效果

mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 
Server version: -MariaDB MariaDB Server

Copyright (c) , , Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>  SHOW VARIABLES LIKE 'character%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
 rows in set ( sec)
           

完毕,成功。

继续阅读