码迷,mamicode.com
首页 > 数据库 > 详细

mysql 实现读写分离详解

时间:2016-07-20 19:52:10      阅读:471      评论:0      收藏:0      [点我收藏+]

标签:password   mysql   local   where   

GRANT ALL PRIVILEGES ON *.* TO ‘root‘@‘%‘ WITH GRANT OPTION;

FLUSH PRIVILEGES;

use mysql
update user set password=PASSWORD(‘mysql‘) where User=‘root‘;
FLUSH PRIVILEGES;


主从库上全做
--------------------------------------------------------------------
yum install -y gcc*
rpm -ivh Atlas-2.2.1.el5.x86_64.rpm   【rpm包直接安装】
rpm -ql Atlas 【查看安装路径】
grep -v ‘^#‘ /usr/local/mysql-proxy/conf/test.cnf  |grep -v ‘^$‘  > /usr/local/mysql-proxy/conf/lipengfei  【过滤垃圾提示符】
mv test.cnf test.cnf.old
cat /usr/local/mysql-proxy/conf/lipengfei > /usr/local/mysql-proxy/conf/test.cnf
rm -rf /usr/local/mysql-proxy/conf/lipengfei

/usr/local/mysql-proxy/bin/encrypt mysql           【mysql是我的密码,把密码加密】
TWbz0dlu35U=

vi /usr/local/mysql-proxy/conf/test.cnf

[mysql-proxy]
plugins = admin,proxy
admin-username=admin
admin-password=admin
admin-lua-script = /usr/local/mysql-proxy/lib/mysql-proxy/lua/admin.lua
proxy-backend-addresses = 192.168.8.100:3306
proxy-read-only-backend-addresses = 192.168.8.101:3306,192.168.8.102:3306
pwds = root:TWbz0dlu35U=
daemon = true
keepalive = true
event-threads = 8
log-level = message
log-path = /usr/local/mysql-proxy/log
instance = test
proxy-address = 0.0.0.0:1234
admin-address = 0.0.0.0:5678
charset = utf8

查看监听端口
netstat -tanlp | grep mysql

开启
/usr/local/mysql-proxy/bin/mysql-proxyd test start
/usr/local/mysql-proxy/bin/mysql-proxyd test stop

登录管理
mysql -h 192.168.8.103 -P 5678 -u admin -padmin
 
正常通过代理操作
mysql -h 192.168.8.103 -P 1234 -u root -pmysql

SHOW VARIABLES LIKE ‘server_id‘;

mysql> select * from help;
+-------------------+---------------------------------------+
| command                  | description                                               |
+-------------------+---------------------------------------+
| SELECT * FROM help         | shows this help                                         |
| SELECT * FROM backends     | lists the backends and their state                      |
| SET OFFLINE $backend_id   | offline backend server, $backend_id is backend_ndx‘s id  |
| SET ONLINE $backend_id     | online backend server, ...                              |
| ADD MASTER $backend        | example: "add master 127.0.0.1:3306", ...               |
| ADD SLAVE $backend         | example: "add slave 127.0.0.1:3306", ...                |
| REMOVE BACKEND $backend_id | example: "remove backend 1", ...                        |
| ADD CLIENT $client         | example: "add client 192.168.1.2", ...                  |
| REMOVE CLIENT $client      | example: "remove client 192.168.1.2", ...               |
| SAVE CONFIG                | save the backends to config file                        |
+-------------------+---------------------------------------+
10 rows in set (0.00 sec)
 
mysql> select * from backends;
+-------------+-------------------+-------+------+
| backend_ndx       |           address        | state    |  type   |
+-------------+-------------------+-------+------+
|           1      | 192.168.0.42:3306         | down     | rw      |
|           2      | 192.168.0.44:3306         | down     | ro      |
|           3      | 192.168.0.43:3306         | down     | ro      |
+-------------+-------------------+-------+------+
3 rows in set (0.00 sec)


5678端口:是Atlas的管理接口,此接口提供了查看mysql状态,设置上线、下线后端mysql服务器
访问方法使用mysql命令

mysql -h 10.142.132.50 -P2345 -u admin -p‘admin‘
这里的用户名密码是上面配置文件中的admin-username = admin、admin-password = admin
下面是管理接口的使用帮助
SELECT * FROM help;  # 查看帮助信息
SELECT * FROM backends; # 查看后端主机状态
SET OFFLINE $backend_id  # 下线一台主机,$backend_id == SELECT * FROM backends;查出来的ID
SET ONLINE $backend_id   # 上线一台主机
ADD MASTER $backend      # 添加一台master节点,如: ADD MASTER 10.57.1.131;
ADD SLAVE $backend       # 添加一台slave节点,如 : ADD SLAVE 10.57.1.132;
REMOVE BACKEND $backend_id # 删除一个节点,


自动读写分离挺好,但有时候我写完马上就想读,万一主从同步延迟怎么办?
A: SQL语句前增加 /*master*/ 就可以将读请求强制发往主库

---------------------------------------------------------------------

