mysql proxy 主从_【MYSQL知识必知必会】MySQL主从复制读写分离(基于mysql-proxy实现)...

MySQL主从复制读写分离(基于mysql-proxy实现)

http://mirror.bit.edu.cn/mysql/Downloads/MySQL-Proxy/mysql-proxy-0.8.4-linux-glibc2.3-x86-64bit.tar.gz

解压

tar zxvf mysql-proxy-0.8.4-linux-glibc2.3-x86-64bit.tar.gz

创建mysql-proxy帐号并授权

分别在主从数据库中创建mysqlproxy帐号

mysql> grant all on *.* to mysqlproxy@'192.168.64.%' identified by 'mysqlproxy';

mysql> flush privileges;

mysql> use mysql;

mysql> select User,Password,Host from user;

启动mysql-proxy

sudo ./mysql-proxy \

--daemon \

--log-level=debug \

--keepalive \

--log-file=/var/log/mysql-proxy.log \

--plugins="proxy" \

--proxy-backend-addresses="192.168.64.131:3306" \

--proxy-read-only-backend-addresses="192.168.64.132:3306" \

--proxy-lua-script="/home/ubuntu/apps/mysql-proxy-0.8.4/share/doc/mysql-proxy/rw-splitting.lua" \

--plugins="admin" \

--admin-username="admin" \

--admin-password="admin" \

--admin-lua-script="/home/ubuntu/apps/mysql-proxy-0.8.4/lib/mysql-proxy/lua/admin.lua"

查看mysql-proxy进程

ubuntu@s4:~/apps/mysql-proxy-0.8.4/bin$ ps -ef | grep mysql-proxy

root 18249 1 0 02:22 ? 00:00:00 /home/ubuntu/apps/mysql-proxy-0.8.4/libexec/mysql-proxy --daemon --log-level=debug --keepalive --log-file=/var/log/mysql-proxy.log --plugins=proxy --proxy-backend-addresses=192.168.64.131:3306 --proxy-read-only-backend-addresses=192.168.64.132:3306 --proxy-lua-script=/home/ubuntu/apps/mysql-proxy-0.8.4/share/doc/mysql-proxy/rw-splitting.lua --plugins=admin --admin-username=admin --admin-password=admin --admin-lua-script=/home/ubuntu/apps/mysql-proxy-0.8.4/lib/mysql-proxy/lua/admin.lua

root 18250 18249 0 02:22 ? 00:00:00 /home/ubuntu/apps/mysql-proxy-0.8.4/libexec/mysql-proxy --daemon --log-level=debug --keepalive --log-file=/var/log/mysql-proxy.log --plugins=proxy --proxy-backend-addresses=192.168.64.131:3306 --proxy-read-only-backend-addresses=192.168.64.132:3306 --proxy-lua-script=/home/ubuntu/apps/mysql-proxy-0.8.4/share/doc/mysql-proxy/rw-splitting.lua --plugins=admin --admin-username=admin --admin-password=admin --admin-lua-script=/home/ubuntu/apps/mysql-proxy-0.8.4/lib/mysql-proxy/lua/admin.lua

ubuntu 18252 15744 0 02:22 pts/1 00:00:00 grep --color=auto mysql-proxy

查看mysql-proxy端口

ubuntu@s4:~/apps/mysql-proxy-0.8.4/bin$ sudo netstat -ntlp | grep mysql-proxy

tcp 0 0 0.0.0.0:4040 0.0.0.0:* LISTEN 18250/mysql-proxy

tcp 0 0 0.0.0.0:4041 0.0.0.0:* LISTEN 18250/mysql-proxy

4040是proxy端口,4041是admin端口

连接管理端口

mysql> mysql -uadmin -padmin -h192.168.64.131 -P4041 连接管理端口

具体如下

ubuntu@s4:~/apps/mysql-proxy-0.8.4/bin$ mysql -uadmin -padmin -h192.168.64.131 -P4041

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, 2015, 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> show databases;

ERROR 1105 (07000): use 'SELECT * FROM help' to see the supported commands

mysql> SELECT * FROM help;

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

| command | description |

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

| SELECT * FROM help | shows this help |

| SELECT * FROM backends | lists the backends and their state |

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

2 rows in set (0.00 sec)

mysql> SELECT * FROM backends;

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

| backend_ndx | address | state | type | uuid | connected_clients |

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

| 1 | 192.168.64.131:3306 | up | rw | NULL | 0 |

| 2 | 192.168.64.132:3306 | unknown | ro | NULL | 0 |

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

2 rows in set (0.00 sec)

多开几个客户端后其状态变为

mysql> SELECT * FROM backends;

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

| backend_ndx | address | state | type | uuid | connected_clients |

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

| 1 | 192.168.64.131:3306 | up | rw | NULL | 0 |

| 2 | 192.168.64.132:3306 | up | ro | NULL | 0 |

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

2 rows in set (0.00 sec)

state都为up表正常

连接同步端口

mysql> mysql -umysqlproxy -pmysqlproxy -h192.168.64.131 -P4040

多开启几个同步端口,在同步端口连接的客户端中插入和查询数据,观察读写分离。

结论:192.168.64.131:3306只写,192.168.64.132:3306只读。

操作演示

不使用proxy连接数据库,查询192.168.64.131:3306上的数据

mysql> select * from zhang;

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

| id | name | address |

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

| 1 | zhang | this_is_master |

| 1 | zhang | this_is_master |

| 1 | zhang | this_is_master |

| 1 | zhang | this_is_master |

| 1 | zhang | this_is_master |

| 1 | zhang | this_is_master |

| 1 | zhang | this_is_master |

| 1 | zhang | this_is_master |

| 1 | zhang | this_is_master |

| 1 | zhang | this_is_master |

| 1 | zhang | this_is_master |

| 2 | zhang | this_is_master |

| 3 | zhang | this_is_master |

| 4 | zhang | this_is_master |

| 5 | zhang | this_is_master |

| 6 | zhang | this_is_master |

| 7 | zhang | this_is_master |

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

17 rows in set (0.00 sec)

不使用proxy连接数据库,查询192.168.64.132:3306上的数据

mysql> select * from zhang;

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

| id | name | address |

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

| 2 | zhang | this_is_slave |

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

1 row in set (0.00 sec)

使用proxy连接数据库,执行查询和插入操作

ubuntu@s4:~/apps$ mysql -umysqlproxy -pmysqlproxy -h192.168.64.131 -P4040

Welcome to the MySQL monitor. Commands end with ; or \g.

server default db: crm

client default db:

syncronizing

Your MySQL connection id is 45

Server version: 5.5.47-0ubuntu0.12.04.1-log (Ubuntu)

Copyright (c) 2000, 2015, 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> use crm;

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A

Database changed

mysql> select * from zhang;

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

| id | name | address |

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

| 2 | zhang | this_is_slave |

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

1 row in set (0.00 sec)

# 此处数据为192.168.64.132:3306中的数据

mysql> insert into zhang values('8','zhang','this_is_master');

Query OK, 1 row affected (0.00 sec)

# 该数据将插入192.168.64.131:3306数据库中

mysql> select * from zhang;

server default db:

client default db: crm

syncronizing

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

| id | name | address |

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

| 2 | zhang | this_is_slave |

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

1 row in set (0.00 sec)

# 该数据仍来自192.168.64.132:3306中数据

不使用proxy连接192.168.64.131:3306观察数据是否插入

mysql> select * from zhang;

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

| id | name | address |

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

| 1 | zhang | this_is_master |

| 1 | zhang | this_is_master |

| 1 | zhang | this_is_master |

| 1 | zhang | this_is_master |

| 1 | zhang | this_is_master |

| 1 | zhang | this_is_master |

| 1 | zhang | this_is_master |

| 1 | zhang | this_is_master |

| 1 | zhang | this_is_master |

| 1 | zhang | this_is_master |

| 1 | zhang | this_is_master |

| 2 | zhang | this_is_master |

| 3 | zhang | this_is_master |

| 4 | zhang | this_is_master |

| 5 | zhang | this_is_master |

| 6 | zhang | this_is_master |

| 7 | zhang | this_is_master |

| 8 | zhang | this_is_master |

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

18 rows in set (0.00 sec)

由此可见使用mysql-proxy读写分离成功。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/539673.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

SecureCRT防止自动断开

今天在宁波连接上海的linux库,是外网访问内网,使用了nat123这个软件映射的。 发现SecureCRT连接后,过几分钟就自动断开,导致使用SecureCRT做跳转机的其他应用使用起来很不方便。 于是设置了下SecureCRT。

mysql 主主结构_高性能mysql主主架构

(3)配置参数说明server-id:ID值唯一的标识了复制群集中的主从服务器,因此它们必须各不相同。master_id必须为1到232–1之间的一个正整数值,slave_id值必须为2到232–1之间的一个正整数值。log-bin:表示打开binlog,打开该选项才可以…

解决ios编译swift报错pcm was built: mtime changed

问题 编译ios工程失败时,其中的几个swift文件报以下错 /Users/tomes/code/project/xxx.swift File /Users/tomes/Library/Developer/Xcode/DerivedData/Spec-dgyhrnmgvfkjkqbboklnfgrudqip/Build/Products/Debug-iphoneos/xxxx.framework/Headers/xxxx.h has been…

AI工程师职业规划和学习路线完整版

AI工程师职业规划和学习路线完整版 如何成为一名机器学习算法工程师 成为一名合格的开发工程师不是一件简单的事情,需要掌握从开发到调试到优化等一系列能 力,这些能力中的每一项掌握起来都需要足够的努力和经验。而要成为一名合格的机器学习算法工程师&…

oracle 多个with as

主要看多个with的格式 [sql] view plaincopy WITH T3 AS ( SELECT T1.ID, T1.CODE1, T2.DESCRIPTION FROM TB_DATA T1, TB_CODE T2 WHERE T1.CODE1 T2.CODE ), T4 AS ( SELECT T1.ID, T1.CODE2, T2.DESCRIPTION FROM TB_DATA T1, TB_CODE T2 WHERE T1.C…

mysql主键 命中率_mysql主键问题

MySQL主键一. MySQL主键设计原则MySQL主键应当是对用户没有意义的。MySQL主键应该是单列的,以便提高连接和筛选操作的效率(当然复合主键是可以的,只是不建议)永远也不要更新MySQL主键MySQL主键不应包含动态变化的数据,如时间戳、创建时间列、…

Centos7常用命令[挂载文件系统]

Centos7常用命令[挂载文件系统]------------------------------------------------------------------------------# 挂载一个叫做hda2的盘-确定目录/mnt/hda2已经存在[rootlocalhost ~]# mount /dev/hda2 /mnt/hda2# 卸载一个叫做hda2的盘-先从挂载点/mnt/hda2退出[rootlocalh…

hadoop SecondNamenode

一、定义 * The Secondary Namenode is a helper to the primary Namenode. * The Secondary is responsible for supporting periodic checkpoints * of the HDFS metadata. The current design allows only one Secondary * Namenode per HDFs cluster. * The Secondary Nam…

Tensorflow Python API 翻译(sparse_ops)

作者:chen_h 微信号 & QQ:862251340 微信公众号:coderpai 我的博客:请点击这里计划现将 tensorflow 中的 Python API 做一个学习,这样方便以后的学习。 原文链接该章介绍有关稀疏张量的API稀疏张量表示对于多维稀疏…

高性能mysql 小查询_高性能MySql进化论(十一):常见查询语句的优化

总结一下常见查询语句的优化方式1 COUNT1. COUNT的作用 COUNT(table.filed)统计的该字段非空值的记录行数 COUNT(*)或者是COUNT(not nullable field) 统计的是全表的行数如果要是统计全表记录数,COUNT(*)效率会比COUNT(not nullable fie…

ORA-01861: 文字与格式字符串不匹配

select to_date(20160401000000,yyyy-mm-dd) from dual; ---------- 报错:ORA-01861: 文字与格式字符串不匹配 原因:字符串20160401000000与要转换的格式 yyyy-mm-dd 格式不对。 20160401000000 是 yyyymmddhh24miss 格式的 -------- select to_da…

首席架构师徐海峰眼中的架构和出色的架构师

CSDN架构领域编辑采访了一些与会讲师,谈谈他们将在会上分享的内容、相关技术和程序人生,带你领略讲师风采。 本期我们采访的讲师是来自阅文集团首席架构师徐海峰,主要负责内容中心的网站架构和分布式存储、分布式计算工作。10年互联网开发经验…

java socket建立长连接_Java Web项目中使用Socket通信多线程、长连接的方法

很多时候在javaweb项目中我们需要用到Socket通信来实现功能,在web中使用Socket我们需要建立一个监听程序,在程序启动时,启动socket监听。我们的应用场景是在java项目中,需要外接如一个硬件设备,通过tcp通信&#xff0c…

hadoop-eclipse-plugin使用

下载hadoop安装包:http://www.carfab.com/apachesoftware/hadoop/common/hadoop-1.0.2/ 但是没有plugin,我到这个地方下载的:http://ishare.iask.sina.com.cn/f/23642243.html?fromlikecopy到你的eclipse_home的plugins下面。配置map/reduce…

hadoop eclipse plugin windows下载集合

收集了hadoop稳定版本的eclipse plugin for windows。资源分一律为0分 hadoop-eclipse-plugin-1.2.1.jar http://download.csdn.net/detail/zengmingen/9488180 hadoop-eclipse-plugin-2.2.0.jar http://download.csdn.net/detail/zengmingen/9488182 hadoop-eclipse-pl…

java 记事本界面_JAVA/GUI程序之记事本

自上半年JAVA课程结束后,再也没有看过JAVA了,最近不是很忙,又简单的看了看,本博客纯属记录学习过程,请大神们别笑,其中错误是难免的,毕竟是新手写的博客。下面就进入我们的正题吧,复…

104. Maximum Depth of Binary Tree

104. Maximum Depth of Binary Tree 题目 Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 解析 // Maximum Depth of Binary Tree class Solution { publ…

mapper-reducer word count 实例

统计一个文件里各单词的个数,假设这个文件很大。 原理如下图: 编写代码: WCMapper.java package zengmg.hadoop.mr.wordcount;import java.io.IOException;import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; …

java 远程调用url_使用Java的URL/HttpURLConnection进行远程调用(POST请求)

利用Java的HttpURLConnection进行远程url请求(调用远程接口)测试类:请求类型为json,以post方式请求,利用OutputStream写入数据实体类:public class User implementsSerializable {privateString name;privateString password;publicString ge…

LindDotNetCore~职责链模式的应用

回到目录 职责链模式 它是一种设计模块,主要将操作流程与具体操作解耦,让每个操作都可以设置自己的操作流程,这对于工作流应用是一个不错的选择! 下面是官方标准的定义:责任链模式是一种设计模式。在责任链模式里&…