CentOS 7.9 64位
2.2 禁用selinux(雙主節(jié)點(diǎn))
2.3 設(shè)置用戶的文件句柄及進(jìn)程數(shù)(雙主節(jié)點(diǎn))
2.4 系統(tǒng)內(nèi)核參數(shù)優(yōu)化(雙主節(jié)點(diǎn))
Vim /etc/sysctl.conf
vm.swappiness = 0
net.ipv6.conf.all.disable_ipv6 = 1
net.core.somaxconn = 65535
net.core.netdev_max_backlog = 65535
net.ipv4.tcp_max_syn_backlog = 65535
net.ipv4.tcp_fin_timeout = 10
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_keepalive_intvl = 30
net.ipv4.tcp_keepalive_probes = 3
net.ipv4.tcp_keepalive_time = 600
net.core.rmem_default = 262144
net.core.rmem_max = 4194304
net.core.wmem_default = 262144
net.core.wmem_max = 1048576
net.ipv4.ip_local_port_range = 40000 65500
kernel.sem = 250 32000 100 128
vm.min_free_kbytes = 524288
kernel.shmmni = 4096
kernel.shmall = 2097152
kernel.shmmax = 2542751744
vm.min_free_kbytes=524288
kernel.shmall = free/4
kernel.shmmax = free*1024*0.8
安裝mysql數(shù)據(jù)庫
在Oracle官網(wǎng)下載對應(yīng)版本的mysql免編譯二進(jìn)制安裝包解壓即可。
配置mysql
配置mysql master節(jié)點(diǎn):
▼▼▼
[mysql]
port=3306
socket=/data/mysql/tmp/mysql.sock
default-character-set=utf8
[mysqld_safe]
log-error=/data/mysql/log/mysql.error
[mysqld]
port=3306
user=shsnc
basedir=/home/shsnc/mysql
datadir=/data/mysql/data
socket=/data/mysql/tmp/mysql.sock
pid-file=/data/mysql/log/mysql.pid
character-set-server=utf8
skip-name-resolve
lower_case_table_names=1
transaction_isolation=READ-COMMITTED
server_id=1
autocommit=1
tmp_table_size=128M
max_heap_table_size=96M
max_connections=1000
max_connect_errors=6000
long_query_time=1
innodb_buffer_pool_size=16G
innodb_flush_log_at_trx_commit=0
innodb_log_buffer_size=8M
innodb_log_file_size=128M
innodb_log_files_in_group=2
innodb_file_per_table=1
innodb_flush_method=O_DIRECT
innodb_write_io_threads=8
innodb_read_io_threads=4
innodb_doublewrite=0
innodb_purge_threads=1
innodb_stats_on_metadata=OFF
innodb_io_capacity=1000
log-bin-trust-function-creators=1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER
event_scheduler=on
max_allowed_packet=2G
slow_query_log=1
auto_increment_offset=1
auto_increment_increment=2
#replication
log_bin=/data/mysql/log/bin.log
log_bin_index=/data/mysql/log/bin.index
sync_binlog=1
master_info_repository=TABLE
relay_log_info_repository=TABLE
sync_binlog=1
log_slave_updates
binlog_format=ROW
binlog_rows_query_log_events=1
relay_log=/data/mysql/log/relay.log
relay_log_recovery=1
slave_skip_errors=ddl_exist_errors
slave-rows-search-algorithms=INDEX_SCAN,HASH_SCAN
#gtid
enforce_gtid_consistency=1
gtid_mode=ON
master_info_repository=table
relay_log_info_repository=table
slave_parallel_type=logical_clock
slave_parallel_workers=4
slave_pending_jobs_size_max=1G
slave_preserve_commit_order=1
sync_master_info=1
sync_relay_log_info=1
注:配置slave節(jié)點(diǎn)的時(shí)候只需修改server_id選項(xiàng)即可。
啟動mysql服務(wù)
▼▼▼
mysqld_safe --defaults-file=/home/mysql/my_cnf/my.cnf &
創(chuàng)建mysql復(fù)制賬號
▼▼▼
create [email protected].% identified with mysql_native_password by xxxxxx;
grant REPLICATION SLAVE, REPLICATION CLIENT on *.* to [email protected].%;
flush privileges;
配置雙主同步
配置113同步到115:
▼▼▼
CHANGE MASTER TO
MASTER_HOST=192.168.1.113,
MASTER_USER=repl,
MASTER_PASSWORD=xxxxxx,
MASTER_PORT=3306,
MASTER_AUTO_POSITION=1;
Start slave;
配置115同步到113:
▼▼▼
CHANGE MASTER TO
MASTER_HOST=192.168.1.115,
MASTER_USER=repl,
MASTER_PASSWORD=xxxxxx,
MASTER_PORT=3306,
MASTER_AUTO_POSITION=1;
Start slave;
查看slave狀態(tài),Show slave statusG,若 IO、SQL進(jìn)程狀態(tài)都為YES則配置OK。
▼▼▼
./configure --prefix=/opt/keepalived && make && make install
配置keeepliaved
配置keepalived MASTER路由器
vi /opt/keepalived/etc/keepalived/keepalived.conf
▼▼▼
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
}
notification_email_from smtp.163.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script checkk_mysql {
script "/opt/keepalived/script/check_mysql.sh"
interval 5
fall 3
rise 2
}
vrrp_instance VI_1 {
state BACKUP
interface ens160
virtual_router_id 51
priority 100
nopreempt
advert_int 1
authentication {
auth_type PASS
auth_pass 110120
}
virtual_ipaddress {
135.10.51.8
}
track_script {
checkk_mysql
}
notify_master "/opt/keepalived/script/keepalived_notify.sh master"
notify_backup "/opt/keepalived/script/keepalived_notify.sh backup"
}
注:BACKUP路由器只需修改優(yōu)先級選項(xiàng)即可。
HA實(shí)時(shí)監(jiān)測腳本,檢查 mysql 進(jìn)程是否存在,如果mysql掛了就停止keepalived,使VIP漂移到BACKUP機(jī)器上
Vim /opt/keepalived/script/check_mysql.sh
▼▼▼
A=`ps -C mysqld --no-header |wc -l`
if [ $A -eq 0 ]
then
/bin/systemctl stop syskeepalived
fi
HA切換執(zhí)行腳本:
Vim /opt/keepalived/script/keepalived_notify.sh
▼▼▼
#!/bin/bash
. ~/.bash_profile
#mysql user
DB_USER="root"
#mysql user password
DB_PASSWORD=shsnc!@#
MYSQL_SOCK=""
#mysql_bin
MYSQL_BIN="/home/shsnc/mysql/bin/mysql"
#mysql client command
MYSQL_CMD="${MYSQL_BIN} -u${DB_USER} -p${DB_PASSWORD}"
#query the killed seesions id sql
MYSQL_SQL="select concat(kill ,id,;) from information_schema.processlist where user not in (system user,repl,replic,backup,bkpuser,bomcjk,root,myrobot)"
#關(guān)閉事件任務(wù)
AMP_EVENT_PARTITION_DISABLE="ALTER event domp.event_add_partitions ON COMPLETION PRESERVE DISABLE"
DOMP_EVENT_PARTITION_DISABLE="ALTER event amp.event_add_partitions ON COMPLETION PRESERVE DISABLE"
#開啟事件任務(wù)
AMP_EVENT_PARTITION_ENABLE="ALTER event domp.event_add_partitions ON COMPLETION PRESERVE ENABLE"
DOMP_EVENT_PARTITION_ENABLE="ALTER event amp.event_add_partitions ON COMPLETION PRESERVE ENABLE"
#define function: get mysql service information
function get_mysql_infor()
{
for sock in `ps -ef | grep mysqld | grep --socket= | awk -F--socket= {print $2} | awk {print $1}`
do
MYSQL_SOCK="${MYSQL_SOCK}$(echo $sock)"
done
}
#define function: mysql kill sessions
function kill_sessions()
{
#receive a mysql socket file parameter
#my_sock=$(get_mysql_infor)
unset MYSQL_SOCK
get_mysql_infor
# kill session
#..................
#begin to kill mysql sessions
${MYSQL_CMD} -S ${MYSQL_SOCK} -Ne "${MYSQL_SQL}" 2>/dev/null | ${MYSQL_CMD} > /dev/null 2>&1
}
#define function: set mysql read_only mode
function set_readonly()
{
#receive mysql socket file && read_only sign parameter
#my_sock=$(get_mysql_infor)
unset MYSQL_SOCK
get_mysql_infor
my_sign=$1
#begin to set mysql read_only mode
${MYSQL_CMD} -S ${MYSQL_SOCK} -Ne "set global read_only=${my_sign}" 2>/dev/null
}
#define function: keepalived state changed to master
function Keepalived_changed_to_master()
{
#my_sock=$(get_mysql_infor)
unset MYSQL_SOCK
get_mysql_infor
#開始事件任務(wù)
${MYSQL_CMD} -S ${MYSQL_SOCK} -Ne "${AMP_EVENT_PARTITION_ENABLE} 2>/dev/null "
${MYSQL_CMD} -S ${MYSQL_SOCK} -Ne "${DOMP_EVENT_PARTITION_ENABLE} 2>/dev/null "
Seconds_Behind_Master=$(${MYSQL_CMD} -S ${MYSQL_SOCK} -e "show slave statusG" | grep -w Seconds_Behind_Master | awk -F": " {print $2})
Slave_IO_Running=$(${MYSQL_CMD} -S ${MYSQL_SOCK} -e "show slave statusG" | grep -w Slave_IO_Running | awk -F": " {print $2})
Slave_SQL_Running=$(${MYSQL_CMD} -S ${MYSQL_SOCK} -e "show slave statusG" | grep -w Slave_SQL_Running | awk -F": " {print $2})
Master_Log_File=$(${MYSQL_CMD} -S ${MYSQL_SOCK} -e "show slave statusG" | grep -w Master_Log_File | awk -F": " {print $2})
Relay_Master_Log_File=$({MYSQL_CMD} -S ${MYSQL_SOCK} -e "show slave statusG" | grep -w Relay_Master_Log_File | awk -F": " {print $2})
Read_Master_Log_Pos=$({MYSQL_CMD} -S ${MYSQL_SOCK} -e "show slave statusG" | grep -w Read_Master_Log_Pos | awk -F": " {print $2})
Exec_Master_Log_Pos=$({MYSQL_CMD} -S ${MYSQL_SOCK} -e "show slave statusG" | grep -w Exec_Master_Log_Pos | awk -F": " {print $2})
echo
#
if [ "${Slave_IO_Running}" = "Yes" -a "${Slave_SQL_Running}" = "Yes" ]
then
if [ $Seconds_Behind_Master -eq 0 ]
then
echo 判斷復(fù)制無延遲,即將切換
#.........set mysql no read_only mode.........
set_readonly 0
exit 0
fi
fi
if [ "${Slave_IO_Running}" = "Connecting" -a "${Slave_SQL_Running}" = "Yes" ]
then
if [ "${Master_Log_File}" = "${Relay_Master_Log_File}" -a "${Read_Master_Log_Pos}" = "${Exec_Master_Log_Pos}" ]
then
echo "主庫宕機(jī),即將切換"
#.........set mysql no read_only mode.........
set_readonly 0
exit 0
fi
fi
}
#define function: keepalived state changed to backup
function Keepalived_changed_to_backup()
{
#my_sock=$(get_mysql_infor)
unset MYSQL_SOCK
get_mysql_infor
${MYSQL_CMD} -S ${MYSQL_SOCK} -Ne "${AMP_EVENT_PARTITION_DISABLE} 2>/dev/null "
${MYSQL_CMD} -S ${MYSQL_SOCK} -Ne "${DOMP_EVENT_PARTITION_DISABLE} 2>/dev/null "
#set mysql read_only mode
set_readonly 1
#kill mysql sessions
kill_sessions
}
#start this shell
case $1 in
master)
Keepalived_changed_to_master
;;
backup)
Keepalived_changed_to_backup
;;
*)
;;
esac
啟動keepalived
systemctl start keepalived
觀察113/115機(jī)器上的日志情況
tailf /var/logs/message
查看113/115機(jī)器上IP信息,查看VIP地址是否起來
ip addr
同時(shí)模擬keepalived故障時(shí)ip漂移情況以及主從節(jié)點(diǎn)只讀狀態(tài)設(shè)置情況:
▼▼▼
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, REFERENCES, INDEX, ALTER, CREATE TEMPORARY TABLES, EXECUTE, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, EVENT, TRIGGER ON *.* TO [email protected] identified by xxxxxx;
使用mysql客戶端命令行通過VIP地址訪問數(shù)據(jù)庫:
mysql -h135.10.51.8 -ushsnc -p
經(jīng)測試,通過VIP地址能正常訪問到mysql數(shù)據(jù)庫。
應(yīng)用修改數(shù)據(jù)庫配置文件,并啟動應(yīng)用及測試:
將配置文件中數(shù)據(jù)庫連接中配置IP 改為 HA方案的虛擬VIP: 135.10.51.8
經(jīng)測試,啟動應(yīng)用后,測試業(yè)務(wù)數(shù)據(jù)正常,且能正常寫入和讀取數(shù)據(jù),MySQL高可用配置ok。
更多精彩干貨分享
點(diǎn)擊下方名片關(guān)注
IT那活兒
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/129882.html
摘要:與大數(shù)據(jù)體系交互上報(bào)運(yùn)行統(tǒng)計(jì)數(shù)據(jù)自帶了運(yùn)行結(jié)果的統(tǒng)計(jì)數(shù)據(jù),我們希望把這些統(tǒng)計(jì)數(shù)據(jù)上報(bào)到元數(shù)據(jù)系統(tǒng),作為的過程元數(shù)據(jù)存儲下來?;谖覀兊拈_發(fā)策略,不要把有贊元數(shù)據(jù)系統(tǒng)的嵌入源碼,而是在之外獲取,截取出打印的統(tǒng)計(jì)信息再上報(bào)。一、需求 有贊大數(shù)據(jù)技術(shù)應(yīng)用的早期,我們使用 Sqoop 作為數(shù)據(jù)同步工具,滿足了 MySQL 與 Hive 之間數(shù)據(jù)同步的日常開發(fā)需求。 隨著公司業(yè)務(wù)發(fā)展,數(shù)據(jù)同步的場景越...
摘要:訪問令牌為提升系統(tǒng)安全性,配置中心和客戶端進(jìn)行安全性校驗(yàn),雙方匹配才允許通訊啟動時(shí),優(yōu)先全量加載鏡像數(shù)據(jù)到層,避免逐個(gè)請求耗時(shí)簡介是一個(gè)輕量級分布式配置管理平臺,擁有輕量級秒級動態(tài)推送多環(huán)境多語言配置監(jiān)聽權(quán)限控制版本回滾等特性。 Release Notes 1、輕量級改造:廢棄ZK,改為 DB + 磁盤 + long polling 方案,部署更輕量,學(xué)習(xí)更簡單;集群部署更方便,與單...
摘要:但你是否知道分庫分表需要哪些要素拆分過程是復(fù)雜的,提前計(jì)劃,不要等真正開工,各種意外的工作接踵而至,以至失控。在實(shí)施分庫分表策略時(shí),這些個(gè)性會造成策略過大不好維護(hù)。 更多文章關(guān)注微信公眾號《小姐姐味道》 https://mp.weixin.qq.com/s?__... 數(shù)據(jù)庫中間件之分庫分表 恭喜你,貴公司終于成長到一定規(guī)模,需要考慮高可用,甚至分庫分表了。但你是否知道分庫分表需要哪...
摘要:大家好,我是系統(tǒng)工程師王煜,今天由來分享在云計(jì)算平臺上構(gòu)建穩(wěn)定可靠的分布式系統(tǒng)架構(gòu)。接下來我來給大家介紹如果利用云計(jì)算的優(yōu)勢,結(jié)合企業(yè)的業(yè)務(wù)特點(diǎn)構(gòu)建穩(wěn)定可靠的分布式系統(tǒng)。 本次分享 William 將從技術(shù)角度分析在云計(jì)算環(huán)境中,當(dāng)用戶業(yè)務(wù)面對流量激增、數(shù)據(jù)量翻番、訪問量指數(shù)級攀升的煩惱時(shí),如何利用云計(jì)算平臺的彈性,結(jié)合業(yè)務(wù)自身特點(diǎn),設(shè)計(jì)和構(gòu)建一個(gè)高可用、高伸縮性的后端系統(tǒng)架構(gòu)。同時(shí)會...
閱讀 1356·2023-01-11 13:20
閱讀 1707·2023-01-11 13:20
閱讀 1215·2023-01-11 13:20
閱讀 1906·2023-01-11 13:20
閱讀 4165·2023-01-11 13:20
閱讀 2757·2023-01-11 13:20
閱讀 1402·2023-01-11 13:20
閱讀 3671·2023-01-11 13:20