作业(五)作业

余生长醉 提交于 2020-10-19 08:48:25

1、如果主节点已经运行了一段时间,且有大量数据时,如何配置并启动slave节点(写出操作步骤)


image.png

主配置MySQL

[root@Centos8 ~]#yum -y install mariadb-server
[root@Centos8 ~]#vim /etc/my.cnf.d/mariadb-server.cnf
[mysqld]
log_bin
server-id=125
[root@Centos8 ~]#systemctl start mariadb
[root@Centos8 ~]#mysql

#创建复制账号
MariaDB [(none)]> grant replication slave on *.* to 'repluser'@'192.168.80.%' identified by 'heshizhe';
Query OK, 0 rows affected (0.000 sec)

#备份数据库
[root@Centos8 ~]#mkdir /backup
[root@Centos8 ~]#mysqldump -A -F --single-transaction --master-data=1 --default-character-set=utf8 > /backup/all.sql
[root@Centos8 ~]#ll /backup/
total 468
-rw-r--r-- 1 root root 479022 Oct 12 19:16 all.sql

从MySQL配置

[root@Centos8-2 ~]#yum -y install mariadb-server

#把备份文件复制到root目录下
[root@Centos8-2 ~]#scp 192.168.80.120:/backup/all.sql /root/

#将完全备份还原到新的从节点
[root@Centos8-2 ~]#vim /etc/my.cnf.d/mariadb-server.cnf
[mysqld]
server-id=121
read-only

#从完全备份的位置之后开始复制
[root@Centos8-2 ~]#vim all.sql
CHANGE MASTER TO
MASTER_HOST='192.168.80.120', #主MySQL IP地址
MASTER_USER='repluser',       #复制的账号
MASTER_PASSWORD='heshizhe',   #密码
MASTER_LOG_FILE='mariadb-bin.000003',
MASTER_LOG_POS=389;

[root@Centos8-2 ~]#systemctl start mariadb
[root@Centos8-2 ~]#mysql < all.sql
[root@Centos8-2 ~]#mysql
MariaDB [(none)]> start slave; #开启进程
MariaDB [(none)]> show slave status\G; #查看进程详细信息
*************************** 1. row ***************************
                Slave_IO_State: Waiting for master to send event
                   Master_Host: 192.168.80.120
                   Master_User: repluser
                   Master_Port: 3306
                 Connect_Retry: 60
               Master_Log_File: mariadb-bin.000003
           Read_Master_Log_Pos: 516
                Relay_Log_File: mariadb-relay-bin.000002
                 Relay_Log_Pos: 684
         Relay_Master_Log_File: mariadb-bin.000003
              Slave_IO_Running: Yes
             Slave_SQL_Running: Yes
               Replicate_Do_DB:
           Replicate_Ignore_DB:
            Replicate_Do_Table:
        Replicate_Ignore_Table:
       Replicate_Wild_Do_Table:
   Replicate_Wild_Ignore_Table:
                    Last_Errno: 0
                    Last_Error:
                  Skip_Counter: 0
           Exec_Master_Log_Pos: 516
               Relay_Log_Space: 995
               Until_Condition: None
                Until_Log_File:
                 Until_Log_Pos: 0
            Master_SSL_Allowed: No
            Master_SSL_CA_File:
            Master_SSL_CA_Path:
               Master_SSL_Cert:
             Master_SSL_Cipher:
                Master_SSL_Key:
         Seconds_Behind_Master: 0  #复制延时
 Master_SSL_Verify_Server_Cert: No
                 Last_IO_Errno: 0
                 Last_IO_Error:
                Last_SQL_Errno: 0
                Last_SQL_Error:
   Replicate_Ignore_Server_Ids:
              Master_Server_Id: 125
                Master_SSL_Crl:
            Master_SSL_Crlpath:
                    Using_Gtid: No
                   Gtid_IO_Pos:
       Replicate_Do_Domain_Ids:
   Replicate_Ignore_Domain_Ids:
                 Parallel_Mode: conservative
                     SQL_Delay: 0
           SQL_Remaining_Delay: NULL
       Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
              Slave_DDL_Groups: 1
Slave_Non_Transactional_Groups: 0
    Slave_Transactional_Groups: 0
1 row in set (0.000 sec)


2、当master服务器宕机,提升一个slave成为新的master(写出操作步骤)

