centos7 安装mysql8

1. 添加 Yum 包

rpm -qa |grep -i mysql   或者mariadb

rpm -e --nodeps mysql-community-libs-compat-8.0.18-1.el7.x86_64

 

wget https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm

rpm -ivh mysql80-community-release-el7-1.noarch.rpm

2. 安装 MySQL

yum -y install mysql-community-server

systemctl start mysqld

3. 修改密码

MySQL 安装过程中会为 root 用户生成一个临时密码,保存在 /var/log/mysqld.log 中。通过以下命令查看:
sudo grep 'temporary password' /var/log/mysqld.log

mysql -u root -p

ALTER USER 'root'@'localhost' IDENTIFIED BY 'your passowrd';

密码强度要求是:不少于12字符,必须包含大写字母、小写字母、数字和特殊字符。

4 创建root远程连接

 CREATE USER 'root'@'%' IDENTIFIED BY 'root';
 GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;

root 用户默认的密码加密方式是:caching_sha2_password;而很多图形客户端工具可能还不支持这种加密认证方式,连接的时候就会报错 。通过以下命令重新修改密码:
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'Root@123';

如果忘记密码
在配置文件添加
skip-grang-tables
use mysql;
update user set authentication_string='' where user='root';
退出,去掉参数,重启服务 ,重复第3 4 步骤



创建MHA

1 首先做成一主两从
主节点
vim /etc/my.cnf
[mysqld]
datadir=/data/mysql/mysql_data/
server_id=202
log-bin=mysql-bin
#relay_log_purge=0
skip_name_resolve
default_authentication_plugin=mysql_native_password
character-set-server=utf8
innodb_file_per_table=ON
max_connections=65536
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
两个 从节点
[mysqld]
datadir=/data/mysql/mysql_data/
server_id=203
log-bin=mysql-bin
read_only
relay_log_purge=0
skip_name_resolve
innodb_file_per_table=ON
max_connections=65536
default_authentication_plugin=mysql_native_password
character-set-server=utf8
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

从库定时删除relay-log
crontab -e
0 2 * * * /usr/bin/purge_relay_logs --user=root --disable_relay_log_purge >> /opt/mha/log/purge_relay_logs.log 2>&1

创建两个用户
CREATE USER 'repluser'@'192.168.1.%' IDENTIFIED BY 'Repluser@123';
GRANT replication slave  ON *.* TO 'repluser'@'192.168.1.%' WITH GRANT OPTION;
ALTER USER 'repluser'@'192.168.1.%' IDENTIFIED WITH mysql_native_password BY 'Repluser@123';

CREATE USER 'mhauser'@'192.168.1.%' IDENTIFIED BY 'Mhauser@123';
GRANT all  ON *.* TO 'mhauser'@'192.168.1.%' WITH GRANT OPTION;
ALTER USER 'mhauser'@'192.168.1.%' IDENTIFIED WITH mysql_native_password BY 'Mhauser@123';
flush privileges;

在主服务器做全量备份,导入到从库
mysqldump -uroot -h192.168.1.202 -p  -A  -F --single-transaction --master-data=1   > all.sql

mysql -uroot -h192.168.1.203 -p < all.sql

配置ssh
ssh-keygen
cat .ssh/id_rsa.pub >> .ssh/authorized_keys
chmod go= .ssh/authorized_keys
scp -p .ssh/id_rsa .ssh/authorized_keys root@192.168.1.202:/root/.ssh (三个地址都做认证)

打开all.sql文件 查看change master 位置
CHANGE MASTER TO MASTER_HOST='192.168.1.202', MASTER_USER='repluser',MASTER_PASSWORD='Repluser@123',。。。。。 (两个从节点都认证)

安装manager mha节点安装mha和node (manager依赖epel) 各mysql 只安装node节点

在manager节点
vim /etc/mha/app1.cnf
[server default]
user=mhauser
password=Mhauser@123
manager_workdir=/data/masterha/app1
manager_log=/data/masterha/app1/manager.log
remote_workdir=/data/masterha/app1
ssh_user=root
repl_user=repluser
repl_password=Repluser@123
ping_interval=1
[server1]
hostname=192.168.1.202
master_binlog_dir=/data/mysql/mysql_data/
candidate_master=1
[server2]
hostname=192.168.1.203
master_binlog_dir=/data/mysql/mysql_data/
candidate_master=1
[server3]
hostname=192.168.1.204

检测
masterha_check_ssh --conf=/etc/mha/app1.cnf
masterha_check_repl --conf=/etc/mha/app1.cnf
开启
masterha_manager --conf=/etc/mha/app1.cnf

注意
当主的宕机后,将新主的配置文件中的read—only注释掉
宕机后的主服务器,上线将作为从服务器,修改配置文件中 read-only
发生过一次故障后必须删除manager下的app1.failover.complete


