mysqlbackup線上配置Mysql主從架構

dbhelper發表於2014-12-04
  1. 前言:

    MYSQL資料庫很多情況下都是用於搭建電商平臺,電商平臺就意味著為公司賺錢的平臺,必須24小時線上的;我們也搭建了屬於自己的電商平臺,但是最近反應需要增加一臺從庫來緩解主庫的讀取壓力。網上百度了很多相關的方法,總結如下:

  • mysqldump搭建,該方法很簡單,但是操作的過程中需要鎖表,並停止應用。該方法適合系統未上線時操作,新手可以搭建用於學習;(http://blog.itpub.net/12679300/viewspace-1315062/)
  • Xtrabackup搭建,該方法需要了解Xtrabackup工具的備份還原,可以實現線上搭建主從架構。

mysql一直推薦的企業版的mysql備份工具:mysqlbackup,抱著學習的心態就在測試環境中透過mysqlbakcup工具來搭建主從架構

 

在進行以下操作之前,需要先進行以下兩個設定:主庫和備庫的引數檔案已經修改完成、在主庫上面建立主從連線使用者

以下是詳細的整理步驟:

資料庫

主機名

IP地址

同步使用者

備份位置

主資料庫

Mysql01

192.168.47.152

server01

/backup

從資料庫

Mysql02

192.168.47.151

 

 2 主庫的操作步驟
2.1 對主庫進行全備,指令碼如下

mysqlbackup --user=root --password --backup-dir=/backup backup-and-apply-log

備份的目錄為/backup,請確認這個目錄的存在;

 

2.2 記錄這個時候主庫的binlog狀態

mysql> show master status;

+------------------+----------+--------------+------------------+

| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |

+------------------+----------+--------------+------------------+

| mysql-bin.000003 | 107 | | |

+------------------+----------+--------------+------------------+

1 row in set (0.00 sec)

 

2.3 為了驗證主從是沒有問題的,這個時候可以往主庫的某個資料庫插入資料,然後再記錄狀態

mysql> show master status;

+------------------+----------+--------------+------------------+

| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |

+------------------+----------+--------------+------------------+

| mysql-bin.000003 | 500 | | |

+------------------+----------+--------------+------------------+

1 row in set (0.00 sec)

 

2.4 把備份從主庫複製到從庫

[root@mysql01 backup]# service iptables stop

iptables:清除防火牆規則: [確定]

iptables:將鏈設定為政策 ACCEPT:filter [確定]

iptables:正在解除安裝模組: [確定]

root@mysql01 backup]# scp bak.tar root@mysql02:/backup/*

Warning: Permanently added the RSA host key for IP address '192.168.47.151' to the list of known hosts.

root@mysql02's password:

bak.tar 100% 69MB 23.1MB/s 00:03

需要先關閉防火牆

 
3.從庫的操作步驟
3.1 進行從庫的恢復

[root@mysql01 backup]# mysqlbackup --defaults-file=/backup/server-my.cnf --datadir=/data/mysql --backup-dir=/backup/ copy-back

MySQL Enterprise Backup version 3.11.0 Linux-3.8.13-16.2.1.el6uek.x86_64-x86_64 [2014/08/26]

Copyright (c) 2003, 2014, Oracle and/or its affiliates. All Rights Reserved.

   

mysqlbackup: INFO: Starting with following command line ...

mysqlbackup --defaults-file=/backup/server-my.cnf --datadir=/data/mysql

--backup-dir=/backup/ copy-back

   

mysqlbackup: INFO:

IMPORTANT: Please check that mysqlbackup run completes successfully.

At the end of a successful 'copy-back' run mysqlbackup

prints "mysqlbackup completed OK!".

   

141118 16:19:35 mysqlbackup: INFO: MEB logfile created at /backup/meta/MEB_2014-11-18.16-19-35_copy_back.log

   

--------------------------------------------------------------------

Server Repository Options:

