快速備份與還原MySQL從伺服器步驟

來源:互聯網
上載者:User

今天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.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.