在Mysql数据库datadir下创建一个目录,会发生什么呢
最近在做一些Mysql的案例实验,结果发现了一个非常有趣的事情,就是在Mysql数据库datadir下创建一个目录,你猜会发生什么呢。
环境
Mysql数据库环境如下:
操作系统:CentOS Linux release 7.6.1810 (Core)
Mysql版本:
[root@localhost] 15:27:47 [testdb]>select @@version;
+------------+
| @@version |
+------------+
| 5.7.26-log |
+------------+
1 row in set (0.00 sec)
在Mysql数据库datadir下创建一个目录
检查数据库状态,在这里一定看仔细,有几个数据库
[root@localhost] 15:29:36 [testdb]>show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| heartbeat |
| mysql |
| performance_schema |
| pt |
| query_rewrite |
| sys |
| testdb |
| testdb1 |
+--------------------+
9 rows in set (0.00 sec)
在Mysql数据库datadir目录下,手工创建一个tmp的目录
[mysql@mysql 3306]$ mkdir tmp
[mysql@mysql 3306]$ ll
total 12412
-rw-r-----. 1 mysql mysql 56 Jul 13 15:34 auto.cnf
-rw-------. 1 mysql mysql 1679 Jul 14 16:00 ca-key.pem
-rw-r--r--. 1 mysql mysql 1107 Jul 14 16:00 ca.pem
-rw-r--r--. 1 mysql mysql 1107 Jul 14 16:00 client-cert.pem
-rw-------. 1 mysql mysql 1675 Jul 14 16:00 client-key.pem
drwxr-x---. 2 mysql mysql 62 Jul 27 15:26 heartbeat
-rw-r-----. 1 mysql mysql 529 Sep 17 15:30 ib_buffer_pool
-rw-r-----. 1 mysql mysql 12582912 Sep 17 15:30 ibdata1
drwxr-x---. 2 mysql mysql 4096 Jul 13 15:34 mysql
-rw-r-----. 1 mysql mysql 55763 Aug 21 10:10 mysql.log
drwxr-x---. 2 mysql mysql 8192 Jul 13 15:34 performance_schema
-rw-------. 1 mysql mysql 1675 Jul 14 16:00 private_key.pem
drwxr-x---. 2 mysql mysql 62 Jul 14 15:55 pt
-rw-r--r--. 1 mysql mysql 451 Jul 14 16:00 public_key.pem
drwxr-x---. 2 mysql mysql 70 Aug 11 16:01 query_rewrite
-rw-r--r--. 1 mysql mysql 1107 Jul 14 16:00 server-cert.pem
-rw-------. 1 mysql mysql 1675 Jul 14 16:00 server-key.pem
drwxr-x---. 2 mysql mysql 8192 Jul 13 15:34 sys
drwxr-x---. 2 mysql mysql 103 Sep 9 15:23 testdb
drwxr-x---. 2 mysql mysql 170 Sep 7 17:54 testdb1
drwxrwxr-x. 2 mysql mysql 6 Sep 17 15:31 tmp
重启Mysql数据库实例
[mysql@mysql ~]$ ./mysql.sh 3306 stop
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
(No info could be read for "-p": geteuid()=2001 but you should be root.)
Mysql stop is successfully. [ OK ]
[mysql@mysql ~]$
[mysql@mysql ~]$ ./mysql.sh 3306 start
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
Mysql start successfully. [ OK
登录数据库,查看数据库状态
[mysql@mysql ~]$ sh mysql3306
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.26-log MySQL Community Server (GPL)
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
[root@localhost] 15:33:11 [(none)]>show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| heartbeat |
| mysql |
| performance_schema |
| pt |
| query_rewrite |
| sys |
| testdb |
| testdb1 |
| tmp |
+--------------------+
10 rows in set (0.00 sec)

看到没有,多了一个tmp的数据库,更有趣的是,这个tmp库还能里面创建表
[root@localhost] 15:34:13 [(none)]>use tmp;
Database changed
[root@localhost] 15:34:17 [tmp]>show tables;
Empty set (0.00 sec)
[root@localhost] 15:34:22 [tmp]>create table t_test (id int not null,name char(10) not null default '',primary key(id));
Query OK, 0 rows affected (0.03 sec)
[root@localhost] 15:34:52 [tmp]>show tables;
+---------------+
| Tables_in_tmp |
+---------------+
| t_test |
+---------------+
1 row in set (0.00 sec)
总结
总结上面的实验结果,在这里给大家2个建议
建议一:不要随意在Mysql的datadir下创建备份目录或者临时目录
建议二:不要直接将Mysql的datadir设置成,操作系统的文件系统最上级目录,不然你的操作系统重启之后,你的数据库会多一个lost+found的一个数据库。