vip漂移
在manager节点配置
[server default]
user=mha
password=Mhauser@123
manager_workdir=/data/masterha/app1
manager_log=/data/masterha/app1/manager.log
remote_workdir=/data/masterha/app1
report_script=/usr/local/bin/send_report #添加
master_ip_failover_script=/usr/local/bin/master_ip_failover #添加
master_ip_online_change_script=/usr/local/bin/master_ip_failover #添加
ssh_user=root
repl_user=repluser
repl_password=Repluser@123
ping_interval=1
[server1]
hostname=192.168.1.202
master_binlog_dir=/data/mysql/mysql_data/
candidate_master=1
[server2]
hostname=192.168.1.203
master_binlog_dir=/data/mysql/mysql_data/
candidate_master=1
[server3]
hostname=192.168.1.204

编辑脚本 并给执行权限
vim /usr/local/bin/send_report
#!/usr/bin/perl
# Copyright (C) 2011 DeNA Co.,Ltd.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
## Note: This is a sample script and is not complete. Modify the script based on your environment.
use strict;
use warnings FATAL => 'all';
use Getopt::Long;
#new_master_host and new_slave_hosts are set only when recovering master succeeded
my ( $dead_master_host, $new_master_host, $new_slave_hosts, $subject, $body );
GetOptions(
  'orig_master_host=s' => \$dead_master_host,
  'new_master_host=s' => \$new_master_host,
  'new_slave_hosts=s' => \$new_slave_hosts,
  'subject=s' => \$subject,
  'body=s' => \$body,
);
# Do whatever you want here
exit 0;

vim /usr/local/bin/master_ip_failover
#!/usr/bin/env perl
use strict;
use warnings FATAL => 'all';

use Getopt::Long;

my (
    $command,          $ssh_user,        $orig_master_host, $orig_master_ip,
    $orig_master_port, $new_master_host, $new_master_ip,    $new_master_port
);

my $vip = '192.168.1.222/24';  # Virtual IP 按实际情况修改
my $key = "0"; #eth冒号后面的数字
my $ssh_start_vip = "/sbin/ifconfig em1:$key $vip"; #注意看ifconfig
my $ssh_stop_vip = "/sbin/ifconfig em1:$key down";
$ssh_user = "root";
GetOptions(
    'command=s'          => \$command,
    'ssh_user=s'         => \$ssh_user,
    'orig_master_host=s' => \$orig_master_host,
    'orig_master_ip=s'   => \$orig_master_ip,
    'orig_master_port=i' => \$orig_master_port,
    'new_master_host=s'  => \$new_master_host,
    'new_master_ip=s'    => \$new_master_ip,
    'new_master_port=i'  => \$new_master_port,
);
exit &main();

sub main {

    print "\n\nIN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip===\n\n";

    if ( $command eq "stop" || $command eq "stopssh" ) {

        # $orig_master_host, $orig_master_ip, $orig_master_port are passed.
        # If you manage master ip address at global catalog database,
        # invalidate orig_master_ip here.
        my $exit_code = 1;
        eval {
            print "Disabling the VIP on old master: $orig_master_host \n";
            &stop_vip();
            $exit_code = 0;
        };
        if ($@) {
            warn "Got Error: $@\n";
            exit $exit_code;
        }
        exit $exit_code;
    }
    elsif ( $command eq "start" ) {

        # all arguments are passed.
        # If you manage master ip address at global catalog database,
        # activate new_master_ip here.
        # You can also grant write access (create user, set read_only=0, etc) here.
        my $exit_code = 10;
        eval {
            print "Enabling the VIP - $vip on the new master - $new_master_host \n";
            &start_vip();
            $exit_code = 0;
        };
        if ($@) {
            warn $@;
            exit $exit_code;
        }
        exit $exit_code;
    }
    elsif ( $command eq "status" ) {
        print "Checking the Status of the script.. OK \n";
        `ssh $ssh_user\@cluster1 \" $ssh_start_vip \"`;
        exit 0;
    }
    else {
        &usage();
        exit 1;
    }
}

# A simple system call that enable the VIP on the new master
sub start_vip() {
    `ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`;
}
# A simple system call that disable the VIP on the old_master
sub stop_vip() {
    `ssh $ssh_user\@$orig_master_host \" $ssh_stop_vip \"`;
}

sub usage {
    print
    "Usage: master_ip_failover --command=start|stop|stopssh|status --orig_master_host=host --orig_master_ip=ip --orig_master_port=port --new_master_host=host --new_master_ip=ip --new_master_port=port\n";
}


















二进制日志说明
max_binlog_size 默认为1G
expire_logs_days=N 二进制日志可以自动删除的天数,默认不自动删除


xtrabackup用法


















posted @ 2019-12-11 20:30  Linus小跟班  阅读(1045)  评论(0编辑  收藏  举报