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

数据库(三)

时间:2018-05-21 16:17:18      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:MYSQL   备份   

忘记数据库的密码了。。。(解决方法)
1.网上教程很多,总结下即可
2.一般分为配置文件跳过密码登陆mysql(此方法要重启数据库)和命令行后台跳过密码登陆
3.修改完密码切记刷新授权表
4.删除配置文件里的参数(此方法需重启)或终止后台命令
5.测试


MVCC


备份的分类
1.备份:数据以隔离的方式保存,恢复速度慢,可以防止人为误操作
2.冗余:主从,恢复速度快,不能防止人为误操作
3.异地灾备:

备份分为
1.以文件的方式备份,最小单元为文件,备份的时间点难控制(如果要控制时间----停服务),速度快。
2.以sql语句方式备份,最小单元sql语句,备份的时间点较好控制,速度慢。
*(文件备份也称物理备份,sql语句备份也称逻辑备份)

3.备份数据量分为:全量备份(对所有数据备份)
增量备份(对于上一次备份的修改的地方备份)
差异备份(对于全量备份的修改的地方备份)

4.备份时服务可用性分为:在线热备----备份(备份过程服务可用)
离线冷备----备份(备份过程不可用)
温备 ----备份 (备份过程可读不可写)

备份原则
数据一致:时间点,状态。
服务可用性:读,写。

物理备份工具
tar, lvm(快照), percona-xtrabackup
逻辑备份工具
mysqldump

物理备份思路
杀服务,保数据。
1.停服务
2.全备
3.恢复服务
物理备份还原思路
杀服务,清环境
1.停止服务
2.清理环境
3.导数据
4.改权限
5.启动服务

实验

 yum install -y mariadb-server  (安装数据库)

[root@localhost mysql]# mysqladmin -uroot password 123123  (设置密码)

tar -czf /tmp/all.tgz /var/lib/mysql/  (打包数据)

 [root@localhost mysql]# mysql -uroot -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.56-MariaDB MariaDB Server

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

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

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

MariaDB [(none)]> drop database test;(误操作将库删除)
Query OK, 1 row affected (0.00 sec)

[root@localhost mysql]# systemctl stop mariadb (停服务)

[root@localhost mysql]# rm -rf /var/lib/mysql/ (清环境)
[root@localhost mysql]# 

[root@localhost mysql]# tar -xf /tmp/all.tgz -C /(导数据)
[root@localhost mysql]# 

[root@localhost mysql]# systemctl start mariadb(启动服务---这里要是起不来服务先kill掉mysql相关进程)

[root@localhost mysql]# mysql -uroot -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.56-MariaDB MariaDB Server

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

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

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |               (数据回来了)
+--------------------+
4 rows in set (0.01 sec)

MariaDB [(none)]> 

*(这种方法有缺点,总不能时时刻刻都打包吧,解决方法)

逻辑备份:mysqldump
myisam 该存储引擎要保证数据完整,服务不一定可用(可用的情况---加全局读锁),时间点mysqldump开始的时候
innodb 服务可用,基于mvcc版本控制,时间点mysqldump开始的时候

1.控制服务状态
myisam --lock-all-tables
innodb --single-transaction

2.备份对象
所有库:-A 单库 库名 多个库 -B

mysqldump -u用户 -p密码 备份对象 控制服务状态 >备份路径

 MariaDB [test]> create table list(id int) engine=myisam; (指定表类型为myisam)
Query OK, 0 rows affected (0.00 sec)

MariaDB [test]> show table status\G;    (查看表属性)
*************************** 1. row ***************************
           Name: list
         Engine: MyISAM
        Version: 10
     Row_format: Fixed
           Rows: 0
 Avg_row_length: 0
    Data_length: 0
Max_data_length: 1970324836974591
   Index_length: 1024
      Data_free: 0
 Auto_increment: NULL
    Create_time: 2018-05-20 15:04:29
    Update_time: 2018-05-20 15:04:29
     Check_time: NULL
      Collation: latin1_swedish_ci
       Checksum: NULL
 Create_options: 
        Comment: 
*************************** 2. row ***************************
           Name: test1
         Engine: InnoDB
        Version: 10
     Row_format: Compact
           Rows: 2
 Avg_row_length: 8192
    Data_length: 16384
Max_data_length: 0
   Index_length: 0
      Data_free: 10485760
 Auto_increment: NULL
    Create_time: 2018-05-20 14:38:32
    Update_time: NULL
     Check_time: NULL
      Collation: latin1_swedish_ci
       Checksum: NULL
 Create_options: 
        Comment: 
2 rows in set (0.00 sec)

ERROR: No query specified

MariaDB [test]> insert into list values (1);(插入一条数据)
Query OK, 1 row affected (0.00 sec)

MariaDB [test]> Bye
[root@localhost mysql]# mysqldump -uroot -p123123 test --lock-all-tables > /tmp/test.sql  (加读锁备份)
[root@localhost mysql]# mysql -uroot -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 5.5.56-MariaDB MariaDB Server

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

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

MariaDB [(none)]> 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
MariaDB [test]> select * from list;
+------+
| id   |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

MariaDB [test]> delete from list where id=1  (误操作)
    -> ;
Query OK, 1 row affected (0.00 sec)

MariaDB [test]> Bye
[root@localhost mysql]# mysql -uroot -p123123 test < /tmp/test.sql   (导入数据)
[root@localhost mysql]# mysql -uroot -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 6
Server version: 5.5.56-MariaDB MariaDB Server

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

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

MariaDB [(none)]> 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
MariaDB [test]> select * from list;
+------+
| id   |
+------+
|    1 |
+------+
1 row in set (0.01 sec)  ()
*(多个库与单库操作一样)

*(对整个库采取全局读锁的方式备份,但是在恢复数据时,要手动删除之前的库)

全库的思路
1.停服务
2.清环境
3.启动服务
4.导数据
5.刷新授权表
6.测试

实时备份
常规日志: /var/log/mariadb/mariadb.log  默认开
二进制日志 binlog DDL DCL DML    默认不开
慢查询日志: slowlog
查询日志: DQL

二进制日志(实时增量备份)

[root@localhost binlog]# vim /etc/my.cnf
[mysqld]
log-bin=/var/lib/binlog/logbin (开启二进制日志)

[root@localhost binlog]# mkdir /var/lib/binlog(创建目录)

[root@localhost binlog]# chown mysql. /var/lib/binlog/(加权限)

[root@localhost binlog]# systemctl restart mariadb(启动服务)

[root@localhost binlog]# ls
logbin.000001  logbin.index(自动生成)

[root@localhost binlog]# file logbin.000001 
logbin.000001: MySQL replication log (二进制日志不能直接使用vim编辑或者cat查看)

[root@localhost binlog]# mysqlbinlog logbin.000001 (使用mysqlbinlog查看)
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!40019 SET @@session.max_insert_delayed_threads=0*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#180520 16:22:23 server id 1  end_log_pos 245   Start: binlog v 4, server v 5.5.56-MariaDB created 180520 16:22:23 at startup
# Warning: this binlog is either in use or was not closed properly.
ROLLBACK/*!*/;
BINLOG ‘
PzABWw8BAAAA8QAAAPUAAAABAAQANS41LjU2LU1hcmlhREIAbG9nAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAA/MAFbEzgNAAgAEgAEBAQEEgAA2QAEGggAAAAICAgCAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAANRcIYA==
‘/*!*/;
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;

[root@localhost binlog]# mysqlbinlog logbin.000001 |grep create -B 10  (-B 是之前 -A 是之后)

/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!40019 SET @@session.max_insert_delayed_threads=0*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#180520 16:22:23 server id 1  end_log_pos 245   Start: binlog v 4, server v 5.5.56-MariaDB created 180520 16:22:23 at startup

mysqlbinlog --help(可以基于时间点恢复,参数帮助里有) 定位文件 | mysql -u用户 -p密码         (恢复操作)

*(时间左闭右开)(问题又来了,如果两个人同时操作怎么基于时间恢复?解决方法----基于位置恢复)

[root@localhost binlog]# mysql -e "flush logs" -uroot -p123123(手动刷新日志)
[root@localhost binlog]# ls
logbin.000001  logbin.000002  logbin.index

DELIMITER /*!*/;
# at 4  (这个是位置标记----左闭右开)

数据写入量大的时候不可能自己去一个个找---解决方法

[root@localhost binlog]# mysqldump -uroot -p123123 test --flush-logs --single-transaction --master-data=2 > /tmp/master.sql

--master-data=2(可以指定好日志位置,下恢复操作直接从这里开始即可)

-- CHANGE MASTER TO MASTER_LOG_FILE=‘logbin.000005‘, MASTER_LOG_POS=245;(这里245就是指定出来的位置)

备份myisam
1.锁表
2.备份数据
文件
            list.frm 表结构
            list.MYD 表数据  myisam data
            list.MYI  表索引  myisam index
3.获取二进制日志位置
4.解锁表

备份innodb
存储结构
文件
库  /list.frm   表结构
/var/lib/mysql/ibdata1  表空间  存放所有数据
ib_logfile0 事务日志
ib_logfile1 

事务日志:begin事物开始就记录   如有故障,进行回滚
二进制日志:commit之后的删写操作   实时备份

xtrabackup  全量备份
基于事物日志做备份
增量备份的手段和二进制日志的标记号类似

xtrabackup  全量备份
实验
[percona-xtrabackup下载地址] (https://www.percona.com/downloads/XtraBackup/LATEST/)

[root@localhost xtraba]# ls
percona-xtrabackup-24-2.4.11-1.el7.x86_64.rpm            percona-xtrabackup-test-24-2.4.11-1.el7.x86_64.rpm
percona-xtrabackup-24-debuginfo-2.4.11-1.el7.x86_64.rpm

yum localinstall -y * (安装)

mkdir ~/mybackup(创建目录)

innobackupex --user=root --password=123123 /root/mybackup/ (备份文件)

MariaDB [(none)]> drop database test;(误操作)

 systemctl stop mariadb (停服务)

 rm -rf /var/lib/mysql/(清环境)

innobackupex --apply-log /root/mybackup/2018-05-20_18-10-44/(回滚并重演)

innobackupex --copy-back /root/mybackup/2018-05-20_18-10-44/ (写入数据)

 chown mysql. -R /var/lib/mysql/(修改权限)

systemctl start mariadb(启动服务)

mysql -uroot -p(验证)

xtrabackup增量备份
实验
1.进入数据库
2.
   insert into list values (100);
     begin;
     insert into list values (200);

3.  2018-05-20 18:45:26 innobackupex --user=root --password=123123 --incremental-basedir=/root/mybackup/2018-05-20_18-10-44/ --incremental /root/mybackup/     (增备)
4.  进入数据库
5.  commit;
     begin;
         insert into list values (300);

6. innobackupex --user=root --password=123123 --incremental-basedir=/root/mybackup/2018-05-20_18-45-26/ --incremental /root/mybackup/  (增备)

7. 
847  2018-05-20 18:57:53 innobackupex --user=root --password=123123 --apply-log --redo-only /root/mybackup/2018-05-20_18-10-44/    (重演不回滚)

8. 
 848  2018-05-20 18:57:53 innobackupex --user=root --password=123123 --apply-log --redo-only /root/mybackup/2018-05-20_18-10-44/ --incremental-dir=/root/mybackup/2018-05-20_18-45-26/  (第一次增备数据合并全量备份)

8.  
  849  2018-05-20 19:00:15 innobackupex --user=root --password=123123 --apply-log --redo-only /root/mybackup/2018-05-20_18-10-44/ --incremental-dir=/root/mybackup/2018-05-20_18-48-28/(第二次增备数据合并全量备份)

9. 
854  2018-05-20 19:01:13 innobackupex --apply-log /root/mybackup/2018-05-20_18-10-44/(回滚并重演)
[root@localhost mysql]# systemctl stop mariadb (停服务)

10.
  855  2018-05-20 19:01:43 rm -rf /var/lib/mysql/  (清环境)
  856  2018-05-20 19:02:03 innobackupex --copy-back /root/mybackup/2018-05-20_18-10-44/(写入数据)
  857  2018-05-20 19:02:26 chown mysql. -R /var/lib/mysql/(给权限)
  866  2018-05-20 19:04:43 systemctl start mariadb(启动服务)

这里300这条数据应该是进不来的,但是我的进来了,我的表存储结构myisam,不知道咋回事。。。。

下午没写完,在家里用6.5的系统写完,有些不一样的地方还请包涵

一开虚拟机就报错了。。。
Linux启动提示“unexpected inconsistency;RUN fsck MANUALLY”
解决方法:

fsck不仅可以对文件系统进行扫描,还能修正文件系统的一些问题,值得注意的是fsck扫描文件系统时一定要在单用户模式、修复模式或把设备umount后进行!!!

  首先进行单用户模式或直接在那里输入root密码进入修复模式(单用户有的也进不去)

  再用命令fsck修复所提示出错的分区或磁盘:
#fsck.ext4 -y /dev/sda2    ——>出错所在分区或磁盘
  注:请选择你的文件系统格式类似(ext2?ext3?ext4?),-y 选项指定检测每个文件是自动输入yes,修复过程也有可能损坏系统文件!
修复ok,reboot下。

[root@CDN ~]# mysqladmin -uroot password 123
mysqladmin:
You cannot use ‘password‘ command as mysqld runs
 with grant tables disabled (was started with --skip-grant-tables).
Use: "mysqladmin flush-privileges password ‘*‘" instead
(这里你的密码已经设置好了,看这个信息应该是提示)

centos6.5下载percona-xtrabackup

[root@CDN opt]# rpm -ivh percona-xtrabackup-24-2.4.11-1.el6.x86_64.rpm
warning: percona-xtrabackup-24-2.4.11-1.el6.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID cd2efd2a: NOKEY
error: Failed dependencies:(缺少依赖关系包--解决方法)

1.wget http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm

2.yum install epel-release-6-8.noarch.rpm

3.yum install libev(如果还是下载不了--解决方法)
 vim /etc/yum.repos.d/epel.repo
   [epel]
name=Extra Packages for Enterprise Linux 6 - $basearch
#baseurl=http://download.fedoraproject.org/pub/epel/6/$basearch
mirrorlist=http://mirrors.fedoraproject.org/metalink?repo=epel-6&arch=$basearch(添加上这条)

技术分享图片

[root@CDN bin]# innobackupex --user=root --password=123 /root/mybackup (有报错,耐心。。)
180520 08:20:50 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!".

180520 08:20:50  version_check Connecting to MySQL server with DSN ‘dbi:mysql:;mysql_read_default_group=xtrabackup‘ as ‘root‘  (using password: YES).
180520 08:20:50  version_check Connected to MySQL server
180520 08:20:50  version_check Executing a version check against the server...
180520 08:20:50  version_check Done.
180520 08:20:50 Connecting to MySQL server host: localhost, user: root, password: set, port: not set, socket: not set
Error: Built-in InnoDB in MySQL 5.1 is not supported in this release. You can either use Percona XtraBackup 2.0, or upgrade to InnoDB plugin.(查看翻译后,好像是版本不支持,还有一点可以升级innodb插件)

升级innodb插件
1.[root@CDN bin]# vim /etc/my.cnf
   [mysqld]
ignore-builtin-innodb
plugin-load=innodb=ha_innodb_plugin.so

2.重启mysqld
3.ok

备份固然是好的,但是恢复速度实在是慢(解决方法---冗余)
备份和冗余相辅相成

下次分享下冗余。。。

数据库(三)

标签:MYSQL   备份   

原文地址:http://blog.51cto.com/13293172/2118652

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