ansible【自动化配置】(thirty day)

回顾

1、mysql和python

(1)不需要执行mysql_ssl_rsa_setup

(2)Change_master_to.不需要get public key

2、可以使用pymysql非交互的管理mysql

(1)conn=pymysql.connect(host,user,password,database,prot)

(2)cursor=conn.cursor();

(3)Cursor.execute("create user ....")

(4)cursor.execute("grant 权限 on 库名.表名 to 用户")

(5) conn.commit()

(6)cursor.fetchall()

3、mycat中间件

(1)独属于mysql主从的负载均衡策略

(2)配置写主读从

(3)步骤

①安装jdk

复制代码
tar -xf jdk_8u192.tar.gz
cp jdk/ /usr/local/jdk

sed -i '$aexport JAVA_HOME=/usr/local/jdk' /etc/profile
sed -i '$aexport PATH=$PATH:$JAVA_HOME/bin' /etc/profile

java --version
javac --version

②mycat

复制代码
tar -xf MyCat.tar.gz
cp -r mycat/ /usr/local/

#测试启动
/usr/local/mycat/bin/mycat console

③配置

server.xml

scheml.xml

4.启动和调试

(1)/usr/local/mycat/bin/mycat start

(2)Netstat -nput|grep 8066

(3)mysql -hmycat的ip或者域名-P8066 -userver.xml中填入账号 -p在service.xml中填入的密码

(4)Cat /usr/local/mysql/logs/wrapper.log

Cause by.......

一、自动化配置(anslible)

公司的服务器越来越多,维护一些简单的事情都会变得很繁琐。用shel脚本来管理少量服务器效率还行,服务器多了之后,shell脚本无法实现高效率运维。这种情况下,我们需要引入自动化运维工具,对多台服务器实现高效运维。

1、管理内容的主要分类

文件目录管理(包括文件的创建,删除,修改,查看状态,远程拷贝等)

**用户和组管理*

cron时间任务管理I

yum源配置与通过yum管理软件包

服务管理

远程执行脚本

远程执行命令

2、常见的开源自动化运维工具比较

(1)puppet(拓展)

基于ruby语言,成熟稳定。适合于大型架构,相对于ansible和saltstack会复杂些。

(2)saltstack(拓展)

基于python语言。相对简单,大并发能力比ansible要好,需要维护被管理端的服务。如果服务断开,连接就会出问题。

(3)ansible

基于python语言。简单快捷,被管理端不需要启服务。直接走ssh协议,需要验证所以机器多的话速度会较慢。

二、搭建自动化部署

环境配置:

root@M0 \~# yum list installed | grep epel

root@M0 \~# yum -y install epel-release

root@M0 \~# yum -y install ansible

root@M0 \~# find /etc/ -name "*ansible*"
/etc/ansible
/etc/ansible/ansible.cfg

root@M0 \~# ssh-copy-id -i 192.168.1.51

root@M0 \~# ssh-keygen

root@M0 \~# ls ./.ssh/
id_rsa id_rsa.pub

root@M0 \~# cat ./.ssh/id_rsa.pub

root@M0 \~# ssh-copy-id -i 192.168.1.51

root@M0 \~# ssh-copy-id -i 192.168.1.52

root@M0 \~# vim /etc/ansible/hosts

复制代码
[group01]
192.168.1.51
192.168.1.52

[group02]
192.168.1.51
192.168.1.52
192.168.1.53

root@M0 \~# ansible group01 -m ping

root@M0 \~# ansible group02 -m ping

root@M0 \~# vim /etc/ansible/hosts

复制代码
[group01]
192.168.1.51
192.168.1.52

other ansible_ssh_host=192.168.1.53 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=1

[group02]
192.168.1.51
192.168.1.52
other

root@M0 \~# ansible group02 -m ping

(1)ansible模块

root@m0 \~# ansible-doc -l #查看所有支持的模块

(2)hostname 模块

用于修改主机名称

root@M0 \~# ansible group02 -m hostname -a 'name=ab.lxe.er'

#将group02组中的主机名称分别改为ab lxe er

(3)file模块(重点)

file模块用于对文件相关的操作(创建,删除,软硬钟接等)

①创建目录和文件

root@M0 \~# ansible group01 -m file -a 'path=/tmp/abc state=directory' #创建目录

root@S0 \~# ls /tmp/
abc

root@S1 \~# ls /tmp/
abc

root@S2 \~# ls /tmp
abc

root@M0 \~# ansible group02 -m file -a 'path=/tmp/abc/def state=touch' #创建文件

root@S0 \~# ll /tmp/abc/
总用量 0
-rw-r--r-- 1 root root 0 8月 16 14:08 def

