

Ansible-playbook实现MySQL8.0.27的二进制部署
source link: https://blog.51cto.com/shone/5066477
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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;查看版本并了解文件格式和基本语法等
[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
........................
#!/usr/bin/python3.6
# 从文件内容可以考到是python3.6开发的
........................
[root@Ansible-PRI ]#
1.1.2 主控端与被控端基于key验证
基本任务:利用编写好的脚本实现与主控端相同网段内的所有主机之间基于key的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
#!/bin/bash
#
#********************************************************************************************<strong>
#Author: WuDongWuXia
#QQ: [email protected]
#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的主机信息;并测通方可进入下一步骤。
[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 ]#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 ]#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 验证安装
[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)
Recommend
-
65
1.lookupsfile支持从外部数据拉取信息,例如从数据库里拉取信息,然后定义给一个变量的形式hosts:allgather_facts:truevars:contents:"{{lookup('file','/etc/sysconfig/network')}}"tasks:name:debuglookupsdebug:msg="T
-
115
使用ansible-playbook role方式部署jdk
-
94
playbook介绍一.为什么引入playbook我们完成一个任务,例如安装部署一个httpd服务,我们需要多个模块(一个模块也可以称之为task)提供功能来完成。而playbook就是组织多个task的容器,他的实质就是一个文件,有着特定的组织格式,它采用的语法格式是YAML(YetAnot...
-
101
目录结构:文件内容tasks目录下的“file.yml”文件,内容如下:tasks目录下的“main.yml”templates目录下的“nginx.conf.j2”{%ifnginx_use_proxy%}{%forproxyinnginx_proxies%}upstream{{proxy.name}}{server{{ansible_eth0.ipv4.address}}:{{proxy.po
-
58
playbook的基础组件name定义playbook或者task的名称hosts用于指定要执行指定任务的主机user用于指定远程主机上的执行任务的用户task任务列表vars定义变量vars_files定义变量文件notify任务执行结果如果是发生更改的则触发定义在handler的任务handlers用于当前关注...
-
72
此playbook的作用:通过指定标签(tags),来说明是安装tomcat7还是tomcat8main.yml:---- include: install_tomcat7.yml tags: tomcat7- include: install_tomcat8.yml tags:&...
-
53
playbook-剧本介绍playbooks是一个不同于使用Ansible命令行执行方式的模式,其功能更强大灵活。简单来说,playbook是一个非常简单的配置管理和多主机部署系统,不同于任何已经存在的模式,可作为一个适合部署复杂应用程序的基础。Playbook可以定制配置,可以按照指...
-
59
PlayBook介绍playbook是由一个或者多个play组成的列表,主要功能是将task定义好的角色归并为一组进行统一管理。playbooks本身组成部分有如下几份:1、tasks:任务,即调用模块完成的操作2、variables:变量3、templates:模板4、handlers:处理器,当条件满足时执...
-
4
这个平台第一篇博客记录下,哈哈哈哈有一说一,这些文档还挺不错的拉去mysql镜像 [root@rzk ~]# docker pull mysql:8.0 8.0.27: Pulling from library/mysql 72a69066d2fe: Pull...
-
4
Linux部署mysql8.0 原创 sqtce 2022-07-22 10:12:48...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK