解决 mariadb 中文乱码的问题
参考
1. 首先创建数据库的时候就设定 utf8 编码create database dbname charset=utf8
2. 查看 msyql 中库的编码格式
# 连接数据库
mysql -uroot -ppassword dbname
# 查看编码格式
show variables like 'character%';
# 输出
MariaDB [blog]> show variables like 'character%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.001 sec)
# character_set_client为客户端编码方式;
# character_set_connection为建立连接使用的编码;character_set_database数据库的编码;
# character_set_results结果集的编码;
# character_set_server数据库服务器的编码;
# 只要保证以上四个采用的编码方式一样,就不会出现乱码问题。
**注意**: 已建的库和表,编码不会改变。如果在已有库中继续建表,表依然会继承来自库的过去使用的编码
3. 修改所有编码格式都为 utf8
[mysqld]
default-time-zone='+8:00'
lower_case_table_names=1
log-error=/var/log/mysql/log-error.log
default-storage-engine = innodb
innodb_file_per_table
max_connections = 4096
collation-server = utf8_general_ci
# 数据库服务器的编码
character-set-server = utf8
[client]
# 客户端编码方式
default-character-set=utf8
[mysql]
default-character-set=utf8
4. 重启 mariadb 服务,再次查看编码格式,搞定
# 查看编码格式
show variables like 'character%';
# 输出
MariaDB [blog]> 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/ |
+--------------------------+----------------------------+
8 rows in set (0.001 sec)