MySQL导出数据到文件中

一.导出一张表数据

把test_time表中的数据导出成txt 文件

mysql> show global variables like '%secure%';  
+--------------------------+-----------------------+
| Variable_name            | Value                 |
+--------------------------+-----------------------+
| require_secure_transport | OFF                   |
| secure_auth              | ON                    |
| secure_file_priv         | /var/lib/mysql-files/ | 这个是导出目录
+--------------------------+-----------------------+
3 rows in set (0.01 sec)

进行导出操作:

mysql> select * from test_time into outfile '/var/lib/mysql-files/test_time.txt';         
Query OK, 1 row affected (0.00 sec)

会在目录下生成数据文件:

[root@master mysql-files]# more test_time.txt
1       no      2019-03-26 11:29:04     2019-03-26 11:30:55

可以进行格式的调整

SELECT  *  INTO OUTFILE '/var/lib/mysql-files/test_time.txt'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
FROM test_time;

此刻生成的数据文件:

[root@master mysql-files]# more test_time.txt
1,"no","2019-03-26 11:29:04","2019-03-26 11:30:55"

 

二 导出SQL文件的数据

[root@master mysql-files]# mysqldump -u root -p mydb test_time > test_time.txt
Enter password: 

以上命令创建的文件内容如下:

-- MySQL dump 10.13  Distrib 5.7.25, for Linux (x86_64)
--
-- Host: localhost    Database: mydb
-- ------------------------------------------------------
-- Server version       5.7.25

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

--
-- Table structure for table `test_time`
--

DROP TABLE IF EXISTS `test_time`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `test_time` (
  `id` int(11) NOT NULL,
  `status` varchar(24) DEFAULT NULL,
  `create_time` datetime DEFAULT CURRENT_TIMESTAMP,
  `update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='测试时间表';
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `test_time`
--

LOCK TABLES `test_time` WRITE;
/*!40000 ALTER TABLE `test_time` DISABLE KEYS */;
INSERT INTO `test_time` VALUES (1,'no','2019-03-26 11:29:04','2019-03-26 11:30:55');
/*!40000 ALTER TABLE `test_time` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

-- Dump completed on 2019-03-26 11:59:06

 

如果你需要导出整个数据库的数据,可以使用以下命令:

[root@master mysql-files]# mysqldump -u root -p mydb>mydb.txt
Enter password: 

 

如果需要备份所有数据库,可以使用以下命令:

[root@master mysql-files]# mysqldump -u root -p --all-databases > database_dump.txt
Enter password: 

 

posted @ 2019-03-26 16:04  醉城、  阅读(12004)  评论(0编辑  收藏  举报