Linux-mysql备份

Linux-mysql备份

1. 二进制格式mysql安装

//第一步 下载二进制格式的mysql软件包
[root@localhost ~]# wget https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz

//第二步 创建用户和组
[root@localhost ~]# groupadd -r mysql
[root@localhost ~]# useradd mysql -M -s /sbin/nologin -g mysql

//第三步 解压软件至/usr/local/
[root@localhost ~]# tar xf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@localhost ~]# ls /usr/local/
bin  etc  games  include  lib  lib64  libexec  mysql-5.7.22-linux-glibc2.12-x86_64  sbin  share  src

// 第四步 将解压后的mysql二进制文件软连接到mysql
[root@localhost local]# ln -s mysql-5.7.22-linux-glibc2.12-x86_64/ mysql //注意这个地方尽量就用mysql这个名字,我在用其他名字时初始化时会报错
[root@localhost local]# ll
总用量 0
drwxr-xr-x. 2 root root   6 12月 15 2017 bin
lrwxrwxrwx. 1 root root  36 4月  29 12:05 cwhmysql -> mysql-5.7.22-linux-glibc2.12-x86_64/

// 第五步 修改目录/usr/local/mysql的属主属组
[root@localhost local]# chown mysql:mysql /usr/local/mysql
[root@localhost local]# ll /usr/local/mysql/ -d
drwxr-xr-x. 9 mysql mysql 129 4月  29 11:55 /usr/local/cwhmysql/

// 第六步添加环境变量
[root@localhost local]# echo 'export PATH=/usr/local/mysql/bin:$PATH' >> /etc/profile.d/mysql.sh
[root@localhost local]# source /etc/profile.d/mysql.sh 

// 第七步建立数据存放目录(数据库数据存放的位置)
[root@localhost local]# mkdir /opt/data
[root@localhost local]# chown -R mysql.mysql /opt/data/
[root@localhost local]# ll -d /opt/data/
drwxr-xr-x. 2 mysql mysql 6 4月  29 13:58 /opt/data/

// 第八步初始化数据库
[root@localhost local]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
2019-04-29T07:08:48.132084Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-04-29T07:08:48.620302Z 0 [Warning] InnoDB: New log files created, LSN=45790
2019-04-29T07:08:48.686189Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2019-04-29T07:08:48.749259Z 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: a2c71314-6a4d-11e9-98ac-000c2938f528.
2019-04-29T07:08:48.750621Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2019-04-29T07:08:48.752402Z 1 [Note] A temporary password is generated for root@localhost: JX5JLlCryl(w
//注意最后这个JX5JLlCryl(w 是初始化后生成的临时随机密码在第一次登陆时需要用到

// 第九步生成配置文件
[root@localhost local]# 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  //若开启该选项,则所有远程主机连接授权都要使用IP地址方
式否则MySQL将无法正常处理连接请求
> EOF

// 第十步配置服务启动脚本
[root@localhost local]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld //将mysql的服务文件复制到init.d中去
[root@localhost local]# vim /etc/init.d/mysqld
basedir=/usr/local/mysql
datadir=/opt/data        
//找到上面两行加入mysql的路径和数据库存放的路径

// 第十一步启动服务
[root@localhost local]# service mysqld start
Starting MySQL.Logging to '/opt/data/localhost.localdomain.err'.
. SUCCESS! 

// 第十二步使用临时密码登录
[root@localhost local]# cat /root/passwd 
JX5JLlCryl(w  //初始化密码我将他保存在这个文件里
[root@localhost local]# mysql -uroot -p
Enter password:    //此处密码为当时初始化时所给的密码
mysql> 
 
// 第十三步修改密码
mysql> set password = password('cwh123!');
Query OK, 0 rows affected, 1 warning (0.00 sec)

2. mysql配置文件

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

/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#             //指定数据库监听的端口,这里的#需用实际的端口号代替,如-P3306
1. 备份整个数据库
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| chengweihong       |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)
mysql> quit
Bye

[root@localhost ~]# mysqldump -uroot -p -hlocalhost --all-databases > /mysql_bak/all-201904291618.sql
Enter password: 
[root@localhost ~]# ls /mysql_bak/
all-201904291618.sql
// -h后可以不加这个选项默认是本地

备份恢复操作
mysql> show databases ;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| chengweihong       |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> drop database chengweihong;
Query OK, 1 row affected (0.01 sec)

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

    //然后在进行恢复操作
[root@localhost ~]# mysql -uroot -p < /mysql_bak/all-201904291618.sql 
Enter password: 

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| chengweihong       |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)
2.备份chengweihong库的cwh表和ddl表
mysql> show tables;
+------------------------+
| Tables_in_chengweihong |
+------------------------+
| cwh                    |
+------------------------+
1 row in set (0.00 sec)

