2020-10-18 數(shù)據(jù)庫與ansible

作業(yè) 1016

1= 如果主節(jié)點已經(jīng)運行了一段時間,且有大量數(shù)據(jù)時嫌佑,如何配置并啟動slave節(jié)點(寫出操作步驟)

# 主節(jié)點 10.0.0.8
# 新增從節(jié)點 10.0.0.81
1.在主服務(wù)器創(chuàng)建復(fù)制賬號
grant replication slave on *.* to repluser@'10.0.0.%' identified by 'centos';

2.在主服務(wù)器完全備份
mkdir /backup
mysqldump -A -F --single-transaction --master-data=1 > /backup/fullbackup.sql
# 并將文件復(fù)制到新增的從節(jié)點
scp /backup/fullbackup.sql 10.0.0.81:

3.在從節(jié)點安裝相同版本的數(shù)據(jù)庫
yum -y install mysql-server

4.從節(jié)點配置
# 編輯配置文件
vim /etc/my.cnf
[mysqld]
server-id=81
# 啟動服務(wù)
systemctl enable --now mysql

5.在從節(jié)點打開備份文件
# 找到以下內(nèi)容:
CHANGE MASTER TO MASTER_LOG_FILE='mariadb-bin.000002', MASTER_LOG_POS=389;
# 將本行修改為下述內(nèi)容:
CHANGE MASTER TO
MASTER_HOST='10.0.0.8',
MASTER_USER='repluser',
MASTER_PASSWORD='centos',
MASTER_PORT=3306, 
MASTER_LOG_FILE='mariadb-bin.000002', MASTER_LOG_POS=389;

6.在從節(jié)點執(zhí)行恢復(fù)命令
mysql < fullbackup.sql

7.從節(jié)點登錄數(shù)據(jù)庫
# 開啟從節(jié)點線程
start slave;

# 查看狀態(tài)
show slave status\G;

8.檢查從節(jié)點數(shù)據(jù)庫內(nèi)容,驗證備份情況

2= 當(dāng)master服務(wù)器宕機譬挚,提升一個slave成為新的master(寫出操作步驟)

# 環(huán)境
master=10.0.0.8
slave1=10.0.0.81
slave2=10.0.0.82
###########################################################
1.比較兩臺從服務(wù)器的復(fù)制進度设捐,選擇數(shù)據(jù)最新的一臺,讓它成為新master
cat /var/lib/mysql/relay-log.info
# 比較第三行的值异希,數(shù)值大的優(yōu)先(假設(shè)為slave2)

2.登錄slave2的數(shù)據(jù)庫
# 停止其線程健盒,并清除從節(jié)點信息
stop slave;
reset slave all;

3.修改slave2的配置文件
# 關(guān)閉read-only配置绒瘦,并開啟二進制日志
vim /etc/my.cnf
[mysqld]
server-id=82
read-only=OFF
log-bin

4.清除原master的復(fù)制信息
set global read_only=off;
stop slave;
reset slave all;

5.slave2作為新的master
# 進行全備份
mysqldump -A -F --single-transaction --master-data=1 > backup.sql
# 將備份拷貝至slave1
scp backup.sql 10.0.0.81:

6.分析舊的 master的二進制日志称簿,將未同步到至新 master的二進制日志導(dǎo)出來,恢復(fù)到新master
# 假設(shè)最新日志為 mariadb-bin.000777
# 首先導(dǎo)出文件
mysqlbinlog /var/lib/mysql/mariadb-bin.000777 > bin.sql
# 分析并編輯內(nèi)容惰帽,只留下還未備份的內(nèi)容
# 同步到新的master:
mysql < bin.sql

7.讓slave1指向新的master
# 打開備份文件憨降,編輯內(nèi)容
vim backup.sql
找到 CHANGE MASTER TO... 修改為如下內(nèi)容:
CHANGE MASTER TO
MASTER_HOST='10.0.0.82',
MASTER_USER='repluser',
MASTER_PASSWORD='centos',
MASTER_PORT=3306,
MASTER_LOG_FILE='mysql-bin.00000X', MASTER_LOG_POS=XXX;

# 在slave1進行還原:
MariaDB [hellodb]> stop slave;
MariaDB [hellodb]> reset slave all;
MariaDB [hellodb]> set sql_log_bin=off;
MariaDB [hellodb]> source backup.sql;
MariaDB [hellodb]> set sql_log_bin=on;
MariaDB [hellodb]> start slave;

