MySQL讀寫分離

來源:互聯網
上載者:User

標籤:主從   box   font   port   客戶   insert   從伺服器   軟體   serve   

MySQL主從複製+代理:客戶機訪問Proxy伺服器,讀、 寫請求交給Proxy識別,從而降低了用戶端程式的複雜度。

【192.168.4.10,192.168.4.11分別作為MySQL主、從伺服器,是整個服務的後端

 另一台192.168.4.15作為MySQLProxy 伺服器】


一、部署mysql-proxyProxy 伺服器

[[email protected] ~]# rpm -ivh maxscale-2.1.2-1.rhel.7.x86_64.rpm 

1、修改設定檔

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

 18 [server1]

 19 type=server

 20 address=192.168.4.11                           主

 21 port=3306

 22 protocol=MySQLBackend

 23 

 24 [server2]

 25 type=server  

 26 address=192.168.4.12                          從

 27 port=3306

 28 protocol=MySQLBackend

 39 servers=server1,server2                     監控兩台伺服器

 40 user=scalemon         定義監控使用者

 41 passwd=123456         定義密碼

 42 monitor_interval=10000         毫米

 53 #[Read-Only Service]         (53-59)注釋

 54 #type=service

 55 #router=readconnroute

 56 #servers=server1

 57 #user=myuser

 58 #passwd=mypwd

 59 #router_options=slave

 64 [Read-Write Service]

 65 type=service

 66 router=readwritesplit

 67 servers=server1,server2        添加使用者

 68 user=maxscale        添加許可權對mysql庫有讀許可權

 69 passwd=123456

 70 max_slave_connections=100%

 76 [MaxAdmin Service]        定義軟體的管理服務

 77 type=service

 78 router=cli

 86 #[Read-Only Listener]                (86-90)注釋

 87 #type=listener

 88 #service=Read-Only Service

 89 #protocol=MySQLClient

 90 #port=4008

 94 service=Read-Write Service定義的服務

 96 port=4006                定義的讀寫連接埠號碼

 98 [MaxAdmin Listener]        定義的監聽

 99 type=listener

100 service=MaxAdmin Service

101 protocol=maxscaled

102 socket=default

103 port=4009                定義的管理連接埠


二、配置讀寫伺服器(只在主伺服器授權即可 從伺服器會自動同步)

添加授權使用者

  mysql> grant replication slave,replication client

    -> on *.*

    -> to

    -> [email protected]"%"

    -> identified by "111111";


建立路由使用者

 mysql> grant select on mysql.* 

       ->to 

       ->[email protected]'%' 

       ->identified by “111111”; 


建立用戶端訪問使用者 

mysql> grant all  on *.*  to  [email protected]'%' identified by “111111”



三、在Proxy 伺服器訪問本機管理服務

測試

[[email protected] ~]# mysql -h192.168.4.11 -uscalemon  -p111111

[[email protected] ~]# mysql -h192.168.4.12 -uscalemon  -p111111


啟動服務

[[email protected] box ~]# maxscale -f /etc/maxscale.cnf 


[[email protected] ~]# maxadmin -uadmin -pmariadb -P4009

MaxScale> list servers     (監視伺服器的狀態)

Servers.

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

Server             | Address         | Port  | Connections | Status              

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

server1            | 192.168.4.11    |  3306 |           0 | Master, Running

server2            | 192.168.4.12    |  3306 |           0 | Slave, Running

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


用戶端訪問

mysql -h192.168.4.15 -P4006 -uyaya -p111111


MySQL [(none)]> select @@hostname;

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

| @@hostname |讀

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

| mysql-12   |

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


插入新紀錄

MySQL [(none)]> insert  into bbsdb.a values(111)


用戶端當前訪問的是從資料庫伺服器,仍然能夠插入紀錄。則表示成功


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.