1. 程式人生 > 其它 >全面瞭解一致性雜湊演算法及PHP程式碼實現

全面瞭解一致性雜湊演算法及PHP程式碼實現

節點 IP
db1 192.168.223.21
db2 192.168.223.22
mycat 192.168.223.23

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


配置hosts檔案
[root@mycat ~]# vim /etc/hosts
192.168.223.23 mycat
192.158.223.21 db1
192.168.223.22 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)

db1、db2節點授權
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 |
+----------+--------+-------+----------------+------+------+--------+------+------+---------+-----------+------------+