mysql> create table ddl(name varchar(50) not null,age tinyint not null);
Query OK, 0 rows affected (0.01 sec)

mysql> insert into ddl value('dd',30);
Query OK, 1 row affected (0.00 sec)

mysql> show tables;
+------------------------+
| Tables_in_chengweihong |
+------------------------+
| cwh                    |
| ddl                    |
+------------------------+
2 rows in set (0.00 sec)

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

mysql> show tables;
+------------------------+
| Tables_in_chengweihong |
+------------------------+
| cwh                    |
| ddl                    |
+------------------------+
2 rows in set (0.00 sec)

mysql> quit
Bye
[root@localhost ~]# mysqldump -uroot -p chengweihong cwh ddl > /mysql_bak/table-cwhandddl-201904291701.sql
Enter password: 
[root@localhost ~]# ls /mysql_bak/
all-201904291618.sql  table-cwhandddl-201904291701.sql

//备份恢复操作

//首先备份chengweihong库下得两个表
[root@localhost ~]# mysqldump -uroot -p chengweihong cwh ddl > /mysql_bak/table-201904291716.sql

//模拟一个误删除得操作删除cwh表
mysql> use chengweihong;
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_chengweihong |
+------------------------+
| cwh                    |
| ddl                    |
+------------------------+
2 rows in set (0.00 sec)

mysql> drop table cwh;
Query OK, 0 rows affected (0.00 sec)

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

//在进入mysql进行恢复
mysql> use chengweihong
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 /mysql_bak/table-201904291716.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)

mysql> show tables;
+------------------------+
| Tables_in_chengweihong |
+------------------------+
| cwh                    |
| ddl                    |
+------------------------+
2 rows in set (0.00 sec)

3.3 mysql差异备份恢复

第一步,开启MySQL服务器的二进制日志功能

