1、ansible-playbook实现MySQL的二进制部署
step1:安装ansible
[root@ansible.node.com ~]# yum -y install ansible
step2:配置ansible
[root@ansible.node.com ~]# mkdir ~/ansible && cd ~/ansible
[root@ansible.node.com ansible]# cp /etc/ansible/ansible.cfg . && cp /etc/ansible/hosts .
[root@ansible.node.com ansible]# grep -i '^inventory' ~/ansible/ansible.cfg
inventory = ./hosts # 修改主机清单文件路径
# 查看主机清单文件
[root@ansible.node.com ansible]# tail ./hosts
[local]
10.0.0.8 ansible_connection=local
[websrvs]
10.0.0.18
10.0.0.28
[dbsrvs]
10.0.0.38
# 查看ansible版本
[root@ansible.node.com ansible]# ansible --version
ansible 2.9.27
config file = /root/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, Aug 24 2020, 17:57:11) [GCC 8.3.1 20191121 (Red Hat 8.3.1-5)]
step3:step3: 配置ssh免密验证
# 准备ssh免密钥脚本
... ...
# 执行脚本
[root@ansible.node.com ansible]# bash ssh_push.sh
step4:准备mysql安装包
[root@ansible.node.com ansible]# mkdir files
[root@ansible.node.com ansible]# cd files
[root@ansible.node.com files]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz
[root@ansible.node.com files]# ls mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz
mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz
step5:准备mysql配置文件
[root@ansible.node.com files]# cat my57.cnf
[mysqld]
server-id=1
log-bin
datadir=/data/mysql
socket=/data/mysql/mysql.sock
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid
[client]
socket=/data/mysql/mysql.sock
step6:编写playbook
[root@ansible.node.com files]# cd ..
[root@ansible.node.com ansible]# vim install_mysql57.yml
---
# Install Mysql5.7 Package.
# Package Name: mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz
- hosts: dbsrvs
remote_user: root
gather_facts: False
vars:
mysql_version: 5.7.35
mysql_file: mysql-{{ mysql_version }}-linux-glibc2.12-x86_64
suffix: tar.gz
mysql_root_password: Mysql2022@12345
tasks:
- name: install mysql depent package
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 system=yes shell=/sbin/nologin home=/data/mysql create_home=no
- name: unarchive mysql file
unarchive: src=./files/{{ mysql_file }}.{{ suffix }} dest=/usr/local/ owner=root group=root mode=750
- name: create soft link
file: src=/usr/local/{{ mysql_file }} dest=/usr/local/mysql state=link
- name: initialization mysql
shell: chdir=/usr/local/mysql ./bin/mysqld --initialize-insecure --user=mysql --datadir=/data/mysql
tags: data
- name: config my.cnf
copy: src=./files/my57.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="export PATH=/usr/local/mysql/bin:$PATH" dest=/etc/profile.d/mysql.sh
- name: PATH variable entry
shell: . /etc/profile.d/mysql.sh
- name: start service
shell: chkconfig --add mysqld && /etc/init.d/mysqld start && chkconfig mysqld on
tags: service
- name: change password
shell: /usr/local/mysql/bin/mysqladmin -uroot password {{ mysql_root_password }}
step7:执行playbook
[root@ansible.node.com ansible]# ansible-playbook install_mysql57.yml
PLAY [dbsrvs] *******************************************************************************************************
TASK [install mysql depent package] *********************************************************************************
changed: [10.0.0.38]
TASK [create mysql group] *******************************************************************************************
changed: [10.0.0.38]
TASK [create mysql user] ********************************************************************************************
changed: [10.0.0.38]
TASK [unarchive mysql file] *****************************************************************************************
changed: [10.0.0.38]
TASK [create soft link] *********************************************************************************************
changed: [10.0.0.38]
TASK [initialization mysql] *****************************************************************************************
changed: [10.0.0.38]
TASK [config my.cnf] ************************************************************************************************
changed: [10.0.0.38]
TASK [service script] ***********************************************************************************************
changed: [10.0.0.38]
TASK [PATH variable] ************************************************************************************************
changed: [10.0.0.38]
TASK [PATH variable entry] ******************************************************************************************
changed: [10.0.0.38]
TASK [start service] ************************************************************************************************
changed: [10.0.0.38]
TASK [change password] **********************************************************************************************
changed: [10.0.0.38]
PLAY RECAP **********************************************************************************************************
10.0.0.38 : ok=12 changed=12 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
step8:测试
[root@10.0.0.38.node.com ~]# mysql -uroot -p'Mysql2022@12345'
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 4
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> status;
--------------
mysql Ver 14.14 Distrib 5.7.35, for linux-glibc2.12 (x86_64) using EditLine wrapper
Connection id: 5
Current database:
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.7.35-log MySQL Community Server (GPL)
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: latin1
Db characterset: latin1
Client characterset: utf8
Conn. characterset: utf8
UNIX socket: /data/mysql/mysql.sock
Uptime: 21 min 13 sec
Threads: 1 Questions: 11 Slow queries: 0 Opens: 109 Flush tables: 2 Open tables: 1 Queries per second avg: 0.008
--------------
mysql> quit
Bye
2、Ansible playbook实现apache批量部署,并对不同主机提供以各自IP地址为内容的index.html
step1:准备playbook
[root@ansible.node.com ansible]# vim install_apache.yml
---
# install apache package
- hosts: websrvs
remote_user: root
gather_facts: yes
tasks:
- name: instsall apache package
yum: name=httpd state=present
- name: modify config port
lineinfile: path=/etc/httpd/conf/httpd.conf regexp="^Listen.*" line="Listen 8081"
- name: modify web page
copy: content='{{ ansible_eth0.ipv4.address }}\n' dest=/var/www/html/index.html
- name: start apache server
service: name=httpd state=started enabled=yes
step2:执行playbook
[root@ansible.node.com ansible]# ansible-playbook install_apache.yml
PLAY [websrvs] ******************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************
ok: [10.0.0.28]
ok: [10.0.0.18]
TASK [instsall apache package] **************************************************************************************
changed: [10.0.0.18]
changed: [10.0.0.28]
TASK [modify config port] *******************************************************************************************
changed: [10.0.0.18]
changed: [10.0.0.28]
TASK [modify web page] **********************************************************************************************
changed: [10.0.0.18]
changed: [10.0.0.28]
TASK [start apache server] ******************************************************************************************
changed: [10.0.0.18]
changed: [10.0.0.28]
PLAY RECAP **********************************************************************************************************
10.0.0.18 : ok=5 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
10.0.0.28 : ok=5 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
step3:测试,访问web页面
[root@ansible.node.com ansible]# curl 10.0.0.18:8081
10.0.0.18
[root@ansible.node.com ansible]# curl 10.0.0.28:8081
10.0.0.28
3、http的报文结构和状态码总结
HTTP报文结构:分为请求报文和响应报文。
请求报文:由三部分组成,开始行、首部行、实体主体。
开始行,请求方法及请求参数:
文档路径,不包括协议和域名的绝对路径URL
使用的HTTP协议版本
首部行,为服务器提供关于所需数据的信息(例如语言,或 MIME 类型),或是一些改变请求行为的数据(例如当数据已经被缓存,就不再应答)。这些 HTTP 首部组成以一个空行结束的一个块。
实体主体,是可选数据块,包含更多数据,主要被 POST 方法所使用
响应报文:由三部分组成,开始行、首部行、实体主体。
开始行,包括使用HTTP的版本、状态码和一个状态描述
首部行,为客户端提供关于所发送数据的一些信息(如类型、数据大小、使用的压缩算法,缓存指示)组成一个块,并以一个空行结束
实体主体,包含了响应的数据
状态码:
1xx:信息提示
2xx:成功
3xx:重定向
4xx:错误类信息,客户端错误
5xx:错误类信息,服务器端错误
http协议常用状态码:
200: 成功,请求数据通过响应报文的entity-body部分发送;OK
301: 请求的资源的 URL 已永久更改。响应中会给出新的 URL;Moved Permanently
302: 此响应代码表示请求的资源的 URI 已临时更改。将来可能会对 URI 进行进一步的更改。因此,客户端应在将来的请求中使用相同的 URI。Found
304: 客户端发出了条件式请求,但服务器上的资源未曾发生改变,则通过响应此响应状态码通知客户端;Not Modified
307: 服务器发送此响应以指示客户端使用与先前请求中使用的相同方法在另一个 URI 处获取请求的资源;Temporary Redirect
401: 需要输入账号和密码认证方能访问资源;Unauthorized
403: 客户对内容没有访问权限;Forbidden
404: 服务器无法找到客户端请求的资源;Not Found
500: 服务器内部错误;Internal Server Error
502: 代理服务器从后端服务器收到了一条伪响应,如无法连接到网关;Bad Gateway
503: 服务不可用,临时服务器维护或过载,服务器无法处理请求;Service Unavailable
504: 网关超时;Gateway Timeout