mariadb配置文件修改字符集:
[mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 # # Settings user and group are ignored when systemd is used. # # If you need to run mysqld under a different user or group, # # customize your systemd unit file for mariadb according to the # # instructions in http://fedoraproject.org/wiki/Systemd # default-storage-engine = INNODB #定义存储引擎 character-set-server = utf8 #设置字符集为utf8 collation-server = utf8_general_ci #设置字符集的检对规则 [client] default-character-set=utf8 [mysqld_safe] log-error=/var/log/mariadb/mariadb.log pid-file=/var/run/mariadb/mariadb.pid # # # # # include all files from the config directory # ## !includedir /etc/my.cnf.d
新安装MYSQL,还没有修改数据库系统编码。
之后由于创建数据库时候:create database db_name; 没有指定编码,之后发现乱码就修改各个属性之后还是乱码,便开始配置数据库属性,之后信息如下:
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
发现character_set_database该属性设置utf8之后依旧还是Latin1 ,其实这个属性是数据库属性,在创建数据库时候如果不指定默认就是这个,因此对于创建的数据库可以修改数据库编码为utf8,修改脚本:
alter database <数据库名> character set utf8;
之后还是乱码,由于之前数据错误编码,所以设置utf8依旧乱码,删除数据重新导入即可。因此为了避免此问题,以后安装数据库首要任务就是配置编码,创建数据库时候指定编码:
创建数据库时候指定编码:
create database `mydb` character set utf8 collate utf8_general_ci; #指定编码字符集
MariaDB [auth]> CREATE DATABASE `mydb` CHARACTER SET utf8 COLLATE utf8_general_ci;
Query OK, 1 row affected (0.00 sec)
MariaDB [auth]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| auth |
| mydb |
| mysql |
| performance_schema |
| test |
+--------------------+
6 rows in set (0.00 sec)
MariaDB [client]> show variables like '%char%';
+--------------------------+----------------------------+
| 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.00 sec)
MariaDB [auth]> use mydb;
Database changed
MariaDB [mydb]> show tables;
Empty set (0.00 sec)
MariaDB [mydb]> create table mydb_info(user_name char(20) not null, user_passwd char(30) default '',primary key(user_name));
Query OK, 0 rows affected (0.00 sec)
MariaDB [mydb]> show tables;
+----------------+
| Tables_in_mydb |
+----------------+
| mydb_info |
+----------------+
1 row in set (0.01 sec)
MariaDB [mydb]> insert into mydb_info(user_name,user_passwd) values('zhangsan',password('123'));
Query OK, 1 row affected, 1 warning (0.00 sec)
MariaDB [mydb]> select database();
+------------+
| database() |
+------------+
| mydb |
+------------+
1 row in set (0.00 sec)
MariaDB [mydb]> desc mydb_info;
+-------------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+----------+------+-----+---------+-------+
| user_name | char(20) | NO | PRI | NULL | |
| user_passwd | char(30) | YES | | | |
+-------------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)
MariaDB [mydb]> select * from mydb_info;
+-----------+--------------------------------+
| user_name | user_passwd |
+-----------+--------------------------------+
| zhangsan | *23AE809DDACAF96AF0FD78ED04B6A |
+-----------+--------------------------------+
1 row in set (0.00 sec)
MariaDB [mydb]> insert into mydb_info values('王五',password('123'));
Query OK, 1 row affected, 1 warning (0.01 sec)
MariaDB [mydb]> select * from mydb_info;
+-----------+--------------------------------+
| user_name | user_passwd |
+-----------+--------------------------------+
| zhangsan | *23AE809DDACAF96AF0FD78ED04B6A |
| 王五 | *23AE809DDACAF96AF0FD78ED04B6A |
+-----------+--------------------------------+
3 rows in set (0.00 sec)