[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 = runtime_binlog  //开启二进制日志功能

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

//此时在/opt/data下会有一个runtime_binlog.000001得文件就是同个这个文件进行备份恢复

第二步,对现有数据库进行进行差异备份得完全备份

[root@localhost ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 16
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 databses;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'databses' at line 1
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| chengweihong       |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> use chengweihong;
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_chengweihong |
+------------------------+
| cwh                    |
| ddl                    |
+------------------------+
2 rows in set (0.00 sec)

mysql> select * from cwh;
+--------+-----+
| name   | age |
+--------+-----+
| tom    |  10 |
| jerry  |  20 |
| lisi   |  30 |
| wangwu |  40 |
+--------+-----+
4 rows in set (0.00 sec)

mysql> select * from ddl;
+------+-----+
| name | age |
+------+-----+
| dd   |  30 |
+------+-----+
1 row in set (0.00 sec)

mysql> insert into ddl value('cc',40);
Query OK, 1 row affected (0.01 sec)

mysql> select * from ddl;
+------+-----+
| name | age |
+------+-----+
| dd   |  30 |
| cc   |  40 |
+------+-----+
2 rows in set (0.00 sec)

mysql> quit
Bye

[root@localhost ~]# mysqldump -uroot -p --single-transaction --flush-logs --master-data=2 --all-databases --delete-master-logs > /mysql_bak/all-201904301411.sql
Enter password: 
[root@localhost ~]# ls /opt/data/
auto.cnf        ib_logfile0                mysql                  runtime_binlog.index
chengweihong    ib_logfile1                mysql.pid              sys
ib_buffer_pool  ibtmp1                     performance_schema
ibdata1         localhost.localdomain.err  runtime_binlog.000004

第三步,往数据库得表里增加新内容

// 原有得表
mysql> use chengweihong;
mysql> select * from cwh;
+--------+-----+
| name   | age |
+--------+-----+
| tom    |  10 |
| jerry  |  20 |
| lisi   |  30 |
| wangwu |  40 |
+--------+-----+
4 rows in set (0.00 sec)

//往表里新增内容
mysql> insert into cwh value('zhaoliu',50);
Query OK, 1 row affected (0.00 sec)

mysql> select * from cwh;
+---------+-----+
| name    | age |
+---------+-----+
| tom     |  10 |
| jerry   |  20 |
| lisi    |  30 |
| wangwu  |  40 |
| zhaoliu |  50 |
+---------+-----+
5 rows in set (0.00 sec)

第四步,模拟误删除数据库

mysql> drop database chengweihong;
Query OK, 2 rows affected (2.29 sec)

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

第五步,用普通完全备份恢复数据库

[root@localhost ~]# mysql -uroot -p < /mysql_bak/all-201904301430.sql 
Enter password: 
[root@localhost ~]# mysql -uroot -p -e "select * from chengweihong.cwh;"
Enter password: 
+--------+-----+
| name   | age |
+--------+-----+
| tom    |  10 |
| jerry  |  20 |
| lisi   |  30 |
| wangwu |  40 |
+--------+-----+

//发现备份之后增加得数据并由被恢复

第六步,用差异备份恢复数据库

[root@localhost ~]# mysql -uroot -p -e "show binlog events in 'runtime_binlog.000006';" |grep drop
Enter password: 
runtime_binlog.000006   497 Query   1   613 drop database chengweihong
[root@localhost ~]# mysqlbinlog --stop-position=497 /opt/data/runtime_binlog.000006 |mysql -uroot -pcwh123!
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@localhost ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 35
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> use chengweihong;
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> select * from cwh;
+---------+-----+
| name    | age |
+---------+-----+
| tom     |  10 |
| jerry   |  20 |
| lisi    |  30 |
| wangwu  |  40 |
| zhaoliu |  50 |
+---------+-----+
5 rows in set (0.00 sec)
//可以看出在完全备份之后添加得也恢复了

4.破解密码以及无密码登录

4.1 破解密码

[root@cwh ~]# vim /etc/my.cnf
[mysqld]
skip-grant-tables        //增加这一行,然后重启服务
[root@cwh ~]# mysql

mysql> use mysql;
mysql> select * from user \G
*************************** 1. row ***************************
-----
 authentication_string: *376C8403A2C3233B69F4763C3E428E4ED7314D42
-----
//在5.7中密码步在时password二十authentication_string
 
mysql> UPDATE user SET password(或authentication_string)=password("123456") WHERE user='root';  
mysql> flush privileges;
 
mysql> exit;

4.2 无密码登录

往/etc/.my.cnf 中写入篇日志文件

[root@cwh ~]# vim .my.cnf 
[client]
user=root //你要无秘登录的用户
password=cl //用户的密码

给不同不同的客户端使用

[root@cwh ~]# vim .my.cnf 
mysql]                        //给/usr/local/mysql/bin/mysql使用
user="root"
password="123456"

[mysqladmin]               //给/usr/local/mysql/bin/mysqladmin使用
user="root"
password="123456"

5.mysql备份扩展xtrabackup备份

Xtrabackup

percona提供的mysql数据库备份工具,惟一开源的能够对innodb和xtradb数据库进行热备的工具

特点:

  • 备份还原过程快速、可靠
  • 备份过程不会打断正在执行的事务
  • 能够基于压缩等功能节约磁盘空间和流量
  • 自动实现备份检验
  • 开源,免费

Xtrabackup2.2版之前包括4个可执行文件:

  1. Innobackupex: perl 脚本
  2. Xtrabackup: C/C++ 编译的二进制
  3. Xbcrypt:加解密
  4. Xbstream:支持并发写的流文件格式

注意:

xtrabackup 是用来备份 InnoDB 表的,不能备份非 InnoDB 表,和 mysqld server 没有交互;innobackupex 脚本用来备份非InnoDB 表,同时会调用 xtrabackup 命令来备份 InnoDB 表,还会和 mysqld server 发送命令进行交互,如加读锁(FTWRL)、获取位点(SHOW SLAVESTATUS)等。即innobackupex是在xtrabackup 之上做了一层封装实现的。
虽然目前一般不用 MyISAM 表,只是mysql 库下的系统表是MyISAM 的,因此备份基本都通过 innobackupex 命令进行

xtrabackup的特性:
使用innobakupex备份时,其会调用xtrabackup备份所有的InnoDB表,复制所有关于表结构定义的相关文件(.frm)、以及MyISAM、MERGE、CSV和ARCHIVE表的相关文件,同时还会备份触发器和数据库配置信息相关的文件。这些文件会被保存至一个以时间命名的目录中,在备份时,innobackupex还会在备份目录中创建如下文件:

  1. xtrabackup_checkpoints:备份类型(如完全或增量)、备份状态(如是否已经为prepared状态)和LSN(日志序列号)范围信息,每个InnoDB页(通常为16k大小)都会包含一个日志序列号,即LSN。LSN是整个数据库系统的系统版本号,每个页面相关的LSN能够表明此页面最近是如何发生改变的
  2. xtrabackup_binlog_info:mysql服务器当前正在使用的二进制日志文件及至备份这一刻为止二进制日志事件的位置
  3. xtrabackup_binlog_pos_innodb:二进制日志文件及用于InnoDB或XtraDB表的二进制日志文件的当position
  4. xtrabackup_binary:备份中用到的xtrabackup的可执行文件
  5. backup-my.cnf:备份命令用到的配置选项信息在使用innobackupex进行备份时,还可以使用--no-timestamp选项来阻止命令自动创建一个以时间命名的目录;innobackupex命令将会创建一个BACKUP-DIR目录来存储备份数据

xtrabackup用法

备份:innobackupex [option] BACKUP-ROOT-DIR
选项说明:
--user:该选项表示备份账号
--password:该选项表示备份的密码
--host:该选项表示备份数据库的地址
--databases:该选项接受的参数为数据名,如果要指定多个数据库,彼此间需要以空格隔开;如:"xtra_test dba_test",同时,在指定某数据库时,也可以只指定其中的某张表。如:"mydatabase.mytable"。该选项对innodb引擎表无效,还是会备份所有innodb表
--defaults-file:该选项指定了从哪个文件读取MySQL配置,必须放在命令行第一个选项的位置
--incremental:该选项表示创建一个增量备份,需要指定--incremental-
basedir
--incremental-basedir:该选项表示接受了一个字符串参数指定含有full backup的目录为增量备份的base目录,与--incremental同时使用
--incremental-dir:该选项表示增量备份的目录
--include=name:指定表名,格式:databasename.tablename
Prepare:innobackupex --apply-log [option] BACKUP-DIR
选项说明:
--apply-log:一般情况下,在备份完成后,数据尚且不能用于恢复操作,因为备份的数据中可能会包含尚未提交的事务或已经提交但尚未同步至数据文件中的事务。因此,此时数据文件仍处理不一致状态。此选项作用是通过回滚未提交的事务及同步已经提交的事务至数据文件使数据文件处于一致性状态
--use-memory:该选项表示和--apply-log选项一起使用,prepare 备份的时候,xtrabackup做crash recovery分配的内存大小,单位字节。也可(1MB,1M,1G,1GB),推荐1G
--defaults-file:该选项指定了从哪个文件读取MySQL配置,必须放在命令行第一个选项的位置
-export:表示开启可导出单独的表之后再导入其他Mysql中
--redo-only:这个选项在prepare base full backup,往其中merge增量备份时候使用
Prepare:innobackupex --apply-log [option] BACKUP-DIR
选项说明:
--apply-log:一般情况下,在备份完成后,数据尚且不能用于恢复操作,因为备份的数据中可能会包含尚未提交的事务或已经提交但尚未同步至数据文件中的事务。因此,此时数据文件仍处理不一致状态。此选项作用是通过回滚未提交的事务及同步已经提交的事务至数据文件使数据文件处于一致性状态
--use-memory:该选项表示和--apply-log选项一起使用,prepare 备份的时候,xtrabackup做crash recovery分配的内存大小,单位字节。也可(1MB,1M,1G,1GB),推荐1G
--defaults-file:该选项指定了从哪个文件读取MySQL配置,必须放在命令行第一个选项的位置
-export:表示开启可导出单独的表之后再导入其他Mysql中
--redo-only:这个选项在prepare base full backup,往其中merge增量备份时候使用

注意事项

  1. .datadir目录必须为空。除非指定innobackupex --force-non-empty-directorires选项指定,否则--copy-backup选项不会覆盖
  2. 在restore之前,必须shutdown MySQL实例,你不能将一个运行中的实例restore到datadir目录中
  3. 由于文件属性会被保留,大部分情况下你需要在启动实例之前将文件的属主改为mysql,这些文件将属于创建备份的用户chown -R mysql:mysql /data/mysql以上需要在用户调用innobackupex之前完成--force-non-empty-directories:指定该参数时候,使得innobackupex --copy-back或--move-back选项转移文件到非空目录,已存在的文件不会被覆盖。如果--copy-back和--move-back文件需要从备份目录拷贝一个在datadir已经存在的文件,会报错失败!

猜你喜欢

转载自www.cnblogs.com/chengweihong/p/10808075.html