码迷,mamicode.com
首页 > 数据库 > 详细

mysql进阶

时间:2019-04-30 23:41:05      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:重复   cto   数据   锁表   增量   进程id   pex   授权   release   

1. 二进制格式mysql安装

//下载二进制格式的mysql软件包
[root@20liuzhenchao ~]# cd /usr/local/
[root@localhost src]# wget [root@20liuzhenchao local]# wget https://downloads.mysql.com/archives/get/file/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
--2019-04-29 19:58:07--  https://downloads.mysql.com/archives/get/file/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
正在解析主机 downloads.mysql.com (downloads.mysql.com)... 137.254.60.14
正在连接 downloads.mysql.com (downloads.mysql.com)|137.254.60.14|:443... 已连接。

100%[++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++=====================================>] 643,790,848 4.55MB/s 用时 30s    

2019-04-29 21:24:36 (5.19 MB/s) - 已保存 “mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz” [643790848/643790848])

//创建用户和组
[root@20liuzhenchao local]# groupadd -r mysql
[root@20liuzhenchao local]# useradd -M -s /sbin/nologin -g mysql mysql

//解压软件至/usr/local/
[root@20liuzhenchao local]# tar -xf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz

[root@20liuzhenchao local]# ln -sv mysql-5.7.22-linux-glibc2.12-x86_64 mysql
"mysql" -> "mysql-5.7.22-linux-glibc2.12-x86_64"

[root@20liuzhenchao local]# ll
总用量 628704
drwxr-xr-x. 2 root root         6 12月 15 2017 bin
drwxr-xr-x. 2 root root         6 12月 15 2017 etc
drwxr-xr-x. 2 root root         6 12月 15 2017 games
drwxr-xr-x. 2 root root         6 12月 15 2017 include
drwxr-xr-x. 2 root root         6 12月 15 2017 lib
drwxr-xr-x. 2 root root         6 12月 15 2017 lib64
drwxr-xr-x. 2 root root         6 12月 15 2017 libexec
lrwxrwxrwx. 1 root root        35 4月  29 22:24 mysql -> mysql-5.7.22-linux-glibc2.12-x86_64
drwxr-xr-x. 9 root root       129 4月  29 22:05 mysql-5.7.22-linux-glibc2.12-x86_64
-rw-r--r--. 1 root root 643790848 3月   4 2018 mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
drwxr-xr-x. 2 root root         6 12月 15 2017 sbin
drwxr-xr-x. 5 root root        49 4月  28 23:15 share
drwxr-xr-x. 2 root root         6 12月 15 2017 src

//修改目录/usr/local/mysql的属主属组
[root@20liuzhenchao local]# chown -R mysql.mysql mysql-5.7.22-linux-glibc2.12-x86_64
[root@20liuzhenchao mysql]# ll -d
drwxr-xr-x. 9 mysql mysql 129 4月  29 22:05 .

//添加环境变量
[root@20liuzhenchao mysql]# ls
bin  COPYING  docs  include  lib  man  README  share  support-files
[root@20liuzhenchao mysql]# echo ‘export PATH=/usr/local/mysql/bin:$PATH‘ > /etc/profile.d/mysql.sh
[root@20liuzhenchao mysql]# . /etc/profile.d/mysql.sh 
[root@20liuzhenchao mysql]# echo $PATH
/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin


//建立数据存放目录
[root@20liuzhenchao mysql]# mkdir /opt/data
[root@20liuzhenchao mysql]# chown -R mysql.mysql /opt/data
[root@20liuzhenchao mysql]# ll /opt/data -d
drwxr-xr-x. 2 mysql mysql 6 4月  29 22:57 /opt/data


