今天Lesca將介紹如何備份與還原MySQL的從伺服器,平台仍然是CentOS 7。
使用mysqldump進行備份與還原
使用MYSQLDUMP進行備份
mysqladmin stop-slave -uroot -p
mysqldump --all-databases > fulldb.dump
mysqladmin start-slave -uroot -p
tar -czf /tmp/dbdump.tar.gz ./fulldb.dump ./mysql-relay-log.info
我們除了要備份了整個資料庫的dump以外,還需要備份relay-log.info檔案(上例為mysql-relay-log.info),該檔案包含類似如下資訊:
/var/lib/mysql/mysql-relay-bin.000002
720
mysql-bin.0000023968
紅色高亮部分,指明了當前MySQL主伺服器上二進位日誌的執行狀態。這個資料在還原從伺服器的時候至關重要。
使用MYSQLDUMP進行還原
mysql -uroot -p < /root/dbdump.db
stop slave;
CHANGE MASTER TO MASTER_HOST='192.168.10.201', MASTER_USER='slave_user', MASTER_PASSWORD='abc@DEF', MASTER_LOG_FILE='mysql-bin.000002', MASTER_LOG_POS=3968;
start slave;
show slave status\G
在狀態中,如果有下面兩行,則表示從伺服器工作正常:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
使用資料庫檔案(Raw Data)進行備份與還原
備份資料庫檔案
service mariadb stop
tar --selinux --acls --xattrs -czPf /root/dbbackup.tar.gz /var/lib/mysql/
service mariadb start
注意:紅色參數讓tar同時備份selinux屬性和其他ACL屬性,以防止還原到目標伺服器後無法使用。
還原資料庫檔案
service mariadb stop
tar --selinux --acls --xattrs -xzPf /root/dbbackup.tar.gz -C /
service mariadb start
同時,還原資料檔案的時候,也需要指定這些參數。
故障排查
錯誤訊息
150401 9:58:06 [ERROR] mysqld: File '/var/lib/mysql/mysql-bin.index' not found (Errcode: 13)
150401 9:58:06 [ERROR] Aborting
檢查SELINUX設定
ll -Z mysql-bin.index
-rw-rw----. mysql mysql unconfined_u :o bject_r:var_lib_t:s0 mysql-bin.index
解決方案
可以禁用SeLinux(設定檔/etc/selinux/config),
SELINUX=disabled
修改完後需要重啟。
也可以在tar命令壓縮、解壓縮時添加如下參數:
tar --selinux --acls --xattrs
拓展知識
-selinux – Save the SELinux context to the archive
-acls – Save the ACLs to the archive
-xattrs – Save the user/root xattrs to the archive. It archives all extended attributes, including SELinux and ACLs.