MHA實現mysql主從資料庫手動切換的方法
本文例項講述了MHA實現mysql主從資料庫手動切換的方法,分享給大家供大家參考。具體方法如下:
一、準備工作
1、分別在Master和Slave執行如下,方便mha檢查複製:
複製程式碼 程式碼如下:grant all privileges on *.* to 'root'@'10.1.1.231' identified by 'rootpass';
grant all privileges on *.* to 'root'@'10.1.1.234' identified by 'rootpass';
grant replication slave on *.* to 'jpsync'@'10.1.1.231' identified by 'jppasswd';
grant replication slave on *.* to 'jpsync'@'10.1.1.234' identified by 'jppasswd';
flush privileges;
2、將master設定為只讀
複製程式碼 程式碼如下:mysql> set global read_only=1;
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like 'read_only';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| read_only | ON |
+---------------+-------+
1 row in set (0.00 sec)
互動模式:
複製程式碼 程式碼如下:#masterha_master_switch --master_state=alive --conf=/etc/masterha/app1.cnf --new_master_host=10.1.1.231 --new_master_port=63306
或非互動模式:
複製程式碼 程式碼如下:#masterha_master_switch --master_state=alive --conf=/etc/masterha/app1.cnf --new_master_host=10.1.1.231 --new_master_port=63306 —interactive=0
二、切換完以後,如何讓10.1.1.231為主,10.1.1.234為從,操作步驟:
1、主上執行:
複製程式碼 程式碼如下:mysql> show master status;
+-------------------------+----------+--------------+--------------------------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------------+----------+--------------+--------------------------------------+-------------------+
| mysql-master-bin.000013 | 120 | denovo_ng | mysql,denovo,test,information_schema | |
+-------------------------+----------+--------------+--------------------------------------+-------------------+
1 row in set (0.00 sec)
2、在10.1.1.234上執行如下sql命令;
複製程式碼 程式碼如下:change master to master_host='10.1.1.231',master_port=63306,master_user='jpsync',
master_password='jppasswd', master_log_file='mysql-master-bin.000013',master_log_pos=120;
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.1.1.231
Master_User: jpsync
Master_Port: 63306
Connect_Retry: 60
Master_Log_File: mysql-master-bin.000013
Read_Master_Log_Pos: 120
Relay_Log_File: compute-0-52-relay-bin.000002
Relay_Log_Pos: 290
Relay_Master_Log_File: mysql-master-bin.000013
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
3、檢視master狀態,並測試
複製程式碼 程式碼如下:mysql> show slave hosts;
+-----------+------+-------+-----------+--------------------------------------+
| Server_id | Host | Port | Master_id | Slave_UUID |
+-----------+------+-------+-----------+--------------------------------------+
| 1052 | | 63306 | 1025 | e25a3e4a-39c0-11e4-80cb-00259086c4b6 |
+-----------+------+-------+-----------+--------------------------------------+
1 row in set (0.00 sec)
主庫10.1.1.231上插入記錄
複製程式碼 程式碼如下:mysql> insert into test_slave_002 values(555551111,1,55555,99999,44.11,2222,91919);
Query OK, 1 row affected (0.00 sec)
從庫查詢記錄已經存在
複製程式碼 程式碼如下:mysql> select * from test_slave_002 where id=555551111;
+-----------+-----+-----------+--------------+----------+----------------+--------------+
| id | tag | ticket_id | candidate_id | duration | source_file_id | source_start |
+-----------+-----+-----------+--------------+----------+----------------+--------------+
| 555551111 | 1 | 55555 | 99999 | 44.11 | 2222 | 91919 |
+-----------+-----+-----------+--------------+----------+----------------+--------------+
1 row in set (0.00 sec)
4、更新配置檔案:
更新主庫my.cnf配置新增
複製程式碼 程式碼如下:skip_slave_start
注意:防止重啟資料庫,啟動slave程式,導致資料不一致。
更新從庫my.cnf配置新增,設定slave庫為只讀:
複製程式碼 程式碼如下:read_only=1
relay_log_purge=0
然後重啟主庫和從庫,觀察庫的資訊:
主庫資訊:
複製程式碼 程式碼如下:mysql> show processlist;
+----+--------+------------------+------+-------------+------+-----------------------------------------------------------------------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+--------+------------------+------+-------------+------+-----------------------------------------------------------------------+------------------+
| 1 | jpsync | 10.1.1.234:49085 | NULL | Binlog Dump | 17 | Master has sent all binlog to slave; waiting for binlog to be updated | NULL |
| 2 | root | localhost | NULL | Query | 0 | init | show processlist |
+----+--------+------------------+------+-------------+------+-----------------------------------------------------------------------+------------------+
2 rows in set (0.00 sec)
mysql> show master status;
+-------------------------+----------+--------------+--------------------------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------------+----------+--------------+--------------------------------------+-------------------+
| mysql-master-bin.000014 | 120 | denovo_ng | mysql,denovo,test,information_schema | |
+-------------------------+----------+--------------+--------------------------------------+-------------------+
1 row in set (0.00 sec)
從庫資訊:
複製程式碼 程式碼如下:mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.1.1.231
Master_User: jpsync
Master_Port: 63306
Connect_Retry: 60
Master_Log_File: mysql-master-bin.000014
Read_Master_Log_Pos: 120
Relay_Log_File: compute-0-52-relay-bin.000005
Relay_Log_Pos: 290
Relay_Master_Log_File: mysql-master-bin.000014
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
mysql> show processlist;
+----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------------+------------------+
| 1 | system user | | NULL | Connect | 58 | Waiting for master to send event | NULL |
| 2 | system user | | NULL | Connect | 58 | Slave has read all relay log; waiting for the slave I/O thread to update it | NULL |
| 3 | root | localhost | NULL | Query | 0 | init | show processlist |
+----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------------+------------------+
3 rows in set (0.00 sec)
希望本文所述對大家的MySQL資料庫程式設計有所幫助。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/2041/viewspace-2805300/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Mysql MHA部署-06手動切換MySql
- MySQL MHA部署 Part 7 MHA手動切換測試MySql
- ostgreSQL主從切換-手動SQL
- MHA+MySQL主從配置實現MySQL高可用MySql
- Dledger是如何實現主從自動切換的
- mysql資料庫實現主從複製MySql資料庫
- 手工切換MySQL主從MySql
- Linux實現MySql資料庫的主從複製(一主一從)LinuxMySql資料庫
- 生產環境中mysql資料庫由主從關係切換為主主關係MySql資料庫
- Django切換MySQL資料庫DjangoMySql資料庫
- 【mysql】mysql的資料庫主從(一主一從)MySql資料庫
- MySQL資料庫實現高可用架構之MHA的實戰MySql資料庫架構
- Mongodb資料同步和主從切換MongoDB
- linux下mysql主從複製,實現資料庫同步LinuxMySql資料庫
- MYSQL5.6主從+keepalive高可用自動切換MySql
- mysql資料庫互為主從配置方法分享MySql資料庫
- 資料庫讀寫分離,主從同步實現方法資料庫主從同步
- mysql主從資料庫配置MySql資料庫
- [資料庫]MYSQL主從同步資料庫MySql主從同步
- mysql資料庫的主從複製和主主複製實踐MySql資料庫
- WPF手動實現切換頁面
- 基於Docker實現MySQL的主從複製和SpringBoot2+MyBatis的動態切換資料來源的讀寫分離DockerMySqlSpring BootMyBatis
- (7)資料庫讀寫分離,主從同步實現方法(資料庫設定)資料庫主從同步
- StoneDB 主從切換實踐方案
- Mysql MHA部署-02主從複製MySql
- 8、MySQL主從資料庫配置MySql資料庫
- 直播帶貨系統原始碼,實現MYSQL資料庫的主從同步原始碼MySql資料庫主從同步
- Spring-Boot 多資料來源配置+動態資料來源切換+多資料來源事物配置實現主從資料庫儲存分離Springboot資料庫
- Redis主從切換Redis
- HeyUI元件庫 | 如何實現線上切換主題UI元件
- 實現自動切換主題的 VSCode 擴充套件VSCode套件
- 如何在MySQL資料庫中使用use來切換資料庫?MySql資料庫
- MySQL高可用之MHA切換測試(switchover & failover)MySqlAI
- MongoDB主從模式手動強制同步資料方法MongoDB模式
- MySQL 主從切換延時高問題分析MySql
- 解決MySQL的主從資料庫沒有同步的兩種方法MySql資料庫
- Mysql(Mariadb)資料庫主從複製MySql資料庫
- 一個月後,我們又從 MySQL 雙主切換成了主 - 從!MySql