8.查看狀態(tài)
show slave status\G;

9.登錄檢查數(shù)據(jù)

3= 通過 MHA 0.58 搭建一個數(shù)據(jù)庫集群結(jié)構(gòu)

00——環(huán)境

# 環(huán)境:四臺主機
10.0.0.7  CentOS7 MHA-manager
10.0.0.8  CentOS8 Master
10.0.0.81 CentOS8 Slave1
10.0.0.82 CentOS8 Slave2

01——在管理節(jié)點(CentOS7)上安裝兩個包

# 將兩個安裝包放入當(dāng)前目錄
yum -y install ./mha4mysql-*

02——在所有MySQL服務(wù)器上安裝 MHA

# 另外3臺 CentOS8 安裝:
yum -y install ./mha4mysql-node-0.58-0.el7.centos.noarch.rpm

03——在所有節(jié)點實現(xiàn)相互之間ssh key驗證

# 在CentOS7操作:
1# 生成密鑰
ssh-keygen  執(zhí)行之后連續(xù)3次回車

2# copy私鑰到本機,得到私鑰文件 authorized_keys
ssh-copy-id 127.0.0.1   執(zhí)行之后驗證本機密碼

3# 將整個ssh目錄復(fù)制到需要通訊的主機
# 首先各個主機全部安裝rsync
yum -y install rsync

# 依次復(fù)制
rsync -a .ssh 10.0.0.8:/root/
rsync -a .ssh 10.0.0.81:/root/
rsync -a .ssh 10.0.0.82:/root/

4# 相互連接進行檢驗

04——在管理節(jié)點 (centos 7) 建立配置文件

# 腳本安裝 mysql 5.7

# 準(zhǔn)備配置文件
mkdir /etc/mastermha/
vim /etc/mastermha/app1.cnf
[server default]
user=mhauser
password=centos
manager_workdir=/data/mastermha/app1/
manager_log=/data/mastermha/app1/manager.log
remote_workdir=/data/mastermha/app1/
ssh_user=root
repl_user=repluser
repl_password=centos
ping_interval=1 
master_ip_failover_script=/usr/local/bin/master_ip_failover
report_script=/usr/local/bin/sendmail.sh
master_binlog_dir=/data/mysql/
check_repl_delay=1

[server1]                       
hostname=10.0.0.8
candidate_master=1

[server2]
hostname=10.0.0.81

[server3]
hostname=10.0.0.82
candidate_master=1

05——準(zhǔn)備相關(guān)腳本

# 郵件報警
cat > /usr/local/bin/sendmail.sh << EOF
echo 'mysql is down' | mail -s 'MHA warning' 1227163339@qq.com
EOF
# 添加執(zhí)行權(quán)限
chmod +x /usr/local/bin/sendmail.sh
#################################################################
# 實現(xiàn)vip(虛擬ip)  perl語言腳本
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 = '10.0.0.100/24';
my $gateway = '10.0.0.254';
my $interface = 'eth0';
my $key = "1";
my $ssh_start_vip = "/sbin/ifconfig $interface:$key $vip;/sbin/arping -I
$interface -c 3 -s $vip $gateway >/dev/null 2>&1";
my $ssh_stop_vip = "/sbin/ifconfig $interface:$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" ) {
# $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\@$orig_master_host \" $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";
}

# 添加執(zhí)行權(quán)限
chmod +x /usr/local/bin/master_ip_failover

06——實現(xiàn)Master(10.0.0.8)

# 腳本安裝 mysql 5.7

# 從 centos7 將安裝文件拷貝過去
[root@centos7 ~]#scp ins.sh mysql-5.7.29-linux-glibc2.12-x86_64.tar.gz 10.0.0.8:

# 配置文件
vim /etc/my.cnf
[mysqld]
server_id=8
log-bin
general_log
general_log=ON
skip_name_resolve=1

# 重啟服務(wù)
systemctl restart mysqld

# 二進制定位
mysql> show master logs;
+-------------------+-----------+
| Log_name          | File_size |
+-------------------+-----------+
| master-bin.000001 |       154 |
+-------------------+-----------+
1 row in set (0.00 sec)

