• 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 '
    '
    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: 
  • 相关阅读:
    python学习笔记(二十三)私有方法和私有属性
    python学习笔记(二十二)实例变量、实例方法、类变量、类方法、属性方法、静态方法
    python学习笔记(二十一)构造函数和析构函数
    python学习笔记(二十)初识面向对象
    大型网站系统架构的演化
    订单系统中并发问题和锁机制的探讨
    利用log4j+mongodb实现分布式系统中日志统一管理
    怎样编写高质量的java代码
    十五分钟学会用Hessian
    Apache Mina实战
  • 原文地址:https://www.cnblogs.com/hello-wei/p/10600774.html
Copyright © 2020-2023  润新知