#找到哪个从节点的数据库是最新,让它成为新master
[root@Centos8-2 ~]#cat /var/lib/mysql/relay-log.info
5
./mariadb-relay-bin.000002
684
mariadb-bin.000003
516
0

#新master修改配置文件,关闭read-only配置
[root@Centos8-2 ~]#vim /etc/my.cnf.d/mariadb-server.cnf
[mysqld]
server-id=21
read-only=off
log-bin
[root@Centos8-2 ~]#systemctl restart mariadb.service #重启服务
#清除旧的master复制信息
MariaDB [(none)]> set global read_only=off;
MariaDB [(none)]> stop slave;
MariaDB [(none)]> reset slave all;

#创建复制账号
MariaDB [(none)]> GRANT REPLICATION SLAVE ON *.* TO 'repluser'@'192.168.80.%' IDENTIFIED BY 'heshizhe';

#在新master上完全备份
[root@Centos8-2 ~]#mysqldump -A -F  --single-transaction --master-data=1  --default-character-set=utf8 > /root/backup_`date +%F_%T`.sql
[root@Centos8-2 ~]#ll
total 480
-rw-r--r-- 1 root root 488111 Oct  3 17:12 backup_2020-10-03_17:12:16.sql

#其它所有 slave 重新还原数据库,指向新的master
[root@Centos8-3 ~]#scp 192.168.80.121:/root/backup_2020-10-03_17:12:16.sql  /root/
[root@Centos8-2 ~]#vim backup_2020-10-03_17\:12\:16.sql
CHANGE MASTER TO
MASTER_HOST='192.168.80.121', #主MySQL IP地址
MASTER_USER='repluser',       #复制的账号
MASTER_PASSWORD='heshizhe',   #密码
MASTER_LOG_FILE='mariadb-bin.000003', MASTER_LOG_POS=375;
MariaDB [mysql]> source /root/backup_2020-10-03_17:12:16.sql;
MariaDB [mysql]> start slave;

#试试有没有同步成功
MariaDB [(none)]> create database db2; #master MySQL

MariaDB [mysql]> show databases;       #slave  MySQL
+--------------------+
| Database           |
+--------------------+
| db1                |
| db2                |
| hellodb            |
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
6 rows in set (0.001 sec)


3、通过 MHA 0.58 搭建一个数据库集群结构

image.png

四台主机

192.168.80.125 centos7 MHA管理端

192.168.80.120 centos8 master

192.168.80.121 centos8 slave1

192.168.80.122 centos8 slave2

在所有节点实现相互之间ssh kye验证

[root@MHA ~]#ssh-keygen  #生成密钥
[root@MHA ~]#ssh-copy-id 192.168.80.125  #复制密钥到本机
[root@MHA ~]#rsync -a .ssh 192.168.80.120:/root  #复制密钥给MySQL服务端
[root@MHA ~]#rsync -a .ssh 192.168.80.121:/root
[root@MHA ~]#rsync -a .ssh 192.168.80.122:/root

在管理节点建立配置文件

[root@MHA ~]#yum -y install mha4mysql-node-0.58-0.el7.noarch.rpm  mhamha4mysql-manager-0.58-0.el7.noarch.rpm 
[root@MHA ~]#mkdir /etc/mastermha/
[root@MHA ~]#vim /etc/mastermha/app1.cnf
[server default]
user=mhauser          #用于远程连接MySQL所有节点的用户,需要有管理员的权限
password=heshizhe     #用于远程连接MySQL所有节点的密码。
manager_workdir=/data/mastermha/app1/                  #工作目录会自行生成不用手动创建
manager_log=/data/mastermha/app1/manager.log           #日志保存位置
master_binlog_dir=/data/mysql-binglog/                 #指定二进制日志存放的目录
remote_workdir=/data/mastermha/app1/ 
ssh_user=root                                          #用于实现远程ssh基于KEY的连接,访问二进制日志
repl_user=repluser                                     #主从复制的用户信息
repl_password=magedu
ping_interval=1                                        #健康性检查的时间间隔
master_ip_failover_script=/usr/local/bin/master_ip_faliover #切换VIP的perl脚本
report_script=/usr/local/bin/sendmail.sh               #当执行报警脚本
check_repl_delay=0
#默认如果slave中从库落后主库relaylog超过100M,主库不会选择这个从库为新的master,因为这个从库进行恢复需要很长的时间.通过这个参数,mha触发主从切换的时候会忽略复制的延时,通过check_repl_delay=0这个参数,mha触发主从切换时会忽略复制的延时,对于设置candidate_master=1的从库非常有用,这样确保这个从库一定能成为最新的master
[server1]
hostname=192.168.80.120
[server2]
hostname=192.168.80.121
candidate_master=1  #设置为优先候选master,即使不是集群中事件最新的slave,也会优先当master
[server3]
hostname=192.168.80.122

