標籤:mysql 主從同步 innobackupex
工作用MySQL資料庫配置了主從同步,但是不知道為啥同步失效了。工作環境不能隨便操作,我就在本地上搭建了主從同步的環境。
備份用的工具是Xtrabackup,安裝及使用教程見《innobackupex實現MySQL資料庫的備份與恢複》。
1、資料庫環境
主庫(Master):192.168.126.150
從庫(Slave):192.168.126.151
資料庫版本:5.5.32
2、修改my.cnf檔案
1)修改主伺服器master:
#vi /etc/my.cnf
[mysqld]
log-bin=/u01/app/mysql/log/mysql-bin #二進位日誌必須啟用
server-id=150 //[必須]伺服器唯一ID,預設是1,一般取IP最後一段
2)修改從伺服器slave:
#vi /etc/my.cnf
[mysqld]
log-bin=/u01/app/mysql/log/mysql-bin //[不是必須]啟用二進位日誌
server-id=151 //[必須]伺服器唯一ID,預設是1,一般取IP最後一段
3、重啟主庫資料庫 /etc/init.d/mysql restart
4、在主伺服器上建立帳戶並授權slave:
#mysql -uroot -p
mysql>GRANT REPLICATION SLAVE ON *.* to ‘rep‘@‘192.168.126.151‘ identified by ‘asdfg123‘;
5、備份主要資料庫
innobackupex --defaults-file=/u01/app/mysql/my.cnf --user=root --password=***** --port=3306 --socket=/u01/app/mysql/run/mysql.sock /u01/app/mysql/backup/
6、將備份拷貝到從伺服器上
tar czvf 2016-12-18_15-08-41.tar.gz 2016-12-18_15-08-41/
scp 2016-12-18_15-08-41.tar.gz [email protected]:/u01/app/mysql/backup/
7、在備庫上執行還原(資料庫關閉狀態,data檔案夾為空白)
innobackupex --defaults-file=/u01/app/mysql/my.cnf --user=root --password=hwj3509 --use-memory=100m --apply-log /u01/app/mysql/backup/2016-12-18_15-08-41
innobackupex --defaults-file=/u01/app/mysql/my.cnf --user=root --password=hwj3509 --copy-back /u01/app/mysql/backup/2016-12-18_15-08-41
#修改data檔案夾許可權
chown -R mysql:dba /u01/app/mysql/data
啟動從資料庫
/etc/init.d/mysql start
8、查看xtrabackup_binlog_info資訊
cat /u01/app/mysql/backup/2016-12-18_15-08-41/xtrabackup_binlog_info
mysql-bin.000005460
9、配置從伺服器Slave:
mysql>change master to master_host=‘192.168.126.150‘,master_user=‘mysync‘,master_password=‘asdfg123‘,
master_log_file=‘mysql-bin.000005‘,master_log_pos=460;
Mysql>start slave; //啟動從伺服器複製功能
#停止同步:stop slave;
#重設同步:reset slave;
10、檢查從伺服器複製功能狀態:
#Slave_IO_Running和Slave_SQL_Running必須為Yes
show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.126.150
Master_User: mysync
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000007
Read_Master_Log_Pos: 277
Relay_Log_File: relaylog.000004
Relay_Log_Pos: 423
Relay_Master_Log_File: mysql-bin.000007
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: 277
Relay_Log_Space: 718
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: 150
11、驗證主從同步
在主庫上執行增刪改操作,驗證從庫上是否與主庫一致。
本文出自 “三國冷笑話” 部落格,請務必保留此出處http://myhwj.blog.51cto.com/9763975/1883754
Mysql 主從同步配置