当前位置:网站首页>MySQL synchronous data configuration and shell script implementation
MySQL synchronous data configuration and shell script implementation
2022-06-25 11:04:00 【Jacob_ Always】
- mysql Synchronize configuration settings :
Both servers have mysql database , Modify the configuration file :
vim /etc/my.cnf The path to mysql Profile path
Synchronization settings need to be configured server_id and replicate-do-db attribute , You need to configure the synchronization user ;
stay “[mysqld]” Add the name of the database to be synchronized under the section
For example, you need to synchronize radius library :
MySQL [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| omc_pm_report |
| performance_schema |
| **radius** |
| sas |
| sys |
+--------------------+
8 rows in set (0.00 sec)
stay my.cnf Of [mysqld] Add the following configuration under section :
[mysqld]
replicate-do-db=radius
To configure server-id
# required unique id between 2 and 2^32 - 1
# (and different from the master)
# defaults to 2 if master-host is set
# but will not function as a slave if omitted
#server-id = 1
According to the prompt , The active and standby cannot use the same server-id, My Lord here mysql Configure to 1, To prepare mysql Configure to 2, Other values are also acceptable ;
Come here , The configuration required by the configuration file has been configured .
Below is the configuration synchronization user :
Give Way root The user to use password Connect from any host to the local mysql The server
GRANT ALL PRIVILEGES ON *.* TO "root"@"%" IDENTIFIED BY 'password' WITH GRANT OPTION;
stay mysql Configure... In command mode :
[[email protected] ~]# mysql -uroot -p password
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 2761
Server version: 5.7.36-log MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]> **GRANT ALL PRIVILEGES ON *.* TO "root"@"%" IDENTIFIED BY ' password ' WITH GRANT OPTION;**
MySQL [(none)]> flush privileges;
Remember to use flush privileges The configuration takes effect immediately ;
see root Is the configuration successful? :
MySQL [(none)]> select Host,User from mysql.user;
+-------------+---------------+
| Host | User |
+-------------+---------------+
| % | root |
| localhost | mysql.session |
| localhost | mysql.sys |
| localhost | root |
+-------------+---------------+
7 rows in set (0.00 sec)
MySQL [(none)]>
Configure the active / standby relationship :
MySQL [(none)]>stop slave;
MySQL [(none)]>reset slave;
MySQL [(none)]>change master to master_host=' Standby machine IP',master_user='root',master_password=' password ';
MySQL [(none)]>start slave;
Standby machine IP Configure peer IP address ;
Restart after configuration mysql service
systemctl restart mysql
View active and standby status :
MySQL [radius]> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: IP.IP.IP.IP there ip The address should be opposite ip Address
Master_User: root
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000029
Read_Master_Log_Pos: 22008
Relay_Log_File: localhost-relay-bin.000003
Relay_Log_Pos: 7945086
Relay_Master_Log_File: mysql-bin.000028
Slave_IO_Running: Yes Check these three items
Slave_SQL_Running: Yes Check these three items
Replicate_Do_DB: radius Check these three items
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
If all the states are the same as above , So at this time ,mysql The synchronization function has been configured successfully ;
At this time, it is better to use the host A Of radius The library creates a jacob_test surface , Standby machine B It can also be found on the :
host A:
MySQL [radius]> create table test(val int);
Query OK, 0 rows affected (0.13 sec)
host B:
MySQL [radius]> show tables;
+------------------+
| Tables_in_radius |
+------------------+
| jacob_test |
+------------------+
14 rows in set (0.00 sec)
Script configuration : Create a script named :mysqlha.sh Script for , Among them HA_STATUS Judgment is negligible
Insert a code chip here [[email protected] mysql]# cat haMysql.sh #!/bin/bash - MY_CNF="./my.cnf" MYSQLD="\[mysqld\]" HA_STATUS=/usr/lib/eGW/.ha.status function mysql_ha_replicate_db() {
echo "The function of replicate" >> /var/log/messages
if [ `grep -c "$MYSQLD" $MY_CNF` -ne '0' ];then echo "The File Has mysqld section" >> /var/log/messages line=`cat ${
MY_CNF} |grep -n "replicate-do-db" |grep -v "\#" |awk -F: '{
print $1}' |sed -n if [ "X${
line}" != "X" ]; then sed -i "${
line}c replicate-do-db=radius" $MY_CNF
if [ $? -eq 0 ]; then
echo "$LINENO: set replicate-do-db=radius successfully" >> /var/log/message
else
echo "$LINENO: set replicate-do-db=radius failed" >> /var/log/messages
fi
else
echo "Append modify" >> /var/log/messages
sed -i '/\[mysqld\]/a\'replicate-do-db=radius'' $MY_CNF
if [ $? -eq 0 ]; then
echo "set replicate-do-db=radius successfully" >> /var/log/messages
else
echo "set replicate-do-db=radius failed" >> /var/log/messages
fi
fi
else
echo "The File don't have mysqld section,Append Configuration" >> /var/log/messages
echo "[mysqld]" >> $MY_CNF
echo "replicate-do-db=radius" >> $MY_CNF
fi
}
function mysql_ha_server_id() {
echo "The function of server_id" >> /var/log/messages
local ha_status=$(cat $HA_STATUS)
echo "The server status:$ha_status" >> /var/log/messages
if [[ $ha_status == "MASTER" ]];then echo "local server is master!" >> /var/log/messages line=`cat ${
MY_CNF} |grep -n "server-id" |grep -v "\#" |awk -F: '{
print $1}' |sed -n 1p` if [ "X${
line}" != "X" ]; then sed -i "${
line}c server-id=1" $MY_CNF
if [ $? -eq 0 ]; then
echo "$LINENO: set maseter server-id=1 successfully" >> /var/log/messages
else
echo "$LINENO: set maseter server-id=1 failed" >> /var/log/messages
fi
else
echo "Skip modify" >> /var/log/messages
fi
elif [[ $ha_status == "BACKUP" ]];then echo "local server is backup!" >> /var/log/messages line=`cat ${
MY_CNF} |grep -n "server-id" |grep -v "\#" |awk -F: '{
print $1}' |sed -n 1p` if [ "X${
line}" != "X" ]; then sed -i "${
line}c server-id=2" $MY_CNF
if [ $? -eq 0 ]; then echo "$LINENO: set backup server-id=2 successfully" >> /var/log/messages else echo "$LINENO: set backup server-id=2 failed" >> /var/log/messages fi else echo "Skip modify" >> /var/log/messages fi else echo "Independent deployment" >> /var/log/messages line=`cat ${
MY_CNF} |grep -n "server-id" |grep -v "\#" |awk -F: '{
print $1}' |sed -n 1p` if [ "X${
line}" != "X" ]; then sed -i "${
line}c server-id=1" $MY_CNF
if [ $? -ne 0 ]; then
echo "$LINENO: set Independent server-id=1 successfully" >> /var/log/messag
else
echo "$LINENO: set Independent server-id=1 failed" >> /var/log/messages
fi
else
echo "Skip modify" >> /var/log/messages
fi
exit 0
fi
}
function mysql_ha_sql_command() {
echo "The function of sql_command" >> /var/log/messages HA_CONF=/etc/eGW/ha.conf local ha_slave_ip=$(awk -F ' = ' '/^slaveip/{
print $2}' $HA_CONF)
mysql -uroot [email protected] -e "
GRANT ALL PRIVILEGES ON *.* TO \"root3\"@\"%\" IDENTIFIED BY '[email protected]' WITH GRANT OPTION;
flush privileges;
stop slave;
reset slave;
change master to master_host='$ha_slave_ip',master_user='root',master_password='[email protected]';
start slave;
quit"
}
function mysql_ha() {
echo "**********Execute HA mysql function**********" >> /var/log/messages
mysql_ha_replicate_db
mysql_ha_server_id
mysql_ha_sql_command
echo "************End HA mysql function************" >> /var/log/messages
}
mysql_ha
边栏推荐
- Get to know Prometheus
- Flask blog practice - realize personal center and authority management
- Detailed explanation of Android interview notes handler
- Daily 3 questions (2) - find out the lucky numbers in the array
- 性能之内存篇
- 网络远程访问的方式使用树莓派
- New school: no fraud Economics
- Oracle彻底卸载的完整步骤
- Cdn+cos ultra detailed steps for drawing bed construction
- Handling of NPM I installation problems
猜你喜欢

