MySQL8+keepalived 主主(双主)模式部署

一、环境介绍

系统版本:Red Hat Enterprise Linux Server release 7.6 (Maipo)
Mysql版本:mysql-boost-8.0.18.tar.gz
部署主机: 192.168.102.110 、 192.168.102.111
必须依赖包: gcc-9 、 cmake-3

二、系统环境初始化

1.关闭防火墙、selinux、删除多余用户

 systemctl stop firewalld && \
 systemctl disable firewalld && \
 setenforce 0 && \
 sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config && \
 for i in adm lp sync shutdown halt news uucp operator games gopher;do userdel $i;done ;
 for i in adm lp news uucp games dip pppusers popusers slipusers ;do groupdel $i;done ;

2.添加登录警示信息

echo '警告!
   如果你现在通过非法途径或在未经授权的情况下登录到这台服务器,请立即停止所有操作并退出登录,
因为你对服务器的任何操作都将会受到法律的惩戒!
Warning! 
	If you log in to this server by illegal means or without authorization, please stop all operations and log out immediately, 
	because any operation you do to the server will be punished by law!' >> /etc/motd

3.创建本地yum源

(1)上传rhel-server-7.6-x86_64-dvd.iso 镜像上传至192.168.102.110:/opt下

(2)挂载镜像文件

mkdir /opt/cdrom
mount -o loop /opt/rhel-server-7.6-x86_64-dvd.iso /opt/cdrom

(3)配置本地yum源

vi /etc/yum.repos.d/local.repo
[local]
name=localhost
baseurl=file:///opt/cdrom
enabled=1
gpgcheck=0
:wq
yum makecache

(4)配置http yum源 (可选)

yum insrall -y httpd
ln -s /opt/cdrom /var/www/html/cdrom
systemctl start httpd
curl http://192.168.102.110/cdrom  可列出cdrom目录中的文件则配置生效
vi /etc/yum.repos.d/local.repo
[localhttp]
name=localhttp
baseurl=http://192.168.102.110/cdrom
enabled=1
gpgcheck=0
:wq
yum makecache

4.配置本地hosts解析

vi /etc/hosts
192.168.102.110 dbserver1
192.168.102.111 dbserver2
:wq

三、安装必须依赖

yum install -y cmake3 git gcc-c++ ncurses-devel perl-Data-Dumper boost boost-doc boost-devel bzip2 openssl-devel m4

四、升级gcc 9.1.0

1.下载离线升级包,上传到/opt/

   wget http://ftp.gnu.org/gnu/gcc/gcc-9.1.0/gcc-9.1.0.tar.gz
   wget ftp://gcc.gnu.org/pub/gcc/infrastructure/gmp-6.1.0.tar.bz2
   wget ftp://gcc.gnu.org/pub/gcc/infrastructure/mpfr-3.1.4.tar.bz2
   wget ftp://gcc.gnu.org/pub/gcc/infrastructure/mpc-1.0.3.tar.gz
   wget ftp://gcc.gnu.org/pub/gcc/infrastructure/isl-0.18.tar.bz2

2.安装gmp-6.1.0

tar -jxf gmp-6.1.0.tar.bz2 && \
cd gmp-6.1.0 && \
mkdir temp && \
cd temp && \
../configure --prefix=/opt/gmp-6.1.0 && \
make -j12 && \
make install

3.安装mpfr

tar -jxf mpfr-3.1.4.tar.bz2 && \
cd mpfr-3.1.4 && \
mkdir temp && \
cd temp && \
../configure --prefix=/opt/mpfr-3.1.4 --with-gmp=/opt/gmp-6.1.0 && \
make -j12 && \
make install

4.安装mpc

tar -xvzf mpc-1.0.3.tar.gz && \
cd mpc-1.0.3 && \
mkdir temp && \
cd temp/ && \
../configure --prefix=/opt/mpc-1.0.3 --with-gmp=/opt/gmp-6.1.0 --with-mpfr=/opt/mpfr-3.1.4 && \
make -j12 && \
make install

5.添加环境变量

