1. 程式人生 > >數據庫(四)

數據庫(四)

MYSQL 備份 集群

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鎖表問題的解決方法

參數調優
技術分享圖片
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 存儲類型默認是文件
技術分享圖片
讓我們把它改成表格類型(只在從機上做)
技術分享圖片
重新修改技術分享圖片

這裏用下老師的圖
如果gtid不好用,請使用手動方式指定出來。

ok。。。

數據庫(四)