lvm snapshot 备份方式

[root@CDN ~]# fdisk -l /dev/sd*(查看分区,切勿在sda的磁盘上分区实验)

Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00052558

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          39      307200   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              39        2358    18631680   83  Linux
/dev/sda3            2358        2611     2031616   82  Linux swap / Solaris

Disk /dev/sda1: 314 MB, 314572800 bytes
255 heads, 63 sectors/track, 38 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/sda2: 19.1 GB, 19078840320 bytes
255 heads, 63 sectors/track, 2319 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/sda3: 2080 MB, 2080374784 bytes
255 heads, 63 sectors/track, 252 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x01c33ee9

     Device Boot      Start         End      Blocks   Id  System

Disk /dev/sdb: 10.7 GB, 10737418240 bytes  (这里我添加一块实验磁盘)
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x630689af

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         393     3156741   83  Linux(分出一个主分区)

Disk /dev/sdb1: 3232 MB, 3232502784 bytes
255 heads, 63 sectors/track, 392 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

[root@CDN ~]# pvcreate /dev/sdb1(创建pv)
  dev_is_mpath: failed to get device for 8:17
  Physical volume "/dev/sdb1" successfully created
[root@CDN ~]# vgcreate mysqlpool /dev/sdb1(创建vg)
  Volume group "mysqlpool" successfully created
[root@CDN ~]# lvcreate -n mysqlpool-1 -L 2G mysqlpool(创建lv)
  Logical volume "mysqlpool-1" created

[root@CDN ~]# mkfs.ext4 /dev/mapper/mysqlpool-mysqlpool--1(对lv格式化文件系统)
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
131072 inodes, 524288 blocks
26214 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=536870912
16 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
    32768, 98304, 163840, 229376, 294912

Writing inode tables: done
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 38 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

[root@CDN ~]# mount /dev/mapper/mysqlpool-mysqlpool--1 /srv(磁盘不能直接使用,我已挂载的方式先拷贝数据到/srv
[root@CDN ~]# service mysqld stop
Stopping mysqld:                                           [  OK  ]

[root@CDN mysql]# tar -czf - *|tar -xzf - -C /srv/(对数据库文件打包并解压)
[root@CDN mysql]# cd /srv/
[root@CDN srv]# ls
ibdata1  ib_logfile0  ib_logfile1  ibtmp1  lost+found  mysql  test  xtrabackup_info  xtrabackup_master_key_id
[root@CDN srv]# cd
[root@CDN ~]# umount /srv/
[root@CDN ~]# mount /dev/mapper/mysqlpool-mysqlpool--1 /var/lib/mysql/  (挂载到数据库位置使用)

[root@CDN mysql]# service mysqld start(启动数据库)
Starting mysqld:                                           [  OK  ]

[root@CDN mysql]# mysql -uroot -p123 -e 'flush tables with read lock'(锁表)
[root@CDN mysql]# lvcreate -s -L 1G -n mysql-snap(创建快照) /dev/mapper/mysqlpool-mysqlpool--1
  Logical volume "mysql-snap" created
[root@CDN mysql]# lvs
  LV          VG        Attr       LSize Pool Origin      Data%  Move Log Cpy%Sync Convert
  mysql-snap  mysqlpool swi-a-s--- 1.00g      mysqlpool-1   0.00
  mysqlpool-1 mysqlpool owi-aos--- 2.00g
[root@CDN mysql]# mysql -uroot -p123 -e 'unlock tables'(解锁表)

[root@CDN ~]# blkid /dev/mapper/mysqlpool-mysqlpool--1
/dev/mapper/mysqlpool-mysqlpool--1: UUID="e9f9bdf1-b421-405c-9354-936cdc54a566" TYPE="ext4"(可以看到这两个设备文件的uuid是一摸一样的,有时候挂载会报错,只能挂一个----解决方法)
[root@CDN ~]# blkid /dev/mapper/mysqlpool-mysql--snap
/dev/mapper/mysqlpool-mysql--snap: UUID="e9f9bdf1-b421-405c-9354-936cdc54a566" TYPE="ext4"

[root@CDN ~]# uuidgen(先生成一个uuid)
ef6fa761-5a17-4780-8646-622ffeae1de6
[root@CDN ~]# umount /srv  (卸载)
[root@CDN ~]# tune2fs /dev/mapper/mysqlpool-mysql--snap -U ef6fa761-5a17-4780-8646-622ffeae1de6(手动指定uuid)
tune2fs 1.41.12 (17-May-2010)
[root@CDN ~]# blkid /dev/mapper/mysqlpool-mysql--snap
/dev/mapper/mysqlpool-mysql--snap: UUID="ef6fa761-5a17-4780-8646-622ffeae1de6" TYPE="ext4"
[root@CDN ~]# mount /dev/mapper/mysqlpool-mysql--snap /srv
[root@CDN ~]# df -h(ok,可以挂载了)
Filesystem                          Size  Used Avail Use% Mounted on
/dev/sda2                            18G  4.2G   13G  25% /
tmpfs                               491M     0  491M   0% /dev/shm
/dev/sda1                           291M   34M  242M  13% /boot
/dev/mapper/mysqlpool-mysqlpool--1  2.0G  100M  1.8G   6% /var/lib/mysql
/dev/mapper/mysqlpool-mysql--snap   2.0G  100M  1.8G   6% /srv

[root@CDN ~]# tar -czf /tmp/snap.tgz /srv/(对快照打包)
[root@CDN ~]# lvremove /dev/mapper/mysqlpool-mysql--snap(删除快照)

percona-xtrabackup完全及增量备份与恢复

如何搭建Percona XtraDB Cluster集群

基于mysql5.1单主从AB复制

环境192.168.10.13(主)
       192.168.10.15(从)

*(实验前先关闭selinux和iptables)

主机操作:
[root@mysql-master ~]# vim /etc/my.cnf
[mysqld]
log-bin=/var/log/binlog/master(启动二进制日志)
server_id=1(声明ID)

[root@mysql-master ~]# mkdir /var/log/binlog/

[root@mysql-master ~]# chown mysql. -R /var/log/binlog/

[root@mysql-master ~]# service mysqld restart
Stopping mysqld:                                           [  OK  ]
Starting mysqld:                                           [  OK  ]

mysql> create table list1 (name char(3));       (插入三条数据)
Query OK, 0 rows affected (0.07 sec)

mysql> insert into list1 values (a),(b),(c);
ERROR 1054 (42S22): Unknown column 'a' in 'field list'
mysql> insert into list1 values ('a'),('b'),('c');
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from list1;
+------+
| name |
+------+
| a    |
| b    |
| c    |
+------+
3 rows in set (0.00 sec)

mysql> grant replication slave on *.* to boke@'192.168.10.15' identified by '123';
Query OK, 0 rows affected (0.00 sec)   (给从机授权,也可以是网段)

mysql> flush privileges
    -> ;
Query OK, 0 rows affected (0.00 sec)   (刷新授权表)

[root@mysql-master binlog]# mysqldump -uroot -p123 -A --lock-all-tables --flush-logs --master-data=2 > /tmp/all.sql    (打包数据,尽可能减少拉取数据的量)
-- Warning: Skipping the data of table mysql.event. Specify the --events option explicitly.
[root@mysql-master binlog]# sed -n '22p' /tmp/all.sql    (查看二进制日志记录到什么位置) 
-- CHANGE MASTER TO MASTER_LOG_FILE='master.000002', MASTER_LOG_POS=106;

[root@mysql-master binlog]# scp /tmp/all.sql 192.168.10.15:/tmp/(传输数据)
Warning: Permanently added '192.168.10.15' (RSA) to the list of known hosts.
all.sql

从机操作:
[root@mysql-slave tmp]# vim /etc/my.cnf          
[mysqld]
server_id=2(声明ID)

[root@mysql-slave tmp]# service mysqld start(启动数据库)

[root@mysql-slave tmp]# mysql -uroot -p123456 < /tmp/all.sql(导入数据)

mysql> CHANGE MASTER TO (声明谁是主服务器)
    ->  MASTER_HOST='192.168.10.13',
    -> MASTER_USER='boke',
    -> MASTER_PASSWORD='123',
    -> MASTER_PORT=3306,
    -> MASTER_LOG_FILE='master.000002',
    -> MASTER_LOG_POS=106,
    -> MASTER_CONNECT_RETRY=10;