# 創(chuàng)建賬號8眯铩J谝!N仄恰;谶础!
# 【復(fù)制賬號】
grant replication slave on *.* to repluser@'10.0.0.%' identified by 'centos';
# 【MHA管理賬號】
grant all on *.* to mhauser@'10.0.0.%' identified by 'centos';

# 配置VIP
ifconfig eth0:1 10.0.0.100/24

07——實現(xiàn)slave(兩臺)

# 腳本安裝 mysql 5.7

# 從 centos7 將安裝文件拷貝過去
[root@centos7 ~]#scp ins.sh mysql-5.7.29-linux-glibc2.12-x86_64.tar.gz 10.0.0.81:
[root@centos7 ~]#scp ins.sh mysql-5.7.29-linux-glibc2.12-x86_64.tar.gz 10.0.0.82:

# 分別配置
vim /etc/my.cnf
[mysqld]
server_id=81 與 82
log-bin
read_only
relay_log_purge=0
skip_name_resolve=1

# 重啟服務(wù)
systemctl restart mysqld

# 實現(xiàn)主從
CHANGE MASTER TO 
MASTER_HOST='10.0.0.8', 
MASTER_USER='repluser',
MASTER_PASSWORD='centos', 
MASTER_LOG_FILE='master-bin.000001',
MASTER_LOG_POS=154;

# 啟動線程
START SLAVE;
# 檢查狀態(tài)
show slave status\G;

08——檢查MHA的環(huán)境

# 檢查ssh
masterha_check_ssh --conf=/etc/mastermha/app1.cnf

# 檢查repl
masterha_check_repl --conf=/etc/mastermha/app1.cnf

# 查看狀態(tài)
masterha_check_status --conf=/etc/mastermha/app1.cnf

09——啟動MHA

# 啟動
nohup masterha_manager --conf=/etc/mastermha/app1.cnf &> /dev/null

# 查看狀態(tài)
masterha_check_status --conf=/etc/mastermha/app1.cnf 

# 在主節(jié)點查看到健康性檢查
tail -f /data/mysql/master.log 

4= 實戰(zhàn)案例:Percona XtraDB Cluster(PXC 5.7)

01——環(huán)境準(zhǔn)備

pxc1:10.0.0.7
pxc2:10.0.0.70
pxc3:10.0.0.71

02——安裝 Percona XtraDB Cluster 5.7

# 官方源太慢爵嗅,此處使用清華大學(xué)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 ~]#yum -y install Percona-XtraDB-Cluster-57

# 將此倉庫拷貝至其他主機
[root@pxc1 ~]#scp /etc/yum.repos.d/pxc.repo 10.0.0.70:/etc/yum.repos.d
[root@pxc1 ~]#scp /etc/yum.repos.d/pxc.repo 10.0.0.71:/etc/yum.repos.d

# 其他節(jié)點安裝pxc
[root@pxc2 ~]#yum -y install Percona-XtraDB-Cluster-57
[root@pxc3 ~]#yum -y install Percona-XtraDB-Cluster-57

03——在各個節(jié)點上分別配置mysql及集群配置文件

# 主配置文件 /etc/my.cnf 不需要修改
# /etc/percona-xtradb-cluster.conf.d/mysqld_safe.cnf   不需要修改

# PXC的配置文件必須修改
[root@pxc1 ~]# vim /etc/percona-xtradb-cluster.conf.d/wsrep.cnf
【8行】 wsrep_cluster_address=gcomm://10.0.0.7,10.0.0.70,10.0.0.71
【25行】 wsrep_node_address=10.0.0.7
【30行】 wsrep_node_name=pxc-cluster-node-1    # 默認(rèn)為1
【39行】 wsrep_sst_auth="sstuser:s3cretPass"
# 補充
vim /etc/percona-xtradb-cluster.conf.d/mysqld.cnf 文件中 server-id=1 保持默認(rèn)值

[root@pxc2 ~]# vim /etc/percona-xtradb-cluster.conf.d/wsrep.cnf
【8行】 wsrep_cluster_address=gcomm://10.0.0.7,10.0.0.70,10.0.0.71
【25行】 wsrep_node_address=10.0.0.70
【30行】 wsrep_node_name=pxc-cluster-node-2    # 修改為2
【39行】 wsrep_sst_auth="sstuser:s3cretPass"
# 補充
vim /etc/percona-xtradb-cluster.conf.d/mysqld.cnf 文件中 server-id=2 # 修改為2

