第十三周学习作业

【第十三周学习作业】不飞则已,一飞冲天;不鸣则已,一鸣惊人。这篇文章主要讲述第十三周学习作业相关的知识,希望能为你提供帮助。
1、ansible-playbook实现mysql的二进制部署
准备安装介质和配置文件

[root@CentOS84 data]# tree file
file
├── my.cnf
└── mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz
[root@CentOS84 data]# cat file/my.cnf
[mysqld]
datadir=/data/mysql
log_bin=1
server_id=1
skip_name_resolve=1
socket=/data/mysql/mysql.sock
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid
[client]
socket=/data/mysql/mysql.sock

ansible-playbook安装脚本
[root@CentOS84 data]# cat mysql_install2.yaml
---
- hosts: db
remote_user: root
gather_facts: no

vars:
version: "mysql-5.7.35-linux-glibc2.12-x86_64"
suffix: "tar.gz"
file: "version.suffix"
password: "mysql123"

tasks:
- name: 创建用户组
group: name=mysql gid=306
- name: 创建用户并加入到组
user: name=mysql uid=306 group=mysql shell=/sbin/nologin system=yes create_home=no home=/data/mysql
- name: 拷贝并解压文件
unarchive: src=https://www.songbingjia.com/data/file/file dest=/usr/local/
- name: 创建链接文件
file: src=https://www.songbingjia.com/usr/local/version dest=/usr/local/mysql state=link owner=mysql group=mysql
- name: 配置环境变量
shell: "echo PATH=/usr/local/mysql/bin:$PATH > /etc/profile.d/mysql.sh"
- name: 环境变量生效
shell: source /etc/profile.d/mysql.sh
- name: 创建数据目录
file: name=/data/mysql state=directory owner=mysql group=mysql
- name: 拷贝配置文件
copy: src=https://www.songbingjia.com/data/file/my.cnf dest=/etc/my.cnf
- name: 初始化数据文件
shell: mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql
- name: 拷贝启动脚本
shell: /bin/cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
- name: 启动mysql服务
shell: chkconfig --add mysqld & & service mysqld start
- name: 更改root口令password
shell: mysqladmin -uroot passwordpassword

执行过程如下:
[root@CentOS84 data]# ansible-playbook mysql_install2.yaml

PLAY [db] *****************************************************************************************************************************

TASK [创建用户组] **************************************************************************************************************************
changed: [10.10.10.12]
changed: [10.10.10.11]

TASK [创建用户并加入到组] **********************************************************************************************************************
changed: [10.10.10.12]
changed: [10.10.10.11]

TASK [拷贝并解压文件] ************************************************************************************************************************
changed: [10.10.10.11]
changed: [10.10.10.12]

TASK [创建链接文件] *************************************************************************************************************************
changed: [10.10.10.11]
changed: [10.10.10.12]

TASK [配置环境变量] *************************************************************************************************************************
changed: [10.10.10.12]
changed: [10.10.10.11]

TASK [环境变量生效] *************************************************************************************************************************
changed: [10.10.10.12]
changed: [10.10.10.11]

TASK [创建数据目录] *************************************************************************************************************************
changed: [10.10.10.12]
changed: [10.10.10.11]

TASK [拷贝配置文件] *************************************************************************************************************************
changed: [10.10.10.12]
changed: [10.10.10.11]

TASK [初始化数据文件] ************************************************************************************************************************
changed: [10.10.10.12]
changed: [10.10.10.11]

TASK [拷贝启动脚本] *************************************************************************************************************************
changed: [10.10.10.12]
changed: [10.10.10.11]

TASK [启动mysql服务] **********************************************************************************************************************
changed: [10.10.10.12]
changed: [10.10.10.11]

TASK [更改root口令 mysql123] **************************************************************************************************************
changed: [10.10.10.12]
changed: [10.10.10.11]

PLAY RECAP ****************************************************************************************************************************
10.10.10.11: ok=12changed=12unreachable=0failed=0skipped=0rescued=0ignored=0
10.10.10.12: ok=12changed=12unreachable=0failed=0skipped=0rescued=0ignored=0

登录验证:
[root@CentOS84 data]# ssh 10.10.10.11
Last login: Thu Mar 10 18:29:22 2022 from 10.10.10.10
[root@CentOS8411 ~]# mysql -uroot -pmysql123
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 3
Server version: 5.7.35-log 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> select user,host,plugin,authentication_stringfrom mysql.user;
+---------------+-----------+-----------------------+-------------------------------------------+
| user| host| plugin| authentication_string|
+---------------+-----------+-----------------------+-------------------------------------------+
| root| localhost | mysql_native_password | *F861720E101148897B0F5239DB926E756B1C28B3 |
| mysql.session | localhost | mysql_native_password

    推荐阅读