需求:根据当前客户的生产环境,模拟安装部署一套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安装对应的包。

yum install gcc libnl libnl-devel libnfnetlink libnfnetlink-devel openssl-devel

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)

MySQL主主+Keepalived架构安装部署的更多相关文章

  1. [MySQL] MySQL的自己主动化安装部署

    有过MySQL运维的人应该都清楚,线上的MySQL一般都採用源代码编译,由于这样才干够依据企业的各自须要选择要编译的功能,尽管MySQL的源代码编译挺简单的,可是试想一下,假设你有几百台server同 ...

  2. node.js express架构安装部署

    安装-g:表示全局安装(必须以安装node.js) npm install -g express-generator 创建一个express架构的项目文件夹express testWebApp 在pa ...

  3. MySQL 5.7 多实例安装部署实例

    1. 背景  MySQL数据库的集中化运维,可以通过在一台服务器上,部署运行多个MySQL服务进程,通过不同的socket监听不同的服务端口来提供各自的服务.各个实例之间是相互独立的,每个实例的dat ...

  4. LNMP 架构安装部署

    PHP 安装 LNMP 下 php 在 Apache 服务下是 libphp5.so nginx 服务下是 9000 端口 #确保 web 服务和 MySQL 正常 143 [root@oldboy ...

  5. MySQL高可用之PXC安装部署(续)

      Preface       Yesterday I implemented a three-nodes PXC,but there were some errors when proceeding ...

  6. MySQL高可用之PXC安装部署

      Preface       Today,I'm gonna implement a PXC,Let's see the procedure.   Framework   Hostname IP P ...

  7. hadoop HA架构安装部署(QJM HA)

    ###################HDFS High Availability Using the Quorum Journal Manager########################## ...

  8. MySQL 5.7 双主复制+keepalived,常规业务一般够用了

    业务需求: 为Zabbix搭建2个数据库,一个库给服务器监控用,一个库给网络监控用. 硬件: 两台服务器,硬盘是1.2 T SSD卡,内存128G 架构: 希望做双主复制+keepalived,架构大 ...

  9. linux安装部署Nginx

    两个参考地址: NGINX的百度百科:https://baike.baidu.com/item/nginx/3817705?fr=aladdin NGINX的中文网站:http://www.nginx ...

  10. 【Hadoop离线基础总结】oozie的安装部署与使用

    目录 简单介绍 概述 架构 安装部署 1.修改core-site.xml 2.上传oozie的安装包并解压 3.解压hadooplibs到与oozie平行的目录 4.创建libext目录,并拷贝依赖包 ...

随机推荐

  1. SpringBoot 动态数据源

    SpringBoot 实现动态数据源切换 Spring Boot + Mybatis Plus + Druid + MySQL 实现动态数据源切换及动态 SQL 语句执行. 项目默认加载 applic ...

  2. PHPCMS V9安装出现DNS解析失败的解决方法-不支持采集和保存远程图片

    目前因为phpcms官网停止解析后,很多人安装phpcms v9出现如下错误:     这是因为检测dns解析的域名是phpcms官网的域名,官网域名停止解析后肯定检测失败.解决方法如下:   打开/ ...

  3. Android——“EditText控件供获取最大长度的方法”

    package utils; import android.app.Activity; import android.content.Context; import android.text.Inpu ...

  4. 44从零开始用Rust编写nginx,命令行参数的设计与解析及说明

    wmproxy wmproxy已用Rust实现http/https代理, socks5代理, 反向代理, 静态文件服务器,四层TCP/UDP转发,七层负载均衡,内网穿透,后续将实现websocket代 ...

  5. [转帖]被误解的CPU利用率、超线程、动态调频 —— CPU 性能之迷 Part 1

    https://blog.mygraphql.com/zh/notes/hw/hyper-threading/ 引 性能测试.压力测试.业务系统性能容量评估.这 3 件事,可以认为是大部分程序员/软件 ...

  6. [转帖]Nginx Rewrite重写功能

    目录 一.rewrite的概述 1.1.概述 1.2 跳转场景 1.3 跳转实现 1.4 Rewrite实际场景 二.常用的nginx正则表达式 三.rewrite命令 3.1 rewrite的语法格 ...

  7. [转帖]CTF -bugku-misc(持续更新直到全部刷完)

    CTF -bugku-misc(持续更新直到全部刷完) https://www.cnblogs.com/cat47/p/11432475.html 1.签到题 点开可见.(这题就不浪费键盘了) CTF ...

  8. [转帖]dd - Linux世界中的搬运工

    <存储工具系列文章>主要介绍存储相关的测试和调试工具,包括不限于dd.fio.vdbench.iozone.iometer.cosbench等性能负载工具,及strace等调试工具. dd ...

  9. [转帖]DD硬盘性能相关因素

    https://www.jianshu.com/p/a15d7a65c876 本文简单介绍下DD测试硬盘性能时,各个因素的影响 首先列出测试结果   image.png oflag分析--/home ...

  10. isolcpus的学习与了解

    isolcpus的学习与了解 前言 最近一直跟同事说要进行CPU的bind bind到具体的core 当时还一直装B, 说这样能够提高性能. 但是今天起床早上查看资料时发现,其实是先设置隔离的. 让操 ...