Query OK, 0 rows affected (0.04 sec)

mysql> start slave;   (启动从服务器)
Query OK, 0 rows affected (0.00 sec)

mysql> show slave status\G;(查看同步状态)
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.10.13
                  Master_User: boke
                  Master_Port: 3306
                Connect_Retry: 10
              Master_Log_File: master.000002
          Read_Master_Log_Pos: 106
               Relay_Log_File: mysqld-relay-bin.000002
                Relay_Log_Pos: 249
        Relay_Master_Log_File: master.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB:
          Replicate_Ignore_DB:
           Replicate_Do_Table:
       Replicate_Ignore_Table:
      Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table:
                   Last_Errno: 0
                   Last_Error:
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 106
              Relay_Log_Space: 406
              Until_Condition: None
               Until_Log_File:
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File:
           Master_SSL_CA_Path:
              Master_SSL_Cert:
            Master_SSL_Cipher:
               Master_SSL_Key:
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 1
1 row in set (0.00 sec)

ERROR:
No query specified

测试:
主数据库
mysql> select * from list1;
+------+
| name |
+------+
| a    |
| b    |
| c    |
+------+
3 rows in set (0.00 sec)
mysql> insert into list1 values ('d');
Query OK, 1 row affected (0.00 sec)

从数据库
mysql> use test;
Database changed
mysql> select * from list1;
+------+
| name |
+------+
| a    |
| b    |
| c    |
| d    |
+------+
4 rows in set (0.00 sec)
基于mysql5.7的主从
这里大部分步骤于msyql5.1一致
下面做些补充即可
1.mysql5.7版本新增一些新的特新(GTID---自动确定二进制日志的位置)
2.默认设置初始密码(grep  /var/log/mysqld.log 可以看到默认密码)
mysql5.1于mysql5.7的破密区别
5.1(直接命令行放后台即可)
5.7 (在my.cnf的配置文件添加一行参数:skip-grant-tables=1)
update user set authentication_string=password('123456') where user='root';

3.mysql5.7数据库关于授权的操作不会记录到历史记录里面

4.mysql5.7要做主从在my.cnf文件里多添加两条参数(gtid_mode=on
                                                                                      enforce_gtid_consistency=1)

mysql> CHANGE MASTER TO (声明谁是主服务器)
    ->  MASTER_HOST='192.168.10.13',
    -> MASTER_USER='boke',
    -> MASTER_PASSWORD='123',
    -> MASTER_PORT=3306,
    -> MASTER_AUTO_POSITION=1,(自动定位日志文件位置)
    -> MASTER_CONNECT_RETRY=10;

    如果中途出了问题怎么排查?---解决思路
    1.stop slave;
    2.reset slave all;
    3.change master to;(重新指定下会将之前的覆盖掉)

mysql5.7调优
再从服务器上实现并行传输
my.cnf文件里添加两条参数1.slave_parallel_type=logical_clock(一组一线程)
2.slave_parallel_workers=2(lscpu---cpu数Xcpu核数)

mysql5.7安装出来如果没有默认密码----解决方法
1.停服务
2.请环境
3.mysqld --initialize(初始化)

MYSQL锁表问题的解决方法

参数调优
数据库(四)_第1张图片
mysql5.7双主双从架构
1.主从同步要求的是数据一致,一旦数据不一致就会出现问题
2.双主怎么做呢?---解决方法
(1)主机授权给网段+声明另一台主机为主人
(2)主机再次授权给自己作为从机+声明另一台主机为主人
3.两个从机怎么认两个主机为主人呢?之前说过执行change master to会覆盖之前的信息----解决方法

 [root@mysql-slave mysql]# cat master.info
18
master.000002
352
192.168.10.13(每次执行次ip都会被覆盖)
boke
123
3306
10
0

0
1800.000

0
[root@mysql-slave mysql]# cat relay-log.info
./mysqld-relay-bin.000007
249
master.000002
199
[root@mysql-slave mysql]#

查看数据库的change master 存储类型默认是文件
数据库(四)_第2张图片
让我们把它改成表格类型(只在从机上做)
数据库(四)_第3张图片
重新修改数据库(四)

这里用下老师的图
如果gtid不好用,请使用手动方式指定出来。

ok。。。