echo "export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/mpc-1.0.3/lib:/opt/gmp-6.1.0/lib:/opt/mpfr-3.1.4/lib" >> /etc/profile
	source /etc/profile

6.安装gcc

tar -zxvf gcc-9.1.0.tar.gz && \
cd gcc-9.1.0 && \
mkdir temp && \
cd temp && \
../configure  --disable-checking --disable-multilib --enable-languages=c,c++  --disable-multilib --prefix=/opt/gcc9 --with-gmp=/opt/gmp-6.1.0 --with-mpfr=/opt/mpfr-3.1.4 --with-mpc=/opt/mpc-1.0.3 

// 如果报错 请使用

 ../configure --disable-multilib --enable-languages=c,c++
 make -j12 && \ make install
 
 echo  "PATH=/opt/gcc9/bin:/opt/gcc9/lib64:$PATH
 LD_LIBRARY_PATH=/opt/gcc9/lib/:$LD_LIBRARY_PATH" >> /etc/profile

7.查看gcc版本

gcc --version 

五、升级cmake3

1.下载并上传到服务器/opt

wget https://cmake.org/files/v3.12/cmake-3.12.4.tar.gz

2.解压安装

tar zxvf cmake-3.12.4.tar.gz && \
cd cmake-3.12.4  && \
./bootstrap 
此处若报错:
 	cp /usr/local/lib64/libstdc++.so.6.0.X /lib64/
 	cd /lib64/
 	rm -f libstdc++.so.6
 	ln -s libstdc++.so.6.0.X libstdc++.so.6

./bootstrap && \
gmake && \
gmake install

3.查看编译后的cmake版本

/usr/local/bin/cmake --version

4.移除原版本cmake

yum remove cmake -y

5.新建软连接

ln -s /usr/local/bin/cmake /usr/bin/

6.终端查看版本

cmake --version

出现版本表示成功!cmake编译完成

六、安装mysql8

1.上传到服务器/opt 并解压

tar -zxvf mysql-boost-8.0.18.tar.gz

2.创建操作目录

  mkdir -p /mysql/data
  cd mysql-8.0.18/  &&   mkdir builder && cd builder

3.编译

cmake ../ -DCMAKE_INSTALL_PREFIX=/mysql \
 -DMYSQL_DATADIR=/mysql/data \
 -DSYSCONFDIR=/mysql \
 -DMYSQL_USER=mysql \
 -DCMAKE_C_COMPILER=/opt/gcc9/bin/gcc \
 -DCMAKE_CXX_COMPILER=/opt/gcc9/bin/g++ \
 -DWITH_MYISAM_STORAGE_ENGINE=1 \
 -DWITH_INNOBASE_STORAGE_ENGINE=1 \
 -DMYSQL_UNIX_ADDR=/mysql/mysql.sock \
 -DMYSQL_TCP_PORT=3306 \
 -DEXTRA_CHARSETS=all \
 -DDEFAULT_CHARSET=utf8mb4 \
 -DDEFAULT_COLLATION=utf8mb4_general_ci \
 -DWITH_DEBUG=0 \
 -DMYSQL_MAINTAINER_MODE=0 \
 -DWITH_SYSTEMD=1 \
 -DDOWNLOAD_BOOST=1 \
 -DWITH_BOOST=../boost

4.修改配置

(1)在文件buf0buf.cc的开头添加:(/opt/mysql-8.0.18/storage/innobase/buf/buf0buf.cc)

#include
#include
#define gettid() syscall(SYS_gettid)

(2)将lock0lock.cc.o 中 (/opt/mysql-8.0.18/storage/innobase/lock/lock0lock.cc)

“os_compare_and_swap_thread_id” 修改为 os_compare_and_swap_lint

(3) 将trx0trx.cc 中

“os_compare_and_swap_thread_id” 修改为 os_compare_and_swap_lint
注意: 这里有两处需要替换

5.安装

make -j12 && make install

