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
  • 相关阅读:
    字节跳动总监知乎5716赞的Java开发笔记
    刚面完阿里Java高级岗40K的offer,这些技术你必须要掌握!
    HDOJ 1253 胜利大逃亡(bfs)
    swjtu 1962 A+B(模拟)
    swjtu 2213 A Game About Cards(模拟题)
    HDU 1544 Palindromes(回文子串)
    第八章 异常控制流
    第四讲 深入介绍信号与槽
    第三讲 对话框的创建
    第二讲 窗口部件的布局与通信
  • 原文地址:https://www.cnblogs.com/liujunjun/p/12199945.html
Copyright © 2011-2022 走看看