[root@pxc3 ~]# vim /etc/percona-xtradb-cluster.conf.d/wsrep.cnf
【8行】 wsrep_cluster_address=gcomm://10.0.0.7,10.0.0.70,10.0.0.71
【25行】 wsrep_node_address=10.0.0.71
【30行】 wsrep_node_name=pxc-cluster-node-3    # 修改為3
【39行】 wsrep_sst_auth="sstuser:s3cretPass"
# 補充
vim /etc/percona-xtradb-cluster.conf.d/mysqld.cnf 文件中 server-id=3 # 修改為3

04——啟動PXC集群中第一個節(jié)點

# 啟動
[root@pxc1 ~]#systemctl start mysql@bootstrap.service

# 查看端口娇澎,發(fā)現(xiàn)多出 3306 和 4567

# 查看root密碼
[root@pxc1 ~]#grep "temporary password" /var/log/mysqld.log
2020-10-16T13:30:57.915970Z 1 [Note] A temporary password is generated for root@localhost: 6K/1!*iSAdr>

# 登錄
[root@pxc1 ~]#mysql -uroot -p'6K/1!*iSAdr>'

# 修改密碼
mysql> alter user 'root'@'localhost' identified by 'centos';

# 創(chuàng)建相關(guān)用戶并授權(quán)
mysql> CREATE USER 'sstuser'@'localhost' IDENTIFIED BY 's3cretPass';
mysql> GRANT RELOAD, LOCK TABLES, PROCESS, REPLICATION CLIENT ON *.* TO 'sstuser'@'localhost';

# 查看相關(guān)變量
mysql> SHOW VARIABLES LIKE 'wsrep%'\G;

# 查看相關(guān)狀態(tài)變量
mysql> SHOW STATUS LIKE 'wsrep%'\G;

05——啟動PXC集群中其它所有節(jié)點

# 啟動
[root@pxc2 ~]#systemctl start mysql
[root@pxc3 ~]#systemctl start mysql

# 分別查看端口
3306 + 4567

06——查看集群狀態(tài),驗證集群是否成功

# 在任意節(jié)點睹晒,查看集群狀態(tài)
[root@pxc2 ~]#mysql -uroot -pcentos
mysql> SHOW VARIABLES LIKE 'wsrep_node_name';
+-----------------+--------------------+
| Variable_name   | Value              |
+-----------------+--------------------+
| wsrep_node_name | pxc-cluster-node-2 |
+-----------------+--------------------+
1 row in set (0.00 sec)

mysql> SHOW VARIABLES LIKE 'wsrep_node_address';
+--------------------+-----------+
| Variable_name      | Value     |
+--------------------+-----------+
| wsrep_node_address | 10.0.0.70 |
+--------------------+-----------+
1 row in set (0.00 sec)

mysql> SHOW VARIABLES LIKE 'wsrep_on';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| wsrep_on      | ON    |
+---------------+-------+
1 row in set (0.00 sec)

mysql> SHOW STATUS LIKE 'wsrep_cluster_size';
+--------------------+-------+
| Variable_name      | Value |
+--------------------+-------+
| wsrep_cluster_size | 3     |
+--------------------+-------+
1 row in set (0.00 sec)


# 在任意節(jié)點查看數(shù)據(jù)庫
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)


# 在任意節(jié)點創(chuàng)建數(shù)據(jù)庫
# pxc3創(chuàng)建:
mysql> create database caokunzi333;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| caokunzi333        |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.01 sec)

# px1和px2驗證:
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| caokunzi333        |      # 同步成功
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

# 利用Xshell撰寫欄工具趟庄,同時在三個節(jié)點數(shù)據(jù)庫括细,只會在其中一個節(jié)點成功
# px1:
mysql> create database  onetime;
ERROR 1007 (HY000): Can't create database 'onetime'; database exists

# px2:
mysql> create database  onetime;
Query OK, 1 row affected (0.01 sec)

# px3:
mysql> create database  onetime;
ERROR 1007 (HY000): Can't create database 'onetime'; database exists

5= 通過 ansible 部署二進制 mysql 8

# 主機準(zhǔn)備
堡壘機 10.0.0.7
# 遠(yuǎn)程主機
[root@centos7 ~]#cat hosts.list 
10.0.0.71
10.0.0.72
10.0.0.81
10.0.0.82