//初始化数据库
[root@20liuzhenchao mysql]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
2019-04-29T15:00:39.845522Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-04-29T15:00:40.456817Z 0 [Warning] InnoDB: New log files created, LSN=45790
2019-04-29T15:00:40.630171Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2019-04-29T15:00:40.750603Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 8e0ba300-6a8f-11e9-9b37-000c29fc116c.
2019-04-29T15:00:40.752085Z 0 [Warning] Gtid table is not ready to be used. Table ‘mysql.gtid_executed‘ cannot be opened.
2019-04-29T15:00:40.754098Z 1 [Note] A temporary password is generated for root@localhost: 6ojuoTdt&de!
//请注意,这个命令的最后会生成一个临时密码,此处密码是6ojuoTdt&de!


//生成配置文件
[root@20liuzhenchao ~]# cat > /etc/my.cnf <<EOF
> [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
> EOF

[root@20liuzhenchao mysql]# cat /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


//配置服务启动脚本
[root@20liuzhenchao ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@20liuzhenchao ~]# sed -ri ‘s#^(basedir=).*#\1/usr/local/mysql#g‘ /etc/init.d/mysqld
[root@20liuzhenchao ~]# sed -ri ‘s#^(datadir=).*#\1/opt/data#g‘ /etc/init.d/mysqld


//启动mysql
[root@20liuzhenchao ~]# /etc/init.d/mysqld start
Starting MySQL.Logging to ‘/opt/data/20liuzhenchao.err‘.
. SUCCESS! 
[root@20liuzhenchao ~]# ps -ef |grep mysql
root       2221      1  0 23:28 pts/1    00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/opt/data --pid-file=/opt/data/mysql.pid
mysql      2399   2221  2 23:28 pts/1    00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/opt/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=20liuzhenchao.err --pid-file=/opt/data/mysql.pid --socket=/tmp/mysql.sock --port=3306
root       2454   1907  0 23:28 pts/1    00:00:00 grep --color=auto mysql
[root@20liuzhenchao ~]# ss -antl
State       Recv-Q Send-Q                                                Local Address:Port                                                               Peer Address:Port              
LISTEN      0      128                                                               *:22                                                                            *:*                  
LISTEN      0      100                                                       127.0.0.1:25                                                                            *:*                  
LISTEN      0      80                                                               :::3306                                                                         :::*                  
LISTEN      0      128                                                              :::22                                                                           :::*                  
LISTEN      0      100                                                             ::1:25                                                                           :::*  

//修改密码
//使用临时密码登录
[root@20liuzhenchao ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.22

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> 

//设置新密码
mysql> set password = password(‘liu123!@#‘);
Query OK, 0 rows affected, 1 warning (0.00 sec)

2. mysql配置文件

mysql的配置文件为/etc/my.cnf

配置文件查找次序:若在多个配置文件中均有设定,则最后找到的最终生效

/etc/my.cnf --> /etc/mysql/my.cnf --> --default-extra-file=/PATH/TO/CONF_FILE --> ~/.my.cnf

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将无法正常处理连接请求

3. mysql数据库备份与恢复

3.1 数据库常用备份方案

数据库备份方案:

  • 全量备份
  • 增量备份
  • 差异备份
备份方案特点
全量备份 全量备份就是指对某一个时间点上的所有数据或应用进行的一个完全拷贝。
数据恢复快。
备份时间长.
增量备份 增量备份是指在一次全备份或上一次增量备份后,以后每次的备份只需备份
与前一次相比增加和者被修改的文件。这就意味着,第一次增量备份的对象
是进行全备后所产生的增加和修改的文件;第二次增量备份的对象是进行第一次增量
备份后所产生的增加和修改的文件,如此类推。
没有重复的备份数据
备份时间短
恢复数据时必须按一定的顺序进行
差异备份 备份上一次的完全备份后发生变化的所有文件。
差异备份是指在一次全备份后到进行差异备份的这段时间
对那些增加或者修改文件的备份。在进行恢复时,我们只需对第一次全量备份和最后一次差异备份进行恢复。

3.2 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 |
| liuzhenchao        |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> use liuzhenchao;
Database changed
mysql> show tables;
+-----------------------+
| Tables_in_liuzhenchao |
+-----------------------+
| class                 |
| student               |
+-----------------------+
2 rows in set (0.00 sec)

[root@20liuzhenchao ~]# ls
anaconda-ks.cfg
[root@20liuzhenchao ~]# mysqldump -uroot -p -h127.0.0.1 --all-databases > all-201904291610.sql
Enter password: 
[root@20liuzhenchao ~]# ls
all-201904291610.sql  anaconda-ks.cfg


//备份liuzhenchao库的student表和class表
[root@20liuzhenchao ~]# mysqldump -uroot -p -h127.0.0.1 liuzhenchao student class > table-201904291616.sql
Enter password: 
[root@20liuzhenchao ~]# ls
all-201904291610.sql  anaconda-ks.cfg  table-201904291616.sql



//备份liuzhenchao库
[root@20liuzhenchao ~]# mysqldump -uroot -p -h127.0.0.1 --databases liuzhenchao > zc-201904291618.sql
Enter password: 
[root@20liuzhenchao ~]# ls
all-201904291610.sql  anaconda-ks.cfg  table-201904291616.sql  zc-201904291618.sql



//模拟误删liuzhenchao数据库
mysql> drop database liuzhenchao;
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)

3.3 mysql数据恢复

//恢复liuzhenchao数据库
[root@20liuzhenchao ~]# ls
    all-201904291610.sql  anaconda-ks.cfg  table-201904291616.sql  zc-201904291618.sql
[root@20liuzhenchao ~]# mysql -uroot -p -h127.0.0.1 < all-201904291610.sql 
Enter password: 
[root@20liuzhenchao ~]# mysql -uroot -p -h127.0.0.1 -e ‘show databases;‘
Enter password: 
+--------------------+
| Database           |
+--------------------+
| information_schema |
| liuzhenchao        |
| mysql              |
| performance_schema |
| sys                |
+--------------------+



//恢复liuzhenchao数据库的student表和class表
mysql> use liuzhenchao;
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-201904291616.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)

mysql> show tables;
+-----------------------+
| Tables_in_liuzhenchao |
+-----------------------+
| class                 |
| student               |
+-----------------------+
2 rows in set (0.00 sec)

3.4 差异备份与恢复

3.4.1. mysql差异备份

开启MySQL服务器的二进制日志功能 [root@20liuzhenchao ~]# vim /etc/my.cnf

[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=10         //设置服务器标识符
log-bin=mysql_bin    //开启二进制日志功能

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

对数据库进行完全备份

//备份整个数据库(全备)
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| liuzhenchao        |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> use liuzhenchao;
Database changed
mysql> show tables;
+-----------------------+
| Tables_in_liuzhenchao |
+-----------------------+
| class                 |
| student               |
+-----------------------+
2 rows in set (0.00 sec)

[root@20liuzhenchao ~]# ls
anaconda-ks.cfg
[root@20liuzhenchao ~]# mysqldump -uroot -p -h127.0.0.1 --all-databases > all-201904291610.sql
Enter password: 
[root@20liuzhenchao ~]# ls
all-201904291610.sql  anaconda-ks.cfg


//增加新内容
[root@20liuzhenchao ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.22-log 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 |
| liuzhenchao        |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.05 sec)

mysql> use liuzhenchao;
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_liuzhenchao |
+-----------------------+
| class                 |
| student               |
+-----------------------+
2 rows in set (0.00 sec)

mysql> select * from class;
+----+--------+
| id | name   |
+----+--------+
|  1 | grade1 |
|  2 | grade2 |
+----+--------+
2 rows in set (0.01 sec)

mysql> insert class(id,name) values(3,‘grade3‘),(4,‘grade4‘);
Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from class;
+----+--------+
| id | name   |
+----+--------+
|  1 | grade1 |
|  2 | grade2 |
|  3 | grade3 |
|  4 | grade4 |
+----+--------+
4 rows in set (0.00 sec)

3.4.2. mysql差异备份恢复

模拟误删数据

[root@20liuzhenchao ~]# mysql -uroot -p‘liu123!@#‘ -e ‘drop database liuzhenchao;‘
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@20liuzhenchao ~]# mysql -uroot -p‘liu123!@#‘ -e ‘show databases;‘
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
//由上可以看到liuzhenchao这个数据库已被删除

刷新创建新的二进制日志

[root@20liuzhenchao ~]# ll /opt/data/
总用量 122964
-rw-r-----. 1 mysql mysql    31892 4月  30 17:34 20liuzhenchao.err
-rw-r-----. 1 mysql mysql       56 4月  29 23:00 auto.cnf
-rw-r-----. 1 mysql mysql      453 4月  30 17:34 ib_buffer_pool
-rw-r-----. 1 mysql mysql 12582912 4月  30 17:49 ibdata1
-rw-r-----. 1 mysql mysql 50331648 4月  30 17:49 ib_logfile0
-rw-r-----. 1 mysql mysql 50331648 4月  29 23:00 ib_logfile1
-rw-r-----. 1 mysql mysql 12582912 4月  30 17:34 ibtmp1
drwxr-x---. 2 mysql mysql     4096 4月  30 00:24 mysql
-rw-r-----. 1 mysql mysql      177 4月  30 17:34 mysql_bin.000001
-rw-r-----. 1 mysql mysql      624 4月  30 17:49 mysql_bin.000002
-rw-r-----. 1 mysql mysql       38 4月  30 17:34 mysql_bin.index
-rw-r-----. 1 mysql mysql        5 4月  30 17:34 mysql.pid
drwxr-x---. 2 mysql mysql     8192 4月  29 23:00 performance_schema
drwxr-x---. 2 mysql mysql     8192 4月  29 23:00 sys

//刷新创建新的二进制日志
[root@20liuzhenchao ~]# mysqladmin -uroot -p‘liu123!@#‘ flush-logs
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
[root@20liuzhenchao ~]# ll /opt/data
总用量 122968
-rw-r-----. 1 mysql mysql    31892 4月  30 17:34 20liuzhenchao.err
-rw-r-----. 1 mysql mysql       56 4月  29 23:00 auto.cnf
-rw-r-----. 1 mysql mysql      453 4月  30 17:34 ib_buffer_pool
-rw-r-----. 1 mysql mysql 12582912 4月  30 17:49 ibdata1
-rw-r-----. 1 mysql mysql 50331648 4月  30 17:49 ib_logfile0
-rw-r-----. 1 mysql mysql 50331648 4月  29 23:00 ib_logfile1
-rw-r-----. 1 mysql mysql 12582912 4月  30 17:34 ibtmp1
drwxr-x---. 2 mysql mysql     4096 4月  30 00:24 mysql
-rw-r-----. 1 mysql mysql      177 4月  30 17:34 mysql_bin.000001
-rw-r-----. 1 mysql mysql      671 4月  30 17:51 mysql_bin.000002
-rw-r-----. 1 mysql mysql      154 4月  30 17:51 mysql_bin.000003
-rw-r-----. 1 mysql mysql       57 4月  30 17:51 mysql_bin.index
-rw-r-----. 1 mysql mysql        5 4月  30 17:34 mysql.pid
drwxr-x---. 2 mysql mysql     8192 4月  29 23:00 performance_schema
drwxr-x---. 2 mysql mysql     8192 4月  29 23:00 sys

恢复完全备份

[root@20liuzhenchao ~]# mysql -uroot -p‘liu123!@#‘ < all-201904291610.sql 
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@20liuzhenchao ~]# mysql -uroot -p‘liu123!@#‘ -e ‘show databases;‘ 
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| liuzhenchao        |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
[root@localhost ~]# mysql -uroot -pwangqing123! -e ‘show tables from wangqing;‘
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Tables_in_wangqing |
+--------------------+
| runtime            |
| student            |
+--------------------+
[root@20liuzhenchao ~]# mysql -uroot -p‘liu123!@#‘ -e ‘select * from liuzhenchao.class;‘ 
mysql: [Warning] Using a password on the command line interface can be insecure.
+----+--------+
| id | name   |
+----+--------+
|  1 | grade1 |
|  2 | grade2 |
+----+--------+