将atlas设置成服务,并实现开机自动启动

# 说明:作者仅在CentOS7.0上进行了测试,而且没有进行可靠性和稳定性的测试!
cp /tmp/atlas /etc/rc.d/init.d/atlas
chmod +x /etc/rc.d/init.d/atlas
chkconfig --add atlas
chkconfig atlas on

#!/bin/sh
#description: atlas autorun
#your instance name
instance_name=test
case "$1" in
    "start")
        /usr/local/mysql-proxy/bin/mysql-proxyd $instance_name start
        ;;
    "stop")
        /usr/local/mysql-proxy/bin/mysql-proxyd $instance_name stop
       ;;
    "restart")
        /usr/local/mysql-proxy/bin/mysql-proxyd $instance_name restart
      ;;
    "status")
        /usr/local/mysql-proxy/bin/mysql-proxyd $instance_name status
       ;;
    *)
  echo "Usage: service atlas start/stop/restart/status "                             
       ;;
  esac
 
使用命令如下:
service atlas start
service atlas stop
service atlas status
service atlas restart 

----------------------------------------------------

监控atlas脚本
cat /usr/local/mysql-proxy/bin/monitor_atlas.sh
#!/bin/bash
if  [ $(ps -ef |grep ‘mysql-proxy‘ |grep -v ‘grep‘ |wc -l) -eq "0" ];then
    /usr/local/mysql-proxy/bin/mysql-proxyd test start
    sleep 5
    if [ $(ps -ef |grep ‘mysql-proxy‘ |grep -v ‘grep‘ |wc -l) -eq "0"  ];then
        killall -9 keepalived
        service network restart
    fi
fi


chmod +x /usr/local/mysql-proxy/bin/monitor_atlas.sh

------------------------配置文件说明------------------------
[mysql-proxy]
#管理接口的用户名,根据自己需要定义
admin-username = admin
#管理接口的密码
admin-password = password
#Atlas后端连接的MySQL主库的IP和端口,可设置多项,用逗号分隔
proxy-backend-addresses = 10.57.1.131:3306
#Atlas后端连接的MySQL从库的IP和端口,@后面的数字代表权重,用来作负载均衡,若省略则默认为1,可设置多项,用逗号分隔
proxy-read-only-backend-addresses = 10.57.1.132:3306@1
#用户名与其对应的加密过的MySQL密码,密码使用PREFIX/bin目录下的加密程序encrypt加密,下行的user1和user2为示例,将其替换为你的MySQL的用户名和加密密码!
pwds = root:YQsbJY4G4oc=,test:YQsbJY4G4oc=
# 需要注意的是,这两个用户要在MySQL-Master上创建并开放对应的访问权限,这里的用户是需要对外开放使用的用户
#设置Atlas的运行方式,设为true时为守护进程方式,设为false时为前台方式,一般开发调试时设为false,线上运行时设为true
daemon = true
#设置Atlas的运行方式,设为true时Atlas会启动两个进程,一个为monitor,一个为worker,monitor在worker意外退出后会自动将其重启,设为false时只有worker,没有monitor,一般开发调试时设为false,线上运行时设为true
keepalive = true
#工作线程数,对Atlas的性能有很大影响,可根据情况适当设置
event-threads = 8
#日志级别,分为message、warning、critical、error、debug五个级别
log-level = message
#日志存放的路径
log-path = /usr/local/mysql-proxy/log
#SQL日志的开关,可设置为OFF、ON、REALTIME,OFF代表不记录SQL日志,ON代表记录SQL日志,REALTIME代表记录SQL日志且实时写入磁盘,默认为OFF
#sql-log = OFF
#实例名称,用于同一台机器上多个Atlas实例间的区分
#instance = test
#Atlas监听的工作接口IP和端口
proxy-address = 0.0.0.0:3306
#Atlas监听的管理接口IP和端口
admin-address = 0.0.0.0:2345
#连接池的最小空闲连接数,可根据业务请求量大小适当调大或调小
min-idle-connections = 128 
#分表设置,此例中person为库名,mt为表名,id为分表字段,3为子表数量,可设置多项,以逗号分隔,若不分表则不需要设置该项
#tables = person.mt.id.3
#默认字符集,设置该项后客户端不再需要执行SET NAMES语句
charset = utf8
#允许连接Atlas的客户端的IP,可以是精确IP,也可以是IP段,以逗号分隔,若不设置该项则允许所有IP连接,否则只允许列表中的IP连接
client-ips = 127.0.0.1, 10.0.0
#Atlas前面挂接的LVS的物理网卡的IP(注意不是虚IP),若有LVS且设置了client-ips则此项必须设置,否则可以不设置
#lvs-ips = 192.168.1.1
# End


本文出自 “一起来吧” 博客,请务必保留此出处http://daxionglaiba.blog.51cto.com/11790757/1828103

mysql 实现读写分离详解

标签:password   mysql   local   where   

原文地址:http://daxionglaiba.blog.51cto.com/11790757/1828103

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!