操作系统版本
CENTOS7.6
MYSQL5.7.22
啥?为啥我的操作系统木有iso的安装镜像,哎算了,不能弄本地yum源,那就弄个网络的吧。只不过默认的用不了,那就自己动手丰衣足食
cd /etc/yum.repos.d/
mkdir bak
cp *.repo bak/
# wget
-bash: wget: 未找到命令
WTF?原来是
原本的CentOS-Base.repo已被删除,所以没办法安装wget命令,所以用curl -O来获取
#curl -O
vi CentOS7-Base-163.repo
替换一些东西,不然可能无法使用
:%s#$releasever#7#g
yum list
OK!YUM源搞定了,下面开始搭建MYSQL
一、MYSQL源码安装
(两个节点都安装,步骤一样的)
安装环境:
操作系统CENTOS7.6
NODE1 主机名 tsdb1 IP地址 192.168.159.4
NODE2 主机名 tsdb2 IP地址 192.168.159.5
1、安装依赖
yum -y install make gcc-c++ cmake bison-devel ncurses-devel perl
2、创建用户和组
groupadd mysql
useradd mysql -g mysql -M -s /sbin/nologin
3、下载5.7.22源码包
4、解压tar -zxvf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
cd /opt/
tar -xvf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
mv /opt/mysql-5.7.22-linux-glibc2.12-x86_64/ /opt/mysql-5.7.22
5、安装
mkdir /data/mysql5.7.22/data/ -p
cd /data/mysql5.7.22
chown -R mysql:mysql ./
/opt/mysql-5.7.22/bin/mysqld --initialize --user=mysql --basedir=/data/mysql5.7.22
--datadir=/data/mysql5.7.22/data/ --lc_messages_dir=/opt/mysql-5.7.22/share/ --lc_messages=en_US
2019-05-25T13:01:50.716538Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-05-25T13:01:51.897953Z 0 [Warning] InnoDB: New log files created, LSN=45790
2019-05-25T13:01:52.039050Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2019-05-25T13:01:52.116959Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 43c9f84c-7eed-11e9-a241-fefcfe4975bb.
2019-05-25T13:01:52.118956Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2019-05-25T13:01:52.119626Z 1 [Note] A temporary password is generated for root@localhost: kar)*.OtV4F)
安装速度贼快,刷~~~的一下就完事了。
kar)*.OtV4F)
这个是自动生成的临时密码
(如果看到有报这个错的直接安装就好)
bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
解决方法: yum install -y libaio //安装后再去初始化就OK了)
#将mysql/目录下除了data/目录的所有文件,改回root用户所有
chown -R root .
#mysql用户只需作为mysql5.7.22/data/目录下所有文件的所有者
chown -R mysql data
5.复制启动文件
cd /opt/mysql-5.7.22
cp /opt/mysql-5.7.22/support-files/mysql.server /etc/init.d/mysqld
chmod 755 /etc/init.d/mysqld
cp /opt/mysql-5.7.22/bin/my_print_defaults /usr/bin/
mkdir /data/mysql5.7.22/bin
cp /opt/mysql-5.7.22/bin/* /data/mysql5.7.22/bin/
6.修改启动脚本
vi /etc/init.d/mysqld
#修改项:
basedir=/data/mysql5.7.22/
datadir=/data/mysql5.7.22/data/
port=3306
#加入环境变量,编辑 /etc/profile,这样可以在任何地方用mysql命令了
vi /etc/profile
#添加mysql路径,加入下面内容,按ESC-->:wq保存
export PATH=$PATH:/data/mysql5.7.22/bin
source /etc/profile
7.修改mysql配置项(第一次启动这样配置)
vi /etc/my.cnf
[mysqld]
basedir=/data/mysql5.7.22
datadir=/data/mysql5.7.22/data
[mysqld_safe]
log-error=/var/log/mysqld.log
8.启动mysql
service mysqld start
有遇到无法启动的情况,第一是将配置文件修改成上面简单的方式,第二是删除data目录下的文件,重新初始化。
mysql -uroot -pkar)*.OtV4F)
遇到无法启动报错的情况,例如:
2019-12-31T07:49:42.258557Z 0 [ERROR] Can't open the mysql.plugin table. Please run mysql_upgrade to create it.
2019-12-31T07:49:42.259636Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2019-12-31T07:49:42.259803Z 0 [Warning] Failed to set up SSL because of the following SSL library error: SSL context is not usable without certificate and private key
2019-12-31T07:49:42.260385Z 0 [Note] Server hostname (bind-address): '*'; port: 3306
2019-12-31T07:49:42.260446Z 0 [Note] IPv6 is available.
2019-12-31T07:49:42.260462Z 0 [Note] - '::' resolves to '::';
2019-12-31T07:49:42.260487Z 0 [Note] Server socket created on IP: '::'.
2019-12-31T07:49:42.261927Z 0 [Note] InnoDB: Loading buffer pool(s) from /data/mysql/mysql/ib_buffer_pool
2019-12-31T07:49:42.262277Z 0 [Note] InnoDB: Buffer pool(s) load completed at 191231 15:49:42
2019-12-31T07:49:42.264556Z 0 [Warning] Failed to open optimizer cost constant tables
2019-12-31T07:49:42.264839Z 0 [ERROR] Fatal error: Can't open and lock privilege tables: Table 'mysql.user' doesn't exist
2019-12-31T07:49:42.264937Z 0 [ERROR] Aborting
(遇到这个报错问题,有可能是因为安装目录设置不对导致的,我试过自己重新安装
一个测试库,然后没有按笔记上的目录设置,自己设置了新的不同的目录)
登陆后第一件事是先修改密码,否则敲啥命令都报错,老老实实改密码
进入mysql修改初始密码,修改远程连接的用户权限问题
alter user 'root'@'localhost' identified by 'TaB)*.Wtf4F)';
flush privileges; #刷新权限
mysql -uroot -pTaB)*.Wtf4F)
use mysql;
update user set host = '%' where user ='root';
flush privileges;
如果遇到报错1:
Starting MySQL.Logging to '/data/mysql5.7.22/data/mysqld.log'.
ERROR! The server quit without updating PID file (/data/mysql5.7.22/data//lcdb2.pid).
cd /data/mysql5.7.22/data/
chown -R mysql.mysql .
如果遇到报错2:
[root@lcdb1 data]# mysql -uroot -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
alter user 'root'@'%' identified by '****';
flush privileges;
mysql -uroot -p"****"
use mysql;
update user set host = '%' where user ='root';
flush privileges;
如果遇到报错3:
mysql> alter user 'root'@'%' identified by '****';
ERROR 1820 (HY000): Unknown error 1820
SET PASSWORD = PASSWORD('****');
flush privileges;
select user,host,authentication_string from mysql.user;
create user 'root'@'%' identified by '****';
grant all privileges on *.* to 'root'@'%';
flush privileges;
如果遇到报错4:
[root@lcdb1 data]# mysql -uroot -p"****" -h192.168.159.4
mysql: [Warning] Using a password on the command line interface can be insecure.
touch /etc/my.password
vi /etc/my.password
[client]
user=root
password="****"
host=127.0.0.1
使用以下命令登录可以消除[Warning]
mysql --defaults-extra-file=/etc/my.password
CENTOS7.6版本开机不会自启动,需修改权限
chmod +x /etc/rc.d/rc.local
主从搭建
主库配置
vi /etc/my.cnf
[mysqld]
server-id=101
datadir=/data/mysql5.7.22/data/
socket=/tmp/mysqld.sock
user=mysql
log-bin=/data/mysql5.7.22/data/mysql-bin.log
log-bin-index=/data/mysql5.7.22/data/mysql-bin.index
expire_logs_days=7
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
####Parameter configuration######
collation-server = utf8_unicode_ci
init-connect='SET NAMES utf8'
character-set-server = utf8
binlog_format="MIXED"
max_allowed_packet = 4M
net_buffer_length = 8K
key_buffer_size=384M
query_cache_type=0
query_cache_size=0
tmp_table_size=512
innodb_buffer_pool_size = 512M
innodb_log_buffer_size=8M
sort_buffer_size=1024
read_buffer_size=1024
read_rnd_buffer_size=768
join_buffer_size=2048
thread_stack=256
binlog_cache_size=128
thread_cache_size=128
table_open_cache = 384
max_connections = 512
innodb_flush_log_at_trx_commit = 2
default-time_zone = '+8:00'
innodb_flush_log_at_trx_commit=2
sync_binlog=0
wait_timeout=500
innodb_flush_log_at_trx_commit=2
sync_binlog=0
#########slow log######
slow_query_log=1
slow_query_log_file=/data/mysql5.7.22/mysql.slow
long_query_time=2
[mysqld_safe]
log-error=/data/mysql5.7.22/data/lcdb1.log
重启
service mysqld restart
主库配置同步用户
grant replication slave on *.* to 'Master-slave'@'192.168.159.5' identified by 'TaB)*.Wtf4F)';
flush privileges;
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 | 601 | Master-slave | mysql | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
从库配置(其实就是server-id修改一下比主库数字大即可)
vi /etc/my.cnf
[mysqld]
server-id=102
datadir=/data/mysql5.7.22/data/
socket=/tmp/mysqld.sock
user=mysql
log-bin=/data/mysql5.7.22/data/mysql-bin.log
log-bin-index=/data/mysql5.7.22/data/mysql-bin.index
expire_logs_days=30
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
####Parameter configuration######
collation-server = utf8_unicode_ci
init-connect='SET NAMES utf8'
character-set-server = utf8
binlog_format="MIXED"
max_allowed_packet = 4M
net_buffer_length = 8K
key_buffer_size=384M
query_cache_type=0
query_cache_size=0
tmp_table_size=512
innodb_buffer_pool_size = 512M
innodb_log_buffer_size=8M
sort_buffer_size=1024
read_buffer_size=1024
read_rnd_buffer_size=768
join_buffer_size=2048
thread_stack=256
binlog_cache_size=128
thread_cache_size=128
table_open_cache = 384
max_connections = 512
innodb_flush_log_at_trx_commit = 2
default-time_zone = '+8:00'
innodb_flush_log_at_trx_commit=2
sync_binlog=0
wait_timeout=500
innodb_flush_log_at_trx_commit=2
sync_binlog=0
#########slow log######
slow_query_log=1
slow_query_log_file=/data/mysql5.7.22/mysql.slow
long_query_time=2
[mysqld_safe]
log-error=/data/mysql5.7.22/data/lcdb2.log
重启
service mysqld restart
在主库查询ID号和日志文件号
show master status;
再登陆从库
从库
mysql> CHANGE MASTER TO MASTER_HOST='192.168.159.4',MASTER_USER='Master-slave',MASTER_PASSWORD='TaB)*.Wtf4F)',MASTER_PORT=3306,MASTER_LOG_FILE='mysql-bin.000002',MASTER_LOG_POS=601,MASTER_CONNECT_RETRY=10;
start slave;
查看
show slave status \G
这里有些地方还有疑惑,比如我在配置文件增加以下信息会无法启动数据库
[mysqld_safe]
log-error=/data/mysql5.7.22/log/tsdb1_error.log
pid-file=/data/mysql5.7.22/data/tsdb1.pid
启动会遇到报错
service mysqld start
Starting MySQL. ERROR! The server quit without updating PID file (/data/mysql5.7.22/data//tsdb1.pid).
一直查资料无法解决
以上都是我根据各种网上资料加上自己遇到的问题,总结修改后的,只作为写给自己的笔记,方便下次搭建的时候查看,只不过那个设置只给自己看的功能不见了。
注意一个问题,在生产环境,要注意时区问题
[mysqld]
default-time_zone = '+8:00'
使用北京时间的时区
error 1045密码错误
select * from mysql.user where user='root';
use mysql;
update user set authentication_string=password('123456') where user='root' and host='localhost';
update user set authentication_string=password('123456') where user='root' and host='%';
flush privileges;
error 1086密码过期
use mysql;
update user set password_expired="N" where user="root";
flush privileges;
主从切换
mysql主从关系中,如果master宕机了,则要提升slave为主,
等原来的主库恢复之后,则要把老的主库变为从库,
这样才能尽最大可能的保证应用层面的业务高可用性和数据的完整性。
## mysql主从切换为从主:
从库变主库
将要由现在的从库切换为主库:
show processlist;# 确保Slave has read all relay log;
STOP SLAVE IO_THREAD
show slave status \G;检查IO及SQL线程是否正常,如果为NO表明同步不一致
stop slave;
reset master;
reset slave all;(看版本号)
grant replication slave on *.* repl@'IP' identified by '密码';
show master status;#记住这里列出的log_file和log_pos信息
------------------------------------------------------------------------------
## 主库变从库
将要由现在的主库切换成从库
Reset master;(新的slave,老的master)
Reset slave;
change master to master_host="IP",
master_port=3306,
master_user="repl",
master_password="密码",
master_log_file="...",#上一步中列出的log_file
master_log_pos="...";#上一部中列出的log_pos
start slave;
show slave status\G;
目前只是基础搭建,还有很多优化的参数没有验证测试,后续再补上
补充一下:
MYSQL还有一个和性能有关的关键参数,生产环境遇到的一个性能问题
innodb_flush_log_at_trx_commit
该参数控制重做日志写入磁盘的过程。我们知道 InnoDB 使用“Write Ahead Log”策略来避免数据丢失问题,即依靠重做日志来保证数据能在丢失后进行恢复。因此,InnoDB 重做日志的持久化非常重要。
该参数的有效值有 0、1、2:
0:事务提交时,不将重做日志缓冲写入磁盘,而是依靠 InnoDB 的主线程每秒执行一次刷新到磁盘。因此如果 MySQL 发生宕机,那么就有可能丢失一部分事务。
1:事务提交时,会将重做日志缓冲写入磁盘,并且立即刷新(fsync())。注意,因为操作系统的“延迟写”特性,此时的刷入只是写到了操作系统的缓冲区中,因此执行同步操作才能保证一定持久化到了硬盘中。
2:事务提交时,会将重做日志缓冲写入磁盘,但是不会立即进行刷新操作,因此只是写到了操作系统的缓冲区。此时若操作系统发生宕机而没有即使的同步,也可能会丢失一部分数据。
可以看到,只有1才能真正地保证事务的持久性,但是由于刷新操作 fsync() 是阻塞的,直到完成后才返回,我们知道写磁盘的速度是很慢的,因此 MySQL 的性能会明显地下降。如果不在乎事务丢失,,0和2能获得更高的性能。
sync_binlog
该参数控制着二进制日志写入磁盘的过程。
该参数的有效值为0 、1、N:
0:默认值。事务提交后,将二进制日志从缓冲写入磁盘,但是不进行刷新操作(fsync()),此时只是写入了操作系统缓冲,若操作系统宕机则会丢失部分二进制日志。
1:事务提交后,将二进制文件写入磁盘并立即执行刷新操作,相当于是同步写入磁盘,不经过操作系统的缓存。
N:每写N次操作系统缓冲就执行一次刷新操作。
二进制日志文件涉及到数据的恢复,以及想在主从之间获得最大的一致性,那么应该将该参数设置为1,但同时也会造成一定的性能损耗。
通常,会将这两个参数都设置成1来保证数据的安全,但是如果在某些情况下性能更重要,那么可以考虑将其设为其他值来获得最大的性能。
设置为1,最安全,但是有较大的性能损失;
设置为0,效率最高,但是最不安全;
设置为2,只要操作系统不崩溃,数据不会丢失;
一般有主从架构可设置为(主主架构也可以,或者对数据安全要求不高),这样设置的好处是提高IO性能,面对大并发也有一定的性能提升
innodb_flush_log_at_trx_commit=2
sync_binlog=0
遇到报错:
service mysqld start
Starting MySQLCouldn't find MySQL server (/data/mysql5.7.22/bin/mysqld_safe)[失败]
检查bin
cp /opt/mysql5.7.22/bin/* /data/mysql5.7.22/bin/
遇到报错2:
service mysqld start
Starting MySQL.Logging to '/data/mysql5.7.22/data/mysqld.log'.
ERROR! The server quit without updating PID file (/data/mysql5.7.22/data//lcdb2.pid).
切换到数据库数据目录
cd /data/mysql5.7.22/data/
chown -R mysql.mysql .
遇到报错3:
主从搭建报错,从库发现
Slave_IO_Running: Connecting
检查 主服务器查看主从同步帐号,已设置登录ip为%
mysql> select user,host from mysql.user;
+---------------+-----------------+
| user | host |
+---------------+-----------------+
| repl_user | % |
遇到报错4:
Slave_IO_Running: NO
检查数据库数据目录下的文件是否都是mysql权限 ,如果没有
chown -R mysql.mysql .
检查防火墙iptables 和 getenforce
遇到报错5:
遇到报错:
2021-08-26T09:02:07.121168Z 0 [ERROR] Can't find error-message file '/data/mysql/errmsg.sys'. Check error-message file location and 'lc-messages-dir' configuration directive.
2021-08-26T09:02:07.122505Z 0 [ERROR]
2021-08-26T09:02:07.122523Z 0 [ERROR] Aborting
log-error 路径设置不对,有目录没创建或者权限之类的
keepalived搭建
安装包:(rpm在某些版本操作系统需要用到)
keepalived-1.4.2.tar.gz
libnfnetlink-1.0.0-1.el6.x86_64.rpm
libnfnetlink-devel-1.0.0-1.el6.x86_64.rpm
libnl-devel-1.1.4-2.el6.x86_64.rpm
安装包下载地址:
keepalived-1.4.2.tar.gz
http://rpmfind.net/linux/RPM/centos/6.10/x86_64/Packages/libnfnetlink-devel-1.0.0-1.el6.x86_64.html
http://rpmfind.net/linux/RPM/centos/6.10/x86_64/Packages/libnl-devel-1.1.4-2.el6.x86_64.html
安装keepalived
tar zxvf keepalived-1.4.2.tar.gz
cd keepalived-1.4.2
./configure
遇到以下报错
!!! OpenSSL is not properly installed on your system. !!!
!!! Can not include OpenSSL headers files.
解决方法:
yum -y install openssl-devel
如果./configure遇到以下报错
no acceptable C compiler found in $PATH
就要安装gcc, yum -y install gcc
make
make install
mkdir /etc/keepalived
cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
cp /usr/local/sbin/keepalived /usr/sbin/
master的keepalived配置文件如下
vi /etc/keepalived/keepalived.conf
global_defs {
router_id MySQL-HA
}
vrrp_script check_run {
script "/data/mysql/mysql_check.sh"
interval 60
}
vrrp_sync_group VG1 {
group {
VI_1
}
}
vrrp_instance VI_1 {
state BACKUP
interface eth1
virtual_router_id 51
priority 100
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass 1234
}
track_script {
check_run
}
notify_master /data/mysql/master.sh
notify_stop /data/mysql/stop.sh
virtual_ipaddress {
192.168.159.100
}
}
slave的keepalived配置文件如下:
mkdir /etc/keepalived
vi /etc/keepalived/keepalived.conf
global_defs {
router_id MySQL-HA
}
vrrp_script check_run {
script "/data/mysql/mysql_check.sh"
interval 60
}
vrrp_sync_group VG1 {
group {
VI_1
}
}
vrrp_instance VI_1 {
state BACKUP
interface eth1
virtual_router_id 51
priority 90
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass 1234
}
track_script {
check_run
}
notify_master /data/mysql/master.sh
notify_stop /data/mysql/stop.sh
virtual_ipaddress {
192.168.159.100
}
}
Mysql_check.sh \ master.sh \ stop.sh脚本
vi mysql_check.sh
#!/bin/bash
export PATH=$PATH:/data/mysql5.7.22/bin
count=1
while true
do
mysql -uroot -p"YF_f#_Zs6dVBrnwE" -h192.168.135.206 -e "show status;" > /dev/null 2>&1
i=$?
ps aux | grep mysqld | grep -v grep > /dev/null 2>&1
j=$?
if [ $i = 0 ] && [ $j = 0 ]
then
exit 0
else
if [ $i = 1 ] && [ $j = 0 ]
then
exit 0
else
if [ $count -gt 5 ]
then
break
fi
let count++
continue
fi
fi
done
/usr/bin/systemctl stop keepalived
vi master.sh
#!/bin/bash
export PATH=$PATH:/data/mysql5.7.22/bin
Master_Log_File=$(mysql -uroot -p"YF_f#_Zs6dVBrnwE" -h192.168.135.206 -e "show slave status\G" | grep -w Master_Log_File | awk -F": " '{print $2}')
Relay_Master_Log_File=$(mysql -uroot -p"YF_f#_Zs6dVBrnwE" -h192.168.135.206 -e "show slave status\G" | grep -w Relay_Master_Log_File | awk -F": " '{print $2}')
Read_Master_Log_Pos=$(mysql -uroot -p"YF_f#_Zs6dVBrnwE" -h192.168.135.206 -e "show slave status\G" | grep -w Read_Master_Log_Pos | awk -F": " '{print $2}')
Exec_Master_Log_Pos=$(mysql -uroot -p"YF_f#_Zs6dVBrnwE" -h192.168.135.206 -e "show slave status\G" | grep -w Exec_Master_Log_Pos | awk -F": " '{print $2}')
i=1
while true
do
if [ $Master_Log_File = $Relay_Master_Log_File ] && [ $Read_Master_Log_Pos -eq $Exec_Master_Log_Pos ]
then
echo "ok"
break
else
sleep 1
if [ $i -gt 60 ]
then
break
fi
continue
let i++
fi
done
mysql -uroot -p"YF_f#_Zs6dVBrnwE" -h192.168.135.206 -e "stop slave;"
mysql -uroot -p"YF_f#_Zs6dVBrnwE" -h192.168.135.206 -e "show master status;" > /tmp/master_status_$(date "+%y%m%d-%H%M").txt
vi stop.sh
#!/bin/bash
export PATH=$PATH:/data/mysql5.7.22/bin
M_File1=$(mysql --defaults-extra-file=/etc/my.password -e "show master status\G" | awk -F': ' '/File/{print $2}')
M_Position1=$(mysql --defaults-extra-file=/etc/my.password -e "show master status\G" | awk -F': ' '/Position/{print $2}')
sleep 1
M_File2=$(mysql --defaults-extra-file=/etc/my.password -e "show master status\G" | awk -F': ' '/File/{print $2}')
M_Position2=$(mysql --defaults-extra-file=/etc/my.password -e "show master status\G" | awk -F': ' '/Position/{print $2}')
i=1
while true
do
if [ $M_File1 = $M_File2 ] && [ $M_Position1 -eq $M_Position2 ]
then
echo "ok"
break
else
sleep 1
if [ $i -gt 60 ]
then
break
fi
continue
let i++
fi
done
在操作系统为CENTOS7.6版本,启动KEEPALIVED命令有变化
systemctl start keepalived
systemctl stop keepalived
KEEPALIVED如果无法启动,注意group V1要一致,且同网段不能用相同的group(多个KEEPALIVED集群)
如果还是无法启动,注意,mysql_check.sh master.sh stop.sh几个脚本里面,是否设置了环境变量(export PATH=$PATH:/data/mysql5.7.22/bin)
主备切换:
发生故障主备切换后,
原主库mysql服务停止,keepalived服务停止,原从库升级为主库,
原主库恢复故障后,启动MYSQL服务,启动KEEPALIVED服务,重新配置原从库为现从库,即恢复原主从结构。
停止原从库KEEPALIVED服务,启动原从库KEEPALIVED服务。
keepalived脚本优化:
vi /etc/keepalived/keepalived.conf
global_defs {
router_id LVS_MYSQL_MASTER
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_script chk_haproxy {
script "/etc/keepalived/chk_haproxy.sh"
interval 2
weight 2
}
vrrp_instance VI_9617 {
state MASTER
interface eth0
virtual_router_id 90
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass mysql9617
}
track_script {
chk_haproxy
}
virtual_ipaddress {
10.18.96.17
}
}
vi /etc/keepalived/chk_haproxy.sh
#!/bin/bash
status=$(ps aux|grep mysqld | grep -v grep | grep -v bash | wc -l)
if [ ${status} -lt 2 ]; then
systemctl stop keepalived.service
fi
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/28371090/viewspace-2738415/ ,如需转载,请注明出处,否则将追究法律责任。