1、主从复制及主主复制的实现
- 主从复制:
主节点配置:
#启用二进制日志:
[root@centos01 ~]# vi /etc/my.cnf
[mysqld]
server_id=147
log_bin=/data/mysql/logbin/mysql-bin
[root@centos01 ~]# mkdir -p /data/mysql/logbin
[root@centos01 ~]# chown -R mysql.mysql /data/mysql/
[root@centos01 ~]# systemctl restart mysqld
#登录mysql后,查看二进制位置:
mysql> show master logs;
+------------------+-----------+-----------+
| Log_name | File_size | Encrypted |
+------------------+-----------+-----------+
| mysql-bin.000001 | 157 | No |
+------------------+-----------+-----------+
1 row in set (0.01 sec)
#创建主从复制账号
mysql> create user repluser@'192.168.184.%' identified with mysql_native_password by 'Zhrmghg@2022';
Query OK, 0 rows affected (0.00 sec)
mysql> grant replication slave on *.* to repluser@'192.168.184.%';
Query OK, 0 rows affected (0.00 sec)
从节点配置:
[root@centos01 ~]# vi /etc/my.cnf
[mysqld]
server_id=129
read-only
log_bin=/data/mysql/logbin/mysql-bin
[root@centos01 ~]# mkdir -p /data/mysql/logbin
[root@centos01 ~]# chown -R mysql.mysql /data/mysql/
[root@centos01 ~]# systemctl restart mysqld
#配置主从复制:
mysql> CHANGE MASTER TO
-> MASTER_HOST='192.168.184.147',
-> MASTER_USER='repluser',
-> MASTER_PASSWORD='Zhrmghg@2022',
-> MASTER_PORT=3306,
-> MASTER_LOG_FILE='mysql-bin.000001',
-> MASTER_LOG_POS=157;
Query OK, 0 rows affected, 9 warnings (0.02 sec)
#开启io线程和sql线程:
mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.01 sec)
#查看主从复制状态:
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for source to send event
Master_Host: 192.168.184.147
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 1022
Relay_Log_File: centos02-relay-bin.000004
Relay_Log_Pos: 1191
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 1022
Relay_Log_Space: 1764
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 147
Master_UUID: 723f7726-93c5-11ec-9d2e-000c294d790f
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Replica has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
Master_public_key_path:
Get_master_public_key: 0
Network_Namespace:
1 row in set, 1 warning (0.00 sec)
验证:
#主节点创建数据库:
mysql> create database testdb;
Query OK, 1 row affected (0.01 sec)
#从节点查看已经同步:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| testdb |
+--------------------+
5 rows in set (0.01 sec)
- 主主复制:
master1和master2:
#master1启用二进制日志:
[root@centos01 ~]# vi /etc/my.cnf
[mysqld]
server_id=147
log_bin=/data/mysql/logbin/mysql-bin
[root@centos01 ~]# mkdir -p /data/mysql/logbin
[root@centos01 ~]# chown -R mysql.mysql /data/mysql/
[root@centos01 ~]# systemctl restart mysqld
#master2启用二进制日志:
[root@centos02 ~]# vi /etc/my.cnf
[mysqld]
server_id=129
log_bin=/data/mysql/logbin/mysql-bin
[root@centos02 ~]# mkdir -p /data/mysql/logbin
[root@centos02 ~]# chown -R mysql.mysql /data/mysql/
[root@centos02 ~]# systemctl restart mysqld
master1:
#记录master1的二进制位置:
mysql> show master logs;
+------------------+-----------+-----------+
| Log_name | File_size | Encrypted |
+------------------+-----------+-----------+
| mysql-bin.000001 | 157 | No |
+------------------+-----------+-----------+
1 row in set (0.00 sec)
#创建账号并授权
mysql> create user repluser@'192.168.184.%' identified with mysql_native_password by 'Zhrmghg@2022';
Query OK, 0 rows affected (0.01 sec)
mysql> grant replication slave on *.* to repluser@'192.168.184.%';
Query OK, 0 rows affected (0.01 sec)
master2:
#配置单向复制:
mysql> CHANGE MASTER TO
-> MASTER_HOST='192.168.184.147',
-> MASTER_USER='repluser',
-> MASTER_PASSWORD='Zhrmghg@2022',
-> MASTER_PORT=3306,
-> MASTER_LOG_FILE='mysql-bin.000001',
-> MASTER_LOG_POS=157;
Query OK, 0 rows affected, 9 warnings (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for source to send event
Master_Host: 192.168.184.147
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 1257
Relay_Log_File: centos02-relay-bin.000002
Relay_Log_Pos: 1426
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 1257
Relay_Log_Space: 1639
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 147
Master_UUID: edd36952-944d-11ec-a11b-000c294d790f
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Replica has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
Master_public_key_path:
Get_master_public_key: 0
Network_Namespace:
1 row in set, 1 warning (0.00 sec)
#至此,master1-->master2单向复制完成。
master2:
#继续记录master2的二进制日志位置:
mysql> show master logs;
+------------------+-----------+-----------+
| Log_name | File_size | Encrypted |
+------------------+-----------+-----------+
| mysql-bin.000001 | 1285 | No |
+------------------+-----------+-----------+
1 row in set (0.00 sec)
master1:
#配置单向复制:
mysql> CHANGE MASTER TO
-> MASTER_HOST='192.168.184.129',
-> MASTER_USER='repluser',
-> MASTER_PASSWORD='Zhrmghg@2022',
-> MASTER_PORT=3306,
-> MASTER_LOG_FILE='mysql-bin.000001',
-> MASTER_LOG_POS=1285;
Query OK, 0 rows affected, 9 warnings (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for source to send event
Master_Host: 192.168.184.129
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 1285
Relay_Log_File: centos01-relay-bin.000002
Relay_Log_Pos: 326
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 1285
Relay_Log_Space: 539
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 129
Master_UUID: f0727b50-9451-11ec-8a1d-000c29b5218f
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Replica has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
Master_public_key_path:
Get_master_public_key: 0
Network_Namespace:
1 row in set, 1 warning (0.00 sec)
#至此,master2-->master1单向复制完成,双向复制全部完成。
验证:
#验证master1-->master2同步
#master1:
mysql> create database testdb1;
Query OK, 1 row affected (0.00 sec)
#master2:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| testdb1 |
+--------------------+
5 rows in set (0.00 sec)
#验证master2-->master1同步
#master2:
mysql> create database testdb2;
Query OK, 1 row affected (0.00 sec)
#master1:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| testdb1 |
| testdb2 |
+--------------------+
6 rows in set (0.00 sec)
2、xtrabackup实现全量+增量+binlog恢复库
原数据库备份:
#下载安装xtrabackup(原备份主机和目标恢复主机都需要安装)
[root@centos01 ~]# wget https://downloads.percona.com/downloads/Percona-XtraBackup-LATEST/Percona-XtraBackup-8.0.27-19/binary/redhat/7/x86_64/percona-xtrabackup-80-8.0.27-19.1.el7.x86_64.rpm
[root@centos01 ~]# yum install -y percona-xtrabackup-80-8.0.27-19.1.el7.x86_64.rpm
#导入测试数据后,完全备份
[root@centos01 ~]# mkdir /backup
[root@centos01 ~]# xtrabackup -uroot -p123456 --backup --target-dir=/backup/base
#第一次插入数据
mysql> insert students values (null,'xiao ming',20,'M',4,4);
Query OK, 1 row affected (0.01 sec)
#第一次增量备份
[root@centos01 ~]# xtrabackup -uroot -p123456 --backup --target-dir=/backup/inc1 --incremental-basedir=/backup/base
#第二次插入数据
mysql> insert students values (null,"xiao hong",19,"F",2,1);
Query OK, 1 row affected (0.00 sec)
#第二次增量备份
[root@centos01 ~]# xtrabackup -uroot -p123456 --backup --target-dir=/backup/inc2 --incremental-basedir=/backup/inc1
#第三次插入数据
mysql> insert students values (null,"xiao zhao",23,"F",1,3);
Query OK, 1 row affected (0.01 sec)
将备份拷贝到目标主机:
scp -r /backup/* 目标主机:/backup/
假设原数据库服务器此时出现故障,但binlog日志未损坏
目标数据库恢复:
#停止mysql服务
[root@centos02 data]# systemctl stop mysqld
#清空mysql数据目录、二进制目录
[root@centos02 ~]# rm -rf /data/mysql/*
[root@centos02 data]# rm -rf logbin/*
#恢复
[root@centos02 ~]# xtrabackup --prepare --apply-log-only --target-dir=/backup/base
#合并第一次增量备份到完全备份
[root@centos02 ~]# xtrabackup --prepare --apply-log-only --target-dir=/backup/base --incremental-dir=/backup/inc1
#合并第二次增量备份到完全备份,最后一次还原不需要加选项--apply-log-only
[root@centos02 ~]# xtrabackup --prepare --target-dir=/backup/base --incremental-dir=/backup/inc2
#复制到数据库目录
[root@centos02 data]# xtrabackup --copy-back --target-dir=/backup/base
#恢复属性
[root@centos02 data]# chown -R mysql:mysql /data/logbin/ /data/mysql/
#启动mysql
[root@centos02 data]# systemctl start mysqld
#登录恢复后的数据查看数据
mysql> select * from students;
...省略...
| 26 | xiao ming | 20 | M | 4 | 4 |
| 27 | xiao hong | 19 | F | 2 | 1 |
+-------+---------------+-----+--------+---------+-----------+
#还缺少xiao zhao的数据,需要使用binlog日志恢复
#继续查看binlog位置
mysql> show master logs
-> ;
+------------------+-----------+-----------+
| Log_name | File_size | Encrypted |
+------------------+-----------+-----------+
| mysql-bin.000005 | 155 | No |
| mysql-bin.000006 | 155 | No |
+------------------+-----------+-----------+
2 rows in set (0.01 sec)
#mysql-bin.000006为数据库启动后的日志,因此这里mysql-bin.000005的155为最后一次增量恢复完成的位置,即还需要通过binlog恢复在此之后的数据
#原数据库二进制日志从mysql-bin.000005的155后导出数据:
[root@centos01 logbin]# mysqlbinlog mysql-bin.000005 --start-position=155 > ~/binlog.sql
#拷贝到目标数据库
[root@centos01 logbin]# scp ~/binlog.sql 目标数据库:~
#目标数据库上导入binlog.sql
mysql> source ~/binlog.sql
#查看最终恢复后的数据
mysql> select * from students;
...省略...
| 26 | xiao ming | 20 | M | 4 | 4 |
| 27 | xiao hong | 19 | F | 2 | 1 |
| 28 | xiao zhao | 23 | F | 1 | 3 |
+-------+---------------+-----+--------+---------+-----------+
28 rows in set (0.00 sec)
至此,3条数据全部恢复
3、MyCAT实现MySQL读写分离
环境:
192.168.184.147 master
192.168.184.129 slave
192.168.184.134 mycat
mycat服务器:
#安装jdk
[root@centos03 ~]# java -version
java version "1.8.0_281"
Java(TM) SE Runtime Environment (build 1.8.0_281-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.281-b09, mixed mode)
#下载mycat
wget http://dl.mycat.org.cn/1.6.7.6/20220221174943/Mycat-server-1.6.7.6-release-20220221174943-linux.tar.gz
#解压
[root@centos03 ~]# tar zxf Mycat-server-1.6.7.6-release-20220221174943-linux.tar.gz -C /usr/local/
#配置环境变量
[root@centos03 ~]# echo 'PATH=/usr/local/mycat/bin:$PATH' > /etc/profile.d/mycat.sh
[root@centos03 ~]# source /etc/profile.d/mycat.sh
#启动mycat
[root@centos03 ~]# mycat start
#查看日志,启动成功
[root@centos03 ~]# tail -f /usr/local/mycat/logs/wrapper.log
STATUS | wrapper | 2022/02/24 13:16:09 | --> Wrapper Started as Daemon
STATUS | wrapper | 2022/02/24 13:16:09 | Launching a JVM...
INFO | jvm 1 | 2022/02/24 13:16:14 | Wrapper (Version 3.2.3) http://wrapper.tanukisoftware.org
INFO | jvm 1 | 2022/02/24 13:16:14 | Copyright 1999-2006 Tanuki Software, Inc. All Rights Reserved.
INFO | jvm 1 | 2022/02/24 13:16:14 |
INFO | jvm 1 | 2022/02/24 13:16:16 | MyCAT Server startup successfully. see logs in logs/mycat.log
#客户端尝试使用默认密码123456连接mycat
[root@centos01 ~]# mysql -uroot -p123456 -h192.168.184.134 -P8066
#在mycat 服务器上修改server.xml文件配置Mycat的连接信息
[root@centos03 ~]# vim /usr/local/mycat/conf/server.xml
...
#取消该段注释,并将8066改为3306端口
<property name="serverPort">3306</property>
<property name="managerPort">9066</property>
<property name="idleTimeout">300000</property>
<property name="authTimeout">15000</property>
<property name="bindIp">0.0.0.0</property>
<property name="dataNodeIdleCheckPeriod">300000</property> #此处删除5 * 60 * 1000L;
<property name="frontWriteQueueSize">4096</property> <property name="processors">32</property>
...
<user name="root" defaultAccount="true"> #此处可修改用户名
<property name="password">123456</property> #此处可修改密码
<property name="schemas">TESTDB</property> #数据库名要和schema.xml相对应
#修改schema.xml实现读写分离策略
[root@centos03 ~]# vi /usr/local/mycat/conf/schema.xml
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
<schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn1"></schema>
<dataNode name="dn1" dataHost="localhost1" database="hellodb" />
<dataHost name="localhost1" maxCon="1000" minCon="10" balance="1"
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<writeHost host="host1" url="192.168.184.147:3306" user="root" password="Zhrmghg@2022">
<readHost host="host2" url="192.168.184.129:3306" user="root" password="Zhrmghg@2022" />
</writeHost>
</dataHost>
</mycat:schema>
#重启mycat
[root@centos03 conf]# mycat restart
#在数据库主节点上创建用户并对mycat授权
mysql> create user root@'192.168.184.%' identified with mysql_native_password by 'Zhrmghg@2022';
Query OK, 0 rows affected (0.00 sec)
mysql> grant all on *.* to root@'192.168.184.%';
Query OK, 0 rows affected (0.00 sec)
验证:
#测试读
[root@centos01 ~]# mysql -uroot -p123456 -h 192.168.184.134
mysql> show databases;
+----------+
| DATABASE |
+----------+
| TESTDB |
+----------+
1 row in set (0.01 sec)
mysql> use TESTDB
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select @@server_id;
+-------------+
| @@server_id |
+-------------+
| 129 |
+-------------+
1 row in set (0.00 sec)
#主从节点开通用日志
mysql> show variables like 'general%';
mysql> set global general_log=1;
#测试从节点读
mysql> select * from students;
[root@centos02 ~]# tail -f /var/lib/mysql/centos02.log
...
2022-02-24T07:23:08.489851Z 148 Query select * from students
#测试主节点写
mysql> insert students values (null,"li si",22,"F",4,2);
[root@centos01 ~]# tail -f /var/lib/mysql/centos01.log
...
2022-02-24T07:26:32.299624Z 234 Query insert students values (null,"li si",22,"F",4,2)
#停止从节点(模拟从节点宕机)
[root@centos01 ~]# systemctl stop mysqld
#读已切换为主节点
mysql> select @@server_id;
+-------------+
| @@server_id |
+-------------+
| 147 |
+-------------+
1 row in set (0.00 sec)
4、ansible常用模块介绍
- command模块
功能:在远程主机执行命令,此为默认模块,可忽略 -m 选项
范列:ansible websrvs -m command -a 'chdir=/data ls -l' - shell模块
功能:和command相似,用shell执行命令,支持各种符号,比如:*,$, > - script模块:
功能:在远程主机上运行ansible服务器上的脚本(无需执行权限)
范列:ansible websrvs -m script -a '/root/test.sh' - copy模块:
功能:从ansible服务器主控端复制文件到远程主机
范列:ansible websrvs -m copy -a 'src=ssh_key.sh dest=/data/ssh.sh owner=root group=bin mode=700'
注意:复制目录不加“/”
范列:ansible websrvs -m copy -a 'src=/etc dest=/data'
范列:指定内容,直接生成文件
ansible websrvs -m copy -a "content='test line1\ntest line2\n' dest=/tmp/test.txt" - get_url模块:
功能: 用于将文件从http、https或ftp下载到被管理机节点上
ansible websrvs -m get_url -a 'url=http://nginx.org/download/nginx-1.18.0.tar.gz dest=/usr/local/src/nginx.tar.gz checksum="md5:b2d33d24d89b8b1f87ff5d251aa27eb8"' - fetch模块:
功能:从远程主机提取文件至ansible的主控端,copy相反,目前不支持目录
范列:ansible websrvs -m fetch -a 'src=/var/log/messages dest=/data/log' - file模块:
功能: 用于将文件从http、https或ftp下载到被管理机节点上
范列:创建空文件:
ansible websrvs -m file -a 'path=/data/a.txt state=touch owner=root'
范列:创建文件夹:
ansible websrvs -m file -a 'path=/data/mysql state=directory'
范列:创建软链接:
ansible websrvs -m file -a 'src=/data/mysql-5.7 path=/data/mysql state=link'
范列:递归修改目录属性,但不递归至子目录
ansible all -m file -a "path=/data/mysql state=directory owner=mysql group=mysql"
范列:递归修改目录及子目录的属性
ansible all -m file -a "path=/data/mysql state=directory owner=mysql group=mysql recurse=yes"
范列:删除目录
ansible websrvs -m file -a 'path=/data/mysql-5.7 state=absent' - stat模块:
功能:检查文件或文件系统的状态
范列:ansible 127.0.0.1 -m stat -a 'path=/etc/passwd' - unarchive模块:
功能:解包解压缩
实现有两种用法:
1、将ansible主机上的压缩包传到远程主机后解压缩至特定目录,设置copy=yes,此为默认值,可省略
2、将远程主机上的某个压缩包解压缩到指定路径下,设置copy=no
范列:
ansible all -m unarchive -a 'src=/data/nginx-1.18.0.tar.gz dest=/usr/local/src owner=wang group=bin'
ansible all -m unarchive -a 'src=/tmp/foo.zip dest=/data copy=no mode=0777'
ansible all -m unarchive -a 'src=https://example.com/example.zip dest=/data copy=no' - archive模块:
功能:打包压缩保存在被管理节点
范列:ansible websrvs -m archive -a 'path=/var/log/ dest=/data/log.tar.bz2 format=bz2 owner=root mode=0600' - hostname模块:
功能:管理主机名
范列:ansible 10.0.0.7 -m hostname -a 'name=test.magedu.org' - cron模块:
功能:计划任务
范列:
ansible 10.0.0.8 -m cron -a 'hour=2 minute=30 weekday=1-5 name="backup mysql" job=/root/mysql_backup.sh'
ansible websrvs -m cron -a "minute=*/5 job='/usr/sbin/ntpdate ntp.aliyun.com &>/dev/null' name=Synctime" - yum和apt模块:
功能:软件包管理
范列:
ansible 10.0.0.100 -m apt -a 'name=sl'
ansible 10.0.0.100 -m yum -a 'name=httpd state=absent'
ansible 10.0.0.100 -m yum -a 'name=httpd,vsftpd' - yum_repository模块:
功能:yum仓库管理
范列:
- name: Add multiple repositories into the same file
yum_repository:
name: epel
description: EPEL YUM repo
file: external_repos
baseurl: https://download.fedoraproject.org/pub/epel/basearch/
gpgcheck: no - service模块:
功能:管理服务
范列:
ansible 10.0.0.8 -m service -a 'name=httpd state=started enabled=yes'
ansible 10.0.0.8 -m service -a 'name=httpd state=stopped enabled=no' - user模块:
功能:管理用户
范列:
ansible all -m user -a 'name=user1 comment="test user" uid=2048 home=/app/user1 group=root'
ansible all -m user -a 'name=nginx comment=nginx uid=88 group=nginx groups="root,daemon" shell=/sbin/nologin system=yes create_home=no home=/data/nginx non_unique=yes' - group模块:
功能:管理组
范列:
ansible websrvs -m group -a 'name=nginx gid=88 system=yes'
ansible websrvs -m group -a 'name=nginx state=absent' - lineinfile模块:
regexp参数 :使用正则表达式匹配对应的行,当替换文本时,如果有多行文本都能被匹配,则只有最后面被匹配到的那行文本才会被替换,当删除文本时,如果有多行文本都能被匹配,这么这些行都会被 删除。 如果想进行多行匹配进行替换需要使用replace模块
范列:
ansible websrvs -m lineinfile -a "path=/etc/httpd/conf/httpd.conf regexp='^Listen' line='Listen 80'"
ansible all -m lineinfile -a 'dest=/etc/fstab state=absent regexp="^#"' - replace模块:
功能:该模块有点类似于sed命令,主要也是基于正则进行匹配和替换,建议使用
ansible all -m replace -a "path=/etc/fstab regexp='^(UUID.)' replace='#\1'"
ansible all -m replace -a "path=/etc/fstab regexp='^#(UUID.)' replace='\1'" - setup模块:
功能: setup 模块来收集主机的系统信息,这些 facts 信息可以直接以变量的形式使用,但是如果主机较多,会影响执行速度
范列:
ansible all -m setup
ansible all -m setup -a 'filter=ansible_python_version' - debug模块:
功能:可以用于输出信息,并且通过 msg 定制输出的信息内容
注意: msg后面的变量有时需要加 " " 引起来
范列:
ansible 10.0.0.18 -m debug
10.0.0.18 | SUCCESS => {
"msg": "Hello world!"
}