搭建Mysql-proxy實現主從同步讀寫分離

來源:互聯網
上載者:User

標籤:Mysql

Proxy 伺服器 Wg61 192.168.0.180 (Mysql-proxy)
主伺服器 Wg62 192.168.0.142 (負責寫入資料)
從伺服器 Wg63 192.168.0.156 (負責唯讀資料)

實驗思路:

  1. 下載Mysql-proxy,在Proxy 伺服器Wg61上安裝lua語言
  2. Wg61安裝proxy,添加/etc/profile的環境變數參數
  3. 修改proxy設定檔參數,測試讀寫分離
  4. 搭建主從伺服器,建立測試資料庫表並授權使用者存取權限
  5. 啟動Mysql-proxy測試讀寫分離
  6. 測試從伺服器掛掉,主伺服器掛掉如何
    步驟如下:
    1、 Wg61伺服器上安裝lua語言,Mysql-proxy需要lua語言調用
    [[email protected] ~]# yum-y insall lua
    2、 下載Mysql-proxy安裝包到Wg61並解壓到/usr/local/下
    [[email protected] ~]# wget http://dev.mysql.com/get/Downloads/MySQL-Proxy/mysql-proxy-0.8.5-linux-el6-x86-64bit.tar.gz--no-check-certificate
    [[email protected] ~]# tar-xf mysql-proxy-0.8.5-linux-el6-x86-64bit.tar.gz -C /usr/local/
    [[email protected] ~]# cd/usr/local/
    [[email protected] local]# ls
    bin games lib libexec sbin src
    etc include lib64 mysql-proxy-0.8.5-linux-el6-x86-64bit share
    [[email protected] local]#mv mysql-proxy-0.8.5-linux-el6-x86-64bit/ mysql-proxy
    3、 修改環境變數參數
    [[email protected] local]#vim /etc/profile
    最後添加exportPATH=/usr/local/mysql-proxy/bin/:/usr/local/mysql/bin:$PATH
    [[email protected] local]# source!$
    source /etc/profile 對命令生效
    4、 修改Mysql-proxy設定檔參數,測試讀寫分離
    [[email protected] local]#vim /usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua
    40 min_idle_connections = 1, 在41行和42行將最小連結數改成1
    41 max_idle_connections = 1,
    5、 Wg62主伺服器上建立測試檔案,並授權使用者user1存取權限
    mysql>show databases;
    +--------------------+
    |Database |
    +--------------------+
    |information_schema |
    |mysql |
    |test |
    +--------------------+
    3rows in set (0.00 sec)

mysql>create database HK;
QueryOK, 1 row affected (0.00 sec)

mysql>use HK;
Databasechanged
mysql>create table city(id int);
QueryOK, 0 rows affected (0.02 sec)

mysql>insert into city values(123);
QueryOK, 1 row affected (0.00 sec)

mysql>select * from city ;
+------+
|id |
+------+
| 123 |
+------+
1row in set (0.00 sec)

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

6、 Wg63從伺服器上建立測試檔案,並授權使用者user1存取權限
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| test |
+--------------------+
3 rows in set (0.00 sec)

mysql> create database HK;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| HK |
| mysql |
| test |
+--------------------+
4 rows in set (0.00 sec)

mysql> create table city(id int);
ERROR 1046 (3D000): No database selected
mysql> use HK;
Database changed
mysql> create table city(id int);
Query OK, 0 rows affected (0.06 sec)

mysql> insert into city values(456);
Query OK, 1 row affected (0.00 sec)

mysql> select * from city;
+------+
| id |
+------+
| 456 |
+------+
1 row in set (0.00 sec)

mysql> grant allon HK.* to [email protected]‘%‘ identified by‘123456‘;

7、 Wg1伺服器上啟動Mysql-proxy服務
[[email protected] local]#mysql-proxy
--proxy-read-only-backend-addresses=192.168.0.156:3306
--proxy-backend-addresses=192.168.0.142:3306
--proxy-lua-script=/usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua&
[1] 2044
[[email protected] local]#2018-01-04 05:25:31: (critical) plugin proxy 0.8.5 started