# 實現(xiàn)key驗證
[root@centos7 ~]#cat ssh.sh 
#!/bin/bash
#
#********************************************
#Author:       jacklee
#QQ:           1227163339
#Time:         2020-10-18_10:14:39
#FileName:     ssh.sh
#Copyright:    2020 All rights reserved
#Description:   
#*********************************************
rpm -q sshpass || yum -y install sshpass
[ -f /root/.ssh/id_rsa ] || ssh-keygen -f /root/.ssh/id_rsa -P ''

export SSHPASS=centos

while read IP; do
    sshpass -e ssh-copy-id -o StrictHostKeyChecking=no $IP
done < hosts.list

# 運行腳本并驗證key
[root@centos7 ~]#ssh 10.0.0.71
Last login: Sun Oct 18 09:46:03 2020 from 10.0.0.1
[root@centos7 ~]#logout
Connection to 10.0.0.71 closed.
[root@centos7 ~]#ssh 10.0.0.72
Last login: Sun Oct 18 09:46:17 2020 from 10.0.0.1
[root@centos7 ~]#logout
Connection to 10.0.0.72 closed.
[root@centos7 ~]#ssh 10.0.0.81
Last login: Sun Oct 18 09:46:24 2020 from 10.0.0.1
[root@centos8 ~]#logout
Connection to 10.0.0.81 closed.
[root@centos7 ~]#ssh 10.0.0.82
Last login: Sun Oct 18 09:46:27 2020 from 10.0.0.1
[root@centos8 ~]#

# 安裝 ansible,并準(zhǔn)備主機列表
yum -y install ansible

[root@centos7 ~]#cat /etc/ansible/hosts
[appsrvs]
10.0.0.71
10.0.0.81
[websrvs]
10.0.0.72
10.0.0.82

# 檢驗
[root@centos7 ~]#ansible all --list
  hosts (4):
    10.0.0.71
    10.0.0.81
    10.0.0.72
    10.0.0.82

# 測試可用性
[root@centos7 ~]#ansible all -m ping
10.0.0.72 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
10.0.0.71 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
10.0.0.81 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    }, 
    "changed": false, 
    "ping": "pong"
}
10.0.0.82 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    }, 
    "changed": false, 
    "ping": "pong"
}
----------------------------------------------------------------------
# 準(zhǔn)備文件目錄
mkdir -pv /data/ansible/files/

# 準(zhǔn)備相關(guān)文件
cd /data/ansible/files/
# 安裝包: 
mysql-8.0.19-linux-glibc2.12-x86_64.tar.xz

# 配置文件
vim my.cnf
[mysqld]
socket=/tmp/mysql.sock
user=mysql
symbolic-links=0
datadir=/data/mysql
innodb_file_per_table=1
log-bin
pid-file=/data/mysql/mysqld.pid
[client]
port=3306
socket=/tmp/mysql.sock
[mysqld_safe]
log-error=/var/log/mysqld.log

# 安全加固解決方案
vim  /data/ansible/files/secure_mysql.sh

#!/bin/bash
yum -y install expect &> /dev/null
expect &> /dev/null <<EOF
spawn /usr/local/mysql/bin/mysql_secure_installation
expect {
        "Press y" { send "n\n";exp_continue }
        "New password" { send "centos\n";exp_continue }
        "Re-enter" { send "centos\n";exp_continue }
        "Remove anonymous users" { send "y\n";exp_continue }
        "Disallow root login" { send "y\n";exp_continue }
        "Remove test database" { send "y\n";exp_continue }
        "Reload privilege" { send "y\n" }
}
EOF


# 查看所有備用文件
/data/ansible/files/
├── my.cnf
├── mysql-8.0.19-linux-glibc2.12-x86_64.tar.xz
└── secure_mysql.sh

