#####################################
Mysql复制的作用和原理
mysql-5.5实现主从复制
mysql-5.6主从复制和Mysql_Proxy的实现
mysql-5.6基于amoeba实现读写分离
#####################################
Mysql复制的作用和原理
复制的作用
辅助实现备份
高可用
异地容灾
scale out:分摊负载
MySQL路由:读写请求交由代理服务器,工作在应用层可以理解SQL语句,从而实现读写分离,再结合LVS、HAPROXY等均摊读的负载。
为了让主服务器不会因为从服务器太多产生多线程复制导致服务器压力过大,使用多级复制。
复制原理
复制流程如下:
Master数据发生改变之后写入二进制日志并通过dump线程复制到Slave。
Slave本地启用I/0线程负责接受Master端dump线程发来的日志并保存至本地中继日志中。
Slave通过启用本地SQL线程将中继日志中那些复制而来的事件重放。
relaylog(中继日志)
mysql日志传输默认是异步传输
mysql支持一主多从
mysql半同步意为只要其中一个从服务器返回确认就认为同步成功
可以使用mysql从服务器做冷备份,配合主服务器上的二进制日志做即时点还原
mysql多级复制从服务器可以记录二进制日志,否则一般情况下不需要记录二进制日志
mysql-5.5实现主从复制
结构图如下:
安装mysql
mkdir -pv /mydata/data useradd -r mysql chown -R mysql.mysql /mydata/data tar xf mysql-5.5.28-linux2.6-i686.tar.gz -C /usr/local cd /usr/local/ ln -sv mysql-5.5.28-linux2.6-i686 mysql cd mysql chown -R root.mysql ./* scripts/mysql_install_db --user=mysql --datadir=/mydata/data/ cp support-files/my-large.cnf /etc/my.cnf cp support-files/mysql.server /etc/rc.d/init.d/mysqld chkconfig --add mysqld vim /etc/profile.d/mysql.sh export PATH=$PATH:/usr/local/mysql/bin . /etc/profile.d/mysql.sh
Master配置
#####修改配置文件 vim /etc/my.cnf datadir = /mydata/data log-bin=master-bin log-bin-index=master-bin.index innodb_file_per_table = 1 binlog_format=mixed server-id = 1 sync_binlog = 1 innodb_flush_logs_at_trx_commit = 1 #####开启Slave权限用户 mysql> GRANT REPLICATION SLAVE ON *.* TO 'repluser'@'192.168.1.%' IDENTIFIED BY 'replpass'; Query OK, 0 rows affected (0.00 sec) mysql> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.00 sec)
Slave配置
#####修改配置文件vim /etc/my.cnf datadir = /mydata/data relay-log = relay-log relay-log-index = relay-log.index read-only = ON server-id = 11 skip_slave_start = 1 read_only = 1 #####查看主日志文件和事件位置 mysql> show master status; +-------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +-------------------+----------+--------------+------------------+ | master-bin.000001 | 340 | | | +-------------------+----------+--------------+------------------+ #####开启slave复制功能 mysql> CHANGE MASTER TO MASTER_HOST='192.168.1.131',MASTER_USER='repluser', MASTER_PASSWORD='replpass',MASTER_LOG_FILE='master-bin.000001',MASTER_LOG_POS=340; #####查看slave状态,可得出master二进制日志和slave中继日志的事件和位置对应未必同意所以在slave备份的时候应该记录主服务器的二进制日志,只有二进制日志才可以完成即使点还原 Slave_IO_Running: No Slave_SQL_Running: No 均为yes代表slave复制正常 #####查看复制状态 mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Master_Host: 192.168.1.131 Master_User: repluser Master_Port: 3306 Connect_Retry: 60 Master_Log_File: master-bin.000001 Read_Master_Log_Pos: 340 Relay_Log_File: relay-log.000001 Relay_Log_Pos: 4 Relay_Master_Log_File: master-bin.000001 Slave_IO_Running: No Slave_SQL_Running: No Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 340 #重放到的位置 Relay_Log_Space: 107 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: NULL Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 0 #####开启复制进程 mysql> START SLAVE;
测试
#####Master mysql> create database soulboy; Query OK, 1 row affected (0.00 sec) #####Slave mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | soulboy | | test | +--------------------+ 5 rows in set (0.00 sec)
mysql-5.6主从复制和Mysql_Proxy的实现
复制功能的改进
支持多线程复制,事实上是针对每个database开启相应的独立线程。即每个库有一个单独的(sql thread)如果线上业务中,只有一个database或者绝大多数压力集中在个别database的话,多线程并发复制特性就没有意义了。
支持启用GTID,对运维人员来说应该是一件令人高兴的事情,在配置主从复制,传统的方式里,你需要找到binlog和POS点,然后change master to指向,而不是很有经验的运维,往往会将其找错,造成主从同步复制报错,在mysql5.6里,无须再知道binlog和POS点,需要知道master的IP、端口,账号密码即可,因为同步复制是自动的,mysql通过内部机制GTID自动找点同步。
结构图如下:
Mysql-5.6主从复制功能的实现步骤
Master配置(安装步骤省略)
#####主节点配置文件 /etc/my.cnf [mysqld] binlog-format=ROW log-bin=master-bin log-slave-updates=true gtid-mode=on enforce-gtid-consistency=true master-info-repository=TABLE relay-log-info-repository=TABLE sync-master-info=1 slave-parallel-workers=2 binlog-checksum=CRC32 master-verify-checksum=1 slave-sql-verify-checksum=1 binlog-rows-query-log_events=1 server-id=1 report-port=3306 port=3306 datadir=/mydata/data socket=/tmp/mysql.sock report-host=192.168.1.201 #####配置完成重启mysql服务,确保gitd_mode为打开状态 mysql> SHOW GLOBAL VARIABLES LIKE '%gtid%'; +--------------------------+-------+ | Variable_name | Value | +--------------------------+-------+ | enforce_gtid_consistency | ON | | gtid_executed | | | gtid_mode | ON | | gtid_owned | | | gtid_purged | | +--------------------------+-------+ #####Slave用户授权 mysql> GRANT REPLICATION SLAVE ON *.* TO 'repluser'@'192.168.1.%' IDENTIFIED BY 'replpass'; Query OK, 0 rows affected (0.00 sec) mysql> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.00 sec)
Slave节点配置(安装步骤省略)
#####Mysql主配置文件 /etc/my.cnf [mysqld] binlog-format=ROW log-slave-updates=true gtid-mode=on enforce-gtid-consistency=true master-info-repository=TABLE relay-log-info-repository=TABLE sync-master-info=1 slave-parallel-workers=2 binlog-checksum=CRC32 master-verify-checksum=1 slave-sql-verify-checksum=1 binlog-rows-query-log_events=1 server-id=11 report-port=3306 port=3306 log-bin = mysql-bin.log datadir=/mydata/data socket=/tmp/mysql.sock report-host=192.168.1.202 #####Slave连接Master服务器 mysql> CHANGE MASTER TO MASTER_HOST='192.168.1.201',MASTER_USER='repluser',MASTER_PASSWORD='replpass',MASTER_AUTO_POSITION=1; Query OK, 0 rows affected, 2 warnings (0.01 sec) #####启动两个线程Slave_IO_Running和Slave_SQL_Running mysql> start slave; Query OK, 0 rows affected, 1 warning (0.01 sec)
测试主从复制成功
#####Master mysql> create database soulboy; Query OK, 1 row affected (0.00 sec) #####Slave mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | soulboy | | test | +--------------------+ 5 rows in set (0.01 sec)
Mysql-Proxy的实现
安装mysql-proxy
useradd -r mysql-proxy tar xf mysql-proxy-0.8.3-linux-glibc2.3-x86-64bit.tar.gz -C /usr/local/ cd /usr/local/ ln -sv mysql-proxy-0.8.3-linux-glibc2.3-x86-64bit mysql-pro cd mysql-proxy vi /etc/profile.d/mysql-proxy.sh export PATH=$PATH:/usr/local/mysql-proxy/bin . /etc/profile.d/mysql-proxy.sh
主服务器创建远程用户
mysql> GRANT ALL ON *.* TO 'root'@'192.168.%.%' IDENTIFIED BY 'redhat'; Query OK, 0 rows affected (0.00 sec) mysql> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.00 sec)
为mysql-proxy提供SysV服务脚本
#!/bin/bash # # mysql-proxy This script starts and stops the mysql-proxy daemon # # chkconfig: - 78 30 # processname: mysql-proxy # description: mysql-proxy is a proxy daemon for mysql # Source function library. . /etc/rc.d/init.d/functions prog="/usr/local/mysql-proxy/bin/mysql-proxy" # Source networking configuration. if [ -f /etc/sysconfig/network ]; then . /etc/sysconfig/network fi # Check that networking is up. [ ${NETWORKING} = "no" ] && exit 0 # Set default mysql-proxy configuration. ADMIN_USER="admin" ADMIN_PASSWD="admin" ADMIN_LUA_SCRIPT="/usr/local/mysql-proxy/share/doc/mysql-proxy/admin.lua" PROXY_OPTIONS="--daemon" PROXY_PID=/var/run/mysql-proxy.pid PROXY_USER="mysql-proxy" # Source mysql-proxy configuration. if [ -f /etc/sysconfig/mysql-proxy ]; then . /etc/sysconfig/mysql-proxy fi RETVAL=0 start() { echo -n $"Starting $prog: " daemon $prog $PROXY_OPTIONS --pid-file=$PROXY_PID --proxy-address="$PROXY_ADDRESS" --user=$PROXY_USER --admin-username="$ADMIN_USER" --admin-lua-script="$ADMIN_LUA_SCRIPT" --admin-password="$ADMIN_PASSWORD" RETVAL=$? echo if [ $RETVAL -eq 0 ]; then touch /var/lock/subsys/mysql-proxy fi } stop() { echo -n $"Stopping $prog: " killproc -p $PROXY_PID -d 3 $prog RETVAL=$? echo if [ $RETVAL -eq 0 ]; then rm -f /var/lock/subsys/mysql-proxy rm -f $PROXY_PID fi } # See how we were called. case "$1" in start) start ;; stop) stop ;; restart) stop start ;; condrestart|try-restart) if status -p $PROXY_PIDFILE $prog >&/dev/null; then stop start fi ;; status) status -p $PROXY_PID $prog ;; *) echo "Usage: $0 {start|stop|restart|reload|status|condrestart|try-restart}" RETVAL=1 ;; esac exit $RETVAL #####将上述内容保存为/etc/rc.d/init.d/mysql-proxy,给予执行权限,而后加入到服务列表。 # chmod +x /etc/rc.d/init.d/mysql-proxy # chkconfig --add mysql-proxy
Mysql-Proxy的远程管理依赖于lua脚本
#####vi /usr/local/mysql-proxy/share/doc/mysql-proxy/admin.lua vi /usr/local/mysql-proxy/share/doc/mysql-proxy/admin.lua function set_error(errmsg) proxy.response = { type = proxy.MYSQLD_PACKET_ERR, errmsg = errmsg or "error" } end function read_query(packet) if packet:byte() ~= proxy.COM_QUERY then set_error("[admin] we only handle text-based queries (COM_QUERY)") return proxy.PROXY_SEND_RESULT end local query = packet:sub(2) local rows = { } local fields = { } if query:lower() == "select * from backends" then fields = { { name = "backend_ndx", type = proxy.MYSQL_TYPE_LONG }, { name = "address", type = proxy.MYSQL_TYPE_STRING }, { name = "state", type = proxy.MYSQL_TYPE_STRING }, { name = "type", type = proxy.MYSQL_TYPE_STRING }, { name = "uuid", type = proxy.MYSQL_TYPE_STRING }, { name = "connected_clients", type = proxy.MYSQL_TYPE_LONG }, } for i = 1, #proxy.global.backends do local states = { "unknown", "up", "down" } local types = { "unknown", "rw", "ro" } local b = proxy.global.backends[i] rows[#rows + 1] = { i, b.dst.name, -- configured backend address states[b.state + 1], -- the C-id is pushed down starting at 0 types[b.type + 1], -- the C-id is pushed down starting at 0 b.uuid, -- the MySQL Server's UUID if it is managed b.connected_clients -- currently connected clients } end elseif query:lower() == "select * from help" then fields = { { name = "command", type = proxy.MYSQL_TYPE_STRING }, { name = "description", type = proxy.MYSQL_TYPE_STRING }, } rows[#rows + 1] = { "SELECT * FROM help", "shows this help" } rows[#rows + 1] = { "SELECT * FROM backends", "lists the backends and their state" } else set_error("use 'SELECT * FROM help' to see the supported commands") return proxy.PROXY_SEND_RESULT end proxy.response = { type = proxy.MYSQLD_PACKET_OK, resultset = { fields = fields, rows = rows } } return proxy.PROXY_SEND_RESULT end
为服务脚本提供配置文件
#####vim /etc/sysconfig/mysql-proxy # Options for mysql-proxy ADMIN_USER="admin" ADMIN_PASSWORD="admin" ADMIN_ADDRESS="" ADMIN_LUA_SCRIPT="/usr/local/mysql-proxy/share/doc/mysql-proxy/admin.lua" PROXY_ADDRESS="0.0.0.0:3306" PROXY_USER="mysql-proxy" PROXY_OPTIONS="--daemon --log-level=info --log-file="/var/log/mysql-proxy.log" --plugins=proxy --plugins=admin --proxy-backend-addresses=192.168.1.201:3306 --proxy-read-only-backend-addresses=192.168.1.202:3306 --proxy-lua-script=/usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua"
mysql-proxy的配置选项
mysql-proxy的配置选项大致可分为帮助选项、管理选项、代理选项及应用程序选项几类,下面一起去介绍它们。 --help --help-admin --help-proxy --help-all ―――― 以上四个选项均用于获取帮助信息; --proxy-address=host:port ―――― 代理服务监听的地址和端口; --admin-address=host:port ―――― 管理模块监听的地址和端口; --proxy-backend-addresses=host:port ―――― 后端mysql服务器的地址和端口; --proxy-read-only-backend-addresses=host:port ―――― 后端只读mysql服务器的地址和端口; --proxy-lua-script=file_name ―――― 完成mysql代理功能的Lua脚本; --daemon ―――― 以守护进程模式启动mysql-proxy; --keepalive ―――― 在mysql-proxy崩溃时尝试重启之; --log-file=/path/to/log_file_name ―――― 日志文件名称; --log-level=level ―――― 日志级别; --log-use-syslog ―――― 基于syslog记录日志; --plugins=plugin,.. ―――― 在mysql-proxy启动时加载的插件; --user=user_name ―――― 运行mysql-proxy进程的用户; --defaults-file=/path/to/conf_file_name ―――― 默认使用的配置文件路径;其配置段使用[mysql-proxy]标识; --proxy-skip-profiling ―――― 禁用profile; --pid-file=/path/to/pid_file_name ―――― 进程文件名;
启动服务并查看端口
#####重启服务 [root@localhost ~]# service mysql-proxy restart Stopping /usr/local/mysql-proxy/bin/mysql-proxy: [ OK ] Starting /usr/local/mysql-proxy/bin/mysql-proxy: [ OK ] #####查看端口,3306为服务端口,4041为管理端口 [root@localhost ~]# netstat -tnlp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1120/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1196/master tcp 0 0 0.0.0.0:4041 0.0.0.0:* LISTEN 2144/mysql-proxy tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 2144/mysql-proxy tcp 0 0 :::22 :::* LISTEN 1120/sshd tcp 0 0 ::1:25 :::* LISTEN 1196/master
管理功能测试(unkown表示后端服务器状况未知)
[root@node2 ~]# mysql -uadmin -h192.168.1.208 --port=4041 -p 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.1.201:3306 | unknown | rw | NULL | 0 | | 2 | 192.168.1.202:3306 | unknown | ro | NULL | 0 | +-------------+--------------------+---------+------+------+-------------------+ 2 rows in set (0.00 sec)
读写分离测试
使用mysql客户端连接mysql-proxy创建数据库
[root@node2 ~]# mysql -uroot -p -h192.168.1.208 -e 'create database mydb;' Enter password:
再次查看Mysql_Proxy管理控制台发现master变为up,表征写请求分配到master
mysql> SELECT * FROM backends; +-------------+--------------------+---------+------+------+-------------------+ | backend_ndx | address | state | type | uuid | connected_clients | +-------------+--------------------+---------+------+------+-------------------+ | 1 | 192.168.1.201:3306 | up | rw | NULL | 0 | | 2 | 192.168.1.202:3306 | unknown | ro | NULL | 0 | +-------------+--------------------+---------+------+------+-------------------+ 2 rows in set (0.00 sec)
使用mysql客户端连接mysql-proxy查询操作
[root@node2 ~]# mysql -uroot -p -h192.168.1.208 -e 'show databases;' Enter password: +--------------------+ | Database | +--------------------+ | information_schema | | mydb | | mysql | | performance_schema | | soulboy | | test | +--------------------+
再次查看Mysql_Proxy管理控制台发现slave也变为了up,表征读请求分配到了slave
mysql> SELECT * FROM backends; +-------------+--------------------+-------+------+------+-------------------+ | backend_ndx | address | state | type | uuid | connected_clients | +-------------+--------------------+-------+------+------+-------------------+ | 1 | 192.168.1.201:3306 | up | rw | NULL | 0 | | 2 | 192.168.1.202:3306 | up | ro | NULL | 0 | +-------------+--------------------+-------+------+------+-------------------+ 2 rows in set (0.00 sec)
至此Mysql_Proxy成功实现读写分离
mysql-5.6基于amoeba实现读写分离
Amoeba(变形虫)项目,该开源框架于2008年 开始发布一款 Amoeba for Mysql软件。这个软件致力于MySQL的分布式数据库前端代理层,它主要在应用层访问MySQL的 时候充当SQL路由功能,专注于分布式数据库代理层(Database Proxy)开发。座落与 Client、DB Server(s)之间,对客户端透明。具有负载均衡、高可用性、SQL 过滤、读写分离、可路由相关的到目标数据库、可并发请求多台数据库合并结果。 通过Amoeba你能够完成多数据源的高可用、负载均衡、数据切片的功能,目前Amoeba已在很多 企业的生产线上面使用,除以之外变形虫具有以下优点:
降低费用:分布式数据库在地理上可以式分布的。其系统的结构符合这种分布的要求。允许用 户在自己的本地录用、查询、维护等操作,实行局部控制,降低通信代价,避免集中式需要更高要求 的硬件设备。而且分布式数据库在单台机器上面数据量较少,其响应速度明显提升。
提高系统整体可用性:避免了因为单台数据库的故障而造成全部瘫痪的后果。
易于扩展处理能力和系统规模:分布式数据库系统的结构可以很容易地扩展系统,在分布式数 据库中增加一个新的节点,不影响现有系统的正常运行。这种方式比扩大集中式系统要灵活经济。在 集中式系统中扩大系统和系统升级,由于有硬件不兼容和软件改变困难等缺点,升级的代价常常是昂贵和不可行的。
安装JDK,由于Amoeba是java程序所研发,所以需要先安装JDK程序 jdk-6u31-linux-x64-rpm
chmod +x jdk-6u31-linux-x64-rpm.bin ./jdk-6u31-linux-x64-rpm.bin vim /etc/profile.d/java.sh export JAVA_HOME=/usr/java/latest export PATH=$JAVA_HOME/bin:$PATH . /etc/profile.d/java.sh [root@amoeba ~]# java -version java version "1.6.0_31" Java(TM) SE Runtime Environment (build 1.6.0_31-b04) Java HotSpot(TM) 64-Bit Server VM (build 20.6-b01, mixed mode)
安装Amoeba amoeba-mysql-binary-2.2.0.tar
mkdir /usr/local/amoeba tar xf amoeba-mysql-binary-2.2.0.tar.gz -C /usr/local/amoeba/ vim /etc/profile.d/amoeba.sh export AMOEBA_HOME=/usr/local/amoeba export PATH=$AMOEBA_HOME/bin:$PATH /etc/profile.d/amoeba.sh [root@amoeba ~]# amoeba amoeba start|stop
分别在Master和Slave上执行创建远程授权用户amoeba,使用之前的root也可以,这里特意为了区别
mysql> GRANT ALL ON *.* TO 'amoeba'@'192.168.1.%' INDENTIFIED BY 'redhat'; mysql> flush privileges;
配置vim /usr/local/amoeba/conf/dbServers.xml 定义连接后端Mysql服务器信息
<?xml version="1.0" encoding="gbk"?> <!DOCTYPE amoeba:dbServers SYSTEM "dbserver.dtd"> <amoeba:dbServers xmlns:amoeba="http://amoeba.meidusa.com/"> <!-- Each dbServer needs to be configured into a Pool, If you need to configure multiple dbServer with load balancing that can be simplified by the following configuration: add attribute with name virtual = "true" in dbServer, but the configuration does not allow the element with name factoryConfig such as 'multiPool' dbServer --> <dbServer name="abstractServer" abstractive="true"> <factoryConfig class="com.meidusa.amoeba.mysql.net.MysqlServerConnectionFactory"> <property name="manager">${defaultManager}</property> <property name="sendBufferSize">64</property> <property name="receiveBufferSize">128</property> <!-- mysql port --> <property name="port">3306</property> #主从mysql端口 <!-- mysql schema --> <property name="schema">test</property> #默认是的库 <!-- mysql user --> <property name="user">amoeba</property> #远程授权用户 <!-- mysql password --> #远程授权密码 <property name="password">redhat</property> </factoryConfig> <poolConfig class="com.meidusa.amoeba.net.poolable.PoolableObjectPool"> <property name="maxActive">500</property> <property name="maxIdle">500</property> <property name="minIdle">10</property> <property name="minEvictableIdleTimeMillis">600000</property> <property name="timeBetweenEvictionRunsMillis">600000</property> <property name="testOnBorrow">true</property> <property name="testOnReturn">true</property> <property name="testWhileIdle">true</property> </poolConfig> </dbServer> <dbServer name="master" parent="abstractServer"> #主节点 <factoryConfig> <!-- mysql ip --> <property name="ipAddress">192.168.1.201</property> </factoryConfig> </dbServer> <dbServer name="slave" parent="abstractServer"> #从节点 <factoryConfig> <!-- mysql ip --> <property name="ipAddress">192.168.1.202</property> </factoryConfig> </dbServer> <dbServer name="multiPool" virtual="true"> <poolConfig class="com.meidusa.amoeba.server.MultipleServerPool"> <!-- Load balancing strategy: 1=ROUNDROBIN , 2=WEIGHTBASED , 3=HA--> <property name="loadbalance">1</property> #算法 <!-- Separated by commas,such as: server1,server2,server1 --> <property name="poolNames">slave</property> #定义数据池 </poolConfig> </dbServer> </amoeba:dbServers>
配置 vim /usr/local/amoeba/conf/amoeba.xml #定义数据库读写分离及节点管理信息等
<?xml version="1.0" encoding="gbk"?> <!DOCTYPE amoeba:configuration SYSTEM "amoeba.dtd"> <amoeba:configuration xmlns:amoeba="http://amoeba.meidusa.com/"> <proxy> <!-- service class must implements com.meidusa.amoeba.service.Service --> <service name="Amoeba for Mysql" class="com.meidusa.amoeba.net.ServerableConnectionManager"> <!-- port --> <property name="port">3306</property> #amoeba监听端口 <!-- bind ipAddress --> <!-- <property name="ipAddress">192.168.1.208</property> #amoeba监听地址 --> <property name="manager">${clientConnectioneManager}</property> <property name="connectionFactory"> <bean class="com.meidusa.amoeba.mysql.net.MysqlClientConnectionFactory"> <property name="sendBufferSize">128</property> <property name="receiveBufferSize">64</property> </bean> </property> <property name="authenticator"> <bean class="com.meidusa.amoeba.mysql.server.MysqlClientAuthenticator"> <property name="user">admin</property> #mysql客户端连接amoeba使用的账户 <property name="password">password</property> #mysql客户端连接amoeba使用的密码 <property name="filter"> <bean class="com.meidusa.amoeba.server.IPAccessController"> <property name="ipFile">${amoeba.home}/conf/access_list.conf</property> </bean> </property> </bean> </property> </service> <!-- server class must implements com.meidusa.amoeba.service.Service --> <service name="Amoeba Monitor Server" class="com.meidusa.amoeba.monitor.MonitorServer"> <!-- port --> <!-- default value: random number <property name="port">9066</property> --> <!-- bind ipAddress --> <property name="ipAddress">127.0.0.1</property> <property name="daemon">true</property> <property name="manager">${clientConnectioneManager}</property> <property name="connectionFactory"> <bean class="com.meidusa.amoeba.monitor.net.MonitorClientConnectionFactory"></bean> </property> </service> <runtime class="com.meidusa.amoeba.mysql.context.MysqlRuntimeContext"> <!-- proxy server net IO Read thread size --> <property name="readThreadPoolSize">20</property> <!-- proxy server client process thread size --> <property name="clientSideThreadPoolSize">30</property> <!-- mysql server data packet process thread size --> <property name="serverSideThreadPoolSize">30</property> <!-- per connection cache prepared statement size --> <property name="statementCacheSize">500</property> <!-- query timeout( default: 60 second , TimeUnit:second) --> <property name="queryTimeout">60</property> </runtime> </proxy> <!-- Each ConnectionManager will start as thread manager responsible for the Connection IO read , Death Detection --> <connectionManagerList> <connectionManager name="clientConnectioneManager" class="com.meidusa.amoeba.net.MultiConnectionManagerWrapper"> <property name="subManagerClassName">com.meidusa.amoeba.net.ConnectionManager</property> <!-- default value is avaliable Processors <property name="processors">5</property> --> </connectionManager> <connectionManager name="defaultManager" class="com.meidusa.amoeba.net.MultiConnectionManagerWrapper"> <property name="subManagerClassName">com.meidusa.amoeba.net.AuthingableConnectionManager</property> <!-- default value is avaliable Processors <property name="processors">5</property> --> </connectionManager> </connectionManagerList> <!-- default using file loader --> <dbServerLoader class="com.meidusa.amoeba.context.DBServerConfigFileLoader"> <property name="configFile">${amoeba.home}/conf/dbServers.xml</property> </dbServerLoader> <queryRouter class="com.meidusa.amoeba.mysql.parser.MysqlQueryRouter"> <property name="ruleLoader"> <bean class="com.meidusa.amoeba.route.TableRuleFileLoader"> <property name="ruleFile">${amoeba.home}/conf/rule.xml</property> <property name="functionFile">${amoeba.home}/conf/ruleFunctionMap.xml</property> </bean> </property> <property name="sqlFunctionFile">${amoeba.home}/conf/functionMap.xml</property> <property name="LRUMapSize">1500</property> <property name="defaultPool">master</property> #默认池 <property name="writePool">master</property> #写库 <property name="readPool">slave</property> #读库 <property name="needParse">true</property> </queryRouter> </amoeba:configuration>
启动amoeba服务
amoeba start & netstat -tnlp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1120/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1196/master tcp 0 0 :::22 :::* LISTEN 1120/sshd tcp 0 0 ::1:25 :::* LISTEN 1196/master tcp 0 0 ::ffff:127.0.0.1:41575 :::* LISTEN 3097/java tcp 0 0 :::3306 :::* LISTEN 3097/java
使用客户端连接amoeba
Master抓包
Slave抓包
至此变形虫成功实现读写分离
本文出自 “星矢” 博客,转载请与作者联系!