N46-第十二周作业
1、通过ansible roles编排实现httpd角色的部署
答:
###安装ansible###
# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo
# yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
# dnf install -y ansible
###添加被管理ip###
# vim /etc/ansible/hosts
[appsvrs]
10.0.0.206
10.0.0.207
###进行免密码登录###
# vim ssh_key.sh
#!/bin/bash
iplist="
10.0.0.206
10.0.0.207
"
rpm -q sshpass &> /dev/null || yum install -y sshpass
[ -f /root/.ssh/id_rsa ] || ssh-keygen -f /root/.ssh/id_rsa -P ''
export SSHPASS=123456
for ip in $iplist; do
sshpass -e ssh-copy-id -o StrictHostKeyChecking=no $ip
done
# chmod +x ssh_key.sh
# ./ssh_key.sh
###创建角色目录###
# mkdir -pv /data/ansible/roles/httpd/{handlers,tasks,files}
# tree .
.
├── files
├── handlers
└── tasks
###进入角色目录###
cd /data/ansible/roles/httpd/
###创建组的yml文件###
# vim tasks/group.yml
- name: create apache group
group: name=apache gid=80 system=yes
###创建用户的yml文件###
# vim tasks/user.yml
- name: create apache user
user: name=apache uid=80 group=apache system=yes shell=/sbin/nologin home=/var/www/
###创建安装的yml文件###
# vim tasks/install.yml
- name: install httpd package
yum: name=httpd state=installed
###配制conf文件###
# vim files/conf.sh
#!/bin/bash
mkdir -p /data/html
sed -i s#'DocumentRoot "/var/www/html"'#'DocumentRoot "/data/html"'#g /etc/httpd/conf/httpd.conf
sed -i s#'<Directory "/var/www/html">'#'<Directory "/data/html">'#g /etc/httpd/conf/httpd.conf
# chmod +x ./files/conf.sh
# vim tasks/config.yml
- name: config file script
script: conf.sh
notify: restart
###创建首页文件###
# vim files/index.sh
#!/bin/bash
echo "<h1>httpd website based ansilbe.</h1>" /data/html/index.html
# chmod +x ./files/index.sh
# vim tasks/index.yml
- name: index.html script
script: index.sh
###启动服务###
# vim tasks/service.yml
- name: start service
service: name=httpd state=started enabled=yes
###创建任务的main.yml文件###
# vim tasks/main.yml
- include: group.yml
- include: user.yml
- include: install.yml
- include: config.yml
- include: index.yml
- include: service.yml
###建立handlers###
# vim handlers/main.yml
- name: restart
service: name=httpd state=restarted
###建立执行的角色yml文件###
# cd ..
# vim role_httpd.yml
---
- hosts: app
remote_user: root
roles:
- role: httpd
# tree .
.
├── httpd
│ ├── files
│ │ ├── conf.sh
│ │ └── index.sh
│ ├── handlers
│ │ └── main.yml
│ └── tasks
│ ├── config.yml
│ ├── group.yml
│ ├── index.yml
│ ├── install.yml
│ ├── main.yml
│ ├── service.yml
│ └── user.yml
└── httpd_roles.yml
###测试playbook执行###
# ansible-playbook -C httpd_roles.yml
PLAY [app] **********************************************************************************************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************************************************************************************
ok: [10.0.0.206]
ok: [10.0.0.207]
TASK [httpd : create apache group] **********************************************************************************************************************************************************
changed: [10.0.0.206]
changed: [10.0.0.207]
TASK [httpd : create apache user] ***********************************************************************************************************************************************************
changed: [10.0.0.206]
changed: [10.0.0.207]
TASK [install httpd package] ****************************************************************************************************************************************************************
changed: [10.0.0.206]
changed: [10.0.0.207]
TASK [httpd : start service] ****************************************************************************************************************************************************************
changed: [10.0.0.206]
changed: [10.0.0.207]
PLAY RECAP **********************************************************************************************************************************************************************************
10.0.0.206 : ok=5 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
10.0.0.207 : ok=5 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
###安装httpd###
# ansible-playbook httpd_roles.yml
PLAY [app] **********************************************************************************************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************************************************************************************
ok: [10.0.0.206]
ok: [10.0.0.207]
TASK [httpd : create apache group] **********************************************************************************************************************************************************
changed: [10.0.0.206]
changed: [10.0.0.207]
TASK [httpd : create apache user] ***********************************************************************************************************************************************************
changed: [10.0.0.206]
changed: [10.0.0.207]
TASK [install httpd package] ****************************************************************************************************************************************************************
changed: [10.0.0.207]
changed: [10.0.0.206]
TASK [httpd : start service] ****************************************************************************************************************************************************************
changed: [10.0.0.207]
changed: [10.0.0.206]
PLAY RECAP **********************************************************************************************************************************************************************************
10.0.0.206 : ok=5 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
10.0.0.207 : ok=5 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
# ansible app -a 'curl http://localhost'
[WARNING]: Consider using the get_url or uri module rather than running 'curl'. If you need to use command because get_url or uri is insufficient you can add 'warn: false' to this command
task or set 'command_warnings=False' in ansible.cfg to get rid of this message.
10.0.0.206 | CHANGED | rc=0 >>
<h1>httpd website based ansilbe.</h1> % Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 38 100 38 0 0 6301 0 --:--:-- --:--:-- --:--:-- 7600
10.0.0.207 | CHANGED | rc=0 >>
<h1>httpd website based ansilbe.</h1> % Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 38 100 38 0 0 38000 0 --:--:-- --:--:-- --:--:-- 38000
2、简述MySQL数据库访问的执行过程。
答:
用户通过ip或socket请求连接mysql。经过连接层判断是否合法,如果合法,分配连接相应的线程。连接层线程将用户需要执行的sql语句送到sql层处理。sql层经过语句、语义、语法、sql类型、权限等判断后,先进行缓存查询,找到则直接返回给用户数据。如果缓存没有找到,将sql送到解析器,解析后生成执行任务,优先器读取执行任务,判断并选择代价最小的执行方案。安执行计划执行,生成获取数据的方案,交由存储引擎层继续处理。存储引擎层根据上层提供的方案,获取磁盘数据返回给用户。此次查询信息会缓存至查询缓存中。
3、select语句的完整语法较复杂,但至少包括的部分是(B)
A、仅select
B、select,from
C、select,group
D、select,into
4、一张表的主键个数为(C)
A、至多3个 B、没有限制 C、至多1个 D、至多2个

浙公网安备 33010602011771号