数据库运维

数据库备份,数据库为school

  1. 创建student和score表

    -------------------------创建student表。SQL代码如下--------------------------------
    CREATE TABLE student (
    id INT(10) NOT NULL UNIQUE PRIMARY KEY ,
    name VARCHAR(20) NOT NULL ,
    sex VARCHAR(4) ,
    birth YEAR,
    department VARCHAR(20) ,
    address VARCHAR(50)
    );
    -------------------------创建score表。SQL代码如下----------------------------------
    CREATE TABLE score (
    id INT(10) NOT NULL UNIQUE PRIMARY KEY AUTO_INCREMENT ,
    stu_id INT(10) NOT NULL ,
    c_name VARCHAR(20) ,
    grade INT(10)
    );
  2. 备份数据库school到/backup目录

[root@master backup]# mysqldump -uroot -p030118 -B school > /backup/school_1.sql
  1. 备份MySQL数据库为带删除表的格式,能够让该备份覆盖已有数据库而不需要手动删除原有数据库

[root@master backup]# mysqldump -uroot -p030118 --add-drop-database school > /backup/school_4.sql
  1. 直接将MySQL数据库压缩备份

systemctl stop mysqld
[root@master data]# tar czf /backup/database.tar.gz *
  1. 备份MySQL数据库某个(些)表。此例备份student表

[root@master backup]# mysqldump -uroot -p030118 school student > /backup/student_1.sql
  1. 同时备份多个MySQL数据库(其他数据库素材自行准备)

[root@master backup]# mysqldump -uroot -p030118 -B school friend > /backup/db_2.sql
  1. 仅仅备份数据库结构

[root@master backup]# mysqldump -uroot -p030118 -d school > /backup/school_2.sql
  1. 备份服务器上所有数据库

[root@master backup]# mysqldump -uroot -p030118 --all-databases > /backup/db_1.sql
  1. 还原MySQL数据库

mysql8.0 [(none)]>drop database school;
mysql8.0 [(none)]>create database school;
Query OK, 1 row affected (0.01 sec)
 
mysql8.0 [(none)]>use school
Database changed
mysql8.0 [school]>source /backup/school_1.sql
  1. 还原压缩的MySQL数据库

systemctl stop mysqld
[root@master data]# tar czf /backup/database.tar.gz *
[root@master data]# scp /backup/database.tar.gz 192.168.140.134:/backup/
The authenticity of host '192.168.140.134 (192.168.140.134)' can't be established.
ECDSA key fingerprint is SHA256:whHWCTyntsyeb2srFJBPjiB8EBZiOFSOvD0A+BA+pJU.
ECDSA key fingerprint is MD5:dd:a9:66:9a:94:86:08:40:e9:bb:6f:c1:a5:ea:f5:a6.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.140.134' (ECDSA) to the list of known hosts.
[email protected]'s password: 
database.tar.gz                                              100% 1383KB  51.4MB/s   00:00
------------------------------下面是目标服务器--------------------------------------
[root@master backup]# systemctl stop mysqld
[root@master backup]# rm -rf /usr/local/mysql/data/*
[root@master backup]# tar xf /backup/database.tar.gz -C /usr/local/mysql/data/
[root@master backup]# chown -R mysql.mysql /usr/local/mysql/data/*
[root@master backup]# systemctl start mysqld
  1. 使用mydumper备份数据库

[root@master ~]# mydumper -u root -p 030118 -B school -S /tmp/mysql.sock -o /backup/school_5
  1. 使用mydumper恢复数据库

[root@master backup]# myloader -u root -p 030118 -S /tmp/mysql.sock -d /backup/school_5 -B school
  1. 使用xtrabackup 备份数据库

[root@master backup]# innobackupex --user=root --password=030118 --socket=/tmp/mysql.sock /server/backup/
  1. 在另外的数据库服务器上还原xtrabackup 备份

[root@master backup]# tar czf /server/backup/full_2023-03-20.tar.gz full_2023-03-20/
[root@master backup]# scp /server/backup/full_2023-03-20.tar.gz 192.168.140.134:/server/backup/ 
[email protected]'s password: 
full_2023-03-20.tar.gz                                       100%  600KB  83.9MB/s   00:00    
[root@master backup]# tar xf full_2023-03-20.tar.gz 
[root@master backup]# innobackupex --copy-back /server/backup/full_2023-03-20/

你可能感兴趣的:(数据库,sql,mysql)