本文主要是介绍培训第三十二天(学习playbook-roles,脚本创建数据库和表,mycat读写分离),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
上午
1、roles(角色)介绍
roles(⻆⾊): 就是通过分别将variables, tasks及handlers等放置于单独 的⽬录中,并可以便捷地调⽤它们的⼀种机制。
假设我们要写⼀个playbook来安装管理lamp环境,那么这个 playbook就会写很⻓。所以我们希望把这个很⼤的⽂件分成多个功能 拆分, 分成apache管理,php管理,mysql管理,然后在需要使⽤的时候 直接调⽤就可以了,以免重复写。就类似编程⾥的模块化的概念,以 达到代码复⽤的效果。
2、roles目录介绍
files:⽤来存放由copy模块或script模块调⽤的⽂件。tasks:⾄少有⼀个main.yml⽂件,定义各tasks。handlers:有⼀个main.yml⽂件,定义各handlers。templates:⽤来存放jinjia2模板。vars:有⼀个main.yml⽂件,定义变量。meta:有⼀个main.yml⽂件,定义此⻆⾊的特殊设定及其依赖关系。
注意: 在每个⻆⾊的⽬录中分别创建files, tasks,handlers,templates,vars和meta⽬录,⽤不到的⽬录可以创建为空⽬录。
3、使用roles安装httpd和mysql
1)创建roles角色基础目录与文件
[root@1 ~]# cd /etc/ansible/roles/[root@1 roles]# mkdir {httpd,mysql}[root@1 roles]# mkdir {httpd,mysql}/{tasks,handlers,vars,meta}[root@1 roles]# touch {httpd,mysql}/{tasks,handlers,vars,meta}/main.yml[root@1 roles]# tree .├── httpd│ ├── files│ ├── handlers│ │ └── main.yml│ ├── meta│ │ └── main.yml│ ├── tasks│ │ └── main.yml│ ├── templates│ └── vars│ └── main.yml├── mysql├── files├── handlers│ └── main.yml├── meta│ └── main.yml├── tasks│ └── main.yml├── templates└── vars└── main.yml
2)创建playbook调用角色
[root@1 roles]# vim /etc/ansible/playbook/test002.yml---- hosts: sremote_user: rootroles:- httpd- mysql [root@1 roles]# cd[root@1 ~]# vim /etc/ansible/roles/mysql/tasks/main.yml ---- name: 卸载mysqlyum: name=mysql state=absent- name: 安装mysqlyum: name=mysql state=present[root@1 ~]# vim /etc/ansible/roles/httpd/tasks/main.yml---- name: 卸载httpdyum: name=httpd state=absent- name: 安装httpdyum: name=httpd state=present- name: 启动httpdservice: name=httpd state=started enabled=yes[root@1 ~]# ansible-playbook /etc/ansible/playbook/test002.yml
4、练习:使用role来实现lnmp
1、nginx改配置8080
2、mariadb中创建eleme数据库
3、创建表 t_user
id | int | 主键约束,自增 | 编号 |
---|---|---|---|
username | varchar | 非空约束 | 账号 |
password | varchar | 非空约束 | 密码 |
remark | varchar | 没有约束 | 账号权限说明 |
1)使用剧本修改nginx的端口号为80
[root@m0 ~]# vim /etc/ansible/playbook/nginx.yml---- hosts: sremote_user: roottasks:- name: 卸载httpdyum: name=httpd state=absent- name: 安装nginxyum: name=nginx state=present- name: 修改资源文件shell: echo 'i am nginx,port is 80' > /usr/share/nginx/html/index.html- name: 修改端口command: sed -i '/listen/ s/80/8080/g' /etc/nginx/nginx.confnotify:- restart nginx- name: 启动服务service: name=nginx state=started enabled=yeshandlers:- name: restart nginxservice: name=nginx state=restarted...[root@m0 ~]# ansible-playbook /etc/ansible/playbook/nginx.yml
2)安装mariadb,命令行形式创建数据库和表
[root@1 ~]# yum -y remove *mariadb*[root@1 ~]# yum -y install mariadb[root@1 ~]# yum -y install mariadb-server.x86_64 [root@1 ~]# yum -y install mariadb-devel.x86_64 [root@1 ~]# systemctl start mariadb[root@1 ~]# mysqlMariaDB [(none)]> quitBye[root@1 ~]# mysql << EOF> create database if not exists eleme charset utf8mb4;> use eleme;> create table user(id int primary key auto_increment,username varchar(45) not null,password varchar(45) not null,remark varchar(45));> quit> EOF[root@1 ~]# mysqlMariaDB [(none)]> show databases;+--------------------+| Database |+--------------------+| information_schema || eleme || mysql || performance_schema || test |+--------------------+5 rows in set (0.00 sec)MariaDB [test]> use eleme;Database changedMariaDB [eleme]> show tables;+-----------------+| Tables_in_eleme |+-----------------+| user |+-----------------+1 row in set (0.00 sec)
3)使用roles安装mariadb并执行脚本创建数据库和表
[root@1 ~]# cd /etc/ansible/roles/[root@1 roles]# cp -r httpd mariadb[root@1 roles]# cd mariadb/[root@1 mariadb]# lsfiles handlers meta tasks templates vars[root@1 mariadb]# vim tasks/main.yml ---- name: 卸载mariadbyum: name=mariadb,mariadb-server,mariadb-devel state=absent- name: 安装mariadbyum: name=mariadb,mariadb-server,mariadb-devel state=present- name: 启动mariadbservice: name=mariadb state=started enabled=yes- name: 执行脚本,创建数据库和数据表script: /etc/ansible/script/dbtable.sh[root@1 mariadb]# cd /etc/ansible/[root@1 ansible]# mkdir script[root@1 ansible]# cd script/[root@1 script]# vim dbtable.sh#!/bin/bashmysql << EOFcreate database if not exists eleme charset utf8mb4;use eleme;create table t_user(id int primary key auto_increment,username varchar(45) not null,password varchar(45) not null,remark varchar(45) );quitEOF[root@1 script]# vim /etc/ansible/playbook/test003.yml---- hosts: sremote_user: rootroles:- mariadb[root@1 script]# cd /etc/ansible/playbook/[root@1 playbook]# ansible-playbook test003.yml
下午
配置⼀主⼆从的mysql服务器 mysql57
1)mysql服务器端⼝3306,不要关闭防⽕墙
# 主从配置[root@mysql57 ~]# firewall-cmd --permanent --add-port=3306/tcp success[root@mysql57 ~]# firewall-cmd --reload success[root@mysql57 ~]# firewall-cmd --list-ports 3306/tcp96 yum -y install lrzsz66 tar -zxf mysql-5.7.44-linux-glibc2.12-x86_64.tar.gz 67 cp -r mysql-5.7.44-linux-glibc2.12-x86_64 /usr/local/mysql68 mkdir /usr/local/mysql/mysql-files69 useradd -r -s /sbin/nologin mysql70 chown mysql:mysql /usr/local/mysql/mysql-files/71 chmod 750 /usr/local/mysql/mysql-files/72 rm -rf /etc/my.cnf# 主配置73 /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql74 /usr/local/mysql/bin/mysql_ssl_rsa_setup --datadir=/usr/local/mysql/data76 cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql5777 service mysql57 start79 sed -i '$aexport PATH=/usr/local/mysql/bin:$PATH' /etc/profile80 source /etc/profilevim /usr/local/mysql/my.cnf[mysqld]basedir=/usr/local/mysqldatadir=/usr/local/mysql/datasocket=/tmp/mysql.sockactivate_all_roles_on_login=onport=3306log-error=/usr/local/mysql/data/db01-master.errlog-bin=/usr/local/mysql/data/binlogserver-id=10character_set_server=utf8mb4service mysql57 restart81 mysql -p'zc0fLfoM%SDP'mysql> alter user 'root'@'localhost' identified by '123';Query OK, 0 rows affected (0.15 sec) //修改root密码mysql> create user 'slave0'@'%' identified by '123';Query OK, 0 rows affected (0.03 sec) //创建slave用户mysql> grant replication slave on *.* to 'slave0'@'%';Query OK, 0 rows affected (0.02 sec) //授予replication slave权限mysql> flush privileges; //重新加载权限表Query OK, 0 rows affected (0.00 sec)mysql> flush tables with read lock; //刷新所有表并施加一个全局读锁Query OK, 0 rows affected (0.01 sec)mysql> show master status; //查看主服务器当前二进制日志的状态信息+---------------+----------+--------------+------------------+-------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |+---------------+----------+--------------+------------------+-------------------+| binlog.000003 | 1178 | | | |+---------------+----------+--------------+------------------+-------------------+1 row in set (0.00 sec)rm -rf /usr/local/mysql/data/auto.cnfrsync -av /usr/local/mysql/data root@10.0.0.58:/usr/local/mysql/rsync -av /usr/local/mysql/data root@10.0.0.59:/usr/local/mysql/# 从配置cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql57service mysql57 startsed -i '$aexport PATH=/usr/local/mysql/bin:$PATH' /etc/profilesource /etc/profilevim /usr/local/mysql/my.cnf[mysqld]basedir=/usr/local/mysqldatadir=/usr/local/mysql/datasocket=/tmp/mysql.sockactivate_all_roles_on_login=onport=3310log-error=/usr/local/mysql/data/db01-slave.errrelay-log=/usr/local/mysql/data/relaylogserver-id=11character_set_server=utf8mb4service mysql57 restartmysql -p'123'mysql> change master to-> master_host='10.0.0.57',-> master_user='slave0',-> master_password='123',-> master_port=3306,-> master_log_file='binlog.000003',-> master_log_pos=1178;Query OK, 0 rows affected, 9 warnings (0.02 sec)mysql> start slave;Query OK, 0 rows affected, 1 warning (0.03 sec)mysql> show slave status\G*************************** 1. row ***************************Slave_IO_State: Waiting for source to send eventMaster_Host: 10.0.0.57Master_User: slaveMaster_Port: 3306Connect_Retry: 60Master_Log_File: binlog.000003Read_Master_Log_Pos: 1178Relay_Log_File: relaylog.000002Relay_Log_Pos: 323Relay_Master_Log_File: binlog.000003Slave_IO_Running: YesSlave_SQL_Running: Yes
2)在mysql中添加eleme数据库设置为utf8mb4
[root@mysql57 ~]# mysql -p'123'mysql> create database if not exists eleme charset utf8mb4;Query OK, 1 row affected (0.00 sec)mysql> show databases;+--------------------+| Database |+--------------------+| information_schema || eleme || mysql || performance_schema || sys |+--------------------+6 rows in set (0.00 sec)
3)添加表t_user
mysql> create table eleme.t_user(-> id int not null primary key,-> name varchar(32) not null,-> username varchar(32) not null,-> password varchar(32) not null,-> remark varchar(32) not null-> );Query OK, 0 rows affected (0.01 sec)mysql> desc eleme.t_user;+----------+--------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+----------+--------------+------+-----+---------+-------+| id | int(11) | NO | PRI | NULL | || name | varchar(32) | NO | | NULL | || username | varchar(32) | NO | | NULL | || password | varchar(32) | NO | | NULL | || remark | varchar(32) | NO | | NULL | |+----------+--------------+------+-----+---------+-------+5 rows in set (0.02 sec)
4)添加2⾏记录
mysql> insert into eleme.t_user values(1,'超级管理员','admin','admin','超级管理员'),(2,'普通用户','guest','guest','普通用户');Query OK, 2 rows affected (0.00 sec)Records: 2 Duplicates: 0 Warnings: 0mysql> select * from eleme.t_user;+----+-----------------+----------+----------+-----------------+| id | name | username | password | remark |+----+-----------------+----------+----------+-----------------+| 1 | 超级管理员 | admin | admin | 超级管理员 || 2 | 普通用户 | guest | guest | 普通用户 |+----+-----------------+----------+----------+-----------------+2 rows in set (0.00 sec)
5)使⽤mycat为3台数据库设置负载均衡(读写分离)
[root@mycat ~]# systemctl stop firewalld[root@mycat ~]# systemctl disable firewalld[root@mycat ~]# lsanaconda-ks.cfg Mycat-server-1.6.5-release-20180122220033-linux.tar.gzjdk-8u192-linux-x64.tar.gz v[root@mycat ~]# tar -xf jdk-8u192-linux-x64.tar.gz [root@mycat ~]# tar -xf Mycat-server-1.6.5-release-20180122220033-linux.tar.gz [root@mycat ~]# cp -r jdk1.8.0_192/ /usr/local/jdk[root@mycat ~]# cp -r mycat/ /usr/local/[root@mycat ~]# ls /usr/local/jdk/bin lib src.zipCOPYRIGHT LICENSE THIRDPARTYLICENSEREADME-JAVAFX.txtinclude man THIRDPARTYLICENSEREADME.txtjavafx-src.zip README.htmljre release[root@mycat ~]# sed -i '$aexport JAVA_HOME=/usr/local/jdk' /etc/profile[root@mycat ~]# source /etc/profile[root@mycat ~]# $JAVA_HOME-bash: /usr/local/jdk: 是一个目录[root@mycat ~]# sed -i '$aexport PATH=$PATH:$JAVA_HOME/bin' /etc/profile[root@mycat ~]# source /etc/profile[root@mycat ~]# $PATH-bash: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/jdk/bin: 没有那个文件或目录[root@mycat ~]# javac -version javac 1.8.0_192[root@mycat ~]# ls /usr/local/mycat/bin catlet conf lib logs version.txt[root@mycat ~]# ll /usr/local/mycat/总用量 12drwxr-xr-x. 2 root root 190 8月 15 15:19 bindrwxr-xr-x. 2 root root 6 8月 15 15:19 catletdrwxr-xr-x. 4 root root 4096 8月 15 15:19 confdrwxr-xr-x. 2 root root 4096 8月 15 15:19 libdrwxr-xr-x. 2 root root 6 8月 15 15:19 logs-rwxr-xr-x. 1 root root 219 8月 15 15:19 version.txt[root@mycat ~]# ls /usr/local/mycat/bin/mycat [root@mycat ~]# vim /usr/local/mycat/conf/server.xml<user name="li" defaultAccount="true"><property name="password">li</property><property name="schemas">eleme</property><!-- 表级 DML 权限设置 --><!-- <privileges check="false"><schema name="TESTDB" dml="0110" ><table name="tb01" dml="0000"></table><table name="tb02" dml="1111"></table></schema></privileges> --></user><!--<user name="user"><property name="password">user</property><property name="schemas">TESTDB</property><property name="readOnly">true</property></user>-->[root@mycat ~]# vim /usr/local/mycat/conf/schema.xml
[root@mycat ~]# /usr/local/mycat/bin/mycat startStarting Mycat-server...[root@mycat ~]# netstat -lnput | grep 8066tcp6 0 0 :::8066 :::* LISTEN 1336/java
6、配置静态资源服务器,为前段提供图⽚和视频
7、部署2台有java17.0.3环境的后端服务器,端⼝8080,不关防⽕墙
8、部署nginx代理java服务器,负载均衡策略为轮询问,端⼝为80
这篇关于培训第三十二天(学习playbook-roles,脚本创建数据库和表,mycat读写分离)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!