相关脚本

[root@MHA ~]#vim /usr/local/bin/master_ip_faliover #切换VIP的perl脚本
#!/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.80.100/24';  #改成我们需要的vip
my $key = '1';
my $ssh_start_vip = "/sbin/ifconfig eth0:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig eth0:$key down";

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" ) {

        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" ) {

        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";
        exit 0;
    }
    else {
        &usage();
        exit 1;
    }
}

sub start_vip() {
    `ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`;
}
sub stop_vip() {
     return 0  unless  ($ssh_user);
    `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";
}

[root@MHA ~]#chmod +x /usr/local/bin/master_ip_faliover
[root@MHA ~]#vim /usr/local/bin/sendmail.sh #邮箱报警脚本
echo "MySQL is down" | mail -s "MHA Warning" 456456456@qq.com
[root@MHA ~]#chmod +x /usr/local/bin/sendmail.sh

实现Master

[root@master ~]#yum -y install mha4mysql-node-0.56-0.el6.noarch.rpm  mariadb-server
[root@master ~]#vim /etc/my.cnf.d/mariadb-server.cnf
[mysqld]
server-id=120
log_bin
plugin-load-add = semisync_master #加载半同步复制插件
rpl_semi_sync_master_enabled=on   #启动半同步复制插件
rpl_semi_sync_master_timeout=3000 #3秒内无法同步,也将返回成功信息给客户端
[root@master ~]#systemctl start mariadb
[root@master ~]#mysql
MariaDB [(none)]> show master logs;
+--------------------+-----------+
| Log_name           | File_size |
+--------------------+-----------+
| mariadb-bin.000001 |     28198 |
| mariadb-bin.000002 |       344 |
+--------------------+-----------+
2 rows in set (0.000 sec)
#创建MHA用于远程连接MySQL所有节点的用户
MariaDB [(none)]> grant all on  *.*  to 'mhauser'@'192.168.80.%' identified by 'heshizhe'; 
#创建主从复制授权用户
MariaDB [(none)]> grant replication slave  on *.* to 'repluser'@'192.168.80.%' identified by 'heshizhe';
[root@master ~]#ifconfig eth0:1 192.168.80.100/24 #配置VIP地址

实现slave1

[root@slave1 ~]#yum -y install  mariadb-server mha4mysql-node-0.58-0.el7.noarch.rpm 
[root@slave1 ~]#vim /etc/my.cnf.d/mariadb-server.cnf
[mysqld]
server-id=121
read_only
log_bin
plugin_load_add = semisync_slave
rpl_semi_sync_slave_enabled=on
skip_name_resolve=1  #禁止反向解析
[root@slave1 ~]#systemctl start mariadb
[root@slave1 ~]#mysql
MariaDB [(none)]> CHANGE MASTER TO
    ->   MASTER_HOST='192.168.80.120',
    ->   MASTER_USER='repluser',
    ->   MASTER_PASSWORD='heshizhe',
    ->   MASTER_PORT=3306,
    ->   MASTER_LOG_FILE='mariadb-bin.000002',
    ->   MASTER_LOG_POS=344;
Query OK, 0 rows affected (0.025 sec)

MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.004 sec)

实现slave2

[root@slave1 ~]#yum -y install   mariadb-server mha4mysql-node-0.58-0.el7.noarch.rpm
[root@slave1 ~]#vim /etc/my.cnf.d/mariadb-server.cnf
[mysqld]
server-id=122
read_only
log_bin
plugin_load_add = semisync_slave
rpl_semi_sync_slave_enabled=on
skip_name_resolve=1  #禁止反向解析
[root@slave1 ~]#systemctl start mariadb
[root@slave1 ~]#mysql
MariaDB [(none)]> CHANGE MASTER TO
    ->   MASTER_HOST='192.168.80.120',
    ->   MASTER_USER='repluser',
    ->   MASTER_PASSWORD='heshizhe',
    ->   MASTER_PORT=3306,
    ->   MASTER_LOG_FILE='mariadb-bin.000002',
    ->   MASTER_LOG_POS=344;
Query OK, 0 rows affected (0.025 sec)

MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.004 sec)

检查Mha的环境

#检查环境
[root@mha ~]#masterha_check_ssh --conf=/etc/mastermha/app1.cnf
[root@mha ~]#masterha_check_repl --conf=/etc/mastermha/app1.cnf
#查看状态
[root@mha ~]#masterha_check_status --conf=/etc/mastermha/app1.cnf

启动MHA

#开启MHA,默认是前台运行
[root@MHA ~]#nohup masterha_manager --conf=/etc/mastermha/app1.cnf &> /dev/null
#查看状态
[root@MHA ~]#masterha_check_status --conf=/etc/mastermha/app1.cnf
app1 (pid:3093) is running(0:PING_OK), master:192.168.80.120

测试

(一)测试原master服务宕机,MHA提升新主节点过程开启manager管理节点日志跟踪,持续查看

[root@MHA ~]#tail -f /data/mastermha/manager.log

检测到原主已经宕机

image.png


原来的主masert 192.168.80.120已经宕机了,提升192.168.80.121为新主



image.png



4、实战案例:Percona XtraDB Cluster(PXC 5.7)


环境准备

三台主机

192.168.80.125 pxc1

192.168.80.126 pxc2

192.168.80.127 pxc3

关闭防火墙和SELinux,保证时间同步

注意:如果已经安装MySQL,必须卸载

安装 Percona XtraDB Cluster 5.7

#此处使用清华大学yum源,官方源太慢了
[root@pxc1 ~]#vim /etc/yum.repos.d/pxc.repo
[percona]
name=percona_repo
baseurl =
https://mirrors.tuna.tsinghua.edu.cn/percona/release/$releasever/RPMS/$basearch
enabled = 1
gpgcheck = 0

[root@pxc1 ~]#scp /etc/yum.repos.d/pxc.repo 192.168.80.126:/etc/yum.repos.d/
[root@pxc1 ~]#scp /etc/yum.repos.d/pxc.repo 192.168.80.127:/etc/yum.repos.d/

#在三个节点都安装好PXC 5.7
[root@pxc1 ~]#yum install Percona-XtraDB-Cluster-57 -y
[root@pxc2 ~]#yum install Percona-XtraDB-Cluster-57 -y
[root@pxc3 ~]#yum install Percona-XtraDB-Cluster-57 -y

在各个节点上分别配置mysql及集群配置文件

#第一个节点
[root@pxc1 ~]#vim /etc/percona-xtradb-cluster.conf.d/mysqld.cnf
[mysqld]
server-id=125
[root@pxc1 ~]#vim /etc/percona-xtradb-cluster.conf.d/wsrep.cnf
[root@pxc1 ~]#grep -Ev "^#|^$" /etc/percona-xtradb-cluster.conf.d/wsrep.cnf
[mysqld]
wsrep_provider=/usr/lib64/galera3/libgalera_smm.so
wsrep_cluster_address=gcomm://192.168.80.125,192.168.80.126,192.168.80.127
binlog_format=ROW
default_storage_engine=InnoDB
wsrep_slave_threads= 8
wsrep_log_conflicts
innodb_autoinc_lock_mode=2
wsrep_node_address=192.168.80.125
wsrep_cluster_name=pxc-cluster
wsrep_node_name=pxc-cluster-node-125
pxc_strict_mode=ENFORCING
wsrep_sst_method=xtrabackup-v2
wsrep_sst_auth="sstuser:123456789"


#第二个节点
[root@pxc2 ~]#vim /etc/percona-xtradb-cluster.conf.d/mysqld.cnf
[mysqld]
server-id=126
[root@pxc2 ~]#vim /etc/percona-xtradb-cluster.conf.d/wsrep.cnf
[root@pxc2 ~]#grep -Ev "^#|^$" /etc/percona-xtradb-cluster.conf.d/wsrep.cnf
[mysqld]
wsrep_provider=/usr/lib64/galera3/libgalera_smm.so
wsrep_cluster_address=gcomm://192.168.80.125,192.168.80.126,192.168.80.127
binlog_format=ROW
default_storage_engine=InnoDB
wsrep_slave_threads= 8
wsrep_log_conflicts
innodb_autoinc_lock_mode=2
wsrep_node_address=192.168.80.126
wsrep_cluster_name=pxc-cluster
wsrep_node_name=pxc-cluster-node-126
pxc_strict_mode=ENFORCING
wsrep_sst_method=xtrabackup-v2
wsrep_sst_auth="sstuser:123465789"

#第三个节点
[root@pxc3 ~]#vim /etc/percona-xtradb-cluster.conf.d/mysqld.cnf
[mysqld]
server-id=127
[root@pxc3 ~]#vim /etc/percona-xtradb-cluster.conf.d/wsrep.cnf
[root@pxc3 ~]#grep -Ev "^#|^$" /etc/percona-xtradb-cluster.conf.d/wsrep.cnf
[mysqld]
wsrep_provider=/usr/lib64/galera3/libgalera_smm.so
wsrep_cluster_address=gcomm://192.168.80.125,192.168.80.126,192.168.80.127
binlog_format=ROW
default_storage_engine=InnoDB
wsrep_slave_threads= 8
wsrep_log_conflicts
innodb_autoinc_lock_mode=2
wsrep_node_address=192.168.80.127
wsrep_cluster_name=pxc-cluster
wsrep_node_name=pxc-cluster-node-127
pxc_strict_mode=ENFORCING
wsrep_sst_method=xtrabackup-v2
wsrep_sst_auth="sstuser:123456789"

启动PXC集群中第一个节点

root@pxc1 ~]#systemctl start mysql@bootstrap.service #启动服务
[root@pxc1 ~]#ss -ntlu
Netid State      Recv-Q Send-Q                                             Local Address:Port                                                            Peer Address:Port
udp   UNCONN     0      0                                                      127.0.0.1:323                                                                        *:*
udp   UNCONN     0      0                                                          [::1]:323                                                                     [::]:*
tcp   LISTEN     0      128                                                            *:4567                                                                       *:*
tcp   LISTEN     0      100                                                    127.0.0.1:25                                                                         *:*
tcp   LISTEN     0      128                                                            *:22                                                                         *:*
tcp   LISTEN     0      100                                                        [::1]:25                                                                      [::]:*
tcp   LISTEN     0      80                                                          [::]:3306                                                                    [::]:*
tcp   LISTEN     0      128                                                         [::]:22                                                                      [::]:*
#取出数据库临时密码
[root@centos7 ~]#awk '/root@/{print $NF}' /var/log/mysqld.log
tb!56)WOKde-

[root@pcx1 ~]#mysql -uroot -p'tb!56)WOKde-'  #登入数据库

mysql> alter user   'root'@'localhost' identified  by '123456789';  #更改数据密码

#创建相关用户并授权
mysql> create user 'sstuser'@'locatlhost' identified by '123456789';
Query OK, 0 rows affected (0.00 sec)

mysql> grant reload,lock tables , process,replication  client on *.* to  'root'@'localhost';
Query OK, 0 rows affected (0.01 sec)
mysql> show status like 'wsrep%';
+----------------------------------+--------------------------------------+
| Variable_name                    | Value                                |
+----------------------------------+--------------------------------------+
| wsrep_local_state_uuid           | d12ab403-0fa7-11eb-b8db-4a1add9f3294 |
| .......                          | .....                                |
| wsrep_local_state                | 4                                    |
| wsrep_local_state_comment        | Synced                               |
| .....            		   | ......                               |
| wsrep_cluster_size               | 1                                    |
| wsrep_cluster_status             | Primary                              |
| wsrep_connected                  | ON                                   |
| wsrep_local_bf_aborts            | 0                                    |
| ....                             | ......                               |
| wsrep_ready                      | ON                                   |
+----------------------------------+--------------------------------------+
71 rows in set (0.01 sec)

启动PXC集群中其它所有节点

[root@pxc2 ~]#systemctl start  mysql.service
[root@pxc2 ~]#ss -ntl
State       Recv-Q Send-Q                                               Local Address:Port                                                              Peer Address:Port
LISTEN      0      128                                                              *:4567                                                                         *:*
LISTEN      0      100                                                      127.0.0.1:25                                                                           *:*
LISTEN      0      128                                                              *:22                                                                           *:*
LISTEN      0      100                                                          [::1]:25                                                                        [::]:*
LISTEN      0      80                                                            [::]:3306                                                                      [::]:*
LISTEN      0      128                                                           [::]:22                                                                        [::]:*

#第三个节点也一样,MySQL密码已经自动同步为123456了。

测试

image-20201017101845089

image.png


5、通过 ansible 部署二进制 mysql 8.0

[root@centos7-4 ~]#ssh-keygen
[root@centos7-4 ~]#ssh-copy-id 192.168.80.128
[root@centos7-4 ~]#rsync -a .ssh 192.168.80.120:/root
[root@centos7-4 ~]#rsync -a .ssh 192.168.80.121:/root
[root@centos7-4 ~]#ll
total 473736
-rw-r--r-- 1 root root       210 Oct 18 12:07 fuzhi.yaml
-rw-r--r-- 1 root root      1455 Oct 18 16:49 install_mysql.yaml
-rw-r--r-- 1 root root       297 Oct 18 11:53 my.cnf
-rw-r--r-- 1 root root 485074552 Oct 18 11:52 mysql-8.0.19-linux-glibc2.12-x86_64.tar.xz
-rw-r--r-- 1 root root       117 Oct 17 17:38 mysql_backup.sh
-rwxr-xr-x 1 root root       118 Oct 18 15:37 mysql_pass.sh
-rw-r--r-- 1 root root        45 Oct 18 15:00 mysql.sh
-rw-r--r-- 1 root root       365 Oct 18 16:27 mysql.yaml
[root@centos7-4 ~]#cat my.cnf
[mysqld]
datadir=/data/mysql #指定数据库目录
socket=/data/mysql/mysql.sock #套接字文件
skip_name_resolve=1 #禁止域名解析
log-error=/data/mysql/mysql.log #日志文件位置
pid-file=/data/mysql/mysql.pid  #记录当前 mysqld 进程的 pid
[client]
socket=/data/mysql/mysql.sock
[root@centos7-4 ~]#cat mysql_pass.sh
#!/bin/bash


mysqladmin -uroot -p`awk '/A temporary password/{print $NF}' /data/mysql/mysql.log`  password heshizhe

[root@centos7-4 ~]#cat install_mysql.yaml
---
 - hosts: dbserver
   remote_user: root
   gather_facts: no
   tasks:
    - name: 安装相关软件
      yum: name=libaio,numactl-libs
    - name: 创建MySQL用户组
      group: name=mysql gid=306 system=yes
    - name: 创建MySQL用户
      user: name=mysql uid=306 home=/data/mysql group=mysql shell=/sbin/nologin system=yes create_home=no
    - name: 解压数据库到远程主机
      unarchive: src=/root/mysql-8.0.19-linux-glibc2.12-x86_64.tar.xz   dest=/usr/local  owner=mysql group=mysql
    - name: 创建软链接
      file: src=/usr/local/mysql-8.0.19-linux-glibc2.12-x86_64 dest=/usr/local/mysql state=link
    - name: 复制配置文件
      copy: src=/root/my.cnf dest=/etc/my.cnf owner=mysql group=mysql
    - name: 配置环境变量
      copy: content='PATH=/usr/local/mysql/bin:$PATH'  dest=/etc/profile.d/mysql.sh
    - name: 运行环境变量
      shell: source /etc/profile.d/mysql.sh
    - name: 复制启动文件
      shell: /usr/bin/cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
    - name: 初始化数据库
      shell: /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/data/mysql
    - name: 启动数据库
      shell: chkconfig --add mysqld; systemctl start mysqld
    - name: 复制取出临时密码脚本到远程主机
      copy: src=/root/mysql_pass.sh  dest=/root/mysql_pass.sh mode=755
    - name: 更改数据库密码
      shell: bash /root/mysql_pass.sh
      
[root@centos7-4 ~]#cat /etc/ansible/hosts      
[webserver]
192.168.80.120
192.168.80.121
[appserver]
192.168.80.122
192.168.80.120
[dbserver]
192.168.80.121
192.168.80.120    
[root@centos7-4 ~]#ansible-playbook install_mysql.yaml


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!