MySQL主從切換

Image

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

個人系統爲 CentOS6.7 x64linux

mysql 版本爲 5.5.33 並經過二進制包安裝sql

上個截圖,三臺系統和 mysql 都是一樣的版本,一樣步驟的安裝數據庫

[root@mysql-01 ~]# mysql -Vvim

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

[root@mysql-01 ~]# uname -r網絡

2.6.32-573.el6.x86_64ide


三臺環境的主機名和 ip 地址,職務測試

mysql-01   192.168.240.137   Masterspa

mysql-02   192.168.240.138   Slave-01

msyql-03   192.168.240.139   Slave-02


Master上的操做

將 master 192.168.240.137 關機,模擬真實場景中的宕機

[root@mysql-01 ~]# /etc/init.d/mysqld stop

Shutting down MySQL... SUCCESS!

[root@mysql-01 ~]# lsof -i :3306

[root@mysql-01 ~]#


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 相同

[root@mysql-02 ~]# vim /etc/my.cnf

log-bin=mysql-bin

server-id=2


編輯完後保存退出,重啓 mysql 服務

[root@mysql-02 ~]# /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 yuci@'%' 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 模式)

[root@mysql-02 ~]# vim /etc/hosts

192.168.240.138   mysql-02

192.168.240.139   mysql-03


在 Slave-02 上的操做

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

[root@mysql-03 ~]# vim /etc/hosts

192.168.240.138   mysql-02

192.168.240.139   mysql-03


測試剛纔建立的 yuci 用戶可否在 Slave-02 上登陸 Slave-01 的數據庫

[root@mysql-03 ~]# 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 服務,查看更新是否所有完成

[root@mysql-03 ~]# 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主要是:網絡不通,密碼不對


結束~~~~~

相關文章
相關標籤/搜索