注解:
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql:MySQL安装的根目录
-DMYSQL_DATADIR=/data/mysql:数据文件所存放的目录
-DSYSCONFDIR=/etc :MySQL配置文件所在目录
-DMYSQL_USER=mysql:MySQL服务的用户名
-DWITH_MYISAM_STORAGE_ENGINE=1:安装MyISAM引擎
-DWITH_INNOBASE_STORAGE_ENGINE=1:安装InnoDB引擎
-DWITH_ARCHIVE_STORAGE_ENGINE=1:安装Archive引擎
-DWITH_MEMORY_STORAGE_ENGINE=1:安装Memory引擎
-DWITH_FEDERATED_STORAGE_ENGINE=1:安装Federated引擎
-DWITH_PARTITION_STORAGE_ENGINE=1:安装Partition引擎
-DWITH_READLINE=1:MySQL的readline library
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock:sock文件的路径
-DMYSQL_TCP_PORT=3306 :MySQL的监听端口
-DENABLED_LOCAL_INFILE=1:启用加载本地数据
-DENABLE_DOWNLOADS=1:编译时允许自主下载相关文件
-DEXTRA_CHARSETS=all :使MySQL支持所有的扩展字符
-DDEFAULT_CHARSET=utf8mb4:设置默认字符集为utf8mb4
-DDEFAULT_COLLATION=utf8mb4_general_ci:设置默认字符校对
-DWITH_DEBUG=0:禁用调试模式
-DMYSQL_MAINTAINER_MODE=0:是否启用mysql维护器特定的开发环境
-DDOWNLOAD_BOOST=1:允许在线更新boost库
-DWITH_BOOST=…/boost:指定boost安装路径 -DCMAKE_C_COMPILER=/opt/gcc9/bin/gcc 指定新安装的gcc位置 -DCMAKE_CXX_COMPILER=/opt/gcc9/bin/g++ \指定新安装的g++位置

6.初始化MySQL

(1)创建my.cnf

vi /mysql/my.cnf

[mysqld]
server-id=71 #第二台mysql-bin-72.log
port=3306
character-set-server=utf8mb4
default-time_zone='+8:00'
lower_case_table_names=1

log-bin=mysql-bin
auto_increment_increment=2 #步进值,主节点数量
auto_increment_offset=1  #起始值,一般填写第n台主机mysql,第二台填写2

max_connections=3000
max_connect_errors=100000


default_authentication_plugin=mysql_native_password
datadir=/mysql/data
socket=/mysql/data/mysql.sock

log-error=/mysql/logs/mysqld.log
pid-file=/mysql/mysqld.pid

lower_case_table_names=1

binlog_format=ROW
log_bin=/data/mysql/data/mysql-bin-71.log #第二台mysql-bin-72.log
expire_logs_days=30

long_query_time=1
slow_query_log = 1
slow_query_log_file=/mysql/logs/slow.log

[client]
port=38965
default-character-set=utf8mb4
socket=/mysql/data/mysql.sock

[mysql]
no-auto-rehash
default-character-set=utf8mb4

(2)初始化

mkdir /mysql/logs
cd /mysql/bin
./mysqld --initialize --user=mysql --basedir=/mysql/ --datadir=/mysql/data

记住密码输入的密码

(3)添加环境变量

echo “PATH=$PATH:$HOME/.local/bin:$HOME/bin” >> /mysql/.bash_profile
source /mysql/.bash_profile

7.启动MySQL

./mysqld --defaults-file=/mysql/my.cnf --datadir=/mysql/data --user=mysql

修改root密码

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'mypwd#2019'

七、配置双主模式

1.在两台节点创建同步用户

create user 'repl'@'192.168.102.%' identified by 'replepassword';
grant replication slave on *.* to 'repl'@'192.168.102.%';

2.主库设置

(1)查看从库show master status;

mysql>  show master status;
+---------------------+----------+--------------+------------------+-------------------+
| File         | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------------+----------+--------------+------------------+-------------------+
| mysql-bin-72.000007 |     1796 |              |                  |                   |
+---------------------+----------+--------------+------------------+-------------------+

(2)在主库配置