Previous string inversion topic

Coscon'22 lecturer solicitation order

How to install SSL certificates in Microsoft Exchange 2010

FPGA基于VGA显示字符及图片

【文件包含漏洞-03】文件包含漏洞的六种利用方式

Detailed explanation of Android interview notes handler

单片机开发---基于ESP32-CAM的人脸识别应用

Handler asynchronous message processing

Opencv learning (II) -- installing opencv on raspberry pie

WPF prism framework
随机推荐
Flask blog practice - archiving and labeling of sidebar articles
Is it safe to open a securities account in changtou school by mobile phone?
Server rendering
开源社邀请您参加OpenSSF开源安全线上研讨会
单片机开发---基于ESP32-CAM的人脸识别应用
Shen Ying, China Academy of communications and communications: font open source protocol -- Introduction to ofl v1.1 and analysis of key points of compliance
[the path of system analyst] Chapter 6: Double inventory demand engineering (comprehensive knowledge concept)
Advanced single chip microcomputer -- development of PCB (2)
Opencv learning (I) -- environment building
FPGA displays characters and pictures based on VGA
Flutter adds event listening | subscription
【文件包含漏洞-03】文件包含漏洞的六种利用方式
Coscon'22 lecturer solicitation order
性能之文件系统篇
Flask blog practice - realize personal center and authority management
16 种企业架构策略
NETCORE performance troubleshooting
Handler asynchronous message processing
Remove the problem of orange border on the desktop control in WebView
Comparison and evaluation of digicert and globalsign single domain ov SSL certificates