操作过程
新建表并插入数据
1 [root@server2 ~]# mysql -uroot -p #进入数据库
2 Enter password:
3
4 mysql> create database test; #创建test库
5 Query OK, 1 row affected (0.00 sec)
6
7 mysql> use test; #使用库
8 Database changed
9
10 mysql> create table tt(id int(3) not null,name varchar(10) not null,score int(3) not null,primary key(id)); #创建表
11
12 mysql> insert into tt values(1,‘zhangsan‘,88),(2,‘lisi‘,80),(3,‘lili‘,90); #插入数据
13
14 mysql> select * from tt; #查询数据
15 +----+----------+-------+
16 | id | name | score |
17 +----+----------+-------+
18 | 1 | zhangsan | 88 |
19 | 2 | lisi | 80 |
20 | 3 | lili | 90 |
21 +----+----------+-------+
22 3 rows in set (0.00 sec)
在/目录下创建abc目录
1 [root@server2 ~]# mkdir /abc
修改配置文件并重启服务
1 [client] 2 port = 3306 3 default-character-set=utf8 4 socket = /usr/local/mysql/mysql.sock 5 port = 3306 6 default-character-set=utf8 7 socket = /usr/local/mysql/mysql.sock 8 9 [mysqld] 10 user = mysql 11 basedir = /usr/local/mysql 12 datadir = /usr/local/mysql/data 13 port = 3306 14 character_set_server=utf8 15 pid-file = /usr/local/mysql/mysqld.pid 16 socket = /usr/local/mysql/mysql.sock 17 secure-file-priv = "" #添加一行,可以指定路径,如果为空可以导入任何有权限的路径 18 server-id = 1 19 20 sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES 21 22 [root@server2 ~]# systemctl restart mysqld.service #重启服务
修改/abc权限
1 [root@server2 ~]# chmod 777 /abc
2 [root@server2 ~]# cd /
3 [root@server2 /]# ll
4 drwxrwxrwx. 2 root root 6 11月 25 17:37 abc
导出数据
1 [root@server2 /]# mysql -u root -p
2 Enter password:
3
4 mysql> use test;
5
6 mysql> select * from tt into outfile ‘/abc/test.xls‘;
进入/abc,查看文件是否生成
1 [root@server2 /]# cd abc/
2 [root@server2 abc]# ll
3 总用量 4
4 -rw-rw-rw-. 1 mysql mysql 34 11月 25 17:47 test.xls
导出数据,查看与数据库数据对比,数据一致
总结
将数据库中的数据导出到EXCEL表格中,便于我们恢复备份时核对数据库数据的准确性