mysql> change master to
    -> master_host='192.168.102.110',
    -> master_port=3306,
    -> master_user='repl',
    -> master_password='Csii@401',
    ->  master_log_file='mysql-bin-71.000008',
    -> master_log_pos=1796;

(3)启动slave

start slave;

3.从库设置

(1)查看主库show master status;

mysql>  show master status;
+---------------------+----------+--------------+------------------+-------------------+
| File         | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------------+----------+--------------+------------------+-------------------+
| mysql-bin-72.000008 |     1796 |              |                  |                   |
+---------------------+----------+--------------+------------------+-------------------+

(2)在从库配置

mysql> change master to
    -> master_host='192.168.102.111',
    -> master_port=3306,
    -> master_user='repl',
    -> master_password='Csii@401',
    ->  master_log_file='mysql-bin-71.000007',
    -> master_log_pos=1796;

(3)启动slave

start slave;

4.查看同步状态

show slave status\G;

5. GTID同步配置

主从机 my.cnf分别增加配置:

#GTID
gtid_mode = on
enforce_gtid_consistency = 1
log_slave_updates = 1

重启MySQL,并执行

mysql> stop slave;

Query OK, 0 rows affected (0.00 sec)

mysql> change master to MASTER_AUTO_POSITION=1;

Query OK, 0 rows affected (0.01 sec)

mysql> start slave;

Query OK, 0 rows affected (0.00 sec)

mysql> show processlist\G;

发现已经有binlog dump GTID 的信息则配置成功。

八、优化

skip-name-resolve  #远程客户端登录
character_set_server=utf8
init_connect='SET NAMES utf8'
max_allowed_packet=48M #设置在网络传输中一次消息传输量的最大值
innodb_log_file_size=256M #确定数据日志文件的大小,更大的设置可以提高性能,但也会增加恢复故障数据库所需的时间
max_connections=1000#MySQL允许最大的进程连接数,如果经常出现Too Many Connections的错误提示,则需要增大此值
wait_timeout=60#指定一个请求的最大连接时间
innodb_buffer_pool_size = 4096M  #最大内存的70%
innodb_thread_concurrency=8 #CPU核心数

九、配置高可用

1. 安装keepalived(两台mysql服务器安装)

yum install -y pcre-devel openssl-devel popt-devel libnl libnl-devel
tar zxvf keepalived-1.2.7.tar.gz  
cd keepalived-1.2.7  
./configure --prefix=/mysql/keepalived  
make  && make install
mkdir /etc/keepalived/  
cp /mysql/keepalived/etc/keepalived/keepalived.conf /etc/keepalived
cp /mysql/keepalived/sbin/keepalived /usr/sbin/ 

2.配置deepalived

本环境服务器信息
MySQL服务器1: 10.1.13.103
MySQL服务器2: 10.1.13.104
vip: 10.1.13.40

MySQL服务器1: /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {
   notification_email {
     [email protected]
     [email protected]
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id mysql_ha
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_script chk_mysql {

    script "/etc/keepalived/mysql.sh"
    interval 3
    weight 3
}

vrrp_instance VI_1 {
    state MASTER
    interface ens192
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        10.1.13.40
    }
    track_script {
       chk_mysql
    }
}


MySQL服务器2:/etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {
   notification_email {
     [email protected]
     [email protected]
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id mysql_ha
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_script chk_mysql {

    script "/etc/keepalived/mysql.sh"
    interval 3
    weight 3
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens192
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        10.1.13.40
    }
    track_script {
       chk_mysql
    }
}


mysql.sh 脚本内容

#!/bin/bash
A=`ps -C mysqld --no-header | wc -l`
if [ $A -eq 0 ];then
	killall keepalived
fi

3. 启动keealived

systemctl start keepalived
systemctl enable keepalived
systemctl status keepalived

ip  addr
查看vip

4.验证高可用切换

killall keepalived  
或
kill mysql_pid

vip 或漂移到另一台MySQL服务器,则切换成功。

你可能感兴趣的:(MySQL8+keepalived 主主(双主)模式部署)