[root@s1 mr]# mysql -uroot -pP@ssword1! -h192.168.100.21 -P7002 -e 'select @@server_id;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+-------------+
| @@server_id |
+-------------+
| 110 |
+-------------+
[root@s1 mr]# mysql -uroot -pP@ssword1! -h192.168.100.21 -P7002 -e 'create database mytest;'
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@s1 mr]# mysql -uroot -pP@ssword1! -h192.168.100.21 -P7002 -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| mytest |
| performance_schema |
| sys |
+--------------------+
再测试下各slave节点,是否能实现rr调度算法的读请求的负载均衡。
[root@s1 mr]# mysql -uroot -pP@ssword1! -h192.168.100.21 -P7001 -e 'select @@server_id;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+-------------+
| @@server_id |
+-------------+
| 120 |
+-------------+
[root@s1 mr]# mysql -uroot -pP@ssword1! -h192.168.100.21 -P7001 -e 'select @@server_id;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+-------------+
| @@server_id |
+-------------+
| 130 |
+-------------+
[root@s1 mr]# mysql -uroot -pP@ssword1! -h192.168.100.21 -P7001 -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| mytest |
| performance_schema |
| sys |
+--------------------+
3.MySQL Router的配置文件解释
MySQL Router的配置文件也很简单,需要配置的项不多。
mysql router默认会寻找安装目录下的"mysqlrouter.conf"和家目录下的".mysqlrouter.conf"。也可以在二进制程序mysqlrouter命令下使用"-c"或者"--config"手动指定配置文件。
MySQL router的配置文件是片段式的,常用的就3个片段:[DEFAULT]、[logger]、[routing:NAME]。片段名称区分大小写,且只支持单行"#"或";"注释,不支持行中、行尾注释。
以上面示例的配置文件为例。
4.为MySQL Router提供SysV脚本
MySQL Router只提供了一个主程序(bin目录下的mysqlrouter),且该程序只能启动,没有停止选项,所以只能使用kill命令来杀掉进程。
MySQL Router也提供了示例启动脚本,该脚本在位置为$basedir/share/doc/mysqlrouter/sample_mysqlrouter.init,但是该脚本是基于Debian平台的,在CentOS上需要设置和安装一些东西,所以不用它,自己写个粗糙点的脚本即可。
shell> vim /etc/init.d/mysqlrouter
#!/bin/bash
# chkconfig: - 78 30
# Description: Start / Stop MySQL Router
DAEMON=/usr/local/mysqlrouter
proc=$DAEMON/bin/mysqlrouter
DAEMON_OPTIONS="-c ${DAEMON}/mysqlrouter.conf"
. /etc/init.d/functions
start() {
if [ -e /var/lock/subsys/mysqlrouter ]; then
action "MySQL Router is working" /bin/false
else
$proc $DAEMON_OPTIONS & &>/dev/null
retval=$?
echo
if [ $retval -eq 0 ]; then
touch /var/lock/subsys/mysqlrouter
action "Starting MySQL Router" /bin/true
else
echo "Starting MySQL Router Failure"
fi
fi
}
stop() {
if [ -e /var/lock/subsys/mysqlrouter ]; then
killall $proc
retval=$?
echo
if [ $retval -eq 0 ]; then
rm -f /var/lock/subsys/mysqlrouter
action "Stoping MySQL Router" /bin/true
fi
else
action "MySQL Router is not working" /bin/false
fi
}
status() {
if [ -e /var/lock/subsys/mysqlrouter ]; then
echo "MySQL Router is running"
else
echo "MySQL Router is not running"
fi
}
case "$1" in
start)
start
sleep 1
;;
stop)
stop
sleep 1
;;
restart)
stop
start
sleep 1
;;
status)
status
;;
*)
echo "Usage: $0 {start|stop|status|restart}"
retval=1
;;
esac
exit $retval