兩臺虛擬機,系統爲CentOS 5.4,分別有三塊磁盤來做邏輯卷。
IP分配及磁盤情況:
HA1 eth0:192.168.0.77 eth1:192.168.10.1 /dev/sdc /dev/sdd /dev/sde
HA2 eth0:192.168.0.69 eth1:192.168.10.2 /dev/sdc /dev/sdd /dev/sde
一、配置邏輯磁盤
查看磁盤情況:
[root@HA1 ~]# fdisk -l
Disk /dev/sda: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sda1 * 1 13 104391 83 Linux
/dev/sda2 14 1305 10377990 8e Linux LVM
Disk /dev/sdb: 6442 MB, 6442450944 bytes
255 heads, 63 sectors/track, 783 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk /dev/sdb doesn’t contain a valid partition table
Disk /dev/sdc: 536 MB, 536870912 bytes
64 heads, 32 sectors/track, 512 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Disk /dev/sdc doesn’t contain a valid partition table
Disk /dev/sdd: 536 MB, 536870912 bytes
64 heads, 32 sectors/track, 512 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Disk /dev/sdd doesn’t contain a valid partition table
Disk /dev/sde: 536 MB, 536870912 bytes
64 heads, 32 sectors/track, 512 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Disk /dev/sde doesn’t contain a valid partition table
爲磁盤分區:
[root@HA1 ~]# fdisk /dev/sdc
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel. Changes will remain in memory only,
until you decide to write them. After that, of course, the previous
content won’t be recoverable.
Warning: invalid flag 0×0000 of partition table 4 will be corrected by w(rite)
Command (m for help): m # 獲取幫助
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition’s system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)
Command (m for help): n # 新建分區
Command action
e extended
p primary partition (1-4)
p # 新建主分區
Partition number (1-4): 1 # 輸入分區號
First cylinder (1-512, default 1): # 回車,默認即可
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-512, default 512): # 回車,使用所有磁盤空間
Using default value 512
Command (m for help): t # 設置分區類型
Selected partition 1
Hex code (type L to list codes): L # 查看分區類型
0 Empty 1e Hidden W95 FAT1 80 Old Minix bf Solaris
1 FAT12 24 NEC DOS 81 Minix / old Lin c1 DRDOS/sec (FAT-
2 XENIX root 39 Plan 9 82 Linux swap / So c4 DRDOS/sec (FAT-
3 XENIX usr 3c PartitionMagic 83 Linux c6 DRDOS/sec (FAT-
4 FAT16 <32M 40 Venix 80286 84 OS/2 hidden C: c7 Syrinx
5 Extended 41 PPC PReP Boot 85 Linux extended da Non-FS data
6 FAT16 42 SFS 86 NTFS volume set db CP/M / CTOS / .
7 HPFS/NTFS 4d QNX4.x 87 NTFS volume set de Dell Utility
8 AIX 4e QNX4.x 2nd part 88 Linux plaintext df BootIt
9 AIX bootable 4f QNX4.x 3rd part 8e Linux LVM e1 DOS access
a OS/2 Boot Manag 50 OnTrack DM 93 Amoeba e3 DOS R/O
b W95 FAT32 51 OnTrack DM6 Aux 94 Amoeba BBT e4 SpeedStor
c W95 FAT32 (LBA) 52 CP/M 9f BSD/OS eb BeOS fs
e W95 FAT16 (LBA) 53 OnTrack DM6 Aux a0 IBM Thinkpad hi ee EFI GPT
f W95 Ext’d (LBA) 54 OnTrackDM6 a5 FreeBSD ef EFI (FAT-12/16/
10 OPUS 55 EZ-Drive a6 OpenBSD f0 Linux/PA-RISC b
11 Hidden FAT12 56 Golden Bow a7 NeXTSTEP f1 SpeedStor
12 Compaq diagnost 5c Priam Edisk a8 Darwin UFS f4 SpeedStor
14 Hidden FAT16 <3 61 SpeedStor a9 NetBSD f2 DOS secondary
16 Hidden FAT16 63 GNU HURD or Sys ab Darwin boot fb VMware VMFS
17 Hidden HPFS/NTF 64 Novell Netware b7 BSDI fs fc VMware VMKCORE
18 AST SmartSleep 65 Novell Netware b8 BSDI swap fd Linux raid auto
1b Hidden W95 FAT3 70 DiskSecure Mult bb Boot Wizard hid fe LANstep
1c Hidden W95 FAT3 75 PC/IX be Solaris boot ff BBT
Hex code (type L to list codes): 8e # 設置分區類型爲Linux LVM
Changed system type of partition 1 to 8e (Linux LVM)
Command (m for help): w # 保存退出
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@HA1 ~]# fdisk /dev/sdd
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel. Changes will remain in memory only,
until you decide to write them. After that, of course, the previous
content won’t be recoverable.
Warning: invalid flag 0×0000 of partition table 4 will be corrected by w(rite)
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-512, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-512, default 512):
Using default value 512
Command (m for help): t
Selected partition 1
Hex code (type L to list codes): 8e
Changed system type of partition 1 to 8e (Linux LVM)
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@HA1 ~]# fdisk /dev/sde
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel. Changes will remain in memory only,
until you decide to write them. After that, of course, the previous
content won’t be recoverable.
Warning: invalid flag 0×0000 of partition table 4 will be corrected by w(rite)
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-512, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-512, default 512):
Using default value 512
Command (m for help): t
Selected partition 1
Hex code (type L to list codes): 8e
Changed system type of partition 1 to 8e (Linux LVM)
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
分區完成後查看磁盤情況:
[root@HA1 ~]# fdisk -l
Disk /dev/sda: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sda1 * 1 13 104391 83 Linux
/dev/sda2 14 1305 10377990 8e Linux LVM
Disk /dev/sdb: 6442 MB, 6442450944 bytes
255 heads, 63 sectors/track, 783 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk /dev/sdb doesn’t contain a valid partition table
Disk /dev/sdc: 536 MB, 536870912 bytes
64 heads, 32 sectors/track, 512 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Device Boot Start End Blocks Id System
/dev/sdc1 1 512 524272 8e Linux LVM
Disk /dev/sdd: 536 MB, 536870912 bytes
64 heads, 32 sectors/track, 512 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Device Boot Start End Blocks Id System
/dev/sdd1 1 512 524272 8e Linux LVM
Disk /dev/sde: 536 MB, 536870912 bytes
64 heads, 32 sectors/track, 512 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Device Boot Start End Blocks Id System
/dev/sde1 1 512 524272 8e Linux LVM
創建物理卷:
[root@HA1 ~]# pvcreate /dev/sdc1 /dev/sdd1 /dev/sde1
Physical volume “/dev/sdc1″ successfully created
Physical volume “/dev/sdd1″ successfully created
Physical volume “/dev/sde1″ successfully created
查看物理卷:
[root@HA1 ~]# pvdisplay
— Physical volume —
PV Name /dev/sda2
VG Name VolGroup00
PV Size 9.90 GB / not usable 22.76 MB
Allocatable yes (but full)
PE Size (KByte) 32768
Total PE 316
Free PE 0
Allocated PE 316
PV UUID 1zBHox-Dla7-0ozU-0IFp-Onl4-V7V2-R10XXW
“/dev/sdc1″ is a new physical volume of “511.98 MB”
— NEW Physical volume —
PV Name /dev/sdc1
VG Name
PV Size 511.98 MB
Allocatable NO
PE Size (KByte) 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID DwoEeZ-NmK5-ZDR6-qCmx-vJsw-7Wet-2qGako
“/dev/sdd1″ is a new physical volume of “511.98 MB”
— NEW Physical volume —
PV Name /dev/sdd1
VG Name
PV Size 511.98 MB
Allocatable NO
PE Size (KByte) 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID YfolqL-6Qlm-bUki-qWTJ-8zIW-zeJI-Ssjxln
“/dev/sde1″ is a new physical volume of “511.98 MB”
— NEW Physical volume —
PV Name /dev/sde1
VG Name
PV Size 511.98 MB
Allocatable NO
PE Size (KByte) 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID Rhdkyp-MBB6-UeTK-dmuP-6Dza-L69O-sW6eNv
創建邏輯卷組:
[root@HA1 ~]# vgcreate dataVg /dev/sdc1 /dev/sdd1 /dev/sde1
Volume group “dataVg” successfully created
創建邏輯卷:
[root@HA1 ~]# lvcreate –name dataLv –size 1G dataVg
Logical volume “dataLv” created
查看邏輯卷:
[root@HA1 ~]# lvdisplay
— Logical volume —
LV Name /dev/dataVg/dataLv
VG Name dataVg
LV UUID gXPZmP-c41N-Yeu8-mT8U-0sUx-Mu2X-pR1PyE
LV Write Access read/write
LV Status available
# open 0
LV Size 1.00 GB
Current LE 256
Segments 3
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:2
— Logical volume —
LV Name /dev/VolGroup00/LogVol00
VG Name VolGroup00
LV UUID yTby3S-TYzd-x7fP-T8HJ-GOEg-lt7E-i90qZy
LV Write Access read/write
LV Status available
# open 1
LV Size 8.88 GB
Current LE 284
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0
— Logical volume —
LV Name /dev/VolGroup00/LogVol01
VG Name VolGroup00
LV UUID bNfOaD-vcTc-hq4c-7Bd0-3a6S-wD0B-aFZMzM
LV Write Access read/write
LV Status available
# open 1
LV Size 1.00 GB
Current LE 32
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:1
格式化邏輯卷:
[root@HA1 ~]# mkfs.ext3 /dev/dataVg/dataLv
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
131072 inodes, 262144 blocks
13107 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=268435456
8 block groups
32768 blocks per group, 32768 fragments per group
16384 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 25 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
掛載邏輯捲到/data目錄:
[root@HA1 ~]# mount /dev/dataVg/dataLv /data/
設置開機自動掛載掛邏輯卷:
[root@HA1 ~]# vi /etc/fstab
/dev/dataVg/dataLv /data ext3 defaults 0 0
在HA2上執行上面步驟。
二、安裝MySQL並遷移MySQL數據到HA1 /data下。
三、安裝MySQL LVM備份工具:
[root@HA1 ~]# wget http://search.cpan.org/CPAN/authors/id/S/SH/SHLOMIF/Config-IniFiles-2.54.tar.gz
[root@HA1 ~]# tar xzvf Config-IniFiles-2.54.tar.gz
[root@HA1 ~]# cd Config-IniFiles-2.54
[root@HA1 Config-IniFiles-2.54]# perl Makefile.PL
Checking if your kit is complete…
Looks good
Writing Makefile for Config::IniFiles
[root@HA1 Config-IniFiles-2.54]# make
cp lib/Config/IniFiles.pm blib/lib/Config/IniFiles.pm
Manifying blib/man3/Config::IniFiles.3pm
[root@HA1 Config-IniFiles-2.54]# make install
Installing /usr/lib/perl5/site_perl/5.8.8/Config/IniFiles.pm
Installing /usr/share/man/man3/Config::IniFiles.3pm
Writing /usr/lib/perl5/site_perl/5.8.8/i386-linux-thread-multi/auto/Config/IniFiles/.packlist
Appending installation info to /usr/lib/perl5/5.8.8/i386-linux-thread-multi/perllocal.pod
[root@HA1 Config-IniFiles-2.54]# cd ..
[root@HA1 ~]# wget http://www.lenzg.net/mylvmbackup/mylvmbackup-0.13.tar.gz
[root@HA1 ~]# tar xzvf mylvmbackup-0.13.tar.gz
[root@HA1 ~]# cd mylvmbackup-0.13
[root@HA1 mylvmbackup-0.13]# make install
[root@HA1 mylvmbackup-0.13]# cd ..
配置mylvmbackup:
[root@HA1 ~]# vi /etc/mylvmbackup.conf
[mysql]
user=root
password=
host=localhost
port=3306
socket=/data/mysql/mysql.sock
mycnf=/etc/my.cnf
#
# LVM-specific options
#
[lvm]
vgname=dataVg
lvname=dataLv
backuplv=backupLv
lvsize=0.45G
#
# File system specific options
#
[fs]
xfs=0
mountdir=/var/tmp/mylvmbackup/mnt/
backupdir=/var/tmp/mylvmbackup/backup/
relpath=
注意修改上面標紅的配置項。
創建下面目錄:
[root@HA1 ~]# mkdir -p /var/tmp/mylvmbackup/backup
[root@HA1 ~]# mkdir -p /var/tmp/mylvmbackup/mnt
查看數據庫情況(employees庫使用InnoDB 引擎):
[root@HA1 ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 8
Server version: 5.0.77 Source distribution
Type ‘help;’ or ‘h’ for help. Type ‘c’ to clear the buffer.
mysql> show databases;
+——————–+
| Database |
+——————–+
| information_schema |
| employees |
| mysql |
+——————–+
3 rows in set (0.01 sec)
mysql> use employees;
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 engines;
+————+———+—————————————————————-+
| Engine | Support | Comment |
+————+———+—————————————————————-+
| MyISAM | DEFAULT | Default engine as of MySQL 3.23 with great performance |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables |
| InnoDB | YES | Supports transactions, row-level locking, and foreign keys |
| BerkeleyDB | YES | Supports transactions and page-level locking |
| BLACKHOLE | NO | /dev/null storage engine (anything you write to it disappears) |
| EXAMPLE | NO | Example storage engine |
| ARCHIVE | NO | Archive storage engine |
| CSV | NO | CSV storage engine |
| ndbcluster | NO | Clustered, fault-tolerant, memory-based tables |
| FEDERATED | NO | Federated MySQL storage engine |
| MRG_MYISAM | YES | Collection of identical MyISAM tables |
| ISAM | NO | Obsolete storage engine |
+————+———+—————————————————————-+
12 rows in set (0.00 sec)
mysql> show tables;
+———————+
| Tables_in_employees |
+———————+
| departments |
| dept_emp |
| dept_manager |
| employees |
| salaries |
| titles |
+———————+
6 rows in set (0.00 sec)
mysql> select count(*) from employees;
+———-+
| count(*) |
+———-+
| 300024 |
+———-+
1 row in set (1.94 sec)
mysql> quit
Bye
lvm快照備份數據庫:
[root@HA1 ~]# mylvmbackup
20091125 14:50:10 Info: Connecting to database…
20091125 14:50:10 Info: Flushing tables with read lock… # 鎖定庫表,準備備份
20091125 14:50:10 Info: Taking position record into /tmp/mylvmbackup-backup-20091125_145009_mysql-odzMgs.pos… # 如開啓bin-log則記錄日誌位置信息
20091125 14:50:10 Info: Running: lvcreate -s –size=0.45G –name=backupLv /dev/dataVg/dataLv
File descriptor 4 (socket:[21544]) leaked on lvcreate invocation. Parent PID 6062: /usr/bin/perl
Rounding up size to full physical extent 464.00 MB
Logical volume “backupLv” created
20091125 14:50:13 Info: DONE: taking LVM snapshot # 只需3s完成lvm快照備份
20091125 14:50:13 Info: Unlocking tables… # 完成備份,解除鎖定,至此數據庫完全恢復正常訪問
20091125 14:50:13 Info: Disconnecting from database…
20091125 14:50:13 Info: Mounting snapshot…
20091125 14:50:13 Info: Running: mount -o rw /dev/dataVg/backupLv /var/tmp/mylvmbackup/mnt/backup
20091125 14:50:13 Info: DONE: mount snapshot
20091125 14:50:13 Info: Copying/tmp/mylvmbackup-backup-20091125_145009_mysql-odzMgs.pos to/var/tmp/mylvmbackup/mnt/backup-pos/backup-20091125_145009_mysql.pos…
20091125 14:50:13 Info: Copying /etc/my.cnf to /var/tmp/mylvmbackup/mnt/backup-pos/backup-20091125_145009_mysql_my.cnf…
20091125 14:50:13 Info: Taking actual backup…
20091125 14:50:13 Info: Creating tar archive /var/tmp/mylvmbackup/backup/backup-20091125_145009_mysql.tar.gz
20091125 14:50:13 Info: Running: cd ‘/var/tmp/mylvmbackup/mnt’ ;’tar’cvf – backup/ backup-pos/backup-20091125_145009_mysql.posbackup-pos/backup-20091125_145009_mysql_my.cnf| gzip –stdout –verbose–best ->/var/tmp/mylvmbackup/backup/backup-20091125_145009_mysql.tar.gz.INCOMPLETE-54lIVbU
backup/
backup/lost+found/
backup/logs/
backup/logs/www.access.log
backup/logs/error.log
backup/backup/
backup/backup/cib.xml
backup/backup/ifcfg-lo:0
backup/mysql/
backup/mysql/ib_logfile0
tar: backup/mysql/mysql.sock: socket ignored
backup/mysql/employees/
backup/mysql/employees/departments.frm
backup/mysql/employees/dept_emp.frm
backup/mysql/employees/salaries.frm
backup/mysql/employees/employees.frm
backup/mysql/employees/db.opt
backup/mysql/employees/dept_manager.frm
backup/mysql/employees/titles.frm
backup/mysql/ib_logfile1
backup/mysql/mysql/
backup/mysql/mysql/help_category.MYD
backup/mysql/mysql/help_topic.MYI
backup/mysql/mysql/help_relation.MYD
backup/mysql/mysql/db.frm
backup/mysql/mysql/time_zone.frm
backup/mysql/mysql/time_zone.MYD
backup/mysql/mysql/time_zone_transition.MYI
backup/mysql/mysql/columns_priv.MYI
backup/mysql/mysql/tables_priv.frm
backup/mysql/mysql/host.MYD
backup/mysql/mysql/procs_priv.MYI
backup/mysql/mysql/proc.frm
backup/mysql/mysql/user.MYD
backup/mysql/mysql/db.MYI
backup/mysql/mysql/time_zone_name.MYI
backup/mysql/mysql/time_zone.MYI
backup/mysql/mysql/func.MYI
backup/mysql/mysql/help_keyword.MYI
backup/mysql/mysql/help_topic.MYD
backup/mysql/mysql/procs_priv.MYD
backup/mysql/mysql/db.MYD
backup/mysql/mysql/time_zone_name.MYD
backup/mysql/mysql/host.MYI
backup/mysql/mysql/time_zone_leap_second.frm
backup/mysql/mysql/time_zone_transition_type.MYD
backup/mysql/mysql/time_zone_transition_type.MYI
backup/mysql/mysql/help_relation.MYI
backup/mysql/mysql/time_zone_leap_second.MYI
backup/mysql/mysql/help_keyword.MYD
backup/mysql/mysql/user.frm
backup/mysql/mysql/func.MYD
backup/mysql/mysql/tables_priv.MYI
backup/mysql/mysql/tables_priv.MYD
backup/mysql/mysql/time_zone_transition.frm
backup/mysql/mysql/user.MYI
backup/mysql/mysql/help_category.frm
backup/mysql/mysql/procs_priv.frm
backup/mysql/mysql/columns_priv.MYD
backup/mysql/mysql/help_category.MYI
backup/mysql/mysql/help_keyword.frm
backup/mysql/mysql/time_zone_leap_second.MYD
backup/mysql/mysql/proc.MYI
backup/mysql/mysql/proc.MYD
backup/mysql/mysql/time_zone_transition_type.frm
backup/mysql/mysql/time_zone_transition.MYD
backup/mysql/mysql/func.frm
backup/mysql/mysql/time_zone_name.frm
backup/mysql/mysql/host.frm
backup/mysql/mysql/help_relation.frm
backup/mysql/mysql/help_topic.frm
backup/mysql/mysql/columns_priv.frm
backup/mysql/ibdata1
backup/html/
backup/html/www.baihe.com/
backup/html/www.baihe.com/test.html
backup/html/www.baihe.com/index.html
backup-pos/backup-20091125_145009_mysql.pos
backup-pos/backup-20091125_145009_mysql_my.cnf
64.0%
20091125 14:56:00 Info: DONE: create tar archive
20091125 14:56:01 Info: Cleaning up…
20091125 14:56:01 Info: Running: umount /var/tmp/mylvmbackup/mnt/backup
20091125 14:56:02 Info: DONE: Unmounting /var/tmp/mylvmbackup/mnt/backup
20091125 14:56:02 Info: LVM Usage stats:
20091125 14:56:02 Info: LV VG Attr LSize Origin Snap% Move Log Copy% Convert
20091125 14:56:02 Info: backupLv dataVg swi-a- 464.00M dataLv 0.09
20091125 14:56:02 Info: Running: lvremove -f /dev/dataVg/backupLv
Logical volume “backupLv” successfully removed
20091125 14:56:03 Info: DONE: Removing snapshot
[root@HA1 ~]# cd /var/tmp/mylvmbackup/backup
You have new mail in /var/spool/mail/root
[root@HA1 backup]# ls
backup-20091125_145009_mysql.tar.gz
[root@HA1 backup]# scp backup-20091125_145009_mysql.tar.gz HA2:/root/
root@ha2’s password:
backup-20091125_145009_mysql.tar.gz 100% 80MB 799.2KB/s 01:42
在HA2上進行有效性驗證:
[root@HA2 data]# tar xzvf /root/backup-20091125_145009_mysql.tar.gz
[root@HA2 data]# ls
backup backup-pos lost+found
You have new mail in /var/spool/mail/root
[root@HA2 data]# cd backup
[root@HA2 backup]# ls
backup html logs lost+found mysql
[root@HA2 backup]# mv mysql/ ..
[root@HA2 backup]# cd ..
[root@HA2 data]# service mysqld start
Starting MySQL: [ OK ]
[root@HA2 data]# mysql
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 2
Server version: 5.0.77 Source distribution
Type ‘help;’ or ‘h’ for help. Type ‘c’ to clear the buffer.
mysql> show databases;
+——————–+
| Database |
+——————–+
| information_schema |
| employees |
| mysql |
+——————–+
3 rows in set (0.00 sec)
mysql> use employees;
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_employees |
+———————+
| departments |
| dept_emp |
| dept_manager |
| employees |
| salaries |
| titles |
+———————+
6 rows in set (0.00 sec)
mysql> select count(*) from employees;
+———-+
| count(*) |
+———-+
| 300024 |
+———-+
1 row in set (0.58 sec)
mysql>
四、配置數據庫主從複製:
HA1(主)
配置mysql
[root@HA1 ~]# cat /etc/my.cnf
[mysqld]
datadir=/data/mysql
socket=/data/mysql/mysql.sock
user=mysql
# Default to using old password format for compatibility with mysql 3.x
# clients (those using the mysqlclient10 compatibility package).
old_passwords=1
log-bin=/data/mysql/log/mysql-bin.log
server-id=1
[mysqld_safe]
log-error=/data/mysql/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
[mysql]
socket=/data/mysql/mysql.sock
如果對配置文件有改動需要重啓MySQL。
lvm快照備份數據庫:
[root@HA1 backup]# mylvmbackup
拷貝備份文件到HA2
[root@HA1 backup]# scp backup-20091125_155132_mysql.tar.gz HA2:/root/
root@ha2’s password:
backup-20091125_155132_mysql.tar.gz 100% 80MB 1.2MB/s 01:07
在主庫上添加同步賬戶:
[root@HA1 backup]# mysql
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 5
Server version: 5.0.77-log Source distribution
Type ‘help;’ or ‘h’ for help. Type ‘c’ to clear the buffer.
mysql> GRANT REPLICATION SLAVE ON *.* TO ‘rep’@'192.168.10.%’ IDENTIFIED BY ’slavepass’;
HA2(從)
解壓主庫備份數據文件到從庫數據目錄:
[root@HA2 data]# tar xzvf /root/backup-20091125_155132_mysql.tar.gz
拷貝數據文件到mysql數據目錄:
[root@HA2 data]# mv backup/mysql/ .
查看備份時mysql日誌位置:
[root@HA2 data]# cat backup-pos/backup-20091125_155132_mysql.pos
Master:File=mysql-bin.000001
Master:Position=244
Master:Binlog_Do_DB=
Master:Binlog_Ignore_DB=
修改mysql配置文件:
[root@HA2 data]# cat /etc/my.cnf
[mysqld]
datadir=/data/mysql
socket=/data/mysql/mysql.sock
user=mysql
# Default to using old password format for compatibility with mysql 3.x
# clients (those using the mysqlclient10 compatibility package).
old_passwords=1
server-id=2
[mysqld_safe]
log-error=/data/mysql/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
[mysql]
socket=/data/mysql/mysql.sock
啓動MySQL:
[root@HA2 log]# service mysqld start
Starting MySQL: [ OK ]
配置mysql從庫:
[root@HA2 log]# mysql
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 4
Server version: 5.0.77 Source distribution
Type ‘help;’ or ‘h’ for help. Type ‘c’ to clear the buffer.
mysql> CHANGE MASTER TO
-> MASTER_HOST=’192.168.10.1′,
-> MASTER_USER=’rep’,
-> MASTER_PASSWORD=’slavepass’,
-> MASTER_LOG_FILE=’mysql-bin.000001′,
-> MASTER_LOG_POS=244;
Query OK, 0 rows affected (0.00 sec)
mysql> slave start;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave statusG
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.10.1
Master_User: rep
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 500
Relay_Log_File: mysqld-relay-bin.000003
Relay_Log_Pos: 637
Relay_Master_Log_File: mysql-bin.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: 500
Relay_Log_Space: 637
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
1 row in set (0.00 sec)
mysql> quit
Bye
驗證同步配置情況:
在HA1上插入一條數據:
[root@HA1 ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 62
Server version: 5.0.77-log Source distribution
Type ‘help;’ or ‘h’ for help. Type ‘c’ to clear the buffer.
mysql> use employees;
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> insert into employees values (66666666,’1982-10-17′,’Shi’,'Dongliang’,”M”,’2008-06-01′) ;
Query OK, 1 row affected (0.03 sec)
在HA2上查詢,看同步情況:
[root@HA2 data]# mysql
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 7
Server version: 5.0.77 Source distribution
Type ‘help;’ or ‘h’ for help. Type ‘c’ to clear the buffer.
mysql> use employees;
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 employees where emp_no=66666666;
+———-+————+————+———–+——–+————+
| emp_no | birth_date | first_name | last_name | gender | hire_date |
+———-+————+————+———–+——–+————+
| 66666666 | 1982-10-17 | Shi | Dongliang | M | 2008-06-01 |
+———-+————+————+———–+——–+————+
1 row in set (0.04 sec)
不管你使用MyISAM存儲引擎還是InnoDB存儲引擎,通過LVM快照都很容易得到一個一致的MySQL備份。LVM快照備份MySQL數據時,一但鎖定數據庫完成只需幾秒就可以做一個快照備份,釋放表鎖,數據庫便可完全恢復正常訪問,剩下的事情便是壓縮數據備份並拷貝到從庫進行恢復(根據數據大小,這可能需要很長時間,但基本不會再影響主庫了)。
MySQL 使用lvm 快照備份
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章
6:多源複製的實現
shixiazhoujun
2019-02-24 13:04:18
7:基於語句和行復制的優缺點
shixiazhoujun
2019-02-24 13:04:18
詳解MySQL執行原理、邏輯分層、更改數據庫處理引擎
laozhang
2019-02-24 12:43:05
MySQL中InnoDB和MyISAM的差別
jackieban
2019-02-23 13:57:24
SQL mapper文件的審計
橡皮高
2019-02-23 13:51:10
數據庫管理工具 Navicat使用教程:SQL Server中的磁盤加密
Aote222
2019-02-23 13:34:13
Redhat環境MySQL+Apache+PHP環境配置
znlingdong
2019-02-23 13:29:59
MySQL字符集選擇
兩袖春風
2019-02-23 13:28:22
MySQL數據類型之Pk10平臺架設字符串(string)
wojiuyong
2019-02-23 13:25:22
MySQL 的並集、交集和差集 交集和差集類似oracle中minus的用法
zhengfj
2019-02-23 13:13:04
在同一臺機器上運行多個MySQL服務器
zhengfj
2019-02-23 13:13:04
14.5 InnoDB Locking and Transaction Model
shixiazhoujun
2019-02-23 13:04:53
14.4.11 Undo Tablespace
shixiazhoujun
2019-02-23 13:04:53