mysql主从+keepalived 切换

https://blog.csdn.net/qq_30889373/article/details/102763057

https://www.cnblogs.com/cannel/p/11104195.html

 

官网:

https://dev.mysql.com/downloads/mysql/

 

 

 

 

 

 

 wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.34-1.el7.x86_64.rpm-bundle.tar

 

解压

tar xvf mysql-5.7.28-1.el7.x86_64.rpm-bundle.tar

 

安装依赖的程序包

yum install -y perl net-tools

 

卸载mariadb程序包

  • Centos 捆绑了 mariadb-libs,所以必须先卸载
rpm -qa|grep mariadb
rpm -e mariadb-libs-5.5.60-1.el7_5.x86_64 --nodeps

 

或者

yum -y remove mari*

 

 

yum search libaio
yum install -y libaio.x86_64
yum install -y libaio-devel.x86_64

 

安装MySQL程序包

rpm -ivh mysql-community-common-5.7.34-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-5.7.34-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-5.7.34-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-5.7.34-1.el7.x86_64.rpm

 

启动mysql

systemctl start mysqld.service

 

grep 'password' /var/log/mysqld.log

 

修改默认密码

ALTER USER 'root'@'localhost' IDENTIFIED BY 'Abc778899!'; 

 

允许远程使用root帐户

UPDATE user SET host = '%' WHERE user ='root';
FLUSH PRIVILEGES;

 

 

 

允许远程访问MySQL数据库(/etc/my.cnf)

character_set_server = utf8
bind-address = 0.0.0.0

 

开启防火墙3360端口

firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd --reload

 

主从原理

master将更新操作  插入操作  持久化到数据库文件中 并写一份到binlog日志中。
slave 会起两个线程  io 线程 和 sql线程  
io线程负责从master的二进制日志中获取更新事件 放到本地的relay日志中  relaylog 也是一个二进制的日志
sql线程 将relay log的内容 回放(在本地实例中执行)

  

 

mysql 主

[mysqld]
datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock character_set_server = utf8 bind-address = 0.0.0.0 collation-server = utf8_general_ci max_connections = 1024 gtid_mode = ON server_id = 1 log-bin = mysql-bin enforce_gtid_consistency = ON #保持gtid一致性 sql_mode=ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid

 

mysql 从

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
character_set_server = utf8
bind-address = 0.0.0.0
collation-server = utf8_general_ci
max_connections = 1024

gtid_mode = ON
server_id = 2
log-bin = mysql-bin
log_slave_updates = ON
enforce_gtid_consistency = ON
slave-parallel-type = LOGICAL_CLOCK
slave-parallel-workers = 8
master-info-repository = TABLE
relay-log-info-repository = TABLE
sql_mode=ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

 

 

mysql 主:

创建复制账户:

mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.0.%' IDENTIFIED BY 'repl123456';

 查看master信息

show master status
file                                  position                 executed_gitd_set
mysql-bin.000001             437

 

查看gtid

gtid格式: source_id:transaction_id
source_id  是实例的uuid  可以 show  variables like 'server_uuid'; 查看
也可以在mysql data目录的 auto.cnf 查看实例的uuid


gtid查看
show global variables like 'gtid%'; 查看

  

  

 mysql 从:

配置复制连接:

mysql> CHANGE MASTER TO 
MASTER_HOST='192.168.0.215',
MASTER_USER='repl',
MASTER_PASSWORD='repl123456',
MASTER_AUTO_POSITION=1;

 

启动复制线程:

mysql> start slave;


查看复制状态:

mysql> show slave status\G;
mysql> show processlist;

 

 

互为主从

mysql主配置文件

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
port=13306
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

