代码改变世界

MySQL主主+Keepalived架构安装部署

2020-05-19 00:16  AlfredZhao  阅读(...)  评论(...编辑  收藏

需求:根据当前客户的生产环境,模拟安装部署一套MySQL主主+Keepalived架构的测试环境,方便后续自己做一些功能性的测试。

1.准备工作

MySQL、Keepalived的具体版本,虚拟主机等配置,都尽可能参考客户目前的生产环境安装规范来准备:
软件名称 版本
MySQL 5.7.29
Keepalived 2.0.20

准备2台虚拟主机,具体环境信息为:

系统版本 主机名 IP地址 主机内存 磁盘空间
RHEL6.8 test01 192.168.1.121 4G 20G
RHEL6.8 test02 192.168.1.122 4G 20G

规划业务连接的vip为:192.168.1.130

MySQL软件下载地址:https://downloads.mysql.com/archives/community/
Keepalived软件下载地址:https://www.keepalived.org/download.html

我这里自己的测试环境可以连接到外网,可以直接wget放到后台下载
比如统一进入到介质存放目录/u01/media下,使用wget下载对应介质:

cd /u01/media
nohup wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.29-linux-glibc2.12-x86_64.tar.gz --no-check-certificate &
nohup wget https://www.keepalived.org/software/keepalived-2.0.20.tar.gz &

实测wget下载MySQL需要加--no-check-certificate参数,不加会下载失败,提示信息为Unable to establish SSL connection.

根据客户模版编辑my.cnf配置文件,存放路径为:/etc/mysql/my.cnf

my.cnf内容,这里也是完全引用当前客户MySQL的安装规范,如果后续测试验证有涉及到个别参数的调整,会特别注明:

#################################
## my.cnf for MySQL 5.7/8.0
#
#以下内容需要修改:
#1)prompt="\u@mysqldb \R:\m:\s [\d]> "  【mysqldb可修改为业务系统名称】
#2)character-set-server = utf8mb4  默认,有特殊要求可修改为GBK,但不建议
#3)innodb_buffer_pool_size = 1024M 设置mysql总内存大小,可使用物理内存3/5(我这里测试环境还有其他测试软件运行,所以设置为1024M)
#4)server-id = 1121 确保主从或主主各个节点不同,规则可考虑使用ip地址后两段,如192.168.1.121 server-id=1121
#
#################################
[client]
port	= 3306
socket	= /data/mysql/mysql.sock

[mysql]
prompt="\u@mysqldb \R:\m:\s [\d]> "
no-auto-rehash

[mysqld]
user	= mysql
port	= 3306
basedir	= /usr/local/mysql
datadir	= /data/mysql
socket	= /data/mysql/mysql.sock
pid-file = mysqldb.pid
character-set-server = utf8mb4
skip_name_resolve = 1

#若你的MySQL数据库主要运行在境外,请务必根据实际情况调整本参数
default_time_zone = "+8:00"

open_files_limit    = 65535
back_log = 1024
max_connections = 800
max_connect_errors = 1000000
table_open_cache = 1024
table_definition_cache = 1024
table_open_cache_instances = 64
thread_stack = 512K
external-locking = FALSE
max_allowed_packet = 32M
sort_buffer_size = 16M
join_buffer_size = 16M
thread_cache_size = 768
interactive_timeout = 600
wait_timeout = 600
tmp_table_size = 96M
max_heap_table_size = 96M
slow_query_log = 1
log_timestamps = SYSTEM
slow_query_log_file = /data/mysql/slow.log
log-error = /data/mysql/error.log
long_query_time = 0.1
log_queries_not_using_indexes =1
log_throttle_queries_not_using_indexes = 60
min_examined_row_limit = 100
log_slow_admin_statements = 1
log_slow_slave_statements = 1
server-id = 1121
log-bin = /data/mysql/mybinlog
sync_binlog = 1
binlog_cache_size = 4M
max_binlog_cache_size = 2G
max_binlog_size = 1G
auto-increment-increment = 2     
auto-increment-offset = 1  

#注意:MySQL 8.0开始,binlog_expire_logs_seconds选项也存在的话,会忽略expire_logs_days选项
expire_logs_days = 7

master_info_repository = TABLE
relay_log_info_repository = TABLE
gtid_mode = on
enforce_gtid_consistency = 1
log_slave_updates
slave-rows-search-algorithms = 'INDEX_SCAN,HASH_SCAN'
binlog_format = row
binlog_checksum = 1
relay_log_recovery = 1
relay-log-purge = 1
key_buffer_size = 32M
read_buffer_size = 8M
read_rnd_buffer_size = 16M
bulk_insert_buffer_size = 64M
myisam_sort_buffer_size = 128M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
lock_wait_timeout = 3600
explicit_defaults_for_timestamp = 1
innodb_thread_concurrency = 0
innodb_sync_spin_loops = 100
innodb_spin_wait_delay = 30

#transaction_isolation = REPEATABLE-READ
transaction_isolation = READ-COMMITTED
sql_mode = "STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION,NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER"
#innodb_additional_mem_pool_size = 16M
innodb_buffer_pool_size = 1024M
innodb_buffer_pool_instances = 4
innodb_buffer_pool_load_at_startup = 1
innodb_buffer_pool_dump_at_shutdown = 1
innodb_data_file_path = ibdata1:1G:autoextend
innodb_flush_log_at_trx_commit = 1
innodb_log_buffer_size = 32M
innodb_log_file_size = 2G
innodb_log_files_in_group = 2
innodb_max_undo_log_size = 4G
innodb_undo_directory = /data/mysql/undolog
innodb_undo_tablespaces = 95

# 根据您的服务器IOPS能力适当调整
# 一般配普通SSD盘的话,可以调整到 10000 - 20000
# 配置高端PCIe SSD卡的话,则可以调整的更高,比如 50000 - 80000
innodb_io_capacity = 4000
innodb_io_capacity_max = 8000
innodb_flush_sync = 0
innodb_flush_neighbors = 0
innodb_write_io_threads = 8
innodb_read_io_threads = 8
innodb_purge_threads = 4
innodb_page_cleaners = 4
innodb_open_files = 65535
innodb_max_dirty_pages_pct = 50
innodb_flush_method = O_DIRECT
innodb_lru_scan_depth = 4000
innodb_checksum_algorithm = crc32
innodb_lock_wait_timeout = 10
innodb_rollback_on_timeout = 1
innodb_print_all_deadlocks = 1
innodb_file_per_table = 1
innodb_online_alter_log_max_size = 4G
innodb_stats_on_metadata = 0
innodb_undo_log_truncate = 1

# some var for MySQL 5.7
innodb_checksums = 1
#innodb_file_format = Barracuda
#innodb_file_format_max = Barracuda
query_cache_size = 0
query_cache_type = 0
innodb_undo_logs = 128

#注意:MySQL 8.0.16开始删除该选项
internal_tmp_disk_storage_engine = InnoDB

innodb_status_file = 1
#注意: 开启 innodb_status_output & innodb_status_output_locks 后, 可能会导致log-error文件增长较快
innodb_status_output = 0
innodb_status_output_locks = 0

#performance_schema
performance_schema = 1
performance_schema_instrument = '%memory%=on'
performance_schema_instrument = '%lock%=on'

#innodb monitor
innodb_monitor_enable="module_innodb"
innodb_monitor_enable="module_server"
innodb_monitor_enable="module_dml"
innodb_monitor_enable="module_ddl"
innodb_monitor_enable="module_trx"
innodb_monitor_enable="module_os"
innodb_monitor_enable="module_purge"
innodb_monitor_enable="module_log"
innodb_monitor_enable="module_lock"
innodb_monitor_enable="module_buffer"
innodb_monitor_enable="module_index"
innodb_monitor_enable="module_ibuf_system"
innodb_monitor_enable="module_buffer_page"
innodb_monitor_enable="module_adaptive_hash"

[mysqldump]
quick
max_allowed_packet = 32M

2.MySQL安装部署

2.1 MySQL解压、初始化:

cd /u01/media
tar -zxf mysql-5.7.29-linux-glibc2.12-x86_64.tar.gz
mv mysql-5.7.29-linux-glibc2.12-x86_64 /usr/local/mysql5.7
ln -s /usr/local/mysql5.7 /usr/local/mysql
mkdir -p /data/mysql
chmod 755 /data
useradd -u 3001 mysql 
chown -R mysql:mysql /data
chown -R root:mysql /usr/local/mysql5.7
/usr/local/mysql/bin/mysqld --defaults-file=/etc/mysql/my.cnf --initialize --datadir=/data/mysql --basedir=/usr/local/mysql --user=mysql --explicit_defaults_for_timestamp

2.2 启动MySQL服务:

/usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/mysql/my.cnf &

2.3 配置MySQL启动脚本:

#拷贝启动脚本
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

#定义basedir和datadir的路径
vi /etc/init.d/mysqld
basedir=/usr/local/mysql
datadir=/data/mysql

#将MySQL加入服务列表,并设置为开机启动:
chkconfig --add mysqld
chkconfig mysqld on

#检查是否生效(2,3,4,5为on)
chkconfig --list mysqld  

2.4 修改MySQL的root密码:

# MySQL 5.7 初始化时生成临时的密码可在日志中查找:
# grep temporary /data/mysql/error.log

# 登录MySQL修改密码:
# mysql -uroot -p
mysql> alter user 'root'@'localhost' identified by '123';
mysql> flush privileges;

注:如果mysql命令不识别,需要将环境变量/usr/local/mysql/bin追加到/etc/profile中的PATH变量中:

export PATH="$PATH:/usr/local/redis/bin:/usr/local/mysql/bin"

3.MySQL主主配置

基本的 my.cnf 参数文件、密码设置已经在之前步骤配置完成,下面直接进行MySQL主主配置:

3.1 配置Master1服务器(121)

grant replication slave, replication client on *.* to 'repl'@'192.168.1.%' identified by "123";
flush privileges;
flush tables with read lock; /* 注意该参数设置后,如果自己同步对方数据,同步前一定要记得先解锁!*/
show master status;

结果如下:

root@mysqldb 21:30:  [(none)]> show master status;
+-----------------+----------+--------------+------------------+------------------------------------------+
| File            | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                        |
+-----------------+----------+--------------+------------------+------------------------------------------+
| mybinlog.000002 |     1035 |              |                  | 08c887bf-98ab-11ea-b70c-080027c2997a:1-4 |
+-----------------+----------+--------------+------------------+------------------------------------------+
1 row in set (0.00 sec)

3.2 配置Master2服务器(122)

grant replication slave, replication client on *.* to 'repl'@'192.168.1.%' identified by "123";
flush privileges;
flush tables with read lock; /* 注意该参数设置后,如果自己同步对方数据,同步前一定要记得先解锁!*/
show master status;

结果如下:

root@mysqldb 21:31:  [(none)]> show master status;
+-----------------+----------+--------------+------------------+------------------------------------------+
| File            | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                        |
+-----------------+----------+--------------+------------------+------------------------------------------+
| mybinlog.000002 |     1035 |              |                  | 5d3f3359-98ab-11ea-8101-080027763d24:1-4 |
+-----------------+----------+--------------+------------------+------------------------------------------+
1 row in set (0.00 sec)

3.3 Master1 => Master2方向同步
在master1(121)上做以下操作,注意master_host输入master2地址,以及master2的show master status信息:

mysql -uroot -p'123'
unlock tables; /*先解锁,将对方数据同步到自己的数据库中*/
stop slave;
change master to master_host='192.168.1.122', master_user='repl', master_password='123', master_log_file='mybinlog.000002', master_log_pos=1035;
start slave;

查看同步状态,如下出现两个“Yes”,表明同步成功:

show slave status\G
...
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

3.4 Master2 => Master1方向同步
在master2(122)上做以下操作,注意master_host输入master1地址,以及master1的show master status信息:

mysql -uroot -p'123'
unlock tables; /*先解锁,将对方数据同步到自己的数据库中*/
stop slave;
change master to master_host='192.168.1.121', master_user='repl', master_password='123', master_log_file='mybinlog.000002', master_log_pos=1035;
start slave;

查看同步状态,如下出现两个“Yes”,表明同步成功:

show slave status\G
...
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

3.5 验证主主同步正常
在master1(121)上创建一个测试库test121;创建表t1,插入一条数据,然后去master2(122)查询确认同步;
在master2(122)上创建一个测试库test122;创建表t1,插入一条数据,然后去master1(121)查询确认同步。

--在master1(121)上:
create database test121;
use test121;
create table t1 (id int, name varchar(20));
insert into t1 values (1,'Alfred Zhao on 121');

--在master2(122)上:
create database test122;
use test122;
create table t1 (id int, name varchar(20));
insert into t1 values (1,'Alfred Zhao on 122');

--在master1(121)上查询test122库中T1表;
存在。
--在master2(122)上查询test121库中T1表;
存在。

4.Keepalived安装部署

4.1 安装keepalived软件
需要准备的系统依赖包:

gcc
libnl-1.1.4-2.el6.x86_64
libnl-devel-1.1.4-2.el6.x86_64
libnfnetlink-1.0.0-1.el6.x86_64
libnfnetlink-devel-1.0.0-1.el6.x86_64
openssl-devel-1.0.1e-58.el6_10.x86_64

配置好yum源,使用yum install安装对应的包。

4.2 编译安装keepalived
解压及配置:

tar -xvf keepalived-2.0.20.tar.gz
mv keepalived-2.0.20 /usr/local/src/
cd /usr/local/src/keepalived-2.0.20
./configure --prefix=/usr/local/keepalived

编译安装:

make&&make install

4.3 配置keepalived服务

  1. 配置服务文件
mkdir /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
cp /usr/local/keepalived/sbin/keepalived /usr/sbin
cp /usr/local/src/keepalived-2.0.20/keepalived/etc/init.d/keepalived /etc/rc.d/init.d/keepalived
  1. 加keepalived服务
chkconfig --list |grep keepalived
chkconfig --add keepalived
chkconfig keepalived on

配置keepalived日志

keepalived的日志默认是输出到/var/log/messages中,这样不便于查看。如何自定义keepalived的日志输出文件呢?
如果是用service启动的,修改/etc/sysconfig/keepalived文件
vi /etc/sysconfig/keepalived

KEEPALIVED_OPTIONS="-D -d -S 0"

如果不是,则启动的时候指定以上参数,如:

/usr/local/keepalived/sbin/keepalived -d -D -S 0 

修改/etc/syslog.conf(RHEL6.8以后配置/etc/rsyslog.conf)

# keepalived -S 0 
local0.*     /var/log/keepalived.log

重启syslog:

# RHEL 5&6:
service syslog restart
# RHEL 7:
service rsyslog restart

注:RHEL6.8也是按照RHEL7的方法来操作。

  1. 配置keepalived的MySQL故障转移
    master1(121)机器上的keepalived.conf配置:
    注:下面配置中没有使用lvs的负载均衡功能,所以不需要配置虚拟服务器virtual server
cd /etc/keepalived/
mv keepalived.conf keepalived.conf.bak
vi /etc/keepalived/keepalived.conf

! Configuration File for keepalived

vrrp_script chk_mysql_port { 
    script "/etc/keepalived/check_mysql.sh" 
    interval 2 
    weight -5 
    fall 2 
    rise 1 
}
       
vrrp_instance VI_1 {
    state MASTER    
    interface eth0 #指定虚拟ip的网卡接口
    mcast_src_ip 192.168.1.121 #指定本服务器物理ip地址
    virtual_router_id 130 #路由器标识,MASTER和BACKUP必须是一致的,取值1-255,注意同一内网中不应有相同virtual_router_id的集群,可以考虑使用vip的最后一位进行标示。
    priority 101 
    advert_int 1         
    authentication {   
        auth_type PASS 
        auth_pass 888888   
    }
    virtual_ipaddress {    
        192.168.1.130 #指定虚拟ip地址
    }
      
track_script {               
   chk_mysql_port             
}
}

编辑 /etc/keepalived/check_mysql.sh
vi /etc/keepalived/check_mysql.sh

#!/bin/bash
counter=$(netstat -na|grep "LISTEN"|grep "3306"|wc -l)
if [ "${counter}" -eq 0 ]; then
    /etc/init.d/keepalived stop
fi
 
# 修改chk_mysql.sh权限
chmod 755 /etc/keepalived/check_mysql.sh

# 启动keepalived
# service keepalived start
Starting keepalived: [ OK ]

master2(122)机器上的keepalived.conf配置:

cd /etc/keepalived/
mv keepalived.conf keepalived.conf.bak
vi /etc/keepalived/keepalived.conf

! Configuration File for keepalived

vrrp_script chk_mysql_port {
    script "/etc/keepalived/check_mysql.sh"
    interval 2            
    weight -5                 
    fall 2                 
    rise 1               
}
       
vrrp_instance VI_1 {
    state BACKUP
    interface eth0    
    mcast_src_ip 192.168.1.122
    virtual_router_id 130  
    priority 99          
    advert_int 1         
    authentication {   
        auth_type PASS 
        auth_pass 888888    
    }
    virtual_ipaddress {    
        192.168.1.130
    }
      
track_script {               
   chk_mysql_port             
}
}

编辑check_mysql.sh

vi /etc/keepalived/check_mysql.sh

#!/bin/bash
counter=$(netstat -na|grep "LISTEN"|grep "3306"|wc -l)
if [ "${counter}" -eq 0 ]; then
    /etc/init.d/keepalived stop
fi
chmod 755 /etc/keepalived/check_mysql.sh

启动keepalived:

# service keepalived start
Starting keepalived: [ OK ]

5.其他补充

5.1 查看keepalived日志:

tail -200f /var/log/keepalived.log

5.2 创建数据库、用户并赋权

--创建业务数据库:
create database demo;

--创建业务用户:
create user 'jingyu'@'%' identified by 'jingyu';

--赋予业务用户权限(默认):
grant insert,update,delete,select,alter,create,index,SHOW VIEW on demo.* to 'jingyu'@'%';

此时应用侧已经可以使用命令mysql -ujingyu -p -h192.168.1.130或者客户端连接工具,登录到demo数据库进行相关操作了。

[root@test01 ~]# mysql -ujingyu -p -h192.168.1.130
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 27
Server version: 5.7.29-log MySQL Community Server (GPL)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

jingyu@mysqldb 00:00:  [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| demo               |
+--------------------+
2 rows in set (0.01 sec)