MySQL主從切換

來源:互聯網
上載者:User

標籤:mysql主從   主從切換   

650) this.width=650;" title="Image" style="background-image:none;" border="0" alt="Image" src="http://s3.51cto.com/wyfs02/M00/9A/3D/wKioL1lTUB6QPMCQAABXs-6S6xE233.png" height="472" />

在一主兩從環境中,類比測試當主庫宕機後的切換步驟

我的系統為 CentOS6.7 x64

mysql 版本為 5.5.33 並通過二進位包安裝

上個,三台系統和 mysql 都是同樣的版本,同樣步驟的安裝

[[email protected] ~]# mysql -V

mysql  Ver 14.14 Distrib 5.5.33, for linux2.6 (x86_64) using readline 5.1

[[email protected] ~]# uname -r

2.6.32-573.el6.x86_64


三台環境的主機名稱和 ip 地址,職務

mysql-01   192.168.240.137   Master

mysql-02   192.168.240.138   Slave-01

msyql-03   192.168.240.139   Slave-02


Master上的操作

將 master 192.168.240.137 關機,類比真實情境中的宕機

[[email protected] ~]# /etc/init.d/mysqld stop

Shutting down MySQL... SUCCESS!

[[email protected] ~]# lsof -i :3306

[[email protected] ~]#


Slave-01上的操作

確保 slave 資料庫上已經執行了 relay log 中的全部命令

在 slave 上執行 show processlist; 命令,返回結果如下,說明更新執行完畢

Slave has read all relay log; waiting for the slave I/O thread to update it

在每個從庫上行 stop slave io_thread; 停止 IO 線程

mysql> show processlist;

mysql> stop slave io_thread;


編輯 /etc/my.cnf 檔案,在 [ mysqld ] 目錄下,開啟 log-bin=mysql-bin 選項

並且注意 server-id ,不能和 Slave-02 上的 server-id 相同

[[email protected] ~]# vim /etc/my.cnf

log-bin=mysql-bin

server-id=2


編輯完後儲存退出,重啟 mysql 服務

[[email protected] ~]# /etc/init.d/mysqld restart

Shutting down MySQL. SUCCESS!

Starting MySQL.. SUCCESS!


在 Slave-01 上登入 mysql

執行 reset master 命令,將 Slave-01 升級為 Master

mysql> reset master;

Query OK, 0 rows affected (0.01 sec)

並查看 master 狀態

mysql> show master status\G

*************************** 1. row ***************************

            File: mysql-bin.000001

        Position: 107

    Binlog_Do_DB:

Binlog_Ignore_DB:

1 row in set (0.00 sec)


建立一個用於同步的帳號並重新整理

mysql> grant replication slave on *.* to [email protected]‘%‘ identified by ‘123456‘;

Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;

Query OK, 0 rows affected (0.01 sec)


編輯 /etc/hosts 檔案,添加 ip 地址及對應的主機名稱

(我使用的的 vmware 的 NAT 模式)

[[email protected] ~]# vim /etc/hosts

192.168.240.138   mysql-02

192.168.240.139   mysql-03


在 Slave-02 上的操作

同樣先添加 ip 地址及對應的主機名稱

[[email protected] ~]# vim /etc/hosts

192.168.240.138   mysql-02

192.168.240.139   mysql-03


測試剛才建立的 yuci 使用者能否在 Slave-02 上登入 Slave-01 的資料庫

[[email protected] ~]# mysql -uyuci -p123456 -h192.168.240.138

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 5

。。。。。。。。。。。。

mysql>

yuci 使用者登入成功,說明可以使用該帳號進行資料同步


使用 root 使用者登入 mysql 服務,查看更新是否全部完成

[[email protected] ~]# mysql -uroot -p123456

mysql> show processlist;

Slave has read all relay log; waiting for the slave I/O thread to update it

停止 slave 服務

mysql> stop slave;

Query OK, 0 rows affected (0.01 sec)


輸入主庫資訊以及用於同步的帳號,注意 log-file和log-pos,這兩個值是之前在 Slave-01 上執行 show maste status\G 的傳回值

mysql> CHANGE MASTER TO

    -> MASTER_HOST=‘192.168.240.138‘,

    -> MASTER_PORT=3306,

    -> MASTER_USER=‘yuci‘,

    -> MASTER_PASSWORD=‘123456‘,

    -> MASTER_LOG_FILE=‘mysql-bin.000001‘,

    -> MASTER_LOG_POS=107;

Query OK, 0 rows affected (0.01 sec)


開啟 slave 服務

mysql> start slave;

Query OK, 0 rows affected (0.00 sec)


查看 slave 狀態的 IO、SQL線程,兩個 yes 說明切換完成

mysql> show slave status\G

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

到此為止主從切換的操作已經全部完成,Slave-01 已經升級為 Master 它的從伺服器是 Slave-02


在 Slave-01 上建立一個新的資料庫,看看是否能夠同步

Slave-01 上

mysql> create database tongbu;

Query OK, 1 row affected (0.00 sec)

mysql> show databases;

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

| Database        |

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

| information_schema |

| mysql          |

| performance_schema |

| tongbu        |         

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

5 rows in set (0.00 sec)

Slave-02 上

mysql> show databases;

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

| Database       |

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

| information_schema |

| mysql         |

| performance_schema |

| tongbu        |

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

5 rows in set (0.00 sec)


最後自己來個小總結吧

1.當從伺服器要升職為主伺服器時,要注意 my.cnf 檔案中要開啟 log-bin 參數。主伺服器是必須開啟的,從伺服器可以不開啟

2.IO線程報錯 connecting主要是:網路不通,密碼不對


結束~~~~~

本文出自 “LULU” 部落格,請務必保留此出處http://aby028.blog.51cto.com/5371905/1942706

MySQL主從切換

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.