• mysql数据库备份


    数据库备份

    数据库备份方案:

    全量备份

    全量备份就是指对某一个时间点上的所有数据或应用进行的一个完全拷贝,

    数据恢复快,备份时间长

    增量备份

    增量备份是指在一次全备份或上一次增量备份后,以后每次的备份只需备份
    与前一次相比增加和者被修改的文件。

    没有重复的备份数据
    备份时间短
    恢复数据时必须按一定的顺序进行

    差异备份

    备份上一次的完全备份后发生变化的所有文件,差异备份是指在一次全备份后到进行差异备份的这段时间内
    对那些增加或者修改文件的备份。在进行恢复时,我们只需对第一次全量备份和最后一次差异备份进行恢复。

     备份的工具mysqldump

    /语法:
        mysqldump [OPTIONS] database [tables ...]
        mysqldump [OPTIONS] --all-databases [OPTIONS]
        mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...]
        
    //常用的OPTIONS:
        -uUSERNAME      //指定数据库用户名
        -hHOST          //指定服务器主机,请使用ip地址
        -pPASSWORD      //指定数据库用户的密码
        -P#             //指定数据库监听的端口,这里的#需用实际的端口号代替,如-P3307
    

      1.全量备份

    mysql> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    | sys                |
    | test               |
    +--------------------+
    5 rows in set (0.01 sec)
    
    mysql> use test;
    Database changed
    mysql> show tables;
    +----------------+
    | Tables_in_test |
    +----------------+
    | name           |
    | run            |
    +----------------+
    2 rows in set (0.00 sec)
    
    #备份所有数据库
    [root@localhost ~]# mysqldump -uroot -p --all-databases > all-$(date +%F-%T).mysql
    Enter password:
     
    #备份test数据库name表
    [root@localhost ~]# mysqldump -uroot -p test name > table-$(date +%F-%T).mysql
    Enter password:
    
    #备份test数据库
    [root@localhost ~]# mysqldump -uroot -p --databases test > DBtest-$(date +%F-%T).mysql
    Enter password: 
    [root@localhost ~]# ls
    all-2020-12-29-22:53:34.mysql  DBtest-2020-12-29-22:59:41.mysql            table-2020-12-29-22:57:34.mysql   table-2020-12-29-22:58:33.mysql
    

     数据库恢复

    #误删test数据库
    mysql> drop database test;
    Query OK, 2 rows affected (0.01 sec)
    mysql> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    | sys                |
    +--------------------+
    4 rows in set (0.00 sec)
    
    #恢复test数据库
    [root@localhost ~]# mysql -uroot -p < DBtest-2020-12-29-22:59:41.mysql 
    Enter password: 
    [root@localhost ~]# mysql -uroot -p -e 'show databases;'
    Enter password: 
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    | sys                |
    | test               |
    +--------------------+
    

     2.差异备份

    开启MySQL服务器的二进制日志功能

    [root@localhost ~]# vim /etc/my.cnf 
    [mysqld]
    basedir = /usr/local/mysql
    datadir = /opt/data
    socket = /tmp/mysql.sock
    port = 3306
    pid-file = /opt/data/mysql.pid
    user = mysql
    skip-name-resolve
    
    server-id=1   #设置服务器标识符
    log-bin=mysql_bin  #开启二进制日志功能
    
    #重启服务
    [root@localhost ~]# service mysqld restart
    Shutting down MySQL.. SUCCESS! 
    Starting MySQL. SUCCESS!
    

     对数据库进行完全备份

    mysql> show databases ;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | test               |
    +--------------------+
    3 rows in set (0.01 sec)
    
    mysql> use test;
    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_test |
    +----------------+
    | name           |
    | run            |
    +----------------+
    2 rows in set (0.00 sec)
    
    mysql> select * from name;
    +----+---------+------+
    | id | name    | age  |
    +----+---------+------+
    |  1 | tom     |   18 |
    |  2 | jerry   |   19 |
    |  3 | battle  |   20 |
    |  4 | between |   21 |
    +----+---------+------+
    4 rows in set (0.00 sec)
    
    mysql> select * from run;
    +----+----------+------+
    | id | name     | age  |
    +----+----------+------+
    |  1 | xiaoming |   20 |
    |  2 | huahua   |   22 |
    |  3 | yechui   |   25 |
    +----+----------+------+
    3 rows in set (0.00 sec)
    
    #全量备份数据库
    [root@localhost ~]#  mysqldump -uroot -p --single-transaction --flush-logs --master-data=2 --all-databases --delete-master-logs > all-$(date +%F-%T).sql
    Enter password: 
    [root@localhost ~]# ls
    all-2020-12-30-09:38:40.sql
    
    #新增内容
    mysql> insert run(name,age) values('xx',12),('cc',18);
    Query OK, 2 rows affected (0.00 sec)
    Records: 2  Duplicates: 0  Warnings: 0
    
    mysql> select * from run;
    +----+----------+------+
    | id | name     | age  |
    +----+----------+------+
    |  1 | xiaoming |   20 |
    |  2 | huahua   |   22 |
    |  3 | yechui   |   25 |
    |  4 | xx       |   12 |
    |  5 | cc       |   18 |
    +----+----------+------+
    5 rows in set (0.00 sec)
    

    误删test数据库

    [root@localhost ~]# 
    [root@localhost ~]# mysql -uroot -p  -e 'drop database test;'
    Enter password: 
    [root@localhost ~]# mysql -uroot -p  -e 'show databases;'
    Enter password: 
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    | sys                |
    +--------------------+
    

     差异备份的恢复

    刷新创建新的二进制日志

    [root@localhost ~]# ll /opt/data/
    total 123128
    -rw-r-----. 1 mysql mysql       56 Dec 28 18:44 auto.cnf
    -rw-------. 1 mysql mysql     1676 Dec 28 18:44 ca-key.pem
    -rw-r--r--. 1 mysql mysql     1112 Dec 28 18:44 ca.pem
    -rw-r--r--. 1 mysql mysql     1112 Dec 28 18:44 client-cert.pem
    -rw-------. 1 mysql mysql     1676 Dec 28 18:44 client-key.pem
    -rw-r-----. 1 mysql mysql      963 Dec 30 09:37 ib_buffer_pool
    -rw-r-----. 1 mysql mysql 12582912 Dec 30 09:54 ibdata1
    -rw-r-----. 1 mysql mysql 50331648 Dec 30 09:54 ib_logfile0
    -rw-r-----. 1 mysql mysql 50331648 Dec 28 18:44 ib_logfile1
    -rw-r-----. 1 mysql mysql 12582912 Dec 30 09:38 ibtmp1
    -rw-r-----. 1 mysql mysql   163944 Dec 30 09:37 localhost.localdomain.err
    drwxr-x---. 2 mysql mysql     4096 Dec 30 09:37 mysql
    -rw-r-----. 1 mysql mysql      582 Dec 30 09:52 mysql_bin.000010
    -rw-r-----. 1 mysql mysql       19 Dec 30 09:38 mysql_bin.index
    -rw-r-----. 1 mysql mysql        5 Dec 30 09:37 mysql.pid
    -rw-r--r--. 1 root  root         6 Dec 30 09:37 mysql_upgrade_info
    drwxr-x---. 2 mysql mysql     8192 Dec 30 09:37 performance_schema
    -rw-------. 1 mysql mysql     1676 Dec 28 18:44 private_key.pem
    -rw-r--r--. 1 mysql mysql      452 Dec 28 18:44 public_key.pem
    -rw-r--r--. 1 mysql mysql     1112 Dec 28 18:44 server-cert.pem
    -rw-------. 1 mysql mysql     1680 Dec 28 18:44 server-key.pem
    drwxr-x---. 2 mysql mysql     8192 Dec 30 09:37 sys
    
    [root@localhost ~]# mysqladmin -uroot -p flush-logs   #刷新创建新的二进制文件
    Enter password: 
    [root@localhost ~]# ll /opt/data/
    total 123132
    -rw-r-----. 1 mysql mysql       56 Dec 28 18:44 auto.cnf
    -rw-------. 1 mysql mysql     1676 Dec 28 18:44 ca-key.pem
    -rw-r--r--. 1 mysql mysql     1112 Dec 28 18:44 ca.pem
    -rw-r--r--. 1 mysql mysql     1112 Dec 28 18:44 client-cert.pem
    -rw-------. 1 mysql mysql     1676 Dec 28 18:44 client-key.pem
    -rw-r-----. 1 mysql mysql      963 Dec 30 09:37 ib_buffer_pool
    -rw-r-----. 1 mysql mysql 12582912 Dec 30 09:54 ibdata1
    -rw-r-----. 1 mysql mysql 50331648 Dec 30 09:54 ib_logfile0
    -rw-r-----. 1 mysql mysql 50331648 Dec 28 18:44 ib_logfile1
    -rw-r-----. 1 mysql mysql 12582912 Dec 30 09:38 ibtmp1
    -rw-r-----. 1 mysql mysql   163944 Dec 30 09:37 localhost.localdomain.err
    drwxr-x---. 2 mysql mysql     4096 Dec 30 09:37 mysql
    -rw-r-----. 1 mysql mysql      629 Dec 30 09:55 mysql_bin.000010
    -rw-r-----. 1 mysql mysql      154 Dec 30 09:55 mysql_bin.000011
    

     恢复完全备份

    [root@localhost ~]# mysql -uroot -p < all-2020-12-30-09:38:40.sql 
    Enter password: 
    [root@localhost ~]# mysql -uroot -p  -e 'show databases;'
    Enter password: 
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    | sys                |
    | test               |
    +--------------------+
    [root@localhost ~]# mysql -uroot -p  -e 'select * from test.run;'
    Enter password: 
    +----+----------+------+
    | id | name     | age  |
    +----+----------+------+
    |  1 | xiaoming |   20 |
    |  2 | huahua   |   22 |
    |  3 | yechui   |   25 |
    +----+----------+------+
    [root@localhost ~]#
    

     恢复差异备份

    #j检查误删时数据库在什么位置
    mysql> show binlog events in 'mysql_bin.000010';
    +------------------+-----+----------------+-----------+-------------+---------------------------------------+
    | Log_name         | Pos | Event_type     | Server_id | End_log_pos | Info                                  |
    +------------------+-----+----------------+-----------+-------------+---------------------------------------+
    | mysql_bin.000010 |   4 | Format_desc    |         1 |         123 | Server ver: 5.7.31-log, Binlog ver: 4 |
    | mysql_bin.000010 | 123 | Previous_gtids |         1 |         154 |                                       |
    | mysql_bin.000010 | 154 | Anonymous_Gtid |         1 |         219 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'  |
    | mysql_bin.000010 | 219 | Query          |         1 |         291 | BEGIN                                 |
    | mysql_bin.000010 | 291 | Table_map      |         1 |         341 | table_id: 175 (test.run)              |
    | mysql_bin.000010 | 341 | Write_rows     |         1 |         394 | table_id: 175 flags: STMT_END_F       |
    | mysql_bin.000010 | 394 | Xid            |         1 |         425 | COMMIT /* xid=956 */                  |
    | mysql_bin.000010 | 425 | Anonymous_Gtid |         1 |         490 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'  |
    | mysql_bin.000010 | 490 | Query          |         1 |         582 | drop database test                    |
    | mysql_bin.000010 | 582 | Rotate         |         1 |         629 | mysql_bin.000011;pos=4                |
    +------------------+-----+----------------+-----------+-------------+---------------------------------------+
    10 rows in set (0.00 sec)
    
    #使用mysqlbinlog恢复差异备份
    Bye
    [root@localhost ~]# mysqlbinlog  --stop-position=490 /opt/data/mysql_bin.000010 | mysql -uroot -p
    Enter password: 
    [root@localhost ~]# mysql -uroot -p -e 'select * from test.run;'
    Enter password: 
    +----+----------+------+
    | id | name     | age  |
    +----+----------+------+
    |  1 | xiaoming |   20 |
    |  2 | huahua   |   22 |
    |  3 | yechui   |   25 |
    |  4 | xx       |   12 |
    |  5 | cc       |   18 |
    +----+----------+------+
    

     报错:mysqldump: Couldn't execute 'SHOW VARIABLES LIKE 'gtid\_mode'': Table 'performance_schema.session_variables' doesn't exist (1146)
    解决办法:mysql_upgrade -u root -p --force    #更新
          service mysqld restart  #重启服务

  • 相关阅读:
    出现 could not open jvm.cfg 的解决办法
    powerdesigner相关概念理解
    UML建模类图
    LAMP环境折腾
    ThinkPHP学习笔记1
    ubuntu14在kDE界面下的关于eclipse提示框黑色背景的修改!
    LAMP环境安装与apache配置
    Unix网络编程---第四次作业
    Unix网络编程---第三次作业
    Unix网络编程---第二次作业
  • 原文地址:https://www.cnblogs.com/diqiyao/p/14209407.html
Copyright © 2020-2023  润新知