恢复差异备份

//检查误删数据库的位置在什么地方
[root@20liuzhenchao ~]# mysql -uroot -p‘liu123!@#‘
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 11
Server version: 5.7.22-log 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 binlog events in ‘mysql_bin.000002‘;
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
| Log_name         | Pos | Event_type     | Server_id | End_log_pos | Info                                  |
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
| mysql_bin.000002 |   4 | Format_desc    |        10 |         123 | Server ver: 5.7.22-log, Binlog ver: 4 |
| mysql_bin.000002 | 123 | Previous_gtids |        10 |         154 |                                       |
| mysql_bin.000002 | 154 | Anonymous_Gtid |        10 |         219 | SET @@SESSION.GTID_NEXT= ‘ANONYMOUS‘  |
| mysql_bin.000002 | 219 | Query          |        10 |         298 | BEGIN                                 |
| mysql_bin.000002 | 298 | Table_map      |        10 |         356 | table_id: 108 (liuzhenchao.class)     |
| mysql_bin.000002 | 356 | Write_rows     |        10 |         415 | table_id: 108 flags: STMT_END_F       |
| mysql_bin.000002 | 415 | Xid            |        10 |         446 | COMMIT /* xid=14 */                   |
| mysql_bin.000002 | 446 | Anonymous_Gtid |        10 |         511 | SET @@SESSION.GTID_NEXT= ‘ANONYMOUS‘  |
| mysql_bin.000002 | 511 | Query          |        10 |         624 | drop database liuzhenchao             |
| mysql_bin.000002 | 624 | Rotate         |        10 |         671 | mysql_bin.000003;pos=4                |
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
10 rows in set (0.01 sec)