另開一個視窗查看服務是否啟動成功:
[[email protected] ~]# lsof -i :4040
COMMAND PIDUSER FD TYPE DEVICE SIZE/OFF NODE NAME
mysql-pro 2044root 9u IPv4 12940 0t0 TCP *:yo-main (LISTEN)
參數說明:
--proxy-read-only-backend-addresses=192.168.0.111:3306 # 定義後端唯讀伺服器
--proxy-backend-addresses=192.168.0.112:3306 #定義後端mysql主伺服器地址,指定mysql寫主伺服器的連接埠
--proxy-lua-script=/usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua& #指定lua指令碼,在這裡,使用的是rw-splitting指令碼,用於讀寫分離
當有多個唯讀伺服器時,可以寫多個以下參數:
--proxy-read-only-backend-addresses=192.168.0.111:3306 # 定義後端唯讀伺服器
--proxy-read-only-backend-addresses=192.168.0.112:3306 # 定義後端唯讀伺服器
#--proxy-address=192.168.0.110:3307指定mysql proxy的監聽連接埠,預設為:4040

8、 測試讀寫功能
(1) 測試寫操作:可以查看到Wg62資料資訊,也可寫入資料,看不到Wg63資料
mysql>select user();
+---------------------+
|user() |
+---------------------+
|[email protected] |
+---------------------+
1row in set (0.00 sec)

mysql>show databases;
+--------------------+
|Database |
+--------------------+
| information_schema|
|HK |
|test |
+--------------------+
3rows in set (0.00 sec)

mysql>use HK;
Readingtable information for completion of table and column names
Youcan turn off this feature to get a quicker startup with -A

Databasechanged
mysql>use HK;
Databasechanged
mysql>show tables;
+--------------+
|Tables_in_HK |
+--------------+
|city |
+--------------+
1row in set (0.00 sec)

mysql>select * from city;
+------+
|id |
+------+
| 123 |
+------+
1row in set (0.00 sec)

mysql>insert into city values(1313);
QueryOK, 1 row affected (0.00 sec)

mysql>select * from city;
+------+
|id |
+------+
| 123 |
| 1313 |
+------+
2rows in set (0.00 sec)
(2) 測試讀資料
從Wg2類比用戶端登入顯示未從伺服器資料,可查看到對應資料資訊
[[email protected] ~]# mysql-uuser1 -p123456 -P4040 -h192.168.0.180
[[email protected] ~]# mysql -uuser1 -p123456 -P4040-h192.168.0.180
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 5.1.73 Source distribution

Copyright (c) 2000, 2013, Oracle and/or itsaffiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporationand/or its
affiliates. Other names may be trademarks of theirrespective
owners.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear thecurrent input statement.

mysql> select * from HK.city;
+------+
| id |
+------+
| 456 |
+------+
1 row in set (0.00 sec)

mysql> Ctrl-C -- exit!
Aborted
[[email protected] ~]# mysql -uuser1 -p123456 -P4040-h192.168.0.180
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.1.73 Source distribution

Copyright (c) 2000, 2013, Oracle and/or itsaffiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporationand/or its
affiliates. Other names may be trademarks of theirrespective
owners.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear thecurrent input statement.

mysql> select * from HK.city;
+------+
| id |
+------+
| 456 |
+------+
1 row in set (0.01 sec)

mysql> select user();
+---------------------+
| user() |
+---------------------+
| [email protected] |
+---------------------+
1 row in set (0.00sec)
插入資料顯示成功,但是查詢發現沒有插入資料
mysql> insert into HK.city values(4545);
Query OK, 1 row affected (0.01 sec)

mysql> select * from HK.city;
+------+
| id |
+------+
| 456 |
+------+
1 row in set (0.00 sec)

mysql> insert into HK.city values(232323);
Query OK, 1 row affected (0.00 sec)

