4

说明:
mysql-master:172.16.200.43
Mycat:172.16.200.43
mysql-slave1:172.16.200.45
mysql-slave2:172.16.200.46
.......................................................................................
MariaDB5.5.51数据库
..............................................................................................

一、 安装jdk

1、检查是否安装java

# java -version

2、安装jdk包

网址:http://www.oracle.com/technet...
上传至/usr/local/java目录下

3、修改环境变量文件,添加如下内容;

添加完成后,source /etc/profile
vi /etc/profile

#add java
export JAVA_HOME=/usr/local/java/jdk1.7.0_80
export JAVA_BIN=/usr/local/java/jdk1.7.0_80/bin
export PATH=$PATH:/usr/local/java/jdk1.7.0_80/bin
export CLASSPATH=./:/usr/local/java/jdk1.7.0_80/lib:/usr/local/java/jdk1.7.0_80/jre/lib
MYCAT_HOME=/usr/local/mycat
export JAVA_HOME JAVA_BIN PATH CALSSPATH

二、mycat安装配置读写分离

1、解压缩至目录/usr/local/

# tar zxvf Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz 

2、查看配置文件

配置文件非常多.如果只是简单配置在不同的服务器上进行读写分离只需要配置两个文件 server.xml 和  schema.xml

3、先配置server.xml

    <user name="root">
        <property name="password">123456</property>
        <property name="schemas">test</property>
    </user>
    <user name="user">
        <property name="password">123456</property>
        <property name="schemas">test</property>
        <property name="readOnly">true</property>
    </user>

4、配置schema

<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
        <schema name="test" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn_test"> </schema>
        <dataNode name="dn_test" dataHost="dh_43" database="test" />
        <dataHost name="dh_43" maxCon="1000" minCon="10" balance="1"
                          writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">
                <heartbeat>select user()</heartbeat>
                <!-- can have multi write hosts -->
                <writeHost host="43_M" url="172.16.200.43:3306" user="root"
                                   password="000000">
                        <!-- can have multi read hosts -->
                        <readHost host="45_S1" url="172.16.200.45:3306" user="root" password="000000" />
                        <readHost host="46_S2" url="172.16.200.46:3306" user="root" password="000000" />
                </writeHost>
                <!-- <writeHost host="hostM2" url="localhost:3316" user="root" password="123456"/> -->
        </dataHost>

5、开启mycat

./mycat start

6、查看端口

# netstat -tnlp
TCP *:9066 (LISTEN) #虚拟schema管理端口 
TCP *:8066 (LISTEN) #虚拟schema登陆端口 

能看到这个说明咱们的Mycat已经启动成功了

7、登录mycat读写分离服务:

# mysql -uroot -p123456 -h127.0.0.1 -P 8066

8、登录mycat管理端:

# mysql -uroot -p123456 -h127.0.0.1 -P 9066
Mysql> show @@heartbeat;

clipboard.png
RS_CODE为1表示心跳正常
Mysql> show @@datasource; --查看读写分离的机器配置情况

三、 mycat读写分离验证

1、更改日志的输出模式

vi log4j.xml
修改<asyncRoot level="info" includeLocation="true">
为 <asyncRoot level="debug" includeLocation="true">

2、创建表

# mysql -uroot -p123456 -h127.0.0.1 -P 8066
mysql> create table test (id bigint not null primary key,user_id varchar(100),date DATE, fee decimal);

3、插入数据

mysql> insert into test(id,user_id,date,fee)  values(1,@@hostname,20161201,100);
mysql> insert into test(id,user_id,date,fee)  values(5000001,@@hostname,20161202,100);

4、查询观察日志的变化

mysql> select * from test
tail -f wrapper.log

clipboard.png

说明读写分离成功


linda玲
438 声望27 粉丝

每天进步一点点