mysql router

 

mgrDetection.sh

#!/bin/bash
  
export PATH=$PATH:/mysqlrouter/bin/
cd /mysqlrouter/scripts/

#  check whether mysql router is runnning,if not start it.
pnum=`ps -ef | grep mysqlrouter | grep -v 'grep\|scripts' | wc -l`
if [ "$pnum" -lt 1 ];then
        echo "`date`:mysql router is down,trying to restart mysql router"
        mysqlrouter &
        sleep 5
fi

# check connection to mgr,if fail then restart mysql router
DBport=`python3 dbCheck.py || echo failed`
if [ "$DBport" == "(3306,)" ];then
    if [ `date "+%M"` == "30" ];then
            echo "`date`:connection test successful."
    fi
else
        echo "`date`:connection to mgr faild,trying to restart mysql router."
        ps -ef| grep -i mysqlrouter |grep -v 'grep\|scripts' | awk '{print $2}'|xargs kill -9
        mysqlrouter &
fi
View Code

 

dbCheck.py

import mysql.connector
  
mydb = mysql.connector.connect(
                        host="localhost",
                        port=7001,
                        user="replica_user",
                        passwd="password")

mycursor = mydb.cursor()

mycursor.execute("SELECT @@PORT")

for x in mycursor:
        print(x)
View Code

 

服务脚本

cat >/etc/init.d/mysqlrouter<<EOF
#!/bin/bash
#chkconfig:2345 85 15
#processname:mysqlrouter
#description:mysqlrouter server daemon

start(){
/app/mysqlrouter/bin/mysqlrouter  -c /app/mysqlrouter/mysqlrouter.conf &
/bin/echo "Starting mysqlrouter...."
}
stop(){
for i in \`ps -ef|grep mysqlrouter|grep -v grep|grep -v status|grep -v restart|awk '{if(\$8!="grep")print \$2;}'\`
do
kill -9 \$i
done
}
status(){
if [ \`ps -ef|grep mysqlrouter|grep -v grep|grep -v status|awk '{if(\$8!="grep")print \$2;}'|wc -l\` -gt 0 ]
then
/bin/echo "\$0 mysqlrouter is running...."
/bin/ps -ef|grep mysqlrouter|grep -v grep |grep -v status
else
/bin/echo "\$0 mysqlrouter is stopped"
fi
}
restart(){
stop
start
}
case "\$1" in
"start")
start
;;
"stop")
stop
;;
"status")
status
;;
"restart")
restart
;;
*)
echo "usage: \$0 start|stop|status|restart"
;;
esac
EOF

 

chmod 755 /etc/init.d/mysqlrouter
chkconfig mysqlrouter on
service mysqlrouter start
View Code

 

 

*/3 * * * * /mysqlrouter/scripts/mgrDetection.sh  >> /mysqlrouter/log/mgrDetection.log  2>&1

 

 

 

 https://www.cnblogs.com/hbxZJ/p/10224984.html

 

mysqlrouter --user=root

 

 

 

 

[DEFAULT]
# 日志路径
logging_folder = /data/myrouter/log

# 插件路径
plugin_folder = /data/myrouter/lib/mysqlrouter

# 配置路径
config_folder = /data/myrouter/config

# 运行时状态路径
runtime_folder = /data/myrouter/run

# 数据文件路径
data_folder = /data/myrouter/data

[logger]
# 日志级别
level = INFO

# 以下选项可用于路由标识的策略部分
[routing:basic_failover]
# Router地址
bind_address = 127.0.0.1
# Router端口
bind_port = 7001
# 读写模式
mode = read-write
# 目标服务器
destinations = 192.168.136.166:3307,192.168.136.166:3308

[routing:load_balance]
bind_address = 127.0.0.1

bind_port = 7002
mode = read-only
destinations = 192.168.136.166:3307,192.168.136.166:3308

posted @ 2020-12-30 17:59  sam_wang10  阅读(183)  评论(0编辑  收藏  举报