1. Ansible-playbook实现MySQL的二进制部署
1.1 架构及主机
五台主机
1 Ansible主控端:
节点1:(只设一个节点)
主机名:Ansible-PRI
CentOS 8.4
IP: 192.168.250.8/24
ansible 2.9.27
2 Ansible被控端--CentOS7组:
节点1:
主机名:Ansible-IP17
CentOS 7.9
IP: 192.168.250.17/24
节点2:
主机名:Ansible-IP27
CentOS 7.9
IP: 192.168.250.27/24
3. Ansible被控端--CentOS8组:
节点1:
主机名:Ansible-IP18
CentOS 8.4
IP: 192.168.250.18/24
节点2:
主机名:Ansible-IP28
CentOS 8.4
IP: 192.168.250.28/24
4. Ansible被控端--DBS组:
节点1:
主机名:Ansible-IP58
CentOS 8.4
IP: 192.168.250.58/24
节点2:
主机名:Ansible-IP68
CentOS 8.4
IP: 192.168.250.68/24
# 说明:按照上面的架构图,准备好五台不同组别和操作系统的主机,将以此为基础环境完成ansible的参数等学习和案例实践
1.2 Ansible 主控端准备
1.2.1 主控端环境准备及软件包安装
基本任务:同步时钟;安装ansible;查看版本并了解文件格式和基本语法等
# 主控端服务器CentOS8.4 上基本配置
[root@CentOS84 ]#hostnamectl set-hostname Ansible-PRI
[root@CentOS84 ]#exit
[root@Ansible-PRI ]#
[root@Ansible-PRI ]#hostname -I
192.168.250.8
[root@Ansible-PRI ]#systemctl enable --now chronyd.service
# Ansible 走的是EPEL源,如果没配置的话需要配置或者启用
[root@Ansible-PRI ]#yum repolist
repo id repo name
AppStream AppStream
BaseOS BaseOS
EPEL EPEL
centosplus centosplus
extras extras
[root@Ansible-PRI ]#
# 查看默认ansible的版本
[root@Ansible-PRI ]#yum info ansible
BaseOS 4.6 kB/s | 3.9 kB 00:00
AppStream 6.8 kB/s | 4.3 kB 00:00
EPEL 30 kB/s | 4.7 kB 00:00
EPEL 326 kB/s | 11 MB 00:35
extras 11 kB/s | 1.5 kB 00:00
centosplus 1.7 kB/s | 1.5 kB 00:00
Available Packages
Name : ansible
Version : 2.9.27
Release : 1.el8
Architecture : noarch
Size : 17 M
Source : ansible-2.9.27-1.el8.src.rpm
Repository : EPEL
Summary : SSH-based configuration management, deployment, and task execution system
URL : http://ansible.com
License : GPLv3+
Description : Ansible is a radically simple model-driven configuration management,
: multi-node deployment, and remote task execution system. Ansible works
: over SSH and does not require any software or daemons to be installed
: on remote nodes. Extension modules can be written in any language and
: are transferred to managed machines automatically.
[root@Ansible-PRI ]#
# 安装ansible
[root@Ansible-PRI ]#yum -y install ansible
# 验证安装及查看版本
[root@Ansible-PRI ]#ansible --version
ansible 2.9.27
config file = /etc/ansible/ansible.cfg
configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3.6/site-packages/ansible
executable location = /usr/bin/ansible
python version = 3.6.8 (default, Mar 19 2021, 05:13:41) [GCC 8.4.1 20200928 (Red Hat 8.4.1-1)]
[root@Ansible-PRI ]#whereis ansible
ansible: /usr/bin/ansible /etc/ansible /usr/share/ansible /usr/share/man/man1/ansible.1.gz
[root@Ansible-PRI ]#file /usr/bin/ansible
/usr/bin/ansible: Python script, ASCII text executable
# ansible是Python script
[root@Ansible-PRI ]#cat /usr/bin/ansible
........................
# 从文件内容可以考到是python3.6开发的
........................
[root@Ansible-PRI ]#
1.1.2 主控端与被控端基于key验证
基本任务:利用编写好的脚本实现与主控端相同网段内的所有主机之间基于key的SSH免密通信
# 修改SSH的配置文件
[root@Ansible-PRI ]#vim /etc/ssh/ssh_config
...............................
StrictHostKeyChecking no
...............................
"/etc/ssh/ssh_config" 53L, 1795C written
[root@Ansible-PRI ]#cat /etc/ssh/ssh_config | grep StrictHostKeyChecking
# StrictHostKeyChecking ask
StrictHostKeyChecking no
# 编写脚本实现Ansible 主控端与被控端的基于key的绵密SSH登录
[root@Ansible-PRI ]#vim ssh_key_iplist.sh
[root@Ansible-PRI ]#cat ssh_key_iplist.sh
#
#********************************************************************************************<strong>
#Author: WuDongWuXia
#QQ: 1050572574@qq.com
#Date: 2022-03-02
#FileName: ssh_key_iplist.sh
#URL: www.shoneinfo.cn
#Description: The Test Script
#Copyright (C):2022 All rights reserved
#</strong>*******************************************************************************************
IPLIST="
192.168.250.17
192.168.250.27
192.168.250.8
192.168.250.18
192.168.250.28
192.168.250.58
192.168.250.68"
rpm -q sshpass &> /dev/null || yum -y install sshpass
[ -f /root/.ssh/id_rsa ] || ssh-keygen -f /root/.ssh/id_rsa -P ''
export SSHPASS=2XXXX8
for IP in $IPLIST;do
sshpass -e ssh-copy-id -o StrictHostKeyChecking=no $IP
done
[root@Ansible-PRI ]#
# 运行脚本
[root@Ansible-PRI ]#bash ssh_key_iplist.sh
# 查看SSH KEY认证的主机信息
[root@Ansible-PRI ]#cat /root/.ssh/known_hosts
1.1.3 主控端 ansbile 基础配置
基本任务:配置好ansible的主机信息;并测通方可进入下一步骤。
# 配置ansible的主机组等,这样为整个ansible 的实验统一准备好环境,本次仅针对 [dbs] 组实践数据库的安装
[root@Ansible-PRI ]#cat /etc/ansible/hosts
........................
[local]
192.168.250.8 ansible_connection=local
[centos7]
192.168.250.17
192.168.250.27
[centos8]
192.168.250.18
192.168.250.28
# 本次数据库安装仅针对[dbs]组
[dbs]
192.168.250.58
192.168.250.68
........................
[root@Ansible-PRI ]#ansible all --list-hosts
hosts (7):
192.168.250.8
192.168.250.17
192.168.250.27
192.168.250.18
192.168.250.28
192.168.250.58
192.168.250.68
[root@Ansible-PRI ]#ansible dbs --list-hosts
hosts (2):
192.168.250.58
192.168.250.68
# ansible 的主控端和被控端之间通信检测,确保pong
[root@Ansible-PRI ]#ansible all -m ping
192.168.250.8 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
192.168.250.27 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
192.168.250.17 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
192.168.250.28 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
192.168.250.18 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
192.168.250.58 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
192.168.250.68 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
[root@Ansible-PRI ]#
[root@Ansible-PRI ]#
[root@Ansible-PRI ]#ll
total 1168604
-rw-r--r-- 1 root root 1196633756 Mar 2 18:42 mysql-8.0.27-linux-glibc2.12-x86_64.tar.xz
-rw-r--r-- 1 root root 781 Mar 2 17:57 ssh_key_iplist.sh
[root@Ansible-PRI ]#
1.3 准备 MySQL8.0.27 二进制包
官网:https://downloads.mysql.com/archives/community/

