业界对系统的高可用有着基本的要求,简单的说,这些要求可以总结为如下所示。
系统架构中不存在单点问题。
可以最大限度的保障服务的可用性。
一般情况下系统的高可用可以用几个9来评估。所谓的几个9就是系统可以保证对外提供的服务的时间达到总时间的百分比。例如如果需要达到99.99的高可用,则系统全年发生故障的总时间不能超过52分钟。
系统高可用架构
我们既然需要实现系统的高可用架构,那么,我们到底需要搭建一个什么样的系统架构呢?我们可以将需要搭建的系统架构简化成下图所示。
服务器规划
由于我电脑资源有限,我这里在4台服务器上搭建高可用环境,大家可以按照本文将环境扩展到更多的服务器,搭建步骤都是一样的。
主机名 | IP地址 | 安装的服务 |
---|---|---|
binghe151 | 192.168.175.151 | Mycat、Zookeeper、 MySQL、HAProxy、 Keepalived、Xinetd |
binghe152 | 192.168.175.152 | Zookeeper、MySQL |
binghe153 | 192.168.175.153 | Zookeeper、MySQL |
binghe154 | 192.168.175.154 | Mycat、MySQL、 HAProxy、Keepalived、 Xinetd |
binghe155 | 192.168.175.155 | MySQL |
注意:HAProxy和Keepalived最好和Mycat部署在同一台服务器上。
安装JDK
由于Mycat和Zookeeper的运行需要JDK环境的支持,所有我们需要在每台服务器上安装JDK环境。
这里,我以在binghe151服务器上安装JDK为例,其他服务器的安装方式与在binghe151服务器上的安装方式相同。安装步骤如下所示。
(1)到JDK官网下载JDK 1.8版本,JDK1.8的下载地址为:https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html。
注:我下载的JDK安装包版本为:jdk-8u212-linux-x64.tar.gz,如果JDK版本已更新,大家下载对应的版本即可。
(2)将下载的jdk-8u212-linux-x64.tar.gz安装包上传到binghe151服务器的/usr/local/src目录下。
(3)解压jdk-8u212-linux-x64.tar.gz文件,如下所示。
tar-zxvfjdk-8u212-linux-x64.tar.gz
(4)将解压的jdk1.8.0_212目录移动到binghe151服务器下的/usr/local目录下,如下所示。
mvjdk1.8.0_212//usr/local/src/
(5)配置JDK系统环境变量,如下所示。
vim/etc/profile
JAVA_HOME=/usr/local/jdk1.8.0_212
CLASS_PATH=.:$JAVA_HOME/lib
PATH=$JAVA_HOME/bin:$PATH
exportJAVA_HOMECLASS_PATHPATH
使系统环境变量生效,如下所示。
source/etc/profile
(6)查看JDK版本,如下所示。
[root@binghe151~]#java-version
javaversion"1.8.0_212"
Java(TM)SERuntimeEnvironment(build1.8.0_212-b10)
JavaHotSpot(TM)64-BitServerVM(build25.212-b10,mixedmode)
结果显示,正确输出了JDK的版本信息,说明JDK安装成功。
安装Mycat
下载Mycat 1.6.7.4 Release版本,解压到服务器的/usr/local/mycat目录下,并配置Mycat的系统环境变量,随后,配置Mycat的配置文件,Mycat的最终结果配置如下所示。
schema.xml
writeType="0"dbType="mysql"dbDriver="native"switchType="1"slaveThreshold="100">
writeType="0"dbType="mysql"dbDriver="native"switchType="1"slaveThreshold="100">
writeType="0"dbType="mysql"dbDriver="native"switchType="1"slaveThreshold="100">
writeType="0"dbType="mysql"dbDriver="native"switchType="1"slaveThreshold="100">
server.xml
1
druidparser
3307
3308
0
0.0.0.0
utf8mb4
2048
2
2
1800000
300
0
0
1
1000
104857600
io.mycat.server.interceptor.impl.StatisticsSqlInterceptor
UPDATE,DELETE,INSERT
/tmp/sql.txt
true
true
1
cTwf23RrpBCEmalp/nx0BAKenNhvNs2NSr9nYiMzHADeEDEfwVWlI6hBDccJjNBJqJxnunHFp5ae63PPnMfGYA==
shop
rule.xml
4
sequence_db_conf.properties
#sequencestoredindatanode
GLOBAL=mycat
ORDER_MASTER=mycat
ORDER_DETAIL=mycat
关于Mycat的配置,仅供大家参考,大家不一定非要按照我这里配置,根据自身业务需要配置即可。本文的重点是实现Mycat的高可用环境搭建。
在MySQL中创建Mycat连接MySQL的账户,如下所示。
CREATEUSER'mycat'@'192.168.175.%'IDENTIFIEDBY'mycat';
ALTERUSER'mycat'@'192.168.175.%'IDENTIFIEDWITHmysql_native_passwordBY'mycat';
GRANTSELECT,INSERT,UPDATE,DELETE,EXECUTEON*.*TO'mycat'@'192.168.175.%';
FLUSHPRIVILEGES;
安装Zookeeper集群
安装配置完JDK后,就需要搭建Zookeeper集群了,根据对服务器的规划,现将Zookeeper集群搭建在“binghe151”、“binghe152”、“binghe153”三台服务器上。
1.下载Zookeeper
到Apache官网去下载Zookeeper的安装包,Zookeeper的安装包下载地址为:https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/。具体如下图所示。
也可以在binghe151服务器上执行如下命令直接下载zookeeper-3.5.5。
wgethttps://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/zookeeper-3.5.5/apache-zookeeper-3.5.5-bin.tar.gz
执行上述命令就可以直接把apache-zookeeper-3.5.5-bin.tar.gz安装包下载到binghe151服务器上。
2.安装并配置Zookeeper
注意:(1)、(2)、(3)步都是在binghe152服务器上执行的。
(1)解压Zookeeper安装包
在binghe151服务器上执行如下命令,将Zookeeper解压到“/usr/local/”目录下,并将Zookeeper目录修改为zookeeper-3.5.5。
tar-zxvfapache-zookeeper-3.5.5-bin.tar.gz
mvapache-zookeeper-3.5.5-binzookeeper-3.5.5
(2)配置Zookeeper系统环境变量
同样,需要在/etc/profile文件中配置Zookeeper系统环境变量,如下:
ZOOKEEPER_HOME=/usr/local/zookeeper-3.5.5
PATH=$ZOOKEEPER_HOME/bin:$PATH
exportZOOKEEPER_HOMEPATH
结合之前配置的JDK系统环境变量,/etc/profile,总体配置如下:
MYSQL_HOME=/usr/local/mysql
JAVA_HOME=/usr/local/jdk1.8.0_212
MYCAT_HOME=/usr/local/mycat
ZOOKEEPER_HOME=/usr/local/zookeeper-3.5.5
MPC_HOME=/usr/local/mpc-1.1.0
GMP_HOME=/usr/local/gmp-6.1.2
MPFR_HOME=/usr/local/mpfr-4.0.2
CLASS_PATH=.:$JAVA_HOME/lib
LD_LIBRARY_PATH=$MPC_LIB_HOME/lib:$GMP_HOME/lib:$MPFR_HOME/lib:$LD_LIBRARY_PATH
PATH=$MYSQL_HOME/bin:$JAVA_HOME/bin:$ZOOKEEPER_HOME/bin:$MYCAT_HOME/bin:$PATH
exportJAVA_HOMEZOOKEEPER_HOMEMYCAT_HOMECLASS_PATHMYSQL_HOMEMPC_LIB_HOMEGMP_HOMEMPFR_HOMELD_LIBRARY_PATHPATH
(3)配置Zookeeper
首先,需要将ZOOKEEPER_HOME为Zookeeper的安装目录)目录下的zoo_sample.cfg文件修改为zoo.cfg文件。具体命令如下:
cd/usr/local/zookeeper-3.5.5/conf/
mvzoo_sample.cfgzoo.cfg
接下来修改zoo.cfg文件,修改后的具体内容如下:
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/usr/local/zookeeper-3.5.5/data
dataLogDir=/usr/local/zookeeper-3.5.5/dataLog
clientPort=2181
server.1=binghe151:2888:3888
server.2=binghe152:2888:3888
server.3=binghe153:2888:3888
在Zookeeper的安装目录下创建data和dataLog两个文件夹。
mkdir-p/usr/local/zookeeper-3.5.5/data
mkdir-p/usr/local/zookeeper-3.5.5/dataLog
切换到新建的data目录下,创建myid文件,具体内容为数字1,如下所示:
cd/usr/local/zookeeper-3.5.5/data
vimmyid
将数字1写入到文件myid。
3.将Zookeeper和系统环境变量文件复制到其他服务器
注意:(1)、(2)步是在binghe151服务器上执行的。
(1)复制Zookeeper到其他服务器
根据对服务器的规划,现将Zookeeper复制到binghe152和binghe53服务器,具体执行操作如下所示:
scp-r/usr/local/zookeeper-3.5.5/binghe152:/usr/local/
scp-r/usr/local/zookeeper-3.5.5/binghe153:/usr/local/
(2)复制系统环境变量文件到其他服务器
根据对服务器的规划,现将系统环境变量文件/etc/profile复制到binghe152、binghe153服务器,具体执行操作如下所示:
scp/etc/profilebinghe152:/etc/
scp/etc/profilebinghe153:/etc/
上述操作可能会要求输入密码,根据提示输入密码即可。
4.修改其他服务器上的myid文件
修改binghe152服务器上Zookeeper的myid文件内容为数字2,同时修改binghe153服务器上Zookeeper的myid文件内容为数字3。具体如下:
在binghe152服务器上执行如下操作:
echo"2">/usr/local/zookeeper-3.5.5/data/myid
cat/usr/local/zookeeper-3.5.5/data/myid
2
在binghe153服务器上执行如下操作:
echo"3">/usr/local/zookeeper-3.5.5/data/myid
cat/usr/local/zookeeper-3.5.5/data/myid
3
5.使环境变量生效
分别在binghe151、binghe152、binghe153上执行如下操作,使系统环境变量生效。
source/etc/profile
6.启动Zookeeper集群
分别在binghe151、binghe152、binghe153上执行如下操作,启动Zookeeper集群。
zkServer.shstart
7.查看Zookeeper集群的启动状态
binghe151服务器
[root@binghe151~]#zkServer.shstatus
ZooKeeperJMXenabledbydefault
Usingconfig:/usr/local/zookeeper-3.5.5/bin/../conf/zoo.cfg
Clientportfound:2181.Clientaddress:localhost.
Mode:follower
binghe152服务器
[root@binghe152local]#zkServer.shstatus
ZooKeeperJMXenabledbydefault
Usingconfig:/usr/local/zookeeper-3.5.5/bin/../conf/zoo.cfg
Clientportfound:2181.Clientaddress:localhost.
Mode:leader
binghe153服务器
[root@binghe153~]#zkServer.shstatus
ZooKeeperJMXenabledbydefault
Usingconfig:/usr/local/zookeeper-3.5.5/bin/../conf/zoo.cfg
Clientportfound:2181.Clientaddress:localhost.
Mode:follower
可以看到,binghe151和binghe153服务器上的Zookeeper角色为follower,binghe152服务器上的Zookeeper角色为leader。
初始化Mycat配置到Zookeeper集群
注意:初始化Zookeeper中的数据,是在binghe151服务器上进行的,原因是之前我们已经在binghe151服务器上安装了Mycat。
1.查看初始化脚本
在Mycat安装目录下的bin目录中提供了一个init_zk_data.sh脚本文件,如下所示。
[root@binghe151~]#ll/usr/local/mycat/bin/
total384
-rwxr-xr-x1rootroot3658Feb2617:10dataMigrate.sh
-rwxr-xr-x1rootroot1272Feb2617:10init_zk_data.sh
-rwxr-xr-x1rootroot15701Feb2820:51mycat
-rwxr-xr-x1rootroot2986Feb2617:10rehash.sh
-rwxr-xr-x1rootroot2526Feb2617:10startup_nowrap.sh
-rwxr-xr-x1rootroot140198Feb2820:51wrapper-linux-ppc-64
-rwxr-xr-x1rootroot99401Feb2820:51wrapper-linux-x86-32
-rwxr-xr-x1rootroot111027Feb2820:51wrapper-linux-x86-64
init_zk_data.sh脚本文件就是用来向Zookeeper中初始化Mycat的配置的,这个文件会通过读取Mycat安装目录下的conf目录下的配置文件,将其初始化到Zookeeper集群中。
2.复制Mycat配置文件
首先,我们查看下Mycat安装目录下的conf目录下的文件信息,如下所示。
[root@binghe151~]#cd/usr/local/mycat/conf/
[root@binghe151conf]#ll
total108
-rwxrwxrwx1rootroot92Feb2617:10autopartition-long.txt
-rwxrwxrwx1rootroot51Feb2617:10auto-sharding-long.txt
-rwxrwxrwx1rootroot67Feb2617:10auto-sharding-rang-mod.txt
-rwxrwxrwx1rootroot340Feb2617:10cacheservice.properties
-rwxrwxrwx1rootroot3338Feb2617:10dbseq.sql
-rwxrwxrwx1rootroot3532Feb2617:10dbseq-utf8mb4.sql
-rw-r--r--1rootroot86Mar122:37dnindex.properties
-rwxrwxrwx1rootroot446Feb2617:10ehcache.xml
-rwxrwxrwx1rootroot2454Feb2617:10index_to_charset.properties
-rwxrwxrwx1rootroot1285Feb2617:10log4j2.xml
-rwxrwxrwx1rootroot183Feb2617:10migrateTables.properties
-rwxrwxrwx1rootroot271Feb2617:10myid.properties
-rwxrwxrwx1rootroot16Feb2617:10partition-hash-int.txt
-rwxrwxrwx1rootroot108Feb2617:10partition-range-mod.txt
-rwxrwxrwx1rootroot988Mar116:59rule.xml
-rwxrwxrwx1rootroot3883Mar323:59schema.xml
-rwxrwxrwx1rootroot440Feb2617:10sequence_conf.properties
-rwxrwxrwx1rootroot84Mar323:52sequence_db_conf.properties
-rwxrwxrwx1rootroot29Feb2617:10sequence_distributed_conf.properties
-rwxrwxrwx1rootroot28Feb2617:10sequence_http_conf.properties
-rwxrwxrwx1rootroot53Feb2617:10sequence_time_conf.properties
-rwxrwxrwx1rootroot2420Mar415:14server.xml
-rwxrwxrwx1rootroot18Feb2617:10sharding-by-enum.txt
-rwxrwxrwx1rootroot4251Feb2820:51wrapper.conf
drwxrwxrwx2rootroot4096Feb2821:17zkconf
drwxrwxrwx2rootroot4096Feb2821:17zkdownload
接下来,将Mycat安装目录下的conf目录下的schema.xml文件、server.xml文件、rule.xml文件和sequence_db_conf.properties文件复制到conf目录下的zkconf目录下,如下所示。
cpschema.xmlserver.xmlrule.xmlsequence_db_conf.propertieszkconf/
3.将Mycat配置信息写入Zookeeper集群
执行init_zk_data.sh脚本文件,向Zookeeper集群中初始化配置信息,如下所示。
[root@binghe151bin]#/usr/local/mycat/bin/init_zk_data.sh
o2020-03-0820:03:13INFOJAVA_CMD=/usr/local/jdk1.8.0_212/bin/java
o2020-03-0820:03:13INFOStarttoinitialize/mycatofZooKeeper
o2020-03-0820:03:14INFODone
根据以上信息得知,Mycat向Zookeeper写入初始化配置信息成功。
4.验证Mycat配置信息是否成功写入Mycat
我们可以使用Zookeeper的客户端命令zkCli.sh 登录Zookeeper来验证Mycat的配置信息是否成功写入Mycat。
首先,登录Zookeeper,如下所示。
[root@binghe151~]#zkCli.sh
Connectingtolocalhost:2181
###################此处省略N行输出######################
WelcometoZooKeeper!
WATCHER::
WatchedEventstate:SyncConnectedtype:Nonepath:null
[zk:localhost:2181(CONNECTED)0]
接下来,在Zookeeper命令行查看mycat的信息,如下所示。
[zk:localhost:2181(CONNECTED)0]ls/
[mycat,zookeeper]
[zk:localhost:2181(CONNECTED)1]ls/mycat
[mycat-cluster-1]
[zk:localhost:2181(CONNECTED)2]ls/mycat/mycat-cluster-1
[cache,line,rules,schema,sequences,server]
[zk:localhost:2181(CONNECTED)3]
可以看到,在/mycat/mycat-cluster-1下存在6个目录,接下来,查看下schema目录下的信息,如下所示。
[zk:localhost:2181(CONNECTED)3]ls/mycat/mycat-cluster-1/schema
[dataHost,dataNode,schema]
接下来,我们查看下dataHost的配置,如下所示。
[zk:localhost:2181(CONNECTED)4]get/mycat/mycat-cluster-1/schema/dataHost
[{"balance":1,"maxCon":1000,"minCon":10,"name":"binghe151","writeType":0,"switchType":1,"slaveThreshold":100,"dbType":"mysql","dbDriver":"native","heartbeat":"selectuser()","writeHost":[{"host":"binghe51","url":"192.168.175.151:3306","password":"root","user":"root"}]},{"balance":1,"maxCon":1000,"minCon":10,"name":"binghe152","writeType":0,"switchType":1,"slaveThreshold":100,"dbType":"mysql","dbDriver":"native","heartbeat":"selectuser()","writeHost":[{"host":"binghe52","url":"192.168.175.152:3306","password":"root","user":"root"}]},{"balance":1,"maxCon":1000,"minCon":10,"name":"binghe153","writeType":0,"switchType":1,"slaveThreshold":100,"dbType":"mysql","dbDriver":"native","heartbeat":"selectuser()","writeHost":[{"host":"binghe53","url":"192.168.175.153:3306","password":"root","user":"root"}]},{"balance":1,"maxCon":1000,"minCon":10,"name":"binghe154","writeType":0,"switchType":1,"slaveThreshold":100,"dbType":"mysql","dbDriver":"native","heartbeat":"selectuser()","writeHost":[{"host":"binghe54","url":"192.168.175.154:3306","password":"root","user":"root"}]}]
上面的输出信息格式比较乱,但可以看出是Json格式的信息,我们可以将输出信息进行格式化,格式化后的结果如下所示。
[
{
"balance":1,
"maxCon":1000,
"minCon":10,
"name":"binghe151",
"writeType":0,
"switchType":1,
"slaveThreshold":100,
"dbType":"mysql",
"dbDriver":"native",
"heartbeat":"selectuser()",
"writeHost":[
{
"host":"binghe51",
"url":"192.168.175.151:3306",
"password":"root",
"user":"root"
}
]
},
{
"balance":1,
"maxCon":1000,
"minCon":10,
"name":"binghe152",
"writeType":0,
"switchType":1,
"slaveThreshold":100,
"dbType":"mysql",
"dbDriver":"native",
"heartbeat":"selectuser()",
"writeHost":[
{
"host":"binghe52",
"url":"192.168.175.152:3306",
"password":"root",
"user":"root"
}
]
},
{
"balance":1,
"maxCon":1000,
"minCon":10,
"name":"binghe153",
"writeType":0,
"switchType":1,
"slaveThreshold":100,
"dbType":"mysql",
"dbDriver":"native",
"heartbeat":"selectuser()",
"writeHost":[
{
"host":"binghe53",
"url":"192.168.175.153:3306",
"password":"root",
"user":"root"
}
]
},
{
"balance":1,
"maxCon":1000,
"minCon":10,
"name":"binghe154",
"writeType":0,
"switchType":1,
"slaveThreshold":100,
"dbType":"mysql",
"dbDriver":"native",
"heartbeat":"selectuser()",
"writeHost":[
{
"host":"binghe54",
"url":"192.168.175.154:3306",
"password":"root",
"user":"root"
}
]
}
]
可以看到,我们在Mycat的schema.xml文件中配置的dataHost节点的信息,成功写入到Zookeeper中了。
为了验证Mycat的配置信息,是否已经同步到Zookeeper的其他节点上,我们也可以在binghe152和binghe153服务器上登录Zookeeper,查看Mycat配置信息是否写入成功。
binghe152服务器
[root@binghe152~]#zkCli.sh
Connectingtolocalhost:2181
#################省略N行输出信息################
[zk:localhost:2181(CONNECTED)0]get/mycat/mycat-cluster-1/schema/dataHost
[{"balance":1,"maxCon":1000,"minCon":10,"name":"binghe151","writeType":0,"switchType":1,"slaveThreshold":100,"dbType":"mysql","dbDriver":"native","heartbeat":"selectuser()","writeHost":[{"host":"binghe51","url":"192.168.175.151:3306","password":"root","user":"root"}]},{"balance":1,"maxCon":1000,"minCon":10,"name":"binghe152","writeType":0,"switchType":1,"slaveThreshold":100,"dbType":"mysql","dbDriver":"native","heartbeat":"selectuser()","writeHost":[{"host":"binghe52","url":"192.168.175.152:3306","password":"root","user":"root"}]},{"balance":1,"maxCon":1000,"minCon":10,"name":"binghe153","writeType":0,"switchType":1,"slaveThreshold":100,"dbType":"mysql","dbDriver":"native","heartbeat":"selectuser()","writeHost":[{"host":"binghe53","url":"192.168.175.153:3306","password":"root","user":"root"}]},{"balance":1,"maxCon":1000,"minCon":10,"name":"binghe154","writeType":0,"switchType":1,"slaveThreshold":100,"dbType":"mysql","dbDriver":"native","heartbeat":"selectuser()","writeHost":[{"host":"binghe54","url":"192.168.175.154:3306","password":"root","user":"root"}]}]
可以看到,Mycat的配置信息成功同步到了binghe152服务器上的Zookeeper中。
binghe153服务器
[root@binghe153~]#zkCli.sh
Connectingtolocalhost:2181
#####################此处省略N行输出信息#####################
[zk:localhost:2181(CONNECTED)0]get/mycat/mycat-cluster-1/schema/dataHost
[{"balance":1,"maxCon":1000,"minCon":10,"name":"binghe151","writeType":0,"switchType":1,"slaveThreshold":100,"dbType":"mysql","dbDriver":"native","heartbeat":"selectuser()","writeHost":[{"host":"binghe51","url":"192.168.175.151:3306","password":"root","user":"root"}]},{"balance":1,"maxCon":1000,"minCon":10,"name":"binghe152","writeType":0,"switchType":1,"slaveThreshold":100,"dbType":"mysql","dbDriver":"native","heartbeat":"selectuser()","writeHost":[{"host":"binghe52","url":"192.168.175.152:3306","password":"root","user":"root"}]},{"balance":1,"maxCon":1000,"minCon":10,"name":"binghe153","writeType":0,"switchType":1,"slaveThreshold":100,"dbType":"mysql","dbDriver":"native","heartbeat":"selectuser()","writeHost":[{"host":"binghe53","url":"192.168.175.153:3306","password":"root","user":"root"}]},{"balance":1,"maxCon":1000,"minCon":10,"name":"binghe154","writeType":0,"switchType":1,"slaveThreshold":100,"dbType":"mysql","dbDriver":"native","heartbeat":"selectuser()","writeHost":[{"host":"binghe54","url":"192.168.175.154:3306","password":"root","user":"root"}]}]
可以看到,Mycat的配置信息成功同步到了binghe153服务器上的Zookeeper中。
配置Mycat支持Zookeeper启动
1.在binghe151服务器上配置Mycat
在binghe151服务器上进入Mycat安装目录的conf目录下,查看文件信息,如下所示。
[root@binghe151~]#cd/usr/local/mycat/conf/
[root@binghe151conf]#ll
total108
-rwxrwxrwx1rootroot92Feb2617:10autopartition-long.txt
-rwxrwxrwx1rootroot51Feb2617:10auto-sharding-long.txt
-rwxrwxrwx1rootroot67Feb2617:10auto-sharding-rang-mod.txt
-rwxrwxrwx1rootroot340Feb2617:10cacheservice.properties
-rwxrwxrwx1rootroot3338Feb2617:10dbseq.sql
-rwxrwxrwx1rootroot3532Feb2617:10dbseq-utf8mb4.sql
-rw-r--r--1rootroot86Mar122:37dnindex.properties
-rwxrwxrwx1rootroot446Feb2617:10ehcache.xml
-rwxrwxrwx1rootroot2454Feb2617:10index_to_charset.properties
-rwxrwxrwx1rootroot1285Feb2617:10log4j2.xml
-rwxrwxrwx1rootroot183Feb2617:10migrateTables.properties
-rwxrwxrwx1rootroot271Feb2617:10myid.properties
-rwxrwxrwx1rootroot16Feb2617:10partition-hash-int.txt
-rwxrwxrwx1rootroot108Feb2617:10partition-range-mod.txt
-rwxrwxrwx1rootroot988Mar116:59rule.xml
-rwxrwxrwx1rootroot3883Mar323:59schema.xml
-rwxrwxrwx1rootroot440Feb2617:10sequence_conf.properties
-rwxrwxrwx1rootroot84Mar323:52sequence_db_conf.properties
-rwxrwxrwx1rootroot29Feb2617:10sequence_distributed_conf.properties
-rwxrwxrwx1rootroot28Feb2617:10sequence_http_conf.properties
-rwxrwxrwx1rootroot53Feb2617:10sequence_time_conf.properties
-rwxrwxrwx1rootroot2420Mar415:14server.xml
-rwxrwxrwx1rootroot18Feb2617:10sharding-by-enum.txt
-rwxrwxrwx1rootroot4251Feb2820:51wrapper.conf
drwxrwxrwx2rootroot4096Feb2821:17zkconf
drwxrwxrwx2rootroot4096Feb2821:17zkdownload
可以看到,在Mycat的conf目录下,存在一个myid.properties文件,接下来,使用vim编辑器编辑这个文件,如下所示。
vimmyid.properties
编辑后的myid.properties文件的内容如下所示。
loadZk=true
zkURL=192.168.175.151:2181,192.168.175.152:2181,192.168.175.153:2181
clusterId=mycat-cluster-1
myid=mycat_151
clusterSize=2
clusterNodes=mycat_151,mycat_154
#serverbooster;boosterinstallondbsameserver,willresetallminConto2
type=server
boosterDataHosts=dataHost1
其中几个重要的参数说明如下所示。
loadZk:表示是否加载Zookeeper配置。true:是;false:否;
zkURL:Zookeeper的连接地址,多个Zookeeper连接地址以逗号隔开;
clusterId:当前Mycat集群的Id标识,此标识需要与Zookeeper中/mycat目录下的目录名称相同,如下所示。
[zk:localhost:2181(CONNECTED)1]ls/mycat
[mycat-cluster-1]
myid:当前Mycat节点的id,这里我的命名方式为mycat_前缀加上IP地址的最后三位;
clusterSize:表示Mycat集群中的Mycat节点个数,这里,我们在binghe151和binghe154节点上部署Mycat,所以Mycat节点的个数为2。
clusterNodes:Mycat集群中,所有的Mycat节点,此处的节点需要配置myid中配置的Mycat节点id,多个节点之前以逗号分隔。这里我配置的节点为:mycat_151,mycat_154。
2.在binghe154服务器上安装全新的Mycat
在binghe154服务器上下载并安装和binghe151服务器上相同版本的Mycat,并将其解压到binghe154服务器上的/usr/local/mycat目录下。
也可以在binghe151服务器上直接输入如下命令将Mycat的安装目录复制到binghe154服务器上。
[root@binghe151~]#scp-r/usr/local/mycatbinghe154:/usr/local
注意:别忘了在binghe154服务器上配置Mycat的系统环境变量。
3.修改binghe154服务器上的Mycat配置
在binghe154服务器上修改Mycat安装目录下的conf目录中的myid.properties文件,如下所示。
vim/usr/local/mycat/conf/myid.properties
修改后的myid.properties文件的内容如下所示。
loadZk=true
zkURL=192.168.175.151:2181,192.168.175.152:2181,192.168.175.153:2181
clusterId=mycat-cluster-1
myid=mycat_154
clusterSize=2
clusterNodes=mycat_151,mycat_154
#serverbooster;boosterinstallondbsameserver,willresetallminConto2
type=server
boosterDataHosts=dataHost1
4.重启Mycat
分别重启binghe151服务器和binghe154服务器上的Mycat,如下所示。
注意:先重启
binghe151服务器
[root@binghe151~]#mycatrestart
StoppingMycat-server...
StoppedMycat-server.
StartingMycat-server...
binghe154服务器
[root@binghe154~]#mycatrestart
StoppingMycat-server...
StoppedMycat-server.
StartingMycat-server...
在binghe151和binghe154服务器上分别查看Mycat的启动日志,如下所示。
STATUS|wrapper|2020/03/0821:08:15|<--WrapperStopped
STATUS|wrapper|2020/03/0821:08:15|-->WrapperStartedasDaemon
STATUS|wrapper|2020/03/0821:08:15|LaunchingaJVM...
INFO|jvm1|2020/03/0821:08:16|Wrapper(Version3.2.3)http://wrapper.tanukisoftware.org
INFO|jvm1|2020/03/0821:08:16|Copyright1999-2006TanukiSoftware,Inc.AllRightsReserved.
INFO|jvm1|2020/03/0821:08:16|
INFO|jvm1|2020/03/0821:08:28|MyCATServerstartupsuccessfully.seelogsinlogs/mycat.log
从日志的输出结果可以看出,Mycat重启成功。
此时,先重启binghe151服务器上的Mycat,再重启binghe154服务器上的Mycat之后,我们会发现binghe154服务器上的Mycat的conf目录下的schema.xml、server.xml、rule.xml和sequence_db_conf.properties文件与binghe151服务器上Mycat的配置文件相同,这就是binghe154服务器上的Mycat从Zookeeper上读取配置文件的结果。
以后,我们只需要修改Zookeeper中有关Mycat的配置,这些配置就会自动同步到Mycat中,这样可以保证多个Mycat节点的配置是一致的。
配置虚拟IP
分别在binghe151和binghe154服务器上配置虚拟IP,如下所示。
ifconfigeth0:1192.168.175.110broadcast192.168.175.255netmask255.255.255.0up
routeadd-host192.168.175.110deveth0:1
配置完虚拟IP的效果如下所示,以binghe151服务器为例。
[root@binghe151~]#ifconfig
eth0Linkencap:EthernetHWaddr00:0C:29:10:A1:45
inetaddr:192.168.175.151Bcast:192.168.175.255Mask:255.255.255.0
inet6addr:fe80::20c:29ff:fe10:a145/64Scope:Link
UPBROADCASTRUNNINGMULTICASTMTU:1500Metric:1
RXpackets:116766errors:0dropped:0overruns:0frame:0
TXpackets:85230errors:0dropped:0overruns:0carrier:0
collisions:0txqueuelen:1000
RXbytes:25559422(24.3MiB)TXbytes:55997016(53.4MiB)
eth0:1Linkencap:EthernetHWaddr00:0C:29:10:A1:45
inetaddr:192.168.175.110Bcast:192.168.175.255Mask:255.255.255.0
UPBROADCASTRUNNINGMULTICASTMTU:1500Metric:1
loLinkencap:LocalLoopback
inetaddr:127.0.0.1Mask:255.0.0.0
inet6addr:::1/128Scope:Host
UPLOOPBACKRUNNINGMTU:65536Metric:1
RXpackets:51102errors:0dropped:0overruns:0frame:0
TXpackets:51102errors:0dropped:0overruns:0carrier:0
collisions:0txqueuelen:0
RXbytes:2934009(2.7MiB)TXbytes:2934009(2.7MiB)
注意:在命令行添加VIP后,当服务器重启后,VIP信息会消失,所以,最好是将创建VIP的命令写到一个脚本文件中,例如,将命令写到/usr/local/script/vip.sh文件中,如下所示。
mkdir/usr/local/script
vim/usr/local/script/vip.sh
文件的内容如下所示。
ifconfigeth0:1192.168.175.110broadcast192.168.175.255netmask255.255.255.0up
routeadd-host192.168.175.110deveth0:1
接下来,将/usr/local/script/vip.sh文件添加到服务器开机启动项中,如下所示。
echo/usr/local/script/vip.sh>>/etc/rc.d/rc.local
配置IP转发
在binghe151和binghe154服务器上配置系统内核IP转发功能,编辑/etc/sysctl.conf文件,如下所示。
vim/etc/sysctl.conf
找到如下一行代码。
net.ipv4.ip_forward=0
将其修改成如下所示的代码。
net.ipv4.ip_forward=1
保存并退出vim编辑器,并运行如下命令使配置生效。
sysctl-p
安装并配置xinetd服务
我们需要在安装HAProxy的服务器上,也就是在binghe151和binghe154服务器上安装xinetd服务来开启48700端口。
(1)在服务器命令行执行如下命令安装xinetd服务,如下所示。
yuminstallxinetd-y
(2)编辑/etc/xinetd.conf文件,如下所示。
vim/etc/xinetd.conf
检查文件中是否存在如下配置。
includedir/etc/xinetd.d
如果/etc/xinetd.conf文件中没有以上配置,则在/etc/xinetd.conf文件中添加以上配置;如果存在以上配置,则不用修改。
(3)创建/etc/xinetd.d目录,如下所示。
mkdir/etc/xinetd.d
注意:如果/etc/xinetd.d目录已经存在,创建目录时会报如下错误。
mkdir:cannotcreatedirectory`/etc/xinetd.d':Fileexists
大家可不必理会此错误信息。
(4)在/etc/xinetd.d目录下添加Mycat状态检测服务器的配置文件mycat_status,如下所示。
touch/etc/xinetd.d/mycat_status
(5)编辑mycat_status文件,如下所示。
vim/etc/xinetd.d/mycat_status
编辑后的mycat_status文件中的内容如下所示。
servicemycat_status
{
flags=REUSE
socket_type=stream
port=48700
wait=no
user=root
server=/usr/local/bin/mycat_check.sh
log_on_failure+=USERID
disable=no
}
部分xinetd配置参数说明如下所示。
socket_type:表示封包处理方式,Stream为TCP数据包。
port:表示xinetd服务监听的端口号。
wait:表示不需等待,即服务将以多线程的方式运行。
user:运行xinted服务的用户。
server:需要启动的服务脚本。
log_on_failure:记录失败的日志内容。
disable:需要启动xinted服务时,需要将此配置项设置为no。
(6)在/usr/local/bin目录下添加mycat_check.sh服务脚本,如下所示。
touch/usr/local/bin/mycat_check.sh
(7)编辑/usr/local/bin/mycat_check.sh文件,如下所示。
vim/usr/local/bin/mycat_check.sh
编辑后的文件内容如下所示。
#!/bin/bash
mycat=`/usr/local/mycat/bin/mycatstatus|grep'notrunning'|wc-l`
if["$mycat"="0"];then
/bin/echo-e"HTTP/1.1200OK\r\n"
else
/bin/echo-e"HTTP/1.1503ServiceUnavailable\r\n"
/usr/local/mycat/bin/mycatstart
fi
为mycat_check.sh文件赋予可执行权限,如下所示。
chmoda+x/usr/local/bin/mycat_check.sh
(8)编辑/etc/services文件,如下所示。
vim/etc/services
在文件末尾添加如下所示的内容。
mycat_status48700/tcp#mycat_status
其中,端口号需要与在/etc/xinetd.d/mycat_status文件中配置的端口号相同。
(9)重启xinetd服务,如下所示。
servicexinetdrestart
(10)查看mycat_status服务是否成功启动,如下所示。
binghe151服务器
[root@binghe151~]#netstat-antup|grep48700
tcp00:::48700:::*LISTEN2776/xinetd
binghe154服务器
[root@binghe154~]#netstat-antup|grep48700
tcp00:::48700:::*LISTEN6654/xinetd
结果显示,两台服务器上的mycat_status服务器启动成功。
至此,xinetd服务安装并配置成功,即Mycat状态检查服务安装成功。
安装并配置HAProxy
我们直接在binghe151和binghe154服务器上使用如下命令安装HAProxy。
yuminstallhaproxy-y
安装完成后,我们需要对HAProxy进行配置,HAProxy的配置文件目录为/etc/haproxy,我们查看这个目录下的文件信息,如下所示。
[root@binghe151~]#ll/etc/haproxy/
total4
-rw-r--r--1rootroot3142Oct212016haproxy.cfg
发现/etc/haproxy/目录下存在一个haproxy.cfg文件。接下来,我们就修改haproxy.cfg文件,修改后的haproxy.cfg文件的内容如下所示。
global
log127.0.0.1local2
chroot/var/lib/haproxy
pidfile/var/run/haproxy.pid
maxconn4000
userhaproxy
grouphaproxy
daemon
statssocket/var/lib/haproxy/stats
defaults
modehttp
logglobal
optionhttplog
optiondontlognull
optionhttp-server-close
optionredispatch
retries3
timeouthttp-request10s
timeoutqueue1m
timeoutconnect10s
timeoutclient1m
timeoutserver1m
timeouthttp-keep-alive10s
timeoutcheck10s
maxconn3000
listenadmin_status
bind0.0.0.0:48800
statsuri/admin-status
statsauthadmin:admin
listenallmycat_service
bind0.0.0.0:3366
modetcp
optiontcplog
optionhttpchkOPTIONS*HTTP/1.1\r\nHost:\www
balanceroundrobin
servermycat_151192.168.175.151:3307checkport48700inter5srise2fall3
servermycat_154192.168.175.154:3307checkport48700inter5srise2fall3
listenallmycat_admin
bind0.0.0.0:3377
modetcp
optiontcplog
optionhttpchkOPTIONS*HTTP/1.1\r\nHost:\www
balanceroundrobin
servermycat_151192.168.175.151:3308checkport48700inter5srise2fall3
servermycat_154192.168.175.154:3308checkport48700inter5srise2fall3
接下来,在binghe151服务器和binghe154服务器上启动HAProxy,如下所示。
haproxy-f/etc/haproxy/haproxy.cfg
接下来,我们使用mysql命令连接HAProxy监听的虚拟IP和端口来连接Mycat,如下所示。
[root@binghe151~]#mysql-umycat-pmycat-h192.168.175.110-P3366--default-auth=mysql_native_password
mysql:[Warning]Usingapasswordonthecommandlineinterfacecanbeinsecure.
WelcometotheMySQLmonitor.Commandsendwith;or\g.
YourMySQLconnectionidis2
Serverversion:5.6.29-mycat-1.6.7.4-release-20200228205020MyCatServer(OpenCloudDB)
Copyright(c)2000,2019,Oracleand/oritsaffiliates.Allrightsreserved.
OracleisaregisteredtrademarkofOracleCorporationand/orits
affiliates.Othernamesmaybetrademarksoftheirrespective
owners.
Type'help;'or'\h'forhelp.Type'\c'toclearthecurrentinputstatement.
mysql>
可以看到,连接Mycat成功。
安装Keepalived
1.安装并配置Keepalived
直接在binghe151和binghe154服务器上输入如下命令安装Keepalived。
yuminstallkeepalived-y
安装成功后,会在/etc目录下生成一个keepalived目录,接下来,我们在/etc/keepalived目录下配置keepalived.conf文件,如下所示。
vim/etc/keepalived/keepalived.conf
binghe151服务器配置
!ConfigurationFileforkeepalived
vrrp_scriptchk_http_port{
script"/etc/keepalived/check_haproxy.sh"
interval2
weight2
}
vrrp_instanceVI_1{
stateMASTER
interfaceeth0
virtual_router_id51
priority150
advert_int1
authentication{
auth_typePASS
auth_pass1111
}
track_script{
chk_http_port
}
virtual_ipaddress{
192.168.175.110deveth0scopeglobal
}
}
binghe154服务器配置
!ConfigurationFileforkeepalived
vrrp_scriptchk_http_port{
script"/etc/keepalived/check_haproxy.sh"
interval2
weight2
}
vrrp_instanceVI_1{
stateSLAVE
interfaceeth0
virtual_router_id51
priority120
advert_int1
authentication{
auth_typePASS
auth_pass1111
}
track_script{
chk_http_port
}
virtual_ipaddress{
192.168.175.110deveth0scopeglobal
}
}
2.编写检测HAProxy的脚本
接下来,需要分别在binghe151和binghe154服务器上的/etc/keepalived目录下创建check_haproxy.sh脚本,脚本内容如下所示。
#!/bin/bash
STARTHAPROXY="/usr/sbin/haproxy-f/etc/haproxy/haproxy.cfg"
STOPKEEPALIVED="/etc/init.d/keepalivedstop"
#STOPKEEPALIVED="/usr/bin/systemctlstopkeepalived"
LOGFILE="/var/log/keepalived-haproxy-state.log"
echo"[check_haproxystatus]">>$LOGFILE
A=`ps-Chaproxy--no-header|wc-l`
echo"[check_haproxystatus]">>$LOGFILE
date>>$LOGFILE
if[$A-eq0];then
echo$STARTHAPROXY>>$LOGFILE
$STARTHAPROXY>>$LOGFILE2>&1
sleep5
fi
if[`ps-Chaproxy--no-header|wc-l`-eq0];then
exit0
else
exit1
fi
使用如下命令为check_haproxy.sh脚本授予可执行权限。
chmoda+x/etc/keepalived/check_haproxy.sh
3.启动Keepalived
配置完成后,我们就可以启动Keepalived了,分别在binghe151和binghe154服务器上启动Keepalived,如下所示。
/etc/init.d/keepalivedstart
查看Keepalived是否启动成功,如下所示。
binghe151服务器
[root@binghe151~]#ps-ef|grepkeepalived
root12211020:06?00:00:00keepalived-D
root12221221020:06?00:00:00keepalived-D
root12231221020:06?00:00:02keepalived-D
root932903787021:42pts/000:00:00grepkeepalived
binghe154服务器
[root@binghe154~]#ps-ef|grepkeepalived
root12241020:06?00:00:00keepalived-D
root12251224020:06?00:00:00keepalived-D
root12261224020:06?00:00:02keepalived-D
root946363798021:43pts/000:00:00grepkeepalived
可以看到,两台服务器上的Keepalived服务启动成功。
4.验证Keepalived绑定的虚拟IP
接下来,我们分别查看两台服务器上的Keepalived是否绑定了虚拟IP。
binghe151服务器
[root@binghe151~]#ipaddr
1:lo:
link/loopback00:00:00:00:00:00brd00:00:00:00:00:00
inet127.0.0.1/8scopehostlo
inet6::1/128scopehost
valid_lftforeverpreferred_lftforever
2:eth0:
link/ether00:0c:29:10:a1:45brdff:ff:ff:ff:ff:ff
inet192.168.175.151/24brd192.168.175.255scopeglobaleth0
inet192.168.175.110/32scopeglobaleth0
inet192.168.175.110/24brd192.168.175.255scopeglobalsecondaryeth0:1
inet6fe80::20c:29ff:fe10:a145/64scopelink
valid_lftforeverpreferred_lftforever
可以看到如下一行代码。
inet192.168.175.110/32scopeglobaleth0
说明binghe151服务器上的Keepalived绑定了虚拟IP 192.168.175.110。
binghe154服务器
[root@binghe154~]#ipaddr
1:lo:
link/loopback00:00:00:00:00:00brd00:00:00:00:00:00
inet127.0.0.1/8scopehostlo
inet6::1/128scopehost
valid_lftforeverpreferred_lftforever
2:eth0:
link/ether00:50:56:22:2a:75brdff:ff:ff:ff:ff:ff
inet192.168.175.154/24brd192.168.175.255scopeglobaleth0
inet192.168.175.110/24brd192.168.175.255scopeglobalsecondaryeth0:1
inet6fe80::250:56ff:fe22:2a75/64scopelink
valid_lftforeverpreferred_lftforever
可以看到binghe154服务器上的Keepalived并没有绑定虚拟IP。
5.测试虚拟IP的漂移
如何测试虚拟IP的漂移呢?首先,我们停止binghe151服务器上的Keepalived,如下所示。
/etc/init.d/keepalivedstop
接下来,查看binghe154服务器上Keepalived绑定虚拟IP的情况,如下所示。
[root@binghe154~]#ipaddr
1:lo:
link/loopback00:00:00:00:00:00brd00:00:00:00:00:00
inet127.0.0.1/8scopehostlo
inet6::1/128scopehost
valid_lftforeverpreferred_lftforever
2:eth0:
link/ether00:50:56:22:2a:75brdff:ff:ff:ff:ff:ff
inet192.168.175.154/24brd192.168.175.255scopeglobaleth0
inet192.168.175.110/32scopeglobaleth0
inet192.168.175.110/24brd192.168.175.255scopeglobalsecondaryeth0:1
inet6fe80::250:56ff:fe22:2a75/64scopelink
valid_lftforeverpreferred_lftforever
可以看到,在输出的结果信息中,存在如下一行信息。
inet192.168.175.110/32scopeglobaleth0
说明binghe154服务器上的Keepalived绑定了虚拟IP 192.168.175.110,虚拟IP漂移到了binghe154服务器上。
6.binghe151服务器上的Keepalived抢占虚拟IP
接下来,我们启动binghe151服务器上的Keepalived,如下所示。
/etc/init.d/keepalivedstart
启动成功后,我们再次查看虚拟IP的绑定情况,如下所示。
binghe151服务器
[root@binghe151~]#ipaddr
1:lo:
link/loopback00:00:00:00:00:00brd00:00:00:00:00:00
inet127.0.0.1/8scopehostlo
inet6::1/128scopehost
valid_lftforeverpreferred_lftforever
2:eth0:
link/ether00:0c:29:10:a1:45brdff:ff:ff:ff:ff:ff
inet192.168.175.151/24brd192.168.175.255scopeglobaleth0
inet192.168.175.110/32scopeglobaleth0
inet192.168.175.110/24brd192.168.175.255scopeglobalsecondaryeth0:1
inet6fe80::20c:29ff:fe10:a145/64scopelink
valid_lftforeverpreferred_lftforever
binghe154服务器
[root@binghe154~]#ipaddr
1:lo:
link/loopback00:00:00:00:00:00brd00:00:00:00:00:00
inet127.0.0.1/8scopehostlo
inet6::1/128scopehost
valid_lftforeverpreferred_lftforever
2:eth0:
link/ether00:50:56:22:2a:75brdff:ff:ff:ff:ff:ff
inet192.168.175.154/24brd192.168.175.255scopeglobaleth0
inet192.168.175.110/24brd192.168.175.255scopeglobalsecondaryeth0:1
inet6fe80::250:56ff:fe22:2a75/64scopelink
valid_lftforeverpreferred_lftforever
由于binghe151服务器上配置的Keepalived优先级要高于binghe154服务器上的Keepalived,所以,再次启动binghe151服务器上的Keepalived后,binghe151服务器上的Keepalived会抢占虚拟IP。
配置MySQL主从复制
这里,为了简单,我将binghe154和binghe155服务器上的MySQL配置成主从复制,大家也可以根据实际情况,自行配置其他服务器上MySQL的主从复制(注意:我这里配置的是一主一从模式)。
1.编辑my.cnf文件
binghe154服务器
server_id=154
log_bin=/data/mysql/log/bin_log/mysql-bin
binlog-ignore-db=mysql
binlog_format=mixed
sync_binlog=100
log_slave_updates=1
binlog_cache_size=32m
max_binlog_cache_size=64m
max_binlog_size=512m
lower_case_table_names=1
relay_log=/data/mysql/log/bin_log/relay-bin
relay_log_index=/data/mysql/log/bin_log/relay-bin.index
master_info_repository=TABLE
relay-log-info-repository=TABLE
relay-log-recovery
binghe155服务器
server_id=155
log_bin=/data/mysql/log/bin_log/mysql-bin
binlog-ignore-db=mysql
binlog_format=mixed
sync_binlog=100
log_slave_updates=1
binlog_cache_size=32m
max_binlog_cache_size=64m
max_binlog_size=512m
lower_case_table_names=1
relay_log=/data/mysql/log/bin_log/relay-bin
relay_log_index=/data/mysql/log/bin_log/relay-bin.index
master_info_repository=TABLE
relay-log-info-repository=TABLE
relay-log-recovery
2.同步两台服务器上MySQL的数据
在binghe154服务器上只有一个customer_db数据库,我们使用mysqldump命令导出customer_db数据库,如下所示。
[root@binghe154~]#mysqldump--master-data=2--single-transaction-uroot-p--databasescustomer_db>binghe154.sql
Enterpassword:
接下来,我们查看binghe154.sql文件。
morebinghe154.sql
在文件中,我们可以找到如下信息。
CHANGEMASTERTOMASTER_LOG_FILE='mysql-bin.000042',MASTER_LOG_POS=995;
说明当前MySQL的二进制日志文件为mysql-bin.000042,二进制日志文件的位置为995。
接下来,我们将binghe154.sql文件复制到binghe155服务器上,如下所示。
scpbinghe154.sql192.168.175.155:/usr/local/src
在binghe155服务器上,将binghe154.sql脚本导入到MySQL中,如下所示。
mysql-uroot-p
此时,完成了数据的初始化。
3.创建主从复制账号
在binghe154服务器的MySQL中,创建用于主从复制的MySQL账号,如下所示。
mysql>CREATEUSER'repl'@'192.168.175.%'IDENTIFIEDBY'repl123456';
QueryOK,0rowsaffected(0.01sec)
mysql>ALTERUSER'repl'@'192.168.175.%'IDENTIFIEDWITHmysql_native_passwordBY'repl123456';
QueryOK,0rowsaffected(0.00sec)
mysql>GRANTREPLICATIONSLAVEON*.*TO'repl'@'192.168.175.%';
QueryOK,0rowsaffected(0.00sec)
mysql>FLUSHPRIVILEGES;
QueryOK,0rowsaffected(0.00sec)
4.配置复制链路
登录binghe155服务器上的MySQL,并使用如下命令配置复制链路。
mysql>changemasterto
>master_host='192.168.175.154',
>master_port=3306,
>master_user='repl',
>master_password='repl123456',
>MASTER_LOG_FILE='mysql-bin.000042',
>MASTER_LOG_POS=995;
其中,MASTER_LOG_FILE='mysql-bin.000042', MASTER_LOG_POS=995 就是在binghe154.sql文件中找到的。
5.启动从库
在binghe155服务器的MySQL命令行启动从库,如下所示。
mysql>startslave;
查看从库是否启动成功,如下所示。
mysql>SHOWslaveSTATUS\G
***************************1.row***************************
Slave_IO_State:Waitingformastertosendevent
Master_Host:192.168.175.151
Master_User:binghe152
Master_Port:3306
Connect_Retry:60
Master_Log_File:mysql-bin.000007
Read_Master_Log_Pos:1360
Relay_Log_File:relay-bin.000003
Relay_Log_Pos:322
Relay_Master_Log_File:mysql-bin.000007
Slave_IO_Running:Yes
Slave_SQL_Running:Yes
#################省略部分输出结果信息##################
结果显示Slave_IO_Running选项和Slave_SQL_Running选项的值均为Yes,说明MySQL主从复制环境搭建成功。
最后,别忘了在binghe155服务器的MySQL中创建Mycat连接MySQL的用户,如下所示。
CREATEUSER'mycat'@'192.168.175.%'IDENTIFIEDBY'mycat';
ALTERUSER'mycat'@'192.168.175.%'IDENTIFIEDWITHmysql_native_passwordBY'mycat';
GRANTSELECT,INSERT,UPDATE,DELETE,EXECUTEON*.*TO'mycat'@'192.168.175.%';
FLUSHPRIVILEGES;
配置Mycat读写分离
修改Mycatd的schema.xml文件,实现binghe154和binghe155服务器上的MySQL读写分离。在Mycat安装目录的conf/zkconf目录下,修改schema.xml文件,修改后的schema.xml文件如下所示。
保存并退出vim编辑器,接下来,初始化Zookeeper中的数据,如下所示。
/usr/local/mycat/bin/init_zk_data.sh
上述命令执行成功后,会自动将配置同步到binghe151和binghe154服务器上的Mycat的安装目录下的conf目录下的schema.xml中。
接下来,分别启动binghe151和binghe154服务器上的Mycat服务。
mycatrestart
如何访问高可用环境
此时,整个高可用环境配置完成,上层应用连接高可用环境时,需要连接HAProxy监听的IP和端口。比如使用mysql命令连接高可用环境如下所示。
[root@binghe151~]#mysql-umycat-pmycat-h192.168.175.110-P3366--default-auth=mysql_native_password
mysql:[Warning]Usingapasswordonthecommandlineinterfacecanbeinsecure.
WelcometotheMySQLmonitor.Commandsendwith;or\g.
YourMySQLconnectionidis2
Serverversion:5.6.29-mycat-1.6.7.4-release-20200228205020MyCatServer(OpenCloudDB)
Copyright(c)2000,2019,Oracleand/oritsaffiliates.Allrightsreserved.
OracleisaregisteredtrademarkofOracleCorporationand/orits
affiliates.Othernamesmaybetrademarksoftheirrespective
owners.
Type'help;'or'\h'forhelp.Type'\c'toclearthecurrentinputstatement.
mysql>showdatabases;
+----------+
|DATABASE|
+----------+
|shop|
+----------+
1rowinset(0.10sec)
mysql>useshop;
Databasechanged
mysql>showtables;
+-----------------------+
|Tablesinshop|
+-----------------------+
|customer_balance_log|
|customer_inf|
|customer_level_inf|
|customer_login|
|customer_login_log|
|customer_point_log|
|order_cart|
|order_customer_addr|
|order_detail|
|order_master|
|product_brand_info|
|product_category|
|product_comment|
|product_info|
|product_pic_info|
|product_supplier_info|
|region_info|
|serial|
|shipping_info|
|warehouse_info|
|warehouse_proudct|
+-----------------------+
21rowsinset(0.00sec)
这里,我只是对binghe154服务器上的MySQL扩展了读写分离环境,大家也可以根据实际情况对其他服务器的MySQL实现主从复制和读写分离,这样,整个高可用环境就实现了HAProxy的高可用、Mycat的高可用、MySQL的高可用、Zookeeper的高可用和Keepalived的高可用。
企业前沿 21-07-14
企业前沿 21-07-14
前沿动态 21-07-14
前沿动态 21-07-14
业界 21-07-14
业界 21-07-14
电商 21-07-14
电商 21-07-14
国际新闻 21-07-14
国际新闻 21-07-14
业界 21-07-14
热点 21-07-14
热点 21-07-14
通信 21-07-14
通信 21-07-14
电子消费 21-07-14
电子消费 21-07-14
数码 21-07-14
数码 21-07-14
业界 21-07-13