skip_name_resolve = 1                           # 只能用IP地址检查客户端的登录,不用主机名
character-set-server = utf8mb4                  # 数据库默认字符集,主流字符集支持一些特殊表情符号(特殊表情符占用4个字节)
transaction_isolation = READ-COMMITTED          # 事务隔离级别,默认为可重复读,MySQL默认可重复读级别
collation-server = utf8mb4_general_ci           # 数据库字符集对应一些排序等规则,注意要和character-set-server对应
init_connect='SET NAMES utf8mb4'                # 设置client连接mysql时的字符集,防止乱码
lower_case_table_names = 1                      # 是否对sql语句大小写敏感,1表示不敏感
max_connections = 1024                          # 最大连接数
max_connect_errors = 1000                       # 最大错误连接数
explicit_defaults_for_timestamp = true          # TIMESTAMP如果没有显示声明NOT NULL,允许NULL值
max_allowed_packet = 128M                       # SQL数据包发送的大小,如果有BLOB对象建议修改成1G
interactive_timeout = 1800                      # MySQL连接闲置超过一定时间后(单位:秒)将会被强行关闭
wait_timeout = 1800                             # MySQL默认的wait_timeout值为8个小时, interactive_timeout参数需要同时配置才能生效
tmp_table_size = 16M                            # 内部内存临时表的最大值 ,设置成128M;比如大数据量的group by ,order by时可能用到临时表;超过了这个值将写入磁盘,系统IO压力增大
max_heap_table_size = 128M                      # 定义了用户可以创建的内存表(memory table)的大小
query_cache_size = 0                            # 禁用mysql的缓存查询结果集功能;后期根据业务情况测试决定是否开启;大部分情况下关闭下面两项
query_cache_type = 0
skip-name-resolve 
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
# 用户进程分配到的内存设置,每个session将会分配参数设置的内存大小
read_buffer_size = 2M                           # MySQL读入缓冲区大小。对表进行顺序扫描的请求将分配一个读入缓冲区,MySQL会为它分配一段内存缓冲区。
read_rnd_buffer_size = 8M                       # MySQL的随机读缓冲区大小
sort_buffer_size = 8M                           # MySQL执行排序使用的缓冲大小
binlog_cache_size = 1M                          # 一个事务,在没有提交的时候,产生的日志,记录到Cache中;等到事务提交需要提交的时候,则把日志持久化到磁盘。默认binlog_cache_size大小32K
back_log = 130                                  # 在MySQL暂时停止响应新请求之前的短时间内多少个请求可以被存在堆栈中;官方建议back_log = 50 + (max_connections / 5),封顶数为900
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
slow_query_log = 1                              # 慢查询sql日志设置
long_query_time = 1                             # 慢查询时间;超过1秒则为慢查询
slow_query_log_file = /var/log/slow.log                  # 慢查询日志文件
log_queries_not_using_indexes = 1               # 检查未使用到索引的sql
log_throttle_queries_not_using_indexes = 5      # 用来表示每分钟允许记录到slow log的且未使用索引的SQL语句次数。该值默认为0,表示没有限制
min_examined_row_limit = 100                    # 检索的行数必须达到此值才可被记为慢查询,查询检查返回少于该参数指定行的SQL不被记录到慢查询日志
expire_logs_days = 15                            # MySQL binlog日志文件保存的过期时间,过期后自动删除
# Innodb设置
innodb_open_files = 500                         # 限制Innodb能打开的表的数据,如果库里的表特别多的情况,请增加这个。这个值默认是300
innodb_buffer_pool_size = 64M                   # InnoDB使用一个缓冲池来保存索引和原始数据,一般设置物理存储的60% ~ 70%;这里你设置越大,你在存取表里面数据时所需要的磁盘I/O越少
innodb_log_buffer_size = 2M                     # 此参数确定写日志文件所用的内存大小,以M为单位。缓冲区更大能提高性能,但意外的故障将会丢失数据。MySQL开发人员建议设置为1-8M之间
innodb_flush_method = O_DIRECT                  # O_DIRECT减少操作系统级别VFS的缓存和Innodb本身的buffer缓存之间的冲突
innodb_write_io_threads = 4                     # CPU多核处理能力设置,根据读,写比例进行调整
innodb_read_io_threads = 4
innodb_lock_wait_timeout = 120                  # InnoDB事务在被回滚之前可以等待一个锁定的超时秒数。InnoDB在它自己的锁定表中自动检测事务死锁并且回滚事务。InnoDB用LOCK TABLES语句注意到锁定设置。默认值是50秒
innodb_log_file_size = 32M                      # 此参数确定数据日志文件的大小,更大的设置可以提高性能,但也会增加恢复故障数据库所需的时间

#主从复制
gtid_mode = ON
server_id = 1
log-bin = mysql-bin
enforce_gtid_consistency = ON
log_slave_updates = ON
slave-parallel-type = LOGICAL_CLOCK
slave-parallel-workers = 8
master-info-repository = TABLE
relay-log-info-repository = TABLE
my.cnf

创建复制账户:

mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.0.%' IDENTIFIED BY 'repl123456';