# 在 /data/ansible/ 目錄準(zhǔn)備好 yml腳本
vim /data/ansible/install_mysql.yml
---
- hosts: appsrvs
  remote_user: root
  gather_facts: no

  tasks: 
    - name: install dependent packages
      yum: name=libaio,perl-Data-Dumper,perl-Getopt-Long,libncurses*
    - name: create  group
      group: name=mysql gid=306
    - name: create  user
      user: name=mysql uid=306 group=mysql shell=/sbin/nologin system=yes create_home=no home=/data/mysql
    - name: copy tar to remote host and Set file attributes
      unarchive: src=/data/ansible/files/mysql-8.0.19-linux-glibc2.12-x86_64.tar.xz dest=/usr/local/ owner=root group=root
    - name: create link /usr/local/mysql
      file: src=/usr/local/mysql-8.0.19-linux-glibc2.12-x86_64 dest=/usr/local/mysql state=link
    - name: set PATH
      copy: content='PATH=/usr/local/mysql/bin:$PATH' dest=/etc/profile.d/mysql.sh
    - name: config my.cnf
      copy: src=/data/ansible/files/my.cnf  dest=/etc/my.cnf
      tags: config
    - name: Initialize the MySQL
      shell: /usr/local/mysql/bin/mysqld --initialize-insecure --datadir=/data/mysql --user=mysql                                
    - name: set service script
      shell: /bin/cp  /usr/local/mysql/support-files/mysql.server  /etc/init.d/mysqld
    - name: enable service
      shell: chkconfig --add mysqld ; chkconfig mysqld on ; /etc/init.d/mysqld start
    - name: secure script
      script: /data/ansible/files/secure_mysql.sh

# 語法檢查
[root@centos7 ~]#ansible-playbook --syntax-check /data/ansible/install_mysql.yml

playbook: /data/ansible/install_mysql.yml

# 選擇在 81 執(zhí)行
[root@centos7 ansible]#ansible-playbook install_mysql.yml --limit 10.0.0.81

PLAY [all] **********************************************************************
..........................

PLAY RECAP **********************************************************************
10.0.0.81 : ok=11   changed=11   unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
# 在 81 主機驗證
# 主動刷新 PATH
source /etc/profile.d/mysql.sh

# 登錄
[root@centos8 ~]#mysql -uroot -pcentos
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.19 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.

mysql>

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末戚啥,一起剝皮案震驚了整個濱河市奋单,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌猫十,老刑警劉巖览濒,帶你破解...
    沈念sama閱讀 221,576評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異拖云,居然都是意外死亡匾七,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,515評論 3 399
  • 文/潘曉璐 我一進店門江兢,熙熙樓的掌柜王于貴愁眉苦臉地迎上來昨忆,“玉大人,你說我怎么就攤上這事杉允∫靥” “怎么了?”我有些...
    開封第一講書人閱讀 168,017評論 0 360
  • 文/不壞的土叔 我叫張陵叔磷,是天一觀的道長拢驾。 經(jīng)常有香客問我,道長改基,這世上最難降的妖魔是什么繁疤? 我笑而不...
    開封第一講書人閱讀 59,626評論 1 296
  • 正文 為了忘掉前任,我火速辦了婚禮秕狰,結(jié)果婚禮上稠腊,老公的妹妹穿的比我還像新娘。我一直安慰自己鸣哀,他們只是感情好架忌,可當(dāng)我...
    茶點故事閱讀 68,625評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著我衬,像睡著了一般叹放。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上挠羔,一...
    開封第一講書人閱讀 52,255評論 1 308
  • 那天井仰,我揣著相機與錄音,去河邊找鬼破加。 笑死俱恶,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播速那,決...
    沈念sama閱讀 40,825評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼俐银,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了端仰?” 一聲冷哼從身側(cè)響起捶惜,我...
    開封第一講書人閱讀 39,729評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎荔烧,沒想到半個月后吱七,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,271評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡鹤竭,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,363評論 3 340
  • 正文 我和宋清朗相戀三年踊餐,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片臀稚。...
    茶點故事閱讀 40,498評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡吝岭,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出吧寺,到底是詐尸還是另有隱情窜管,我是刑警寧澤,帶...
    沈念sama閱讀 36,183評論 5 350
  • 正文 年R本政府宣布稚机,位于F島的核電站幕帆,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏赖条。R本人自食惡果不足惜失乾,卻給世界環(huán)境...
    茶點故事閱讀 41,867評論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望纬乍。 院中可真熱鬧碱茁,春花似錦、人聲如沸蕾额。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,338評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽诅蝶。三九已至,卻和暖如春募壕,著一層夾襖步出監(jiān)牢的瞬間调炬,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,458評論 1 272
  • 我被黑心中介騙來泰國打工舱馅, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留缰泡,地道東北人。 一個月前我還...
    沈念sama閱讀 48,906評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像棘钞,于是被迫代替她去往敵國和親缠借。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,507評論 2 359