mysql主從搭建切換
環境:
172.30.249.156 percona5.6.27 主機名:tr-mysql 跑著生產環境的應用的虛擬機器
192.168.129.153 percona5.6.27 主機名:trcloud-gtt-db 新申請的物理機
工作內容:給生產環境資料庫搭建主從架構,並將主庫放到新申請的物理機上,連續停機不能超過10分鐘
大致流程:搭建主從-->主備切換-->測試結果,因為生產環境資料大概有100多G,考慮到停機時間和效率的問題,恢復備庫使用Xtrabackup方式備份恢復,不使用mysqldump和冷備的方式
一:搭建主從
1、建立複製賬號在Master的資料庫中建立一個備份帳戶:每個slave使用標準的MySQL使用者名稱和密碼連線master。進行復制操作的使用者會授予REPLICATION SLAVE許可權。
命令如下:
mysql> create user 'replicat'@'192.168.129.153' identified by 'passw';Query OK, 0 rows affected (0.03 sec)
mysql> grant replication slave,super on *.* to 'replicat'@'192.168.129.153';
Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
建立一個帳戶backup,並且只能允許從10.211.55.5這個地址上來登陸,密碼是passw。
2、配置master、slave引數
主庫:
開啟二進位制日誌、指定唯一server ID、
[root@localhost data]# vi /etc/my.cnf
……
log-bin=/data/DB/mysql/mysql-bin
server-id=1
…….
備庫:
開啟二進位制日誌、指定唯一server ID
[root@localhost mysql]# vi /etc/my.cnf
……
log-bin=/data/DB/mysql/mysql-bin
server-id=2
…….
3、複製並恢復備庫資料
主庫備份:
[root@tr-mysql backup]# xtrabackup --backup --user=root --datadir=/data/DB/mysql/ --target-dir=/data/backup/20160628bak/
........
160628 09:55:17 Executing UNLOCK TABLES
160628 09:55:17 All tables unlocked
160628 09:55:17 Backup created in directory '/data/backup/20160628bak/'
160628 09:55:17 [00] Writing backup-my.cnf
160628 09:55:17 [00] ...done
160628 09:55:18 [00] Writing xtrabackup_info
160628 09:55:18 [00] ...done
xtrabackup: Transaction log of lsn (1696091795) to (1696091805) was copied.
160628 09:55:18 completed OK!
[root@tr-mysql backup]# xtrabackup --prepare --target-dir=/data/backup/20160628bak/ --讓備份資料一致
[root@tr-mysql backup]# scp 20160628bak/* root@192.168.129.153:/data/backup/20160628bak/
備庫恢復:
[root@trcloud-gtt-db ~]# rsync -avrp /data/backup/20160628bak/ /data/DB/mysql/
........
sent 6426459950 bytes received 259 bytes 197737237.20 bytes/sec
total size is 6425674808 speedup is 1.00
[root@trcloud-gtt-db~ ]# chown -R mysql:mysql /data/DB/mysql
4、主庫準備測試資料
mysql> use test;
mysql> create table id(id int(10));
Query OK, 0 rows affected (1.02 sec)
mysql> insert into id values(1);
Query OK, 1 row affected (0.14 sec)
5、設定複製程式
檢視master當前binlog檔案
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 | 834 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
slave建立複製程式
mysql> change master to master_host='172.30.249.156',
-> master_user='replicat',
-> master_password='passw',
-> master_log_file='mysql-bin.000002',
-> master_log_pos=0;
6、啟動slave,檢視程式
啟動
mysql> start slave;
Query OK, 0 rows affected (0.03 sec)
檢視slave程式
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.30.249.156
Master_User: replicat
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 834
Relay_Log_File: mysqld-relay-bin.000002
Relay_Log_Pos: 997
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: 834
Relay_Log_Space: 1171
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
Master_UUID: 7d7ee32c-26cd-11e6-8fe2-fa163ecb3078
Master_Info_File: /data/DB/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
1 row in set (0.00 sec)
在這裡主要是看:
Slave_IO_Running=Yes
Slave_IO_Running=Yes
Slave_SQL_Running=Yes
slave的I/O和SQL執行緒都已經開始執行,而且Seconds_Behind_Master不再是NULL。日誌的位置增加了,意味著一些事件被獲取並執行了。如果你在master上進行修改,你可以在slave上看到各種日誌檔案的位置的變化,同樣,你也可以看到資料庫中資料的變化。
檢視master程式
mysql> show processlist\G;
........
*************************** 3. row ***************************
Id: 4
User: canal
Host: 172.30.248.95:35966
db: NULL
Command: Binlog Dump
Time: 11821
State: Master has sent all binlog to slave; waiting for binlog to be updated
Info: NULL
Rows_sent: 0
Rows_examined: 0
........
*************************** 5. row ***************************
Id: 12
User: replicat
Host: 192.168.129.153:51948
db: NULL
Command: Binlog Dump
Time: 614
State: Master has sent all binlog to slave; waiting for binlog to be updated
Info: NULL
Rows_sent: 0
Rows_examined: 0
7、測試結果
slave上檢視建立的表和資料都有了
mysql> select * from id;
+------+
| id |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
在master上修改資料
mysql> update id set id=2 where id =1;
Query OK, 1 row affected (0.08 sec)
Rows matched: 1 Changed: 1 Warnings: 0
檢視slave資料
mysql> select * from id;
+------+
| id |
+------+
| 2 |
+------+
1 row in set (0.00 sec)
測試透過
二:主從切換
1、在從庫(新的主伺服器)上建立使用者
mysql> create user 'replicat'@'172.30.249.156' identified by 'passw';
Query OK, 0 rows affected (0.03 sec)
mysql> grant replication slave,super on *.* to 'replicat'@'172.30.249.156';
Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
2、從庫(新的主伺服器) 操作
mysql> STOP SLAVE IO_THREAD;
Query OK, 0 rows affected (0.03 sec)
mysql> SHOW PROCESSLIST;
+----+-----------------+-----------+------+---------+------+-----------------------------------------------------------------------------+------------------+-----------+---------------+
| Id | User | Host | db | Command | Time | State | Info | Rows_sent | Rows_examined |
+----+-----------------+-----------+------+---------+------+-----------------------------------------------------------------------------+------------------+-----------+---------------+
| 1 | event_scheduler | localhost | NULL | Daemon | 2238 | Waiting on empty queue | NULL | 0 | 0 |
| 5 | root | localhost | test | Query | 0 | init | SHOW PROCESSLIST | 0 | 0 |
| 7 | system user | | NULL | Connect | 809 | Slave has read all relay log; waiting for the slave I/O thread to update it | NULL | 0 | 0 |
+----+-----------------+-----------+------+---------+------+-----------------------------------------------------------------------------+------------------+-----------+---------------+
確保狀態為:has read all relay log
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 172.30.249.156
Master_User: replicat
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 1523
Relay_Log_File: mysqld-relay-bin.000002
Relay_Log_Pos: 1686
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: No
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: 1523
Relay_Log_Space: 1860
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: NULL
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
Master_UUID: 7d7ee32c-26cd-11e6-8fe2-fa163ecb3078
Master_Info_File: /data/DB/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
3、從庫變主庫
mysql> STOP SLAVE;
Query OK, 0 rows affected (0.03 sec)
mysql> RESET MASTER;
Query OK, 0 rows affected (0.12 sec)
mysql> RESET SLAVE;
Query OK, 0 rows affected (0.11 sec)
mysql> show master status \G
*************************** 1. row ***************************
File: mysql-bin.000009
Position: 120
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)
4、主庫變從庫
mysql> RESET MASTER;
Query OK, 0 rows affected (3.36 sec)
mysql> RESET SLAVE;
Query OK, 0 rows affected (0.00 sec)
設定slave程式
mysql> change master to master_host='192.168.129.153',
-> master_user='replicat',
-> master_password='passw',
-> master_log_file='mysql-bin.000009',
-> master_log_pos=120;
啟動slave程式
mysql> start slave;
5、修改引數並重啟
將從庫變成只讀
修改現在的從庫引數
read-only =1
innodb_read_only =1
super_read_only =1 #(擁有super許可權也無法修改)
重啟
[root@tr-mysql mysql]# service mysql restart
6、檢查主從是否都正常
主庫
SHOW PROCESSLIST;
show master status \G
從庫
SHOW PROCESSLIST;
start slave;
show slave status \G
7、測試資料
主庫
mysql> update id set id=1 where id=2;
Query OK, 1 row affected (0.02 sec)
Rows matched: 1 Changed: 1 Warnings: 0
從庫
mysql> select * from id;
+------+
| id |
+------+
| 1 |
+------+
切換成功
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29989552/viewspace-2121159/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- MySQL主從切換MySql
- 手工切換MySQL主從MySql
- mysql for linux主從切換MySqlLinux
- mysql主從複製+主備切換MySql
- Redis主從切換Redis
- 5.6 MySql主從自動切換指令碼MySql指令碼
- mysql mha 主從自動切換 高可用MySql
- MySQL 主從切換延時高問題分析MySql
- Redis sentinel主從切換Redis
- 一個月後,我們又從 MySQL 雙主切換成了主 - 從!MySql
- mysql主從搭建MySql
- ORACLE 12C DATAGUARD環境搭建和主從切換Oracle
- 實戰dataguard主從切換
- nginx keepalived 主從切換Nginx
- ostgreSQL主從切換-手動SQL
- Mysql+keepalived主主切換薦MySql
- MYSQL5.6主從+keepalive高可用自動切換MySql
- mysql 5.7+keepalived主從切換步驟簡述MySql
- MYSQL主從搭建5.6.38MySql
- keepalived配置redis主從切換Redis
- StoneDB 主從切換實踐方案
- MySQL 主備庫切換記錄MySql
- mysql MHA搭建和切換測試MySql
- Docker 方式 MySQL 主從搭建DockerMySql
- mysql主從複製搭建MySql
- MySQL(14)---Docker搭建MySQL主從複製(一主一從)MySqlDocker
- REDIS主從頻繁切換事件排查Redis事件
- Mongodb資料同步和主從切換MongoDB
- Redis叢集的主從切換研究Redis
- oracle 11g datagurd主從切換Oracle
- Mysql 5.6 Master和Slave 主備切換MySqlAST
- MHA實現mysql主從資料庫手動切換的方法MySql資料庫
- MySQL從庫切換成主庫後的relay-bin問題MySql
- MYSQL5.6.40原始碼安裝 主從搭建 主主搭建MySql原始碼
- keepalived與mysql主主叢集自動切換MySql
- 生產環境中mysql資料庫由主從關係切換為主主關係MySql資料庫
- mysql雙主雙從 搭建配置MySql
- 新環境搭建Mysql主從MySql