下载地址: https://cdn.mysql.com/archives/mysql-8.0/mysql-8.0.27-linux-glibc2.12-x86_64.tar.xz
[root@Ansible-PRI ]#cd /data/
[root@Ansible-PRI ]#wget https://cdn.mysql.com/archives/mysql-8.0/mysql-8.0.27-linux-glibc2.12-x86_64.tar.xz
--2022-03-03 13:55:30-- https://cdn.mysql.com/archives/mysql-8.0/mysql-8.0.27-linux-glibc2.12-x86_64.tar.xz
Resolving cdn.mysql.com (cdn.mysql.com)... 23.2.84.230
Connecting to cdn.mysql.com (cdn.mysql.com)|23.2.84.230|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1196633756 (1.1G) [text/plain]
Saving to: ‘mysql-8.0.27-linux-glibc2.12-x86_64.tar.xz’
mysql-8.0.27-linux-glibc2.12-x 100%[=================================================>] 1.11G 4.41MB/s in 5m 5s
2022-03-03 14:00:36 (3.74 MB/s) - ‘mysql-8.0.27-linux-glibc2.12-x86_64.tar.xz’ saved [1196633756/1196633756]
[root@Ansible-PRI ]#ll
total 1168588
-rw-r--r-- 1 root root 1196633756 Sep 29 05:18 mysql-8.0.27-linux-glibc2.12-x86_64.tar.xz
[root@Ansible-PRI ]#
1.4 Ansible-playbook二进制部署MySQL8.0.27
基本任务:创建Anisible的目录,并构建好清晰的结构;编写二进制部署MySQL8.0.27的 Ansible-playbook yaml 文件;完成部署。
[root@Ansible-PRI ]#mkdir -p /data/ansible/files
[root@Ansible-PRI ]#ll /data/ansible/files
total 0
[root@Ansible-PRI ]#tree /data/
/data/
├── ansible
│ └── files
├── mysql-8.0.27-linux-glibc2.12-x86_64.tar.xz
└── ssh_key_iplist.sh
2 directories, 5 files
[root@Ansible-PRI ]#tree -d /data/
/data/
└── ansible
└── files
[root@Ansible-PRI ]#mv mysql-8.0.27-linux-glibc2.12-x86_64.tar.xz /data/ansible/files/
[root@Ansible-PRI ]#tree /data/
/data/
├── ansible
│ └── files
│ └── mysql-8.0.27-linux-glibc2.12-x86_64.tar.xz
├── ansible.ymls.tar
├── hosts.list
├── ssh_key_hosts.sh
└── ssh_key_iplist.sh
2 directories, 5 files
[root@Ansible-PRI ]#vim files/mysql8.cnf
[root@Ansible-PRI ]#cat files/mysql8.cnf
[mysqld]
server-id=1
log-bin
datadir=/data/mysql
socket=/data/mysql/mysql.sock
skip_name_resolve = on
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid
[client]
port=3306
socket=/data/mysql/mysql.sock
[root@Ansible-PRI ]#tree
.
└── files
├── mysql-8.0.27-linux-glibc2.12-x86_64.tar.xz
└── mysql8.cnf
1 directory, 2 files
[root@Ansible-PRI ]#
[root@Ansible-PRI ]#vim ansible/install-mysql8.0.27-v02.yml
[root@Ansible-PRI ]#cat ansible/install-mysql8.0.27-v02.yml
---
# 在线或本地用二进制文件批量部署 mysql8.0.27
# install mysql-8.0.27-linux-glibc2.12-x86_64.tar.xz
# 配置文件 mysql8.cnf 放到目录 /data/ansible/files 将被复制到被控端的my.cnf
- hosts: dbs
remote_user: root
gather_facts: no
vars:
mysql_version: 8.0.27
mysql_file: mysql-{{mysql_version}}-linux-glibc2.12-x86_64.tar.xz
mysql_root_password: shoneXXXXX6
tasks:
- name: install packages
yum:
name:
- libaio
- numactl-libs
state: latest
- name: create mysql group
group: name=mysql gid=306
- name: create mysql user
user: name=mysql uid=306 group=mysql shell=/sbin/nologin system=yes create_home=no home=/data/mysql
# 在线方式 测试了下,可能要等很久,建议还是下载好安装包方式安装
# - name: download mysql_file
# unarchive :
# src: "https://cdn.mysql.com/archives/mysql-8.0/mysql-{{mysql_version}}-linux-glibc2.12-x86_64.tar.xz"
# dest: "/usr/local"
# owner: root
# group: root
# remote_src: yes
# 离线方式配置
- name: copy tar to remote host and file mode
unarchive: src=/data/ansible/files/{{mysql_file}} dest=/usr/local/ owner=root group=root
- name: create linkfile /usr/local/mysql
file: src=/usr/local/mysql-{{ mysql_version }}-linux-glibc2.12-x86_64 dest=/usr/local/mysql state=link
- name: data dir
shell: /usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --datadir=/data/mysql
tags: data
- name: config my.cnf
copy: src=/data/ansible/files/mysql8.cnf dest=/etc/my.cnf
- name: service script
shell: /bin/cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
- name: PATH variable
copy: content='PATH=/usr/local/mysql/bin:$PATH' dest=/etc/profile.d/mysql.sh
- name: enable service
shell: chkconfig --add mysqld;/etc/init.d/mysqld start
tags: service
- name: change password
shell: /usr/local/mysql/bin/mysqladmin -uroot password
shell: chkconfig --add mysqld;/etc/init.d/mysqld start
tags: service
- name: change password
shell: /usr/local/mysql/bin/mysqladmin -uroot password {{mysql_root_password}}
[root@Ansible-PRI ]#
[root@Ansible-PRI ]#tree /data/
/data/
├── ansible
│ ├── files
│ │ ├── mysql-8.0.27-linux-glibc2.12-x86_64.tar.xz
│ │ └── mysql8.cnf
│ ├── install-mysql8.0.27-v01.yml
│ └── install-mysql8.0.27-v02.yml #此文件是二进制部署MySQL8.0.27的 Ansible-playbook yaml 文件
└── ssh_key_iplist.sh
2 directories, 5 files
# 部署
[root@Ansible-PRI ]#ansible-playbook --syntax-check install-mysql8.0.27-v02.yml
[WARNING]: While constructing a mapping from /data/ansible/install-mysql8.0.27-v02.yml, line 47, column 7, found a duplicate dict
key (shell). Using last defined value only.
playbook: install-mysql8.0.27-v02.yml
[root@Ansible-PRI ]#
[root@Ansible-PRI ]#ansible-playbook install-mysql8.0.27-v02.yml
[WARNING]: While constructing a mapping from /data/ansible/install-mysql8.0.27-v02.yml, line 47, column 7, found a duplicate dict key (shell).
Using last defined value only.
PLAY [dbs] **********************************************************************************************************************************<strong>
TASK [install packages] </strong>*******************************************************************************************************************<strong>
ok: [192.168.250.58]
ok: [192.168.250.68]
TASK [create mysql group] </strong>*****************************************************************************************************************<strong>
ok: [192.168.250.58]
ok: [192.168.250.68]
TASK [create mysql user] </strong>******************************************************************************************************************<strong>
ok: [192.168.250.58]
ok: [192.168.250.68]
TASK [copy tar to remote host and file mode] </strong>**********************************************************************************************<strong>
changed: [192.168.250.68]
changed: [192.168.250.58]
TASK [create linkfile /usr/local/mysql] </strong>***************************************************************************************************<strong>
changed: [192.168.250.68]
changed: [192.168.250.58]
TASK [data dir] </strong>***************************************************************************************************************************<strong>
changed: [192.168.250.58]
changed: [192.168.250.68]
TASK [config my.cnf] </strong>**********************************************************************************************************************<strong>
changed: [192.168.250.58]
changed: [192.168.250.68]
TASK [service script] </strong>*********************************************************************************************************************<strong>
changed: [192.168.250.58]
changed: [192.168.250.68]
TASK [PATH variable] </strong>**********************************************************************************************************************<strong>
changed: [192.168.250.68]
changed: [192.168.250.58]
TASK [enable service] </strong>*********************************************************************************************************************<strong>
changed: [192.168.250.58]
changed: [192.168.250.68]
TASK [change password] </strong>********************************************************************************************************************<strong>
changed: [192.168.250.58]
changed: [192.168.250.68]
TASK [change password] </strong>********************************************************************************************************************<strong>
changed: [192.168.250.58]
changed: [192.168.250.68]
PLAY RECAP </strong>**********************************************************************************************************************************
192.168.250.58 : ok=12 changed=9 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.250.68 : ok=12 changed=9 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@Ansible-PRI ]#
1.5 验证安装
# 在被控端IP192.168.250.58 上验证安装。 注意:安装完后要退出终端后再重新登录,再登录 mysql
[root@Ansible-IP58 ]#mysql -V
bash: mysql: command not found...
Packages providing this file are:
'mariadb'
'mysql'
[root@Ansible-IP58 ]#
[root@Ansible-IP58 ]#exit
logout
[root@Ansible-IP58 ]#
[root@Ansible-IP58 ]#mysql -u root -pshoneXXXXX6
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.27 MySQL Community Server - GPL
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.01 sec)
mysql>
mysql> quit
Bye
[root@Ansible-IP58 ]#mysql -V
mysql Ver 8.0.27 for Linux on x86_64 (MySQL Community Server - GPL)