mysql从配置文件

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
port=13306
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

skip_name_resolve = 1                           # 只能用IP地址检查客户端的登录,不用主机名
character-set-server = utf8mb4                  # 数据库默认字符集,主流字符集支持一些特殊表情符号(特殊表情符占用4个字节)
transaction_isolation = READ-COMMITTED          # 事务隔离级别,默认为可重复读,MySQL默认可重复读级别
collation-server = utf8mb4_general_ci           # 数据库字符集对应一些排序等规则,注意要和character-set-server对应
init_connect='SET NAMES utf8mb4'                # 设置client连接mysql时的字符集,防止乱码
lower_case_table_names = 1                      # 是否对sql语句大小写敏感,1表示不敏感
max_connections = 1024                          # 最大连接数
max_connect_errors = 1000                       # 最大错误连接数
explicit_defaults_for_timestamp = true          # TIMESTAMP如果没有显示声明NOT NULL,允许NULL值
max_allowed_packet = 128M                       # SQL数据包发送的大小,如果有BLOB对象建议修改成1G
interactive_timeout = 1800                      # MySQL连接闲置超过一定时间后(单位:秒)将会被强行关闭
wait_timeout = 1800                             # MySQL默认的wait_timeout值为8个小时, interactive_timeout参数需要同时配置才能生效
tmp_table_size = 16M                            # 内部内存临时表的最大值 ,设置成128M;比如大数据量的group by ,order by时可能用到临时表;超过了这个值将写入磁盘,系统IO压力增大
max_heap_table_size = 128M                      # 定义了用户可以创建的内存表(memory table)的大小
query_cache_size = 0                            # 禁用mysql的缓存查询结果集功能;后期根据业务情况测试决定是否开启;大部分情况下关闭下面两项
query_cache_type = 0
skip-name-resolve 
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
# 用户进程分配到的内存设置,每个session将会分配参数设置的内存大小
read_buffer_size = 2M                           # MySQL读入缓冲区大小。对表进行顺序扫描的请求将分配一个读入缓冲区,MySQL会为它分配一段内存缓冲区。
read_rnd_buffer_size = 8M                       # MySQL的随机读缓冲区大小
sort_buffer_size = 8M                           # MySQL执行排序使用的缓冲大小
binlog_cache_size = 1M                          # 一个事务,在没有提交的时候,产生的日志,记录到Cache中;等到事务提交需要提交的时候,则把日志持久化到磁盘。默认binlog_cache_size大小32K
back_log = 130                                  # 在MySQL暂时停止响应新请求之前的短时间内多少个请求可以被存在堆栈中;官方建议back_log = 50 + (max_connections / 5),封顶数为900
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
slow_query_log = 1                              # 慢查询sql日志设置
long_query_time = 1                             # 慢查询时间;超过1秒则为慢查询
slow_query_log_file = /var/log/slow.log                  # 慢查询日志文件
log_queries_not_using_indexes = 1               # 检查未使用到索引的sql
log_throttle_queries_not_using_indexes = 5      # 用来表示每分钟允许记录到slow log的且未使用索引的SQL语句次数。该值默认为0,表示没有限制
min_examined_row_limit = 100                    # 检索的行数必须达到此值才可被记为慢查询,查询检查返回少于该参数指定行的SQL不被记录到慢查询日志
expire_logs_days = 15                            # MySQL binlog日志文件保存的过期时间,过期后自动删除
# Innodb设置
innodb_open_files = 500                         # 限制Innodb能打开的表的数据,如果库里的表特别多的情况,请增加这个。这个值默认是300
innodb_buffer_pool_size = 64M                   # InnoDB使用一个缓冲池来保存索引和原始数据,一般设置物理存储的60% ~ 70%;这里你设置越大,你在存取表里面数据时所需要的磁盘I/O越少
innodb_log_buffer_size = 2M                     # 此参数确定写日志文件所用的内存大小,以M为单位。缓冲区更大能提高性能,但意外的故障将会丢失数据。MySQL开发人员建议设置为1-8M之间
innodb_flush_method = O_DIRECT                  # O_DIRECT减少操作系统级别VFS的缓存和Innodb本身的buffer缓存之间的冲突
innodb_write_io_threads = 4                     # CPU多核处理能力设置,根据读,写比例进行调整
innodb_read_io_threads = 4
innodb_lock_wait_timeout = 120                  # InnoDB事务在被回滚之前可以等待一个锁定的超时秒数。InnoDB在它自己的锁定表中自动检测事务死锁并且回滚事务。InnoDB用LOCK TABLES语句注意到锁定设置。默认值是50秒
innodb_log_file_size = 32M                      # 此参数确定数据日志文件的大小,更大的设置可以提高性能,但也会增加恢复故障数据库所需的时间
#主从复制
gtid_mode = ON
server_id = 2
log-bin = mysql-bin
log_slave_updates = ON
enforce_gtid_consistency = ON
slave-parallel-type = LOGICAL_CLOCK
slave-parallel-workers = 8
master-info-repository = TABLE
relay-log-info-repository = TABLE
my.cnf

