MySQL进阶

mysql进阶

文章目录

  • mysql进阶
    • 1.2 mysql数据库备份与恢复
      • 1.3 数据库常用备份方案
      • 1.4 mysql备份工具mysqldump
      • 1.5 mysql数据恢复
      • 1.6 差异备份与恢复
        • 1.6.1 mysql差异备份
      • 1.6.2. mysql差异备份恢复

mysql常用配置文件参数:

参数 说明
port = 3306 设置监听端口
socket = /tmp/mysql.sock 指定套接字文件位置
basedir = /usr/local/mysql 指定MySQL的安装路径
datadir = /data/mysql 指定MySQL的数据存放路径
pid-file = /data/mysql/mysql.pid 指定进程ID文件存放路径
user = mysql 指定MySQL以什么用户的身份提供服务
skip-name-resolve 禁止MySQL对外部连接进行DNS解析
使用这一选项可以消除MySQL进行DNS解析的时间。
若开启该选项,则所有远程主机连接授权都要使用IP地址方
式否则MySQL将无法正常处理连接请求

1.2 mysql数据库备份与恢复

1.3 数据库常用备份方案

数据库备份方案:

全量备份
增量备份
差异备份
备份方案 特点
全量备份 全量备份就是指对某一个时间点上的所有数据或应用进行的一个完全拷贝。
数据恢复快。
备份时间长
增量备份 增量备份是指在一次全备份或上一次增量备份后,以后每次的备份只需备份
与前一次相比增加和者被修改的文件。这就意味着,第一次增量备份的对象
是进行全备后所产生的增加和修改的文件;第二次增量备份的对象是进行第一次增量
备份后所产生的增加和修改的文件,如此类推。

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

1.4 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
 
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| swk                |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> use swk;
Database changed
mysql> desc student;
+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| id    | int(11)      | NO   | PRI | NULL    | auto_increment |
| name  | varchar(100) | NO   |     | NULL    |                |
| age   | tinyint(4)   | YES  |     | NULL    |                |
+-------+--------------+------+-----+---------+----------------+
3 rows in set (0.01 sec)


//备份全部的库
[root@yasuo ~]# mysqldump -uroot -p123456 --all-databases > all-20190819.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@yasuo ~]# ls
all-20190819.sql  anaconda-ks.cfg  mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz


//备份swk库的student表
[root@mysql ~]# mysqldump -uroot -p1 zjy student > table-20190818.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@mysql ~]# ls
all-20190818.sql  mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
anaconda-ks.cfg   table-20190818.sql



//备份swk这个库
[root@yasuo ~]# mysqldump -uroot -p123456 swk student > table-20190819.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@yasuo ~]# ls
all-20190819.sql  mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
anaconda-ks.cfg   table-20190819.sql

//误删除了swk库
mysql> drop database swk;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)


1.5 mysql数据恢复

//恢复swk数据库
[root@yasuo ~]# mysql -uroot -p123456 < all-20190819.sql 
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@yasuo ~]# mysql -uroot -p123456 -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| swk                |
| sys                |
+--------------------+


//恢复swk里的student表
mysql> use swk;
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> source table-20190819.sql
Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected, 1 warning (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected, 1 warning (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

mysql> show tables;
+---------------+
| Tables_in_swk |
+---------------+
| student       |
+---------------+
1 row in set (0.00 sec)


//模拟删除整个数据库
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| swk                |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> drop database swk;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

[root@yasuo ~]# mysql -uroot -p123456 -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| swk                |
| sys                |
+--------------------+

1.6 差异备份与恢复

1.6.1 mysql差异备份

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

[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
user = mysql
pid-file = /tmp/mysql.pid
skip-name-resolve

server-id=1         //设置服务器标识符
log-bin=mysql_bin    //开启二进制日志功能

[root@yasuo ~]# service mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!

对数据库进行完全备份

[root@yasuo ~]# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 16
Server version: 5.7.22 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| swk                |
+--------------------+
5 rows in set (0.00 sec)

mysql> show tables from swl;
+---------------+
| Tables_in_swk |
+---------------+
| student       |
+---------------+
1 row in set (0.00 sec)

mysql> select * from swk.student;
+----+-------------+------+
| id | name        | age  |
+----+-------------+------+
|  1 | tom         |   20 |
|  2 | jerry       |   23 |
|  3 | wangqing    |   25 |
|  4 | sean        |   28 |
|  5 | zhangshan   |   26 |
|  7 | lisi        | NULL |
|  8 | chenshuo    |   10 |
|  9 | wangwu      |  100 |
| 10 | qiuyi       |   15 |
| 11 | qiuxiaotian |   20 |
+----+-------------+------+
10 rows in set (0.00 sec)




//完全备份
[root@yasuo ~]# mysqldump -uroot -p123456 --single-transaction --flush-logs --master-data=2 --all-databases --delete-master-logs >all-20190819.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@yasuo ~]# ll
total 628712
-rw-r--r--. 1 root root      1251 Aug 19 00:09 all-20190819.sql
-rw-------. 1 root root      1525 Mar 22 03:01 anaconda-ks.cfg
-rw-r--r--. 1 root root 643790848 Aug  7 23:11 mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz




//增加新内容
mysql> create table A(id int,age int);
Query OK, 0 rows affected (0.11 sec)

mysql> 
mysql> desc A;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int(11) | YES  |     | NULL    |       |
| age   | int(11) | YES  |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
2 rows in set (0.00 sec)
### 3.4.2. mysql差异备份恢复
mysql> insert into A(id,age)values(1,1);
Query OK, 1 row affected (0.01 sec)
mysql> insert into A(id,age)values(2,2);
Query OK, 1 row affected (0.00 sec)

mysql> select * from A;
+------+------+
| id   | age  |
+------+------+
|    1 |    1 |
|    2 |    2 |
+------+------+
2 rows in set (0.00 sec)

1.6.2. mysql差异备份恢复

模拟误删数据

mysql> drop database swk;
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)

刷新创建新的二进制日志

[root@yasuo ~]# ll /opt/data/
total 123160
-rw-r-----. 1 mysql mysql    36886 Aug 11 02:12 128.err
-rw-r-----. 1 mysql mysql       56 Aug  7 23:20 auto.cnf
-rw-r-----. 1 mysql mysql     1224 Aug 19 00:32 ib_buffer_pool
-rw-r-----. 1 mysql mysql 12582912 Aug 19 00:32 ibdata1
-rw-r-----. 1 mysql mysql 50331648 Aug 19 00:32 ib_logfile0
-rw-r-----. 1 mysql mysql 50331648 Aug  7 23:20 ib_logfile1
-rw-r-----. 1 mysql mysql 12582912 Aug 19 00:32 ibtmp1
drwxr-x---. 2 mysql mysql     4096 Aug 19 00:05 mysql
-rw-r-----. 1 mysql mysql      154 Aug 19 00:32 mysql_bin.000001
-rw-r-----. 1 mysql mysql       19 Aug 19 00:32 mysql_bin.index
-rw-r-----. 1 mysql mysql   196174 Aug 19 00:32 mysql.err
-rw-r-----. 1 mysql mysql        5 Aug 19 00:32 mysql.pid
drwxr-x---. 2 mysql mysql     8192 Aug  7 23:20 performance_schema
drwxr-x---. 2 mysql mysql     8192 Aug  7 23:20 sys

//刷新创建新的二进制日志
[root@yasuo ~]# mysqladmin -uroot -p123456 flush-logs
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
[root@yasuo ~]# ll /opt/data/
总用量 188552
-rw-r-----. 1 mysql mysql       56 2月  20 10:11 auto.cnf
-rw-r-----. 1 mysql mysql      996 2月  21 14:54 ib_buffer_pool
-rw-r-----. 1 mysql mysql 79691776 2月  21 16:17 ibdata1
-rw-r-----. 1 mysql mysql 50331648 2月  21 16:18 ib_logfile0
-rw-r-----. 1 mysql mysql 50331648 2月  20 10:11 ib_logfile1
-rw-r-----. 1 mysql mysql 12582912 2月  21 14:54 ibtmp1
-rw-r-----. 1 mysql mysql     8304 2月  21 08:45 linux-node1.com.err
-rw-r-----. 1 mysql mysql    74620 2月  21 14:54 localhost.err
drwxr-x---. 2 mysql mysql     4096 2月  21 16:16 mysql
-rw-r-----. 1 mysql mysql      945 2月  21 16:18 mysql_bin.000001
-rw-r-----. 1 mysql mysql      154 2月  21 16:18 mysql_bin.000002
-rw-r-----. 1 mysql mysql       38 2月  21 16:18 mysql_bin.index
drwxr-x---. 2 mysql mysql     8192 2月  20 10:11 performance_schema
drwxr-x---. 2 mysql mysql     8192 2月  20 10:11 sys

恢复差异备份

[root@mysql ~]# ll /opt/data/
总用量 189572
-rw-r-----. 1 mysql mysql       56 2月  20 10:11 auto.cnf
-rw-r-----. 1 mysql mysql      996 2月  21 14:54 ib_buffer_pool
-rw-r-----. 1 mysql mysql 79691776 2月  21 16:20 ibdata1
-rw-r-----. 1 mysql mysql 50331648 2月  21 16:20 ib_logfile0
-rw-r-----. 1 mysql mysql 50331648 2月  20 10:11 ib_logfile1
-rw-r-----. 1 mysql mysql 12582912 2月  21 14:54 ibtmp1
-rw-r-----. 1 mysql mysql     8304 2月  21 08:45 linux-node1.com.err
-rw-r-----. 1 mysql mysql    74620 2月  21 14:54 localhost.err
drwxr-x---. 2 mysql mysql     4096 2月  21 16:20 mysql
-rw-r-----. 1 mysql mysql      945 2月  21 16:18 mysql_bin.000001
-rw-r-----. 1 mysql mysql   786443 2月  21 16:20 mysql_bin.000002
-rw-r-----. 1 mysql mysql       38 2月  21 16:18 mysql_bin.index
drwxr-x---. 2 mysql mysql     8192 2月  20 10:11 performance_schema
drwxr-x---. 2 mysql mysql     8192 2月  20 10:11 sys
drwxr-x---. 2 mysql mysql       96 2月  21 16:20 swk

//检查误删数据库的位置在什么地方
[root@yasuo ~]# mysql -uroot -p123456
mysql> show binlog events in 'mysql_bin.000001';
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
| Log_name         | Pos | Event_type     | Server_id | End_log_pos | Info                                  |
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
| mysql_bin.000001 |   4 | Format_desc    |         1 |         123 | Server ver: 5.7.22-log, Binlog ver: 4 |
| mysql_bin.000001 | 123 | Previous_gtids |         1 |         154 |                                       |
| mysql_bin.000001 | 154 | Anonymous_Gtid |         1 |         219 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'  |
| mysql_bin.000001 | 219 | Query          |         1 |         295 | BEGIN                                 |
| mysql_bin.000001 | 295 | Table_map      |         1 |         353 | table_id: 330 (zjy.student)      |
| mysql_bin.000001 | 353 | Write_rows     |         1 |         410 | table_id: 330 flags: STMT_END_F       |
| mysql_bin.000001 | 410 | Xid            |         1 |         441 | COMMIT /* xid=2628 */                 |
| mysql_bin.000001 | 441 | Anonymous_Gtid |         1 |         506 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'  |
| mysql_bin.000001 | 506 | Query          |         1 |         582 | BEGIN                                 |
| mysql_bin.000001 | 582 | Table_map      |         1 |         640 | table_id: 330 (zjy.student)      |
| mysql_bin.000001 | 640 | Update_rows    |         1 |         698 | table_id: 330 flags: STMT_END_F       |
| mysql_bin.000001 | 698 | Xid            |         1 |         729 | COMMIT /* xid=2630 */                 |
| mysql_bin.000001 | 729 | Anonymous_Gtid |         1 |         794 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'  |
| mysql_bin.000001 | 791 | Query          |         1 |         898 | drop database swk                             |     //此处就是删除数据库的位置,对应的pos位置是791
| mysql_bin.000001 | 898 | Rotate         |         1 |         945 | mysql_bin.000002;pos=4                |
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
15 rows in set (0.00 sec)

//使用mysqlbinlog恢复差异备份
[root@yasuo ~]# mysqlbinlog --stop-position=791/opt/data/mysql_bin.000001 |mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@yasuo ~]# mysql -uroot -p123456 -e 'select * from swk.student;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+------+-------+------+
| id   | name  | age  |
+------+-------+------+
|    1 | tom   |   10 |
|    2 | jerry |   30 |
|    3 | biubiu|   40 |
|    4 | kang  |   50 |
+------+-------+------+

你可能感兴趣的:(MySQL进阶)