设为首页 收藏本站
查看: 416|回复: 0

[经验分享] MySQL读写分离

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-11-26 08:33:08 | 显示全部楼层 |阅读模式
准备工作:

useradd mysql-proxy -s /sbin/nologin -M
cd tools
wget http://mirrors.sohu.com/mysql/My ... .3-x86-64bit.tar.gz
tar xf mysql-proxy-0.8.5-linux-glibc2.3-x86-64bit.tar.gz
mv mysql-proxy-0.8.5-linux-glibc2.3-x86-64bit /application/mysql-proxy
echo "export PATH=/application/mysql-proxy/bin/:$PATH" >>/etc/profile
tail -1 /etc/profile
source /etc/profile
which mysql-proxy
cd /application
chown -R mysql-proxy.root mysql-proxy


mysql-proxy命令参数:
[iyunv@test-A ~]# mysql-proxy --help
Usage:
mysql-proxy [OPTION...] - MySQL Proxy
                           
--help-all                                                   Show all help options
--help-proxy                                              Show options for the proxy-module

Application Options:
-V, --version                                              Show version
--defaults-file=<file>                                configuration file
--verbose-shutdown                                 Always log the exit code when shutting down
--daemon                                                  Start in daemon-mode
--user=<user>                                          Run mysql-proxy as user
--basedir=<absolute path>                      Base directory to prepend to relative paths in the config
--pid-file=<file>                                        PID file in case we are started as daemon
--plugin-dir=<path>                                 path to the plugins
--plugins=<name>                                    plugins to load
--log-level=(error|warning|info|message|debug)          log all messages of level ... or higher
--log-file=<file>                                        log all messages in a file
--log-use-syslog                                        log all messages to syslog
--log-backtrace-on-crash                          try to invoke debugger on crash
--keepalive                                                 try to restart the proxy if it crashed
--max-open-files                                        maximum number of open files (ulimit -n)
--event-threads                                          number of event-handling threads (default: 1)
--lua-path=<...>                                         set the LUA_PATH
--lua-cpath=<...>                                       set the LUA_CPATH



master操作:
mysql> grant all on *.* to 'mysql-proxy'@'192.168.0.%' identified by '123456';
mysql> select user,host from mysql.user;

slave操作:
mysql> grant all on *.* to 'mysql-proxy'@'192.168.0.%' identified by '123456';
mysql> select user,host from mysql.user;



vi /etc/init.d/mysql-proxy  #编辑mysql-proxy管理脚本
#mysql-proxy.scripts,2015-11.25,linux
############################################
#!/bin/sh
export LUA_PATH=/application/mysql-proxy/share/doc/mysql-prox
y/?.lua:/applicaton/mysql-proxy/lib/mysql-proxy/lua/?.lua

mode=$1
if [ -z "$mode" ] ; then
  mode="start"
fi

case $mode in
  'start')
mysql-proxy --daemon \
--log-level=debug \
--user=mysql-proxy \
--keepalive \
--log-file=/var/log/mysql-proxy.log \
--plugins="proxy" \
#mysql-proxy.scripts

  mode="start"
fi

case $mode in
  'start')
mysql-proxy --daemon \
--log-level=debug \
--user=mysql-proxy \
--keepalive \
--log-file=/var/log/mysql-proxy.log \
--plugins="proxy" \
--proxy-backend-addresses=10.0.0.134:3306 \
--proxy-read-only-backend-addresses=10.0.0.111:3306 \
--plugins="admin" \
--admin-username="admin" \
--admin-password="admin" \
    ;;

  'stop')
    killall mysql-proxy
    ;;

  'restart')
    if $0 stop ; then
      $0 start
    else
      echo  "retart failed!!!"
      exit 1
    fi
    ;;
esac

exit 0
####################################################
chmod 700 /etc/init.d/mysql-proxy
/etc/init.d/mysql-proxy start

[iyunv@test-A application]# netstat -tunlp|grep mysql-proxy
tcp        0      0 0.0.0.0:4040                0.0.0.0:*                   LISTEN      4022/mysql-proxy   
tcp        0      0 0.0.0.0:4041                0.0.0.0:*                   LISTEN      4022/mysql-proxy   
#4040是proxy端口;4041是admin端口,也就是管理

[iyunv@test-C ~]# mysql -umysql-proxy -p -h 192.168.0.149 --port 4040
Enter password:   #多开几个终端,测试连接
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 32
Server version: 5.5.32-log MySQL Community Server (GPL)

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

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> select user,host from mysql.user;   


[iyunv@test-C ~]# mysql -uadmin -p -h 192.168.0.149 --port 4041
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.0.99-agent-admin

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

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> select * from backends;         
+-------------+--------------------+---------+------+------+-------------------+
| backend_ndx | address            | state   | type | uuid | connected_clients |
+-------------+--------------------+---------+------+------+-------------------+
|           1 | 192.168.0.150:3306 | up      | rw   | NULL |                 0 |
|           2 | 192.168.0.151:3307 | unknown | ro   | NULL |                 0 |
+-------------+--------------------+---------+------+------+-------------------+
2 rows in set (0.00 sec)
注意:因为rw-splitting.lua脚本默认有4个链接才启用分离;所以多开启几个终端;多测试几下;你也可以去修改里面的相关值

mysql> select * from backends;
+-------------+--------------------+-------+------+------+-------------------+
| backend_ndx | address            | state | type | uuid | connected_clients |
+-------------+--------------------+-------+------+------+-------------------+
|           1 | 192.168.0.150:3306 | up    | rw   | NULL |                 0 |
|           2 | 192.168.0.151:3307 | up    | ro   | NULL |                 0 |
+-------------+--------------------+-------+------+------+-------------------+
2 rows in set (0.00 sec)
#出现两个up,代表读写分离成功。

[iyunv@test-A ~]# lsof -i:4040
COMMAND    PID        USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
mysql-pro 4062 mysql-proxy   10u  IPv4 156977      0t0  TCP *:yo-main (LISTEN)
mysql-pro 4062 mysql-proxy   12u  IPv4 160117      0t0  TCP test-A:yo-main->test-C:53173 (ESTABLISHED)
mysql-pro 4062 mysql-proxy   16u  IPv4 160132      0t0  TCP test-A:yo-main->192.168.0.254:56915 (ESTABLISHED)
mysql-pro 4062 mysql-proxy   17u  IPv4 160060      0t0  TCP test-A:yo-main->test-B:50068 (ESTABLISHED)
mysql-pro 4062 mysql-proxy   18u  IPv4 160145      0t0  TCP test-A:yo-main->192.168.0.254:56916 (ESTABLISHED)
mysql-pro 4062 mysql-proxy   19u  IPv4 159920      0t0  TCP test-A:yo-main->192.168.0.254:56914 (ESTABLISHED)

分离成功,哈哈!


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-143624-1-1.html 上篇帖子: MySQL单双实例安装 下篇帖子: mysql主主复制+keepalived高可用(使用VIP访问数据库提示不允许连接)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表