mysql> select from HK.city;
+------+
| id |
+------+
| 456 |
+------+
1 row in set (0.00sec)
9、 配置MYsql主從並實現讀寫分離
(1) 同步Wg2和Wg3兩台伺服器資料資訊
[[email protected] ~]#mysqldump -uroot -p -A > all.sql
[[email protected] ~]# scpall.sql 192.168.0.156:/root/
[[email protected] ~]# mysql-uroot -p123456
mysql> source/root/all.sql
(2) Wg2主伺服器配置為MASTER,並授權user2使用者作為同步處理的使用者
[[email protected] ~]# vim/etc/my.cnf
log-bin=mysql-binlog
binlog-do-db=HK
binlog_format=row
server-id=1
mysql> grant all on
.*to [email protected]‘%‘ identified by ‘123456‘;
重啟資料庫

(3) Wg3從伺服器配置為SLAVE
方法一:[[email protected] ~]#vim /etc/my.cnf
master_host=‘192.168.0.142‘
master_user=‘user2‘
master_password=‘123456‘
方法二:登入資料庫
MySQL>changemaster to master_host=‘192.168.0.142‘,master_user=‘user2‘,master_password=‘123456‘;
重啟資料庫
10、測試通過Mysql-proxy是否實現主從同步讀寫分離
(1) 查看主從是否同步
mysql> select user();
+---------------------+
| user() |
+---------------------+
| [email protected] |
+---------------------+
1 row in set (0.00 sec)

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| HK |
| test |
+--------------------+
3 rows in set (0.00 sec)

mysql> use HK;
Reading table information for completion of table andcolumn names
You can turn off this feature to get a quicker startupwith -A

Database changed
mysql> use HK;
Database changed
mysql> select * from city;
+--------+
| id |
+--------+
| 123 |
| 1313 |
| 4545 |
| 232323 |
+--------+
4 rows in set (0.00 sec)

mysql> insert into city values(789);
Query OK, 1 row affected (0.00 sec)

mysql> select * from city;
+--------+
| id |
+--------+
| 123 |
| 1313 |
| 4545 |
| 232323 |
| 789 |
+--------+
5 rows in set (0.00 sec)
登入主伺服器查看,顯示插入成功
登入從伺服器查看,顯示同步成功
mysql> select user();
+----------------+
| user() |
+----------------+
| [email protected] |
+----------------+
1 row in set (0.00 sec)

mysql> select * from HK.city;
+--------+
| id |
+--------+
| 123 |
| 1313 |
| 4545 |
| 232323 |
| 789 |
+--------+
5 rows in set (0.00sec)
(2) 測試從伺服器宕機(可寫入查看資料)
mysql> insert into HK.city values(888);
Query OK, 1 row affected (0.01 sec)

mysql> select * from HK.city;
+--------+
| id |
+--------+
| 123 |
| 1313 |
| 4545 |
| 232323 |
| 789 |
| 0 |
| 888 |
+--------+

7 rows in set (0.00sec)

總結:1.當停止掉 slave 資料庫,proxy 的查詢就會轉移到 master 上,當把 slave 啟動後,proxy 依然在讀 master,當有新的連結進來的時候才會重新去讀取 slave 的資料。有時可能需要重啟下 mysql-proxy

  1. 從伺服器恢複後,再次同步剛剛所插入的資料
    (3) 測試主伺服器宕機
    mysql> select * from HK.city
    -> ;
    +--------+
    | id |
    +--------+
    | 123 |
    | 1313 |
    | 4545 |
    | 232323 |
    | 789 |
    | 0 |
    | 888 |
    +--------+
    7 rows in set (0.00 sec)

mysql> use HK;
ERROR 2013 (HY000): Lost connection to MySQL serverduring query
mysql> insert into HK.city values(999);
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id: 6
Current database: NONE

ERROR 2013 (HY000):Lost connection to MySQL server during query
顯示只能讀取資料不能寫入資料

搭建Mysql-proxy實現主從同步讀寫分離

聯繫我們

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