MySql教程

MySQL读写分离

本文主要是介绍MySQL读写分离,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

准备

分别给三个节点准备不同的ip

过程

主机名

[root@localhost ~]# hostnamectl set-hostname mycat
[root@localhost ~]# bash

配置hosts文件

[root@mycat ~]# vim /etc/hosts
192.168.200.53 mycat
192.158.200.51 db1
192.168.200.52 db2

配置本地yum源

[root@mycat ~]# mv /etc/yum.repos.d/ * /media/
[root@mycat ~]# vim /etc/yum.repos.d/local.repo
[mariadb]
name=mariadb
baseurl=file:///opt/gpmall-repo
gpgcheck=0
enabled=1
[centos]
name=centos
baseurl=file:///opt/centos
gpgcheck=0
enabled=1

[root@mycat ~]# mount /opt/CentOS-7-x86_64-DVD-1511.iso /opt/centos/
mount: /dev/loop0 is write-protected, mounting read-only

安装jdk

[root@mycat ~]# yum install -y java-1.8.0-openjdk java-1.8.0-openjdk-devel
[root@mycat ~]# java -version
openjdk version "1.8.0_222"
OpenJDK Runtime Environment (build 1.8.0_222-b10)
OpenJDK 64-Bit Server VM (build 25.222-b10, mixed mode)

节点授权

mysql> grant all privileges  on *.* to root@'%' identified by "000000";
Query OK, 0 rows affected (0.00 sec)

安装mycat

[root@mycat ~]# tar -zxf Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz -C /usr/local/
[root@mycat ~]# chmod -R 777 /usr/local/mycat
[root@mycat ~]# echo export MYCAT_HOME=/usr/local/mycat/ >> /etc/profile
[root@mycat ~]# source /etc/profile

修改配置文件

[root@mycat ~]# vim /usr/local/mycat/conf/schema.xml
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
<schema name="USERDB" checkSQLschema="true" sqlMaxLimit="100" dataNode="dn1"></schema>
<dataNode name="dn1" dataHost="localhost1" database="test" />
<dataHost name="localhost1" maxCon="1000" minCon="10" balance="3" dbType="mysql" dbDriver="native" writeType="0" switchType="1"  slaveThreshold="100">
    <heartbeat>select user()</heartbeat>
    <writeHost host="hostM1" url="192.168.200.51:3306" user="root" password="000000">
        <readHost host="hostS1" url="192.168.200.52:3306" user="root" password="000000" />
    </writeHost>
</dataHost>
</mycat:schema>

80行左右修改

[root@mycat ~]# chown root:root /usr/local/mycat/conf/schema.xml
<property name="password">000000</property>
<property name="schemas">USERDB</property>

最下面删除

[root@mycat ~]# /usr/local/mycat/bin/mycat start 
Starting Mycat-server...
[root@mycat ~]# netstat -ntlp
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      1095/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2221/master         
tcp        0      0 127.0.0.1:32000         0.0.0.0:*               LISTEN      12070/java          
tcp6       0      0 :::9066                 :::*                    LISTEN      12070/java          
tcp6       0      0 :::59094                :::*                    LISTEN      12070/java          
tcp6       0      0 :::22                   :::*                    LISTEN      1095/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      2221/master         
tcp6       0      0 :::1984                 :::*                    LISTEN      12070/java          
tcp6       0      0 :::8066                 :::*                    LISTEN      12070/java          
tcp6       0      0 :::38310                :::*                    LISTEN      12070/java 

测试

[root@mycat mycat]#  yum install -y MariaDB-client
[root@mycat mycat]# mysql -h127.0.0.1 -P8066 -uroot -p123456
MySQL [(none)]> show databases
   -> ;
+----------+
| DATABASE |
+----------+
| USERDB   |
+----------+
1 row in set (0.002 sec)
MySQL [(none)]> use USERDB
Database changed
MySQL [USERDB]> create table company(id int not null primary key,name varchar(50),addr varchar(255));
Query OK, 0 rows affected (0.180 sec)
MySQL [USERDB]> show tables;
+----------------+
| Tables_in_test |
+----------------+
| company        |
+----------------+
1 row in set (0.002 sec)
MySQL [USERDB]> insert into company values(1,"facebook","usa");
Query OK, 1 row affected (0.040 sec)
MySQL [USERDB]> select * from company
    -> ;
+----+----------+------+
| id | name     | addr |
+----+----------+------+
|  1 | facebook | usa  |
+----+----------+------+
1 row in set (0.048 sec)
[root@mycat ~]# mysql -h127.0.0.1 -P9066 -uroot -p000000 -e 'show  @@datasource;'
+----------+--------+-------+----------------+------+------+--------+------+------+---------+-----------+------------+
| DATANODE | NAME   | TYPE  | HOST           | PORT | W/R  | ACTIVE | IDLE | SIZE | EXECUTE | READ_LOAD | WRITE_LOAD |
+----------+--------+-------+----------------+------+------+--------+------+------+---------+-----------+------------+
| dn1      | hostM1 | mysql | 192.168.200.51 | 3306 | W    |      0 |   10 | 1000 |     116 |         0 |          2 |
| dn1      | hostS1 | mysql | 192.168.200.52 | 3306 | R    |      0 |    7 | 1000 |     115 |         4 |          0 |
+----------+--------+-------+----------------+------+------+--------+------+------+---------+-----------+------------+
这篇关于MySQL读写分离的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!