②递归修改(owner group mode

root@M0 \~# ansible group01 -m file -a 'path=/tmp/abc recurse=yes owner=bin group=daemon mode=1777' #权限是777

root@S0 \~# ll /tmp/abc/
总用量 0
-rwxrwxrwt 1 bin daemon 0 8月 16 14:08 def

③删除目录(连同目录里的所有文件)

root@M0 \~# ansible group01 -m file -a 'path=/tmp/abc state=absent'

④创建文件并指定owner,group,mode等

root@M0 \~# ansible group01 -m file -a 'path=/tmp/xiaojiang state=touch owner=bin group=daemon mode=1777'

⑤删除文件

root@M0 \~# ansible group01 -m file -a 'path=/tmp/xiaojiang state=absent'
⑥创建软连接文件

root@M0 \~# ansible group01 -m file -a 'src=/etc/fstab path=/tmp/xxx state=link'

root@S0 \~# ll /tmp/

lrwxrwxrwx 1 root root 10 8月 16 14:28 xxx -> /etc/fstab

⑦创建硬链接文件

root@M0 \~# ansible group01 -m file -a 'src=/etc/fstab path=/tmp/xxx2 state=hard'

root@S0 \~# ll /tmp/

-rw-r--r--. 2 root root 503 5月 26 21:16 xxx2

(硬链接创建文件,软连接指向硬链接)

(4)stat模块(了解)

root@M0 \~# ansible group02 -m stat -a 'path=/etc/fstab'

(5)copy模块(重点)

root@M0 \~# mv mysql-5.7.44-linux-glibc2.12-x86_64.tar.gz my57.tar.gz

root@M0 \~# ansible group02 -m copy -a 'src=./my57.tar.gz dest=~'
使⽤content参数直接往远程⽂件⾥写内容(会覆盖原内容)

root@M0 \~# ansible group1 -m copy -a 'content="ha ha\n" dest=/tmp/333'

注意:ansible中-a后⾯的参数⾥也有引号时,记得要单引双引交叉 使⽤,如果都为双引会出现问题

使⽤force参数控制是否强制覆盖

如果⽬标⽂件已经存在,则不覆盖

root@M0 \~# ansible group1 -m copy -a 'src=/tmp/222 dest=/tmp/333 force=no'

如果⽬标⽂件已经存在,则会强制覆盖

root@M0 \~# ansible group1 -m copy -a 'src=/tmp/222 dest=/tmp/333 force=yes'

使⽤backup参数控制是否备份⽂件

backup=yes表示如果拷⻉的⽂件内容与原内容不⼀样,则会备份⼀ 份 group1的机器上会将/tmp/333备份⼀份(备份⽂件命名加上时 间),再远程拷⻉新的⽂件为/tmp/333

root@M0 \~# ansible group1 -m copy -a 'src=/etc/fstab dest=/tmp/333 backup=yes owner=daemon group=daemon mode=1777'

copy模块拷⻉时要注意拷⻉⽬录后⾯是否带"/"符号

/etc/yum.repos.d后⾯不带/符号,则表示

把/etc/yum.repos.d整个⽬录拷⻉到/tmp/⽬录下

root@M0 \~# ansible group1 -m copy -a 'src=/etc/yum.repos.d dest=/tmp/'

/etc/yum.repos.d/后⾯带/符号,则表示

把/etc/yum.repos.d/⽬录⾥的所有⽂件拷⻉到/tmp/⽬录下

root@M0 \~# ansible group1 -m copy -a 'src=/etc/yum.repos.d/ dest=/tmp/'

(6)fetch模块

(7)user模块

root@M0 \~# ansible group02 -m user -a 'name=aaa state=present'

root@M0 \~# ansible group02 -m file -a 'path=/user/local/mysql state=directory'

root@M0 \~# ansible group02 -m file -a 'path=/user/local/mysql/mysql-files state=directory owner=mysql group=mysql mode=750'

相关推荐
刮风那天1 小时前
Android Framework 核心架构图
android
__Witheart__1 小时前
3588 安卓编译空间不足报错
android
aaajj2 小时前
【Android】手机屏幕劫持防护
android·智能手机
写做四月一日的四月一日2 小时前
在安卓手机上安装小龙虾openclaw并配置QQ机器人接入
android·人工智能
流星白龙2 小时前
【MySQL高阶】6.MySQL数据目录,日志
android·mysql·adb
福大大架构师每日一题2 小时前
rust 1.96.0 更新:语言、编译器、Cargo、Rustdoc、兼容性全面升级,必看完整解读
android·开发语言·rust
城管不管2 小时前
Agent——001
android·java·数据库·llm·prompt
刮风那天3 小时前
Android 理解onTransitionReady(一)
android
流星白龙3 小时前
【MySQL高阶】2.MySQL命令行客户端(2)
android·mysql·adb
努力努力再努力wz3 小时前
【Qt入门系列】:QLabel控件详解:从文本显示到图片展示,再到内容布局与伙伴机制
android·开发语言·数据结构·数据库·c++·qt·mysql