mysql ——读写分离
生活随笔
收集整理的這篇文章主要介紹了
mysql ——读写分离
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、實驗環境:
server1:master 172.24.54.1
server2:slave 172.25.54.2 主從復制
server3:mysql-proxy代理 172.25.54.3
此實驗在mysql的主從復制的基礎上進行, 需要配備好主從的環境
二、配置server3
[root@server3 ~]# ls mysql-proxy-0.8.5-linux-el6-x86-64bit.tar.gz [root@server3 ~]# tar zxf mysql-proxy-0.8.5-linux-el6-x86-64bit.tar.gz mysql-proxy-0.8.5-linux-el6-x86-64bit mysql-proxy-0.8.5-linux-el6-x86-64bit.tar.gz [root@server3 ~]# mv mysql-proxy-0.8.5-linux-el6-x86-64bit /usr/local/mysql-proxy [root@server3 ~]# cd /usr/local/mysql-proxy/ [root@server3 mysql-proxy]# ls bin include lib libexec licenses share [root@server3 mysql-proxy]# mkdir conf [root@server3 mysql-proxy]# mkdir logs [root@server3 mysql-proxy]# vim ~/.bash_profile [root@server3 mysql-proxy]# cat ~/.bash_profile //修改第10行10 PATH=$PATH:$HOME/bin:/usr/local/mysql-proxy/bin [root@server3 mysql-proxy]# source ~/.bash_profile [root@server3 mysql-proxy]# vim conf/mysql-proxy.conf [mysql-proxy] user=root //運行mysql-proxy進程的用戶 proxy-address=0.0.0.0:3306 //監聽本機所有地址的3306端口 proxy-backend-addresses=172.25.54.1:3306 //backend主 注意addresses proxy-read-only-backend-addresses=172.25.54.2:3306 //backend從 proxy-lua-script=/usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua //lua腳本地址 log-file=/usr/local/mysql-proxy/logs/mysql-proxy //proxy日志路徑 log-level=debug //日志級別 daemon=true //打入后臺 keepalive=true //在mysql-proxy崩潰時嘗試重啟之 [root@server3 mysql-proxy]# chmod 660 conf/mysql-proxy.conf [root@server3 mysql-proxy]# vim /usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua //修改最大最小鏈接數,最大鏈接兩個, 超過則會讀寫分離38 if not proxy.global.config.rwsplit then39 proxy.global.config.rwsplit = {40 min_idle_connections = 1, 41 max_idle_connections = 2,42 43 is_debug = false44 } [root@server3 mysql-proxy]# mysql-proxy --defaults-file=/usr/local/mysql-proxy/conf/mysql-proxy.conf //啟動mysql-proxy [root@server3 mysql-proxy]# netstat -antpl主庫授權
mysql> grant all on *.* to 'root'@'%' identified by '5820hhXM!@#'; Query OK, 0 rows affected, 1 warning (0.37 sec)三、測試
物理機
server1
server2
server1、server2
yum install -y lsof
物理機
此時增加一個客戶端鏈接——客戶端2
//客戶端2 [root@foundation54 ~]# mysql -h 172.25.54.3 -u root -p Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MySQL connection id is 15 Server version: 5.7.17-log MySQL Community Server (GPL)Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.MySQL [(none)]> select * from test.userlist; +----------+----------+ | username | password | +----------+----------+ | user1 | 147 | | user2 | 258 | +----------+----------+ 2 rows in set (0.00 sec)MySQL [(none)]> insert into test.userlist values ('user3','369'); Query OK, 1 row affected (0.15 sec)MySQL [(none)]> select * from test.userlist; +----------+----------+ | username | password | +----------+----------+ | user1 | 147 | | user2 | 258 | | user3 | 369 | +----------+----------+ 3 rows in set (0.00 sec)server1、server2
物理機
此時增加一個客戶端鏈接——客戶端3
//客戶端3 [root@foundation54 ~]# mysql -h 172.25.54.3 -u root -p Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MySQL connection id is 15 Server version: 5.7.17-log MySQL Community Server (GPL)Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.MySQL [(none)]> select * from test.userlist; +----------+----------+ | username | password | +----------+----------+ | user1 | 147 | | user2 | 258 | | user3 | 369 | +----------+----------+ 3 rows in set (0.00 sec)server1、server2
客戶端3是與從庫server2相連接的
在客戶端3上寫入,然后在server1即masetr上訪問
//客戶端3 MySQL [(none)]> insert into test.userlist values ('user4','666'); Query OK, 1 row affected (0.09 sec)MySQL [(none)]> select * from test.userlist; +----------+----------+ | username | password | +----------+----------+ | user1 | 147 | | user2 | 258 | | user3 | 369 | | user4 | 666 | +----------+----------+ 4 rows in set (0.00 sec)server1
雖然客戶端3是與從庫server2相連接的, 但是當在客戶端3寫入時,還是寫入到了master上
yum install -y tcpdump 抓包工具, 可以在proxy端安裝,抓包查看
總結
以上是生活随笔為你收集整理的mysql ——读写分离的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql ——MHA
- 下一篇: 分布式系统——网络监视系统zabbix3