zoukankan      html  css  js  c++  java
  • 备份数据库(myqldump)

    mysql> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mingongge          |
    | mysql              |
    | performance_schema |
    | sys                |
    +--------------------+
    5 rows in set (0.08 sec)
    
    mysql> create database a;
    Query OK, 1 row affected (0.00 sec)
    
    mysql> use a;
    Database changed
    mysql> create table a1(name char(20),age int(10));
    Query OK, 0 rows affected (0.07 sec)
    
    mysql> insert into  a1(name,age) values('lishi',19);
    Query OK, 1 row affected (0.06 sec)
    
    mysql> select * from a1;
    +-------+------+
    | name  | age  |
    +-------+------+
    | lishi |   19 |
    +-------+------+
    1 row in set (0.00 sec)
    
    mysql> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | a                  |
    | mingongge          |
    | mysql              |
    | performance_schema |
    | sys                |
    +--------------------+
    6 rows in set (0.00 sec)

    备份某一个库。

    [root@localhost ~]# mysqldump -uroot -p123 a > 1.sql
    mysqldump: [Warning] Using a password on the command line interface can be insecure.
    [root@localhost ~]# ls 1.sql -l
    -rw-r--r-- 1 root root 1798 1月  16 09:55 1.sql

    恢复到a2库。

    [root@localhost ~]# mysql -uroot -p123 a2 < 1.sql
    mysql: [Warning] Using a password on the command line interface can be insecure.
    ERROR 1049 (42000): Unknown database 'a2'
    a2库必须事先存在
    
    mysql> create database a2;
    Query OK, 1 row affected (0.00 sec)
    
    mysql> exit
    Bye
    [root@localhost ~]# mysql -uroot -p123 a2 < 1.sql
    mysql: [Warning] Using a password on the command line interface can be insecure.
    
    mysql> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | a                  |
    | a2                 |
    | mingongge          |
    | mysql              |
    | performance_schema |
    | sys                |
    +--------------------+
    7 rows in set (0.00 sec)
    
    mysql> use a2;
    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> show tables;
    +--------------+
    | Tables_in_a2 |
    +--------------+
    | a1           |
    +--------------+
    1 row in set (0.00 sec)
    
    mysql> select * from a1;
    +-------+------+
    | name  | age  |
    +-------+------+
    | lishi |   19 |
    +-------+------+
    1 row in set (0.00 sec)

    备份其中的一个表

    mysql> create database b;
    Query OK, 1 row affected (0.00 sec)
    
    mysql> use b
    Database changed
    mysql> create table b1(name char(20),age int(10));
    Query OK, 0 rows affected (0.06 sec)
    
    [root@localhost ~]# mysqldump -uroot -p123 b b1 > 4.sql
    mysqldump: [Warning] Using a password on the command line interface can be insecure.
    [root@localhost ~]# ls 4.sql 
    4.sql

    还原一张表

    [root@localhost ~]# mysql -uroot -p123 yu < 4.sql
    mysql: [Warning] Using a password on the command line interface can be insecure.
    ERROR 1049 (42000): Unknown database 'yu'
    
    mysql> create database yu;
    Query OK, 1 row affected (0.00 sec)
    
    [root@localhost ~]# mysql -uroot -p123 yu < 4.sql
    mysql: [Warning] Using a password on the command line interface can be insecure.
    mysql> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | a                  |
    | a2                 |
    | b                  |
    | mingongge          |
    | mysql              |
    | performance_schema |
    | sys                |
    | yu                 |
    +--------------------+
    9 rows in set (0.00 sec)
    mysql> show tables;
    +--------------+
    | Tables_in_yu |
    +--------------+
    | b1           |
    +--------------+
    1 row in set (0.00 sec)

    同时备份多个数据库

    [root@localhost ~]# mysqldump -uroot -p123 --databases a b > 10.sql
    mysqldump: [Warning] Using a password on the command line interface can be insecure.
    [root@localhost ~]# ls 10.sql -l
    -rw-r--r-- 1 root root 2576 1月  16 10:15 10.sql

    恢复。

    [root@localhost ~]# mysql -uroot -p123 < 10.sql
    mysql: [Warning] Using a password on the command line interface can be insecure.

    备份

    [root@localhost ~]# mysqldump -uroot -p -B a b mysql|gzip >/root/a_mysql_$(date +%F).sql.gz
    Enter password: 
    [root@localhost ~]# ls -l a_mysql_2020-01-16.sql.gz 
    -rw-r--r-- 1 root root 228989 1月  16 10:23 a_mysql_2020-01-16.sql.gz
    [root@localhost ~]# mysqldump -uroot -p -B --events a b mysql|gzip >/root/a_mysql0_$(date +%F).sql02.gz
    Enter password: 
    [root@localhost ~]# ls -l a_mysql*
    -rw-r--r-- 1 root root 229027 1月  16 10:25 a_mysql0_2020-01-16.sql02.gz
    -rw-r--r-- 1 root root 228989 1月  16 10:23 a_mysql_2020-01-16.sql.gz
  • 相关阅读:
    【PAT甲级】1043 Is It a Binary Search Tree (25 分)(判断是否为BST的先序遍历并输出后序遍历)
    Educational Codeforces Round 73 (Rated for Div. 2)F(线段树,扫描线)
    【PAT甲级】1042 Shuffling Machine (20 分)
    【PAT甲级】1041 Be Unique (20 分)(多重集)
    【PAT甲级】1040 Longest Symmetric String (25 分)(cin.getline(s,1007))
    【PAT甲级】1039 Course List for Student (25 分)(vector嵌套于map,段错误原因未知)
    Codeforces Round #588 (Div. 2)E(DFS,思维,__gcd,树)
    2017-3-9 SQL server 数据库
    2017-3-8 学生信息展示习题
    2017-3-5 C#基础 函数--递归
  • 原文地址:https://www.cnblogs.com/liujunjun/p/12199945.html
Copyright © 2011-2022 走看看