First, the preface
Using a proxy server haproxy load balancing for MySQL is a common scenario, to improve availability, when a MySQL problem occurs, such as a server failure, or data replication is interrupted, it is best to let haproxy know immediately, and then stop forwarding requests to it
Haproxy How do you know if Mysql is a problem?
Second, the solution to the idea
(1) Write a shell script, check the state of MySQL, and then output the result, for example, when the state is normal, return status code 200 and correct information, or return status code 503 and error message
(2) Implement an HTTP service, after the request is connected, call the above check script, return the check result
(3) Haproxy accesses this HTTP service to determine whether the MySQL is available based on the returned result information
How to quickly implement an HTTP service that can invoke a shell script?
The more common solution is xinetd.
XINETD is the daemon of Linux, all called Extended interent daemon, extended Network daemon
XINETD can open a port, wait for a connection, and you can tell xinetd which script to run, and when a connection comes in, XINETD executes the script and returns the contents of the script output directly.
HAProxy -> xinetd -> mysql-check
Script, Haproxy Gets the status information of MySQL
Configuration Case for xinetd
Let's implement a simple xinetd example, open port 9200, and return the output of a test script
If there is no xinetd on the machine, install it first, CENTOS7 can use the commandyum install xinetd
(1) Test script
#!/bin/bash
echo ' uptime | egrep-o ' Up ([0-9]+] days ' | awk ' {print $} '
This script is used to output the number of days online for the current server
chmod +x
to add executable permissions to a script
(2) Writing xinetd service
For example, the service name, helloworld
file name /etc/xinetd.d/helloworld
, content is
Service HelloWorld
{
disable = no
port = 9200
socket_type = Stream
Protocol = TCP
Wait = no
user = root
Server =/root/test.sh
Server_args = Test
}
port
Specify the port on which to listen
server
Specify the script to execute
(3) Join the list of services
Modify/etc/services
Find the location that defines Port 9200, comment out the original, add the helloworld
service
HelloWorld 9200/tcp
#wap-wsp 9200/tcp
#wap-wsp 9200/udp
(4) Restart xinetd
The restart command under CentOS is:
Systemctl Restart Xinetd.service
(5) test
Test with nc
telnet
two commands, respectively.
Can correctly output script execution, complete the sample
Summarize
Linux shell Implementation of the content of HTTP services to this is basically the end of the hope for everyone's work and learning can help, if there are questions you can leave a message to discuss.