Export the mysql table to the Windows system Excel table in the Linux system

Method 1: Export data with table structure

1. First create a directory to store the table

[root@localhost ~]# mkdir /opt/share    ##创建一个目录存放表
[root@localhost ~]# chmod +777 /opt/share    ##授予最高权限

2. Modify the /etc/my.cnf configuration

Syntax: select * from source table into outfile'path to export table';

[root@localhost ~]# vi /etc/my.cnf
[mysqld]
secure-file-priv=''    ##插入这条配置
……
[root@localhost ~]# systemctl restart mysqld

3. Enter the database and export the table to the Linux system

[root@localhost ~]# mysql -uroot -p   ##进入MySQL
mysql> use school;    ##进库
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed

mysql> select * from 乘法表;   ##查看表信息
+----------------------------------------------------------------------------------+
| 九九                                                                             |
+----------------------------------------------------------------------------------+
|    1*1=1                                                                         |
|    2*1=2   2*2=4                                                                 |
|    3*1=3   3*2=6   3*3=9                                                         |
|    4*1=4   4*2=8   4*3=12   4*4=16                                               |
|    5*1=5   5*2=10   5*3=15   5*4=20   5*5=25                                     |
|    6*1=6   6*2=12   6*3=18   6*4=24   6*5=30   6*6=36                            |
|    7*1=7   7*2=14   7*3=21   7*4=28   7*5=35   7*6=42   7*7=49                   |
|    8*1=8   8*2=16   8*3=24   8*4=32   8*5=40   8*6=48   8*7=56   8*8=64          |
|    9*1=9   9*2=18   9*3=27   9*4=36   9*5=45   9*6=54   9*7=63   9*8=72   9*9=81 |
+----------------------------------------------------------------------------------+
9 rows in set (0.00 sec)

mysql> select * from 乘法表 into outfile '/opt/share/乘法表.xls';    ##使用这条SQL语句将表导出为xls格式文件
Query OK, 9 rows affected (0.01 sec)
mysql> \q   ##退出数据库
Bye

4. Check whether the bid in the catalog has been successfully exported

[root@localhost ~]# cd /opt/share/     ##进入到存放表的目录,查看是否出现导出的表
[root@localhost share]# ll
total 12
-rw-rw-rw- 1 mysql mysql  401 Oct 19 14:26 乘法表.xls
[root@localhost share]# cat 乘法表.xls    ##导出成功
   1*1=1
   2*1=2   2*2=4
   3*1=3   3*2=6   3*3=9
   4*1=4   4*2=8   4*3=12   4*4=16
   5*1=5   5*2=10   5*3=15   5*4=20   5*5=25
   6*1=6   6*2=12   6*3=18   6*4=24   6*5=30   6*6=36
   7*1=7   7*2=14   7*3=21   7*4=28   7*5=35   7*6=42   7*7=49
   8*1=8   8*2=16   8*3=24   8*4=32   8*5=40   8*6=48   8*7=56   8*8=64
   9*1=9   9*2=18   9*3=27   9*4=36   9*5=45   9*6=54   9*7=63   9*8=72   9*9=81


5. Pull the xls file to the Windows system

Copy files in Linux system directly to Windows for viewing
Insert picture description here

Insert picture description here

Insert picture description here

Method 2: Only data, no table structure

[root@localhost ~]# mysql -uroot -p school -e "select * from 乘法表;" >/opt/九九.xls 
Enter password: 
[root@localhost ~]# cat /opt/九九.xls 
九九
 1
 2 4
 3 6 9
 4 8 12 16
 5 10 15 20 25
 6 12 18 24 30 36
 7 14 21 28 35 42 49
 8 16 24 32 40 48 56 64
 9 18 27 36 45 54 63 72 81

The data is successfully exported, but there are only data, no tables
Insert picture description here

Guess you like

Origin blog.csdn.net/CN_LiTianpeng/article/details/109153858