//使用mysqlbinlog恢复差异备份
[root@20liuzhenchao ~]# mysqlbinlog --stop-position=511 /opt/data/mysql_bin.000002 |mysql -uroot -p‘liu123!@#‘
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@20liuzhenchao ~]# mysql -uroot -p‘liu123!@#‘ -e ‘select * from liuzhenchao.class‘
mysql: [Warning] Using a password on the command line interface can be insecure.
+----+--------+
| id | name   |
+----+--------+
|  1 | grade1 |
|  2 | grade2 |
|  3 | grade3 |
|  4 | grade4 |
+----+--------+

3.4 Xtrabackup

Xtrabackup是一个对InnoDB做数据备份的工具,支持在线热备份(备份时不影响数据读写),是商业备份工具InnoDB Hotbackup的一个很好的替代品。它能对InnoDB和XtraDB存储引擎的数据库非阻塞地备份(对于MyISAM的备份同样需要加表锁)。XtraBackup支持所有的Percona Server、MySQL、MariaDB和Drizzle。

Xtrabackup是由percona开源的免费数据库热备份软件,它能对InnoDB数据库和XtraDB存储引擎的数据库非阻塞地备份(对于MyISAM的备份同样需要加表锁);mysqldump备份方式是采用的逻辑备份,其最大的缺陷是备份和恢复速度较慢,如果数据库大于50G,mysqldump备份就不太适合。

3.4.1 Xtrabackup优点

  • 备份速度快,物理备份可靠
  • 备份过程不会打断正在执行的事务(无需锁表)
  • 能够基于压缩等功能节约磁盘空间和流量
  • 自动备份校验
  • 还原速度快
  • 可以流传将备份传输到另外一台机器上
  • 在不增加服务器负载的情况备份数据

3.4.2 Xtrabackup备份原理

备份开始时首先会开启一个后台检测进程,实时检测mysq redo的变化,一旦发现有新的日志写入,立刻将日志记入后台日志文件xtrabackup_log中,之后复制innodb的数据文件一系统表空间文件ibdatax,复制结束后,将执行flush tables with readlock,然后复制.frm MYI MYD等文件,最后执行unlock tables,最终停止xtrabackup_log

3.4.3 Xtrabackup增量备份介绍

xtrabackup增量备份的原理是:

  • 首先完成一个完全备份,并记录下此时检查点LSN;
  • 然后增量备份时,比较表空间中每个页的LSN是否大于上次备份的LSN,若是则备份该页并记录当前检查点的LSN。

增量备份优点:

  • 数据库太大没有足够的空间全量备份,增量备份能有效节省空间,并且效率高;
  • 支持热备份,备份过程不锁表(针对InnoDB而言),不阻塞数据库的读写;
  • 每日备份只产生少量数据,也可采用远程备份,节省本地空间;
  • 备份恢复基于文件操作,降低直接对数据库操作风险;
  • 备份效率更高,恢复效率更高。