创建复制账户:

mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.0.%' IDENTIFIED BY 'repl123456';

 

配置复制连接

从上

mysql> CHANGE MASTER TO 
MASTER_HOST='10.156.163.235',
MASTER_USER='repl',
MASTER_PASSWORD='repl123456',
MASTER_PORT=13306
MASTER_AUTO_POSITION=1;

 

主上

mysql> CHANGE MASTER TO 
MASTER_HOST='10.156.163.234',
MASTER_USER='repl',
MASTER_PASSWORD='repl123456',
MASTER_PORT=13306
MASTER_AUTO_POSITION=1;

 

 互为主从 不能同时写。主主的配置多一个主键自增步长

log_slave_updates = ON

auto_increment_increment=2

auto_increment_offset=1

auto_increment_increment=2
auto_increment_offset=2

主上

yum install keepalived -y

vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id MYSQL-1
}

vrrp_script check_run {
script "/etc/keepalived/mysql.sh"
interval 60
}

vrrp_instance VI_1 {
    state SLAVE
    interface ens192
    virtual_router_id 51
    priority 90
    advert_int 1
    nopreempt
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
    check_run
    }
    unicast_src_ip 10.156.163.235
    unicast_peer {
        10.156.163.234
    }

    virtual_ipaddress {
        10.156.163.236
    }
}

virtual_server 10.156.163.236 13306 {
    delay_loop 2
    persistence_timeout 50
    protocol TCP

    real_server 10.156.163.235 13306

#    real_server 10.156.163.234 13306 {
#        weight 1
#        notify_down /usr/local/bin/keepalived/etc/mysql.sh
#        TCP_CHECK {
#            connect_timeout 3
#            nb_get_retry 3
#            delay_before_retry 3
#            connect_port 3306
#        }
#    }

}

 

从上的keepalived配置文件

! Configuration File for keepalived

global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id MYSQL-2
}

vrrp_script check_run {
script "/etc/keepalived/mysql.sh"
interval 60
}

vrrp_instance VI_1 {
    state SLAVE
    interface ens192
    virtual_router_id 51
    priority 90
    advert_int 1
    nopreempt
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
    check_run
    }
    unicast_src_ip 10.156.163.234
    unicast_peer {
        10.156.163.235
    }
    
    virtual_ipaddress {
        10.156.163.236
    }
}

virtual_server 10.156.163.236 13306 {
    delay_loop 2
    persistence_timeout 50
    protocol TCP

    real_server 10.156.163.234 13306

#    real_server 10.156.163.235 13306 {
#        weight 1
#        notify_down /usr/local/bin/keepalived/etc/mysql.sh
#        TCP_CHECK {
#            connect_timeout 3
#            nb_get_retry 3
#            delay_before_retry 3
#            connect_port 3306
#        }
#    }

}

 

主从都做

mysql检测脚本

#!/bin/sh
counter=$(netstat -tunlp |grep 13306|wc -l)
if [ "${counter}" = "0" ]; then
    /usr/bin/systemctl stop keepalived
fi

 

keepalived检测脚本

vim /opt/check_keepalived.sh

#!/bin/bash
counter=$(ps -ef|grep keepalived|wc -l)
counter1=$(netstat -tunlp |grep 13306 |wc -l)
if [ "${counter}" = "1" ] && [ "${counter1}" = "1" ]; then
    sleep 5
    /usr/bin/systemctl start keepalived
fi

 

写入crontab

vim /etc/crontab

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed
*/1  *  *  *  * /opt/check_keepalived.sh

 

posted @ 2021-10-12 10:45  xihuxiangri  阅读(275)  评论(0编辑  收藏  举报