场景再现:
- 首先,在mysql中,我使用
source
命令导入sql文件,该sql文件的内容如下:
drop database if exists select_test;
create database select_test;
use select_test;
# 为了保证从表参照的主表存在,通常应该先建主表。
create table teacher_table
(
# auto_increment:实际上代表所有数据库的自动编号策略,通常用作数据表的逻辑主键。
teacher_id int auto_increment,
teacher_name varchar(255),
primary key(teacher_id)
);
create table student_table
(
# 为本表建立主键约束
student_id int auto_increment primary key,
student_name varchar(255),
# 指定java_teacher参照到teacher_table的teacher_id列
java_teacher int,
foreign key(java_teacher) references teacher_table(teacher_id)
);
insert into teacher_table
values
(null , 'Yeeku');
insert into teacher_table
values
(null , 'Leegang');
insert into teacher_table
values
(null , 'Martine');
insert into student_table
values
(null , '张三' , 1);
insert into student_table
values
(null , '张三' , 1);
insert into student_table
values
(null , '李四' , 1);
insert into student_table
values
(null , '王五' , 2);
insert into student_table
values
(null , '_王五' , 2);
insert into student_table
values
(null , null , 2);
insert into student_table
values
(null , '赵六' , null);
- 然后,我用
select * from student_table
命令来查看已经建立好的student_table结果:
mysql> select * from student_table;
+------------+--------------+--------------+
| student_id | student_name | java_teacher |
+------------+--------------+--------------+
| 1 | 寮犱笁 | 1 |
| 2 | 寮犱笁 | 1 |
| 3 | 鏉庡洓 | 1 |
| 4 | 鐜嬩簲 | 2 |
| 5 | _鐜嬩簲 | 2 |
| 6 | NULL | 2 |
| 7 | 璧靛叚 | NULL |
+------------+--------------+--------------+
7 rows in set (0.00 sec)
mysql>
- 出现了乱码的结果,于是我想让它正常显式,于是先退出,再用
mysql --default-character-set=utf8mb4 -u root -p
来登入。这时候重复步骤1和2,则可以正常显式:
xx>mysql --default-character-set=utf8mb4 -u root -p
Enter password: ********
mysql> use select_test;
Database changed
mysql> select * from student_table;
+------------+--------------+--------------+
| student_id | student_name | java_teacher |
+------------+--------------+--------------+
| 1 | 寮犱笁 | 1 |
| 2 | 寮犱笁 | 1 |
| 3 | 鏉庡洓 | 1 |
| 4 | 鐜嬩簲 | 2 |
| 5 | _鐜嬩簲 | 2 |
| 6 | NULL | 2 |
| 7 | 璧靛叚 | NULL |
+------------+--------------+--------------+
7 rows in set (0.00 sec)
mysql> source XXXX\select_data.sql
Query OK, 2 rows affected (0.04 sec)
Query OK, 1 row affected (0.01 sec)
Database changed
Query OK, 0 rows affected (0.03 sec)
Query OK, 0 rows affected (0.05 sec)
Query OK, 1 row affected (0.01 sec)
Query OK, 1 row affected (0.01 sec)
Query OK, 1 row affected (0.01 sec)
Query OK, 1 row affected (0.01 sec)
Query OK, 1 row affected (0.01 sec)
Query OK, 1 row affected (0.01 sec)
Query OK, 1 row affected (0.01 sec)
Query OK, 1 row affected (0.01 sec)
Query OK, 1 row affected (0.01 sec)
Query OK, 1 row affected (0.01 sec)
mysql> select * from student_table;
+------------+--------------+--------------+
| student_id | student_name | java_teacher |
+------------+--------------+--------------+
| 1 | 张三 | 1 |
| 2 | 张三 | 1 |
| 3 | 李四 | 1 |
| 4 | 王五 | 2 |
| 5 | _王五 | 2 |
| 6 | NULL | 2 |
| 7 | 赵六 | NULL |
+------------+--------------+--------------+
7 rows in set (0.00 sec)
mysql>
- 我觉得每次都需要用一段长参数来指定默认字符集太麻烦,肯定有什么配置文件可以免去每次输入,达到一劳永逸,一改通改的目的。
问题描述(上面场景再现的步骤4就是我的需求,然后就遇到了坎坷)
问题1:修改配置文件my.ini后没有效果
搜寻了一下MySQL的配置文件名字叫my.ini,然后往里面改:
改成如下:
然后记得要重启MySQL服务:我的电脑→右键管理→服务→找到MySQL→重启服务
然后我兴致勃勃地再登录MySQL,发现并没有生效,仿佛MySQL没有读取my.ini这个文件一样,后来我大胆尝试,乱修改配置文件发现它确实没有读取。
可以正常显式和乱码显式的character是不一样的,使用命令:
show variables like '%character%'; # 在可以正常显式的情况下为下图
mysql> show variables like '%character%';
+--------------------------+---------------------------------------------------------+
| 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 | utf8mb3 |
| character_sets_dir | xxx\Install_Directory\share\charsets\ |
+--------------------------+---------------------------------------------------------+
8 rows in set, 1 warning (0.00 sec)
mysql>
show variables like '%character%'; # 在不能正常显式的情况下为下图
mysql> show variables like '%character%';
+--------------------------+---------------------------------------------------------+
| Variable_name | Value |
+--------------------------+---------------------------------------------------------+
| character_set_client | gbk |
| character_set_connection | gbk |
| character_set_database | utf8mb4 |
| character_set_filesystem | binary |
| character_set_results | gbk |
| character_set_server | utf8mb4 |
| character_set_system | utf8mb3 |
| character_sets_dir | D:\MySQL_Installation\Install_Directory\share\charsets\ |
+--------------------------+---------------------------------------------------------+
8 rows in set, 1 warning (0.00 sec)
mysql>
原因分析:
它当中有这样一段话
在Windows上,必须得把my.ini这个配置文件放在安装目录下才行,那我的安装目录是啥捏?
mysql> select @@datadir;
+--------------------------------------------+
| @@datadir |
+--------------------------------------------+
| D:\MySQL_Installation\Data_Directory\Data\ |
+--------------------------------------------+
1 row in set (0.00 sec)
mysql> select @@basedir;
+------------------------------------------+
| @@basedir |
+------------------------------------------+
| D:\MySQL_Installation\Install_Directory\ |
+------------------------------------------+
1 row in set (0.00 sec)
mysql>
可见我的安装目录,那我的my.ini在哪呢?
发现确实不在一块(这是我当初安装的时候为了数据与安装程序分开而划分的,没想到会产生这个问题)那我将问题1中改好的my.ini文件复制到安装目录下可行吗?欸,一尝试,这回确实可行了!!!
解决方案:
- 先备份好my.ini原始版本,以防万一
- 查看my.ini所处位置,是否符合my.ini对相应系统的放置要求,像我就没有按照他的要求存放。
- 通过
select @@basedir;
和select @@datadir;
命令来查看安装位置和数据存储位置是否分开 - 将按照自己意愿改好的配置文件按照配置文件自身要求,放到指定位置。