3.4.4 Xtrabackup安装

[root@20liuzhenchao ~]# yum install http://www.percona.com/downloads/percona-release/redhat/0.1-4/percona-release-0.1-4.noarch.rpm
已加载插件:product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
percona-release-0.1-4.noarch.rpm                                                                                                  | 6.4 kB  00:00:00     
正在检查 /var/tmp/yum-root-N7jrMj/percona-release-0.1-4.noarch.rpm: percona-release-0.1-4.noarch
/var/tmp/yum-root-N7jrMj/percona-release-0.1-4.noarch.rpm 将作为 percona-release-0.1-3.noarch 的更新
正在解决依赖关系
--> 正在检查事务
---> 软件包 percona-release.noarch.0.0.1-3 将被 升级
---> 软件包 percona-release.noarch.0.0.1-4 将被 更新
更新完毕:
  percona-release.noarch 0:0.1-4                                                      
完毕!
[root@20liuzhenchao ~]# yum list | grep percona
percona-release.noarch                   0.1-4                           installed
Percona-Server-55-debuginfo.x86_64       5.5.62-rel38.14.el7             percona-release-x86_64
Percona-Server-56-debuginfo.x86_64       5.6.43-rel84.3.el7              percona-release-x86_64
Percona-Server-57-debuginfo.x86_64       5.7.25-28.1.el7                 percona-release-x86_64
Percona-Server-80-info.x86_64            8.0-1.el7                       percona-release-x86_64

//安装percona
[root@20liuzhenchao ~]# yum -y install percona-xtrabackup-24
已加载插件:product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
正在解决依赖关系
--> 正在检查事务
已安装:
  percona-xtrabackup-24.x86_64 0:2.4.13-1.el7                                         
作为依赖被安装:
  libev.x86_64 0:4.15-7.el7                    perl-Compress-Raw-Bzip2.x86_64 0:2.061-3.el7          perl-Compress-Raw-Zlib.x86_64 1:2.061-4.el7         
  perl-DBD-MySQL.x86_64 0:4.023-6.el7          perl-DBI.x86_64 0:1.627-4.el7                         perl-Data-Dumper.x86_64 0:2.145-3.el7               
  perl-Digest.noarch 0:1.17-245.el7            perl-Digest-MD5.x86_64 0:2.52-3.el7                   perl-IO-Compress.noarch 0:2.061-2.el7               
  perl-Net-Daemon.noarch 0:0.48-5.el7          perl-PlRPC.noarch 0:0.2020-14.el7                     rsync.x86_64 0:3.1.2-4.el7                        
完毕!
[root@20liuzhenchao ~]# which xtrabackup
/usr/bin/xtrabackup
[root@20liuzhenchao ~]# innobackupex -v
xtrabackup: recognized server arguments: --datadir=/opt/data --server-id=10 --log_bin=mysql_bin 
innobackupex version 2.4.13 Linux (x86_64) (revision id: 3e7ca7c)
//已安装完成

3.4.5 创建测试数据

mysql> create database test;
Query OK, 1 row affected (0.01 sec)

mysql> use test;
Database changed
mysql> create table T1(name varchar(10) not null,sex varchar(10) not null);
Query OK, 0 rows affected (0.01 sec)

mysql> insert into T1 values(‘zhang‘,‘man‘),(‘zhan‘,‘man‘),(‘sun‘,‘woman‘);
Query OK, 3 rows affected (0.05 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from T1;
+-------+-------+
| name  | sex   |
+-------+-------+
| zhang | man   |
| zhan  | man   |
| sun   | woman |
+-------+-------+
3 rows in set (0.00 sec)

3.4.6 Xtrabackup全量备份与恢复

[root@20liuzhenchao ~]# innobackupex --defaults-file=/etc/my.cnf --user=root --host=127.0.0.1 --password=‘liu123!@#‘ --port=3306 --backup /root
xtrabackup: recognized server arguments: --datadir=/opt/data --server-id=10 --log_bin=mysql_bin 
xtrabackup: recognized client arguments: --datadir=/opt/data --server-id=10 --log_bin=mysql_bin --backup=1 
190501 04:53:58 innobackupex: Starting the backup operation

IMPORTANT: Please check that the backup run completes successfully.
           At the end of a successful backup run innobackupex
           prints "completed OK!".

190501 04:53:58  version_check Connecting to MySQL server with DSN ‘dbi:mysql:;mysql_read_default_group=xtrabackup;host=127.0.0.1;port=3306‘ as ‘root‘  (using password: YES).
190501 04:54:00  version_check Connected to MySQL server
190501 04:54:00  version_check Executing a version check against the server...
190501 04:54:00  version_check Done.
190501 04:54:00 Connecting to MySQL server host: 127.0.0.1, user: root, password: set, port: 3306, socket: not set
Using server version 5.7.22-log
innobackupex version 2.4.13 based on MySQL server 5.7.19 Linux (x86_64) (revision id: 3e7ca7c)
xtrabackup: uses posix_fadvise().
xtrabackup: cd to /opt/data
xtrabackup: open files limit requested 0, set to 1024
xtrabackup: using the following InnoDB configuration:
xtrabackup:   innodb_data_home_dir = .
xtrabackup:   innodb_data_file_path = ibdata1:12M:autoextend
xtrabackup:   innodb_log_group_home_dir = ./
xtrabackup:   innodb_log_files_in_group = 2
xtrabackup:   innodb_log_file_size = 50331648
InnoDB: Number of pools: 1
......
......
190501 04:54:04 [00] Writing /root/2019-05-01_04-53-58/xtrabackup_info
190501 04:54:04 [00]        ...done
xtrabackup: Transaction log of lsn (5374234) to (5374243) was copied.
190501 04:54:04 completed OK!


//这里面就是相关的备份文件,同样也可以看到我们创建的库的名称
[root@20liuzhenchao ~]# ll ./2019-05-01_04-53-58/
总用量 12340
-rw-r-----. 1 root root      488 5月   1 04:54 backup-my.cnf
-rw-r-----. 1 root root      705 5月   1 04:54 ib_buffer_pool
-rw-r-----. 1 root root 12582912 5月   1 04:54 ibdata1
drwxr-x---. 2 root root       92 5月   1 04:54 liuzhenchao
drwxr-x---. 2 root root     4096 5月   1 04:54 mysql
drwxr-x---. 2 root root     8192 5月   1 04:54 performance_schema
drwxr-x---. 2 root root     8192 5月   1 04:54 sys
drwxr-x---. 2 root root       48 5月   1 04:54 test
-rw-r-----. 1 root root       21 5月   1 04:54 xtrabackup_binlog_info
-rw-r-----. 1 root root      113 5月   1 04:54 xtrabackup_checkpoints
-rw-r-----. 1 root root      529 5月   1 04:54 xtrabackup_info
-rw-r-----. 1 root root     2560 5月   1 04:54 xtrabackup_logfile



//使用此参数使用相关数据性文件保持一致性状态
[root@20liuzhenchao ~]# innobackupex --apply-log /root/2019-05-01_04-53-58/
xtrabackup: recognized server arguments: --innodb_checksum_algorithm=crc32 --innodb_log_checksum_algorithm=strict_crc32 --innodb_data_file_path=ibdata1:12M:autoextend --innodb_log_files_in_group=2 --innodb_log_file_size=50331648 --innodb_fast_checksum=0 --innodb_page_size=16384 --innodb_log_block_size=512 --innodb_undo_directory=./ --innodb_undo_tablespaces=0 --server-id=10 --redo-log-version=1 
xtrabackup: recognized client arguments: --innodb_checksum_algorithm=crc32 --innodb_log_checksum_algorithm=strict_crc32 --innodb_data_file_path=ibdata1:12M:autoextend --innodb_log_files_in_group=2 --innodb_log_file_size=50331648 --innodb_fast_checksum=0 --innodb_page_size=16384 --innodb_log_block_size=512 --innodb_undo_directory=./ --innodb_undo_tablespaces=0 --server-id=10 --redo-log-version=1 
190501 04:59:57 innobackupex: Starting the apply-log operation

IMPORTANT: Please check that the apply-log run completes successfully.
           At the end of a successful apply-log run innobackupex
           prints "completed OK!".

innobackupex version 2.4.13 based on MySQL server 5.7.19 Linux (x86_64) (revision id: 3e7ca7c)
xtrabackup: cd to /root/2019-05-01_04-53-58/
xtrabackup: This target seems to be not prepared yet.

//模拟误删表
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> drop table T1;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from T1;
ERROR 1146 (42S02): Table ‘test.T1‘ doesn‘t exist

3.4.7 接下来准备恢复误删除数据

恢复数据之前需要保证数据目录是空的状态
[root@20liuzhenchao data]# innobackupex --defaults-file=/etc/my.cnf --user=root --password=‘liu123!@#‘ --port=3306 --host=127.0.0.1 --copy-back /root/2019-05-01_04-53-58/
xtrabackup: recognized server arguments: --datadir=/opt/data --server-id=10 --log_bin=mysql_bin 
xtrabackup: recognized client arguments: --datadir=/opt/data --server-id=10 --log_bin=mysql_bin 
190501 05:08:24 innobackupex: Starting the copy-back operation

IMPORTANT: Please check that the copy-back run completes successfully.
           At the end of a successful copy-back run innobackupex
           prints "completed OK!".

innobackupex version 2.4.13 based on MySQL server 5.7.19 Linux (x86_64) (revision id: 3e7ca7c)
190501 05:08:24 [01] Copying ib_logfile0 to /opt/data/ib_logfile0
190501 05:08:24 [01]        ...done
190501 05:08:24 [01] Copying ib_logfile1 to /opt/data/ib_logfile1
190501 05:08:24 [01]        ...done
.....
.....
190501 05:08:24 [01]        ...done
190501 05:08:24 [01] Copying ./ibtmp1 to /opt/data/ibtmp1
190501 05:08:24 [01]        ...done
190501 05:08:25 completed OK!

//重启服务
[root@20liuzhenchao ~]# service mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS! 

//查看数据是否恢复
[root@20liuzhenchao ~]# mysql -uroot -p‘liu123!@#‘ -e ‘select * from test.T1;‘
mysql: [Warning] Using a password on the command line interface can be insecure.
+-------+-------+
| name  | sex   |
+-------+-------+
| zhang | man   |
| zhan  | man   |
| sun   | woman |
+-------+-------+

mysql进阶

标签:重复   cto   数据   锁表   增量   进程id   pex   授权   release   

原文地址:https://www.cnblogs.com/liuzhenchao/p/10798392.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!