--------------------------------------------------------------------

datadir = /data/mysql

innodb_data_home_dir = /data/mysql

innodb_data_file_path = ibdata1:10M:autoextend

innodb_log_group_home_dir = /data/mysql/

innodb_log_files_in_group = 2

innodb_log_file_size = 5242880

innodb_page_size = Null

innodb_checksum_algorithm = none

   

--------------------------------------------------------------------

Backup Config Options:

--------------------------------------------------------------------

datadir = /backup/datadir

innodb_data_home_dir = /backup/datadir

innodb_data_file_path = ibdata1:10M:autoextend

innodb_log_group_home_dir = /backup/datadir

innodb_log_files_in_group = 2

innodb_log_file_size = 5242880

innodb_page_size = 16384

innodb_checksum_algorithm = none

   

mysqlbackup: INFO: Creating 14 buffers each of size 16777216.

141118 16:19:35 mysqlbackup: INFO: Copy-back operation starts with following threads

        1 read-threads 1 write-threads

mysqlbackup: INFO: Could not find binlog index file. If this is online backup then server may not have started with --log-bin.

        Hence, binlogs will not be copied for this backup. Point-In-Time-Recovery will not be possible.

141118 16:19:35 mysqlbackup: INFO: Copying /backup/datadir/ibdata1.

141118 16:19:37 mysqlbackup: INFO: Copying the database directory 'john'

141118 16:19:37 mysqlbackup: INFO: Copying the database directory 'mysql'

141118 16:19:37 mysqlbackup: INFO: Copying the database directory 'performance_schema'

141118 16:19:37 mysqlbackup: INFO: Completing the copy of all non-innodb files.

141118 16:19:37 mysqlbackup: INFO: Copying the log file 'ib_logfile0'

141118 16:19:37 mysqlbackup: INFO: Copying the log file 'ib_logfile1'

141118 16:19:39 mysqlbackup: INFO: Creating server config files server-my.cnf and server-all.cnf in /data/mysql

141118 16:19:39 mysqlbackup: INFO: Copy-back operation completed successfully.

141118 16:19:39 mysqlbackup: INFO: Finished copying backup files to '/data/mysql'

 

3.2 進行授權,並開啟資料庫

[root@mysql02 data]# chmod -R 777 mysql

[root@mysql02 data]# service mysqld start

Starting MySQL [確定]

 

3.3 從庫連線到主庫

mysql> CHANGE MASTER TO MASTER_HOST='mysql01',MASTER_USER='server01',MASTER_PASSWORD='server01', MASTER_LOG_FILE='mysql-bin.000003',MASTER_LOG_POS=107;

 

3.4 啟動從庫服務

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: mysql01

Master_User: server01

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: mysql-bin.000005

Read_Master_Log_Pos: 1082

Relay_Log_File: mysql02-relay-bin.000003

Relay_Log_Pos: 253

Relay_Master_Log_File: mysql-bin.000005

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

Replicate_Do_DB: john

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: 1082

Relay_Log_Space: 1532

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)

 

3.5 經過以上檢查主從同步已經沒有問題了,需要再驗證相應的表資料;

mysql> select * from wzq;

+-------+

| name |

+-------+

| qiang |

| wu |

| zhi |

| 1 |

| 2 |

| 3 |

+-------+

8 rows in set (0.00 sec)

  1. 總結過驗證透過msyqlbackup搭建主從的資料庫,可以實現不停機進行搭建,整個過程也是挺簡單的,但是是在測試環境中進行的,真正的生產環境還要再考慮詳細的步驟。

*********************************************************************************************************************

本文作者:JOHN QQ:1916066696 (請備註資料庫)

ORACLE技術部落格:ORACLE 獵人筆記 http://blog.itpub.net/12679300/

請掃描加微訊號!

********************************************************************************************************************

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/8494287/viewspace-1356130/,如需轉載,請註明出處,否則將追究法律責任。

相關文章