MySQL主从同步和读写分离的配置

时间:2021-10-23 14:49:16

主服务器:192.168.1.126

从服务器:192.168.1.163

amoeba代理服务器:192.168.1.237

系统全部是CentOS 6.7

1、配置主从同步

  1.1、修改主服务器(192.168.1.126)的my.conf配置文件

    vim  /etc/my.cnf

    MySQL主从同步和读写分离的配置

    server-id:必须是唯一的,不能与其他服务器相同

  1.2、重启mysql服务,在主服务器上建立帐户并授权,查看主数据库的binlog文件,以及位置

    MySQL主从同步和读写分离的配置

    注:执行完此步骤后不要再操作主服务器的mysql,防止主服务器的状态值发生变化

  1.3、修改从服务器(192.168.1.163)的my.conf配置文件

    MySQL主从同步和读写分离的配置

    完成后重启mysql服务

    [root@server ~]# /etc/init.d/mysql restart
    Shutting down MySQL. SUCCESS!
    Starting MySQL.. SUCCESS!
    [root@server ~]#

  1.4、配置从服务器(192.168.1.163)

    MySQL主从同步和读写分离的配置

  1.5、解锁主库(192.168.1.126)

    MySQL主从同步和读写分离的配置

  1.6、查看状态(192.168.1.163)

    MySQL主从同步和读写分离的配置

    主从复制配置完毕,下面配置读写分离

2、配置读写分离

  2.1、在amoeba代理服务器(192.168.1.237)上安装java环境

    MySQL主从同步和读写分离的配置

    配置java环境变量

    [root@cento ~]# vim /etc/profile

JAVA_HOME=/usr/java/jdk1.8.0_20
JRE_HOME=/usr/java/jdk1.8.0_20/jre
CLASS_PATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib
PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
export JAVA_HOME JRE_HOME CLASS_PATH PATH

    [root@cento ~]# source /etc/profile

    查看是否安装成功

    MySQL主从同步和读写分离的配置

  2.2、安装amoeba

    解压amoeba,移动到/usr/local/目录下

  2.3、配置amoeba

    amoeba的配置文件都在/usr/local/amoeba/conf/目录下,如果只配置读写分离,只需要配置dbServers.xml和amoeba.xml文件即可

    vim  /usr/local/amoeba/conf/dbServers.xml

<?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="connectionManager">${defaultManager}</property>
<property name="sendBufferSize">64</property>
<property name="receiveBufferSize">128</property>

<!-- mysql port -->
<property name="port"></property>         </property>    #设置amoeba连接后端数据库服务器的账号和密码
</factoryConfig>

<poolConfig class="com.meidusa.toolkit.common.poolable.PoolableObjectPool">
<property name="maxActive"></property>      </property>      </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="</property>     #选择调度算法,1表示复制均衡,2表示权重,3表示HA, 这里选择1

<!-- Separated by commas,such as: server1,server2,server1 -->
<property name="poolNames">slave</property>    #myslave组成员
</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.mysql.server.MySQLService">
<!-- port -->
<property name="port"></property>          </property>

<property name="filter">
<bean class="com.meidusa.toolkit.net.authenticate.server.IPAccessController">
<property name="ipFile">${amoeba.home}/conf/access_list.conf</property>
</bean>
</property>
</bean>
</property>

</service>

<runtime class="com.meidusa.amoeba.mysql.context.MysqlRuntimeContext">

<!-- proxy server client process thread size -->
<property name="executeThreadSize">128</property>

<!-- per connection cache prepared statement size -->
<property name="statementCacheSize">500</property>

<!-- default charset -->
<property name="serverCharset">utf8</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="defaultManager" class="com.meidusa.toolkit.net.MultiConnectionManagerWrapper">
<property name="subManagerClassName">com.meidusa.toolkit.net.AuthingableConnectionManager</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">writedb</property>      #设置amoeba默认的池,这里设置为writedb

<property name="writePool">writedb</property>          #这两个选项默认是注销掉的,需要取消注释,这里用来指定前面定义好的俩个读写池
<property name="readPool">slave</property>        #这两个选项默认是注销掉的,需要取消注释,这里用来指定前面定义好的俩个读写池

<property name="needParse">true</property>
</queryRouter>
</amoeba:configuration>

    2.4、修改JDK配置,否则启动amoeba会报错

      [root@cento ~]# vim /usr/local/amoeba/jvm.properties

      原: JVM_OPTIONS="-server -Xms256m -Xmx1024m -Xss196k -XX:PermSize=16m -XX:MaxPermSize=96m"

      改为:JVM_OPTIONS="-server -Xms1024m -Xmx1024m -Xss256k -XX:PermSize=16m -XX:MaxPermSize=96m"

    启动amoeda服务

MySQL主从同步和读写分离的配置

      查看端口

      MySQL主从同步和读写分离的配置

    2.5、测试

      远程登录mysql通过amoeba中指定的用户名、密码、IP、端口连接mysql数据库

      MySQL主从同步和读写分离的配置