1、主从复制及主主复制的实现
#实现主从复制配置,官网参考 https://dev.mysql.com/doc/refman/8.0/en/replication-configuration.html https://dev.mysql.com/doc/refman/5.7/en/replication-configuration.html https://dev.mysql.com/doc/refman/5.5/en/replication-configuration.html https://mariadb.com/kb/en/library/setting-up-replication/ #一、两台centos8安装mariadb10.3.28 实现主从复制 #主节点配置: [root@Centos8 ~]##hostnamectl set-hostname master [root@master ~]#yum install -y mariadb-server [root@master ~]#vim /etc/my.cnf.d/mariadb-server.cnf [mysqld] log_bin server-id=11 #设置开机自启并启动服务 [root@master ~]#systemctl --now enable mariadb Created symlink /etc/systemd/system/mysql.service → /usr/lib/systemd/system/mariadb.service. Created symlink /etc/systemd/system/mysqld.service → /usr/lib/systemd/system/mariadb.service. Created symlink /etc/systemd/system/multi-user.target.wants/mariadb.service → /usr/lib/systemd/system/mariadb.service. #登录数据库 [root@master ~]#mysql Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 9 Server version: 10.3.28-MariaDB-log MariaDB Server Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> show master status; +-------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +-------------------+----------+--------------+------------------+ | master-bin.000002 | 343 | | | +-------------------+----------+--------------+------------------+ 1 row in set (0.000 sec) #创建复制账号 MariaDB [(none)]> grant replication slave on *.* to 'repluser'@'10.0.0.%' identified by 'replpass'; Query OK, 0 rows affected (0.000 sec) #刷新权限 MariaDB [(none)]> flush privileges; Query OK, 0 rows affected (0.000 sec) #查看二进制文件名称及位置,后面从节点会用到 MariaDB [(none)]> show master status; +-------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +-------------------+----------+--------------+------------------+ | master-bin.000003 | 343 | | | +-------------------+----------+--------------+------------------+ 1 row in set (0.000 sec) #退出数据库 MariaDB [(none)]> exit Bye #从节点配置: [root@Centos8 ~]#hostnamectl set-hostname slave1 [root@slave1 ~]#yum install -y mariadb-server [root@slave1 ~]#vim /etc/my.cnf.d/mariadb-server.cnf [mysqld] server_id=22 log-bin read_only=ON relay_log=relay-log relay_log_index=relay-log.index [root@slave1 ~]#systemctl --now enable mariadb Created symlink /etc/systemd/system/mysql.service → /usr/lib/systemd/system/mariadb.service. Created symlink /etc/systemd/system/mysqld.service → /usr/lib/systemd/system/mariadb.service. Created symlink /etc/systemd/system/multi-user.target.wants/mariadb.service → /usr/lib/systemd/system/mariadb.service. #使用有复制权限的用户账号连接至主服务器,并启动复制线程 [root@slave1 ~]#mysql Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 9 Server version: 10.3.28-MariaDB-log MariaDB Server Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> change master to master_host='10.0.0.150',master_user='repluser',master_password='replpass',master_log_file='master-bin.000003',master_log_pos=343; Query OK, 0 rows affected (0.002 sec) #开始从节点复制 MariaDB [(none)]> start slave ; Query OK, 0 rows affected (0.001 sec) #查看从节点复制状态 MariaDB [(none)]> show slave status \G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 10.0.0.150 Master_User: repluser Master_Port: 3306 Connect_Retry: 60 Master_Log_File: master-bin.000003 Read_Master_Log_Pos: 343 Relay_Log_File: relay-log.000002 Relay_Log_Pos: 556 Relay_Master_Log_File: master-bin.000003 Slave_IO_Running: Yes Slave_SQL_Running: Yes #出现这两个yes表示连接成功,配置成功 #说明,从节点配置错误,清除信息后检查从节点配置重新配置change master to MariaDB [(none)]> stop slave; Query OK, 0 rows affected (0.002 sec) MariaDB [(none)]> reset slave all; Query OK, 0 rows affected (0.000 sec) #验证主从复制 #主节点创建数据库 第一次查看数据库 MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | +--------------------+ 3 rows in set (0.000 sec) 主节点创建test111数据库 MariaDB [(none)]> create database test111; Query OK, 1 row affected (0.000 sec) #从节点查看是否同步 第一次查看数据库 MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | +--------------------+ 3 rows in set (0.001 sec) 在主节点创建test111数据库后,在从节点查看数据库,发现已经同步过来 MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test111 | +--------------------+ 4 rows in set (0.000 sec) #二、两台centos8安装mariadb10.3.28 实现主主复制 主主复制:两个节点,都可以更新数据,并且互为主从 容易产生的问题:数据不一致;因此慎用 考虑要点:自动增长id 配置一个节点使用奇数id auto_increment_offset=1 #开始点 auto_increment_increment=2 #增长幅度 另一个节点使用偶数id auto_increment_offset=2 auto_increment_increment=2 主主复制的配置步骤简述: (1) 各节点使用一个惟一server_id (2) 都启动binary log和relay log (3) 创建拥有复制权限的用户账号 (4) 定义自动增长id字段的数值范围各为奇偶 (5) 均把对方指定为主节点,并启动复制线程 主主复制的具体实现步骤: #第一台mster节点 [root@master1 ~]#vim /etc/my.cnf.d/mariadb-server.cnf [mysqld] log_bin server-id=11 auto_increment_offset=1 auto_increment_increment=2 [root@master1 ~]#systemctl restart mariadb [root@master1 ~]#mysql MariaDB [(none)]> show master status; +--------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +--------------------+----------+--------------+------------------+ | master1-bin.000001 | 330 | | | +--------------------+----------+--------------+------------------+ 1 row in set (0.000 sec) MariaDB [(none)]> show master logs; +--------------------+-----------+ | Log_name | File_size | +--------------------+-----------+ | master1-bin.000001 | 330 | +--------------------+-----------+ 1 row in set (0.000 sec) #说明,如果是先查看在创建复制账号,则下面的创建账号信息不会同步到从节点。如果先创建的复制账号,再查看的二进制文件位置,则会把账号创建也复制过去。 MariaDB [(none)]> grant replication slave on *.* to 'repluser'@'*' identified by 'replpass'; MariaDB [(none)]>flush privileges; #第二台master节点 [root@master2 ~]#vim /etc/my.cnf.d/mariadb-server.cnf [mysqld] server_id=22 log-bin auto_increment_offset=2 auto_increment_increment=2 [root@master2 ~]#systemctl restart mariadb [root@master2 ~]#mysql MariaDB [(none)]> change master to master_host='10.0.0.150',master_user='repluser',master_password='replpass',master_log_file='master1-bin.000001',master_log_pos=330; Query OK, 0 rows affected (0.011 sec) MariaDB [(none)]> start slave; Query OK, 0 rows affected (0.001 sec) MariaDB [(none)]> show master logs; +--------------------+-----------+ | Log_name | File_size | +--------------------+-----------+ | master2-bin.000001 | 913 | +--------------------+-----------+ 1 row in set (0.000 sec) #因上面没有先创建复制用户再查看二进制文件位置,所以创建账号这步没有同步过来,因此这台也要创建复制账号。 MariaDB [(none)]>grant replication slave on *.* to 'repluser'@'*' identified by 'replpass'; MariaDB [(none)]>flush privileges; #回到第一台master节点 因为第一台的复制账号信息已经同步到第二台机器,因此有复制账号了,直接配置change master to MariaDB [(none)]> change master to master_host='10.0.0.160',master_user='repluser',master_password='replpass',master_log_file='master2-bin.000001',master_log_pos=913; Query OK, 0 rows affected (0.014 sec) #启动复制线程 MariaDB [(none)]> start slave; Query OK, 0 rows affected (0.001 sec) #验证 #第一台 MariaDB [(none)]> create database d111; Query OK, 1 row affected (0.000 sec) MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | d111 | | d222 | | information_schema | | mysql | | performance_schema | | t1 | | test111 | +--------------------+ 7 rows in set (0.000 sec) #第二台 MariaDB [(none)]> create database d222; Query OK, 1 row affected (0.000 sec) MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | d111 | | d222 | | information_schema | | mysql | | performance_schema | | t1 | | test111 | +--------------------+ 7 rows in set (0.000 sec)
2、xtrabackup实现全量+增量+binlog恢复库
#一、利用xtrabackup实现完全备份及还原 1.下载并安装xtrabackup包 [root@centos8 ~]#wget https://downloads.percona.com/downloads/Percona-XtraBackup-2.4/Percona-XtraBackup-2.4.23/binary/redhat/7/x86_64/percona-xtrabackup-24-2.4.23-1.el7.x86_64.rpm [root@localhost ~]# yum install -y percona-xtrabackup-24-2.4.23-1.el7.x86_64.rpm 2.安装mariadb数据库并启动服务 [root@localhost ~]# yum install -y mariadb-server [root@localhost ~]# systemctl enable mariadb --now [root@localhost ~]# mysql_secure_installation(初始化设置,设置root密码为123456,移除匿名用户等) 3.在原主机做完全备份到/backup目录 [root@localhost ~]# mkdir /backup [root@localhost ~]# xtrabackup -uroot -p123456 --backup --target-dir=/backup/base [root@localhost ~]# ls /backup/base/ backup-my.cnf mysql xtrabackup_checkpoints xtrabackup_logfile ibdata1 performance_schema xtrabackup_info 4.目标主机无需创建/backup目录,直接复制目录本身 [root@centos7 ~]#scp -r /backup/ 目标主机:/backup1 5.删除数据库数据模拟目标主机 [root@localhost ~]# systemctl stop mariadb [root@localhost ~]# rm -rf /var/lib/mysql/* 6.还原数据 1)预准备:确保数据一致,提交完成的事务,回滚未完成的事务 [root@localhost ~]# xtrabackup --prepare --target-dir=/backup/base 2)复制到数据库目录 注意:数据库目录必须为空,MySQL服务不能启动 [root@localhost ~]# xtrabackup --copy-back --target-dir=/backup/base [root@localhost ~]# ll /var/lib/mysql/ total 40976 -rw-r----- 1 root root 18874368 Nov 9 16:47 ibdata1 -rw-r----- 1 root root 5242880 Nov 9 16:47 ib_logfile0 -rw-r----- 1 root root 5242880 Nov 9 16:47 ib_logfile1 -rw-r----- 1 root root 12582912 Nov 9 16:47 ibtmp1 drwxr-x--- 2 root root 4096 Nov 9 16:47 mysql drwxr-x--- 2 root root 4096 Nov 9 16:47 performance_schema -rw-r----- 1 root root 440 Nov 9 16:47 xtrabackup_info -rw-r----- 1 root root 1 Nov 9 16:47 xtrabackup_master_key_id 3)还原属性 [root@centos7 ~]#chown -R mysql:mysql /var/lib/mysql [root@localhost ~]# ll /var/lib/mysql/ total 40976 -rw-r----- 1 mysql mysql 18874368 Nov 9 16:47 ibdata1 -rw-r----- 1 mysql mysql 5242880 Nov 9 16:47 ib_logfile0 -rw-r----- 1 mysql mysql 5242880 Nov 9 16:47 ib_logfile1 -rw-r----- 1 mysql mysql 12582912 Nov 9 16:47 ibtmp1 drwxr-x--- 2 mysql mysql 4096 Nov 9 16:47 mysql drwxr-x--- 2 mysql mysql 4096 Nov 9 16:47 performance_schema -rw-r----- 1 mysql mysql 440 Nov 9 16:47 xtrabackup_info -rw-r----- 1 mysql mysql 1 Nov 9 16:47 xtrabackup_master_key_id 4)启动服务 [root@centos7 ~]#systemctl start mariadb 7.验证数据恢复成功 [root@localhost ~]# mysql -uroot -p123456 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 2 Server version: 5.5.68-MariaDB MariaDB Server Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | +--------------------+ 3 rows in set (0.00 sec) #二、完全,增量+binlog还原 #完全备份 [root@centos7 ~]#yum install -y percona-xtrabackup-24-2.4.23-1.el7.x86_64.rpm [root@centos7 ~]#mkdir /backup/ [root@centos7 ~]#xtrabackup -uroot -p123456 --backup --target-dir=/backup/base #新增数据 MariaDB [(none)]> create database testdb -> ; Query OK, 1 row affected (0.00 sec) MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | testdb | +--------------------+ 4 rows in set (0.00 sec) #第一次增量备份 [root@localhost ~]# xtrabackup -uroot -p123456 --backup --target-dir=/backup/inc1 --incremental-basedir=/backup/base #第二次新增数据 MariaDB [(none)]> create database testdb2 -> ; Query OK, 1 row affected (0.00 sec) MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | testdb | | testdb2 | +--------------------+ 5 rows in set (0.00 sec) #第二次增量备份 [root@localhost ~]# xtrabackup -uroot -p123456 --backup --target-dir=/backup/inc2 --incremental-basedir=/backup/inc1 #二次增量备份后查看二进制位置,防止二次备份后再次有少量数据写入,这时就需要二进制日志来恢复数据 MariaDB [(none)]> show master logs; +--------------------+-----------+ | Log_name | File_size | +--------------------+-----------+ | mariadb-bin.000001 | 1645 | +--------------------+-----------+ 1 row in set (0.00 sec) #第二次增备后新增数据: MariaDB [(none)]> create database testdb3 -> ; Query OK, 1 row affected (0.00 sec) MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | testdb | | testdb2 | | testdb3 | +--------------------+ 6 rows in set (0.00 sec) #拷贝binlog日志留作恢复 [root@localhost mysql]# cp -p /var/lib/mysql/mariadb-bin.000001 /backup/ #拷贝到还原主机 [root@localhost ~]#scp -r /backup/ 目标主机:/backup/ #备份过程生成三个备份目录 /backup/{base,inc1,inc2} #数据还原过程:(数据库未启动状态) 1)预准备完成备份,此选项--apply-log-only 阻止回滚未完成的事务 [root@localhost ~]#xtrabackup --prepare --apply-log-only --target-dir=/backup/base 2)合并第1次增量备份到完全备份 [root@localhost ~]#xtrabackup --prepare --apply-log-only --target-dir=/backup/base --incremental-dir=/backup/inc1 3)合并第2次增量备份到完全备份:最后一次还原不需要加选项--apply-log-only [root@localhost ~]#xtrabackup --prepare --target-dir=/backup/base --incrementaldir=/backup/inc2 4)复制到数据库目录,注意数据库目录必须为空,MySQL服务不能启动 [root@localhost ~]#xtrabackup --copy-back --target-dir=/backup/base 5)还原属性: [root@localhost ~]# chown -R mysql:mysql /var/lib/mysql/ 6)启动服务: [root@localhost ~]# systemctl start mariadb #截至第二次增备的数据都已经恢复,但是第二次增备后原数据库还有可能会写入数据,最后的少量数据使用binlog恢复 [root@localhost mysql]# cat /backup/inc2/xtrabackup_binlog_info mariadb-bin.000001 1645 #查看二进制文件,从1645开始,并导入到sql文件 [root@localhost ~]# mysqlbinlog /backup/mariadb-bin.000001 --start-position=1645 >/backup/binlog.sql #临时关闭binlog记录功能,导入sql文件,再开机binlog功能 MariaDB [(none)]> set sql_log_bin=0; MariaDB [(none)]> source /backup/binlog.sql MariaDB [(none)]> set sql_log_bin=1; #验证 MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | testdb | | testdb2 | | testdb3 | | performance_schema | +--------------------+ 5 rows in set (0.00 sec)
3、MyCAT实现MySQL读写分离
#三台服务器 centos7 mycat-server 10.0.0.152 内存2G以上 centos8 mysql-master 10.0.0.150 mariadb 10.3 centos8 mysql-master 10.0.0.160 mariadb 10.3 #关闭防火墙 systemctl stop firewalld setenforce 0 时间同步 #1)创建数据库主从 [root@centos8 ~]#yum install -y mariadb-server #master和slave修改配置文件 #master: [root@centos8 ~]#vim /etc/my.cnf.d/mariadb-server.cnf [mysqld]# server-id=1 log-bin #slave: [root@centos8 ~]#vim /etc/my.cnf.d/mariadb-server.cnf [mysqld] server-id=2 #修改配置文件后重启服务使生效 [root@centos8 ~]#systemctl start mariadb #master:创建复制账号 [root@centos8 ~]#mysql Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 9 Server version: 10.3.28-MariaDB-log MariaDB Server Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> grant replication slave on *.* to 'repluser'@'%' identified by 'replpass'; Query OK, 0 rows affected (0.000 sec) MariaDB [(none)]> flush privileges; Query OK, 0 rows affected (0.000 sec) MariaDB [(none)]> show master logs; +--------------------+-----------+ | Log_name | File_size | +--------------------+-----------+ | mariadb-bin.000001 | 645 | +--------------------+-----------+ 1 row in set (0.000 sec) #slave:连接主数据库 MariaDB [(none)]> change master to master_host='10.0.0.150',master_user='repluser',master_password='replpass',master_log_file='mariadb-bin.000001',master_log_pos=645; Query OK, 0 rows affected (0.002 sec) MariaDB [(none)]> start slave; Query OK, 0 rows affected (0.001 sec) MariaDB [(none)]> show slave status \G; ...... Slave_IO_Running: Yes Slave_SQL_Running: Yes ......省略 #2)在mysql代理服务器10.0.0.152上安装mycat并启动 #安装jdk [root@localhost ~]# yum install -y java [root@localhost ~]# java -version openjdk version "1.8.0_312" OpenJDK Runtime Environment (build 1.8.0_312-b07) OpenJDK 64-Bit Server VM (build 25.312-b07, mixed mode) #下载并安装mycat [root@localhost ~]# wget http://dl.mycat.org.cn/1.6.7.6/20210303094759/Mycat-server-1.6.7.6-release-20210303094759-linux.tar.gz 或者#wget http://dl.mycat.org.cn/1.6.7.4/Mycat-server-1.6.7.4-release/Mycat-server-1.6.7.4-release-20200105164103-linux.tar.gz [root@localhost ~]# mkdir /apps [root@localhost ~]# tar xvf Mycat-server-1.6.7.6-release-20210303094759-linux.tar.gz -C /apps #配置环境变量 [root@localhost ~]# echo 'PATH=$PATH:/apps/mycat/bin'>/etc/profile.d/mycat.sh [root@localhost ~]# source /etc/profile.d/mycat.sh #启动mycat [root@localhost ~]# ss -ntl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:111 *:* LISTEN 0 128 *:22 *:* LISTEN 0 100 127.0.0.1:25 *:* LISTEN 0 128 [::]:111 [::]:* LISTEN 0 128 [::]:22 [::]:* LISTEN 0 100 [::1]:25 [::]:* [root@localhost ~]# mycat start Starting Mycat-server... [root@localhost ~]# ss -ntl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 1 127.0.0.1:32000 *:* LISTEN 0 128 *:111 *:* LISTEN 0 128 *:22 *:* LISTEN 0 100 127.0.0.1:25 *:* LISTEN 0 50 [::]:1984 [::]:* LISTEN 0 128 [::]:8066 [::]:* LISTEN 0 50 [::]:43650 [::]:* LISTEN 0 128 [::]:9066 [::]:* LISTEN 0 128 [::]:111 [::]:* LISTEN 0 128 [::]:22 [::]:* LISTEN 0 100 [::1]:25 [::]:* LISTEN 0 50 [::]:34334 [::]:* #可以看到启动了多个端口,其中8066用于连接myscat [root@localhost ~]# tail /apps/mycat/logs/wrapper.log 可以看到启动成功了 #用默认密码123456来连接mycat [root@localhost ~]# mysql -uroot -p123456 -h10.0.0.152 -P8066 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.6.29-mycat-1.6.7.6-release-20210303094759 MyCat Server (OpenCloudDB) Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MySQL [(none)]> show databases; +----------+ | DATABASE | +----------+ | TESTDB | +----------+ 1 row in set (0.00 sec) #3)在mycat服务器上修改server.xml文件配置mycat的连接信息 [root@localhost ~]# vim /apps/mycat/conf/server.xml #修改下面行的8066改为3306复制到到独立非注释行 <property name="serverPort">3306</property> <property name="handlelDistributedTransactions">0</property> #将上面行放在此行前面 或者删除注释,并修改下面的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"> #连接Mycat的用户名 <property name="password">magedu</property> #连接Mycat的密码 <property name="schemas">TESTDB</property> #数据库名要和schema.xml相 对应 #这里使用的是root,密码为magedu,逻辑数据库为TESTDB,这些信息都可以自己随意定义,读写权限都有,没有针对表做任何特殊的权限。重点关注上面这段配置,其他默认即可。 #4)修改schema.xml实现读写分离策略 [root@localhost ~]# vim /apps/mycat/conf/schema.xml #最终文件内容 [root@mycat ~]#cat /apps/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="10.0.0.18:3306" user="mycat" password="123456"> <readHost host="host2" url="10.0.0.28:3306" user="mycat" password="123456" /> </writeHost> </dataHost> </mycat:schema> #重新启动mycat root@localhost ~]# mycat restart Stopping Mycat-server... Mycat-server was not running. Starting Mycat-server... [root@localhost ~]# tail /apps/mycat/logs/wrapper.log INFO | jvm 1 | 2021/11/09 22:49:04 | at io.mycat.config.util.ConfigUtil.getDocument(ConfigUtil.java:115) INFO | jvm 1 | 2021/11/09 22:49:04 | at io.mycat.config.loader.xml.XMLSchemaLoader.load(XMLSchemaLoader.java:111) INFO | jvm 1 | 2021/11/09 22:49:04 | ... 13 more STATUS | wrapper | 2021/11/09 22:49:06 | <-- Wrapper Stopped STATUS | wrapper | 2021/11/09 22:53:37 | --> Wrapper Started as Daemon STATUS | wrapper | 2021/11/09 22:53:38 | Launching a JVM... INFO | jvm 1 | 2021/11/09 22:53:38 | Wrapper (Version 3.2.3) http://wrapper.tanukisoftware.org INFO | jvm 1 | 2021/11/09 22:53:38 | Copyright 1999-2006 Tanuki Software, Inc. All Rights Reserved. INFO | jvm 1 | 2021/11/09 22:53:38 | INFO | jvm 1 | 2021/11/09 22:53:41 | MyCAT Server startup successfully. see logs in logs/mycat.log #5)在10.0.0.150的后端数据库创建用户并授权 MariaDB [(none)]> grant all privileges on *.* to 'mycat'@'10.0.0.%' identified by '123456'; Query OK, 0 rows affected (0.000 sec) MariaDB [(none)]> flush privileges; Query OK, 0 rows affected (0.000 sec) MariaDB [(none)]> exit Bye [root@centos8 ~]#mysql -h10.0.0.150 -umycat -p123456 #6)连接并测试,可以看到进入逻辑数据库TESTDB,查到我后端hellodb数据库里面的内容,成功 [root@localhost ~]# mysql -uroot -pmagedu -h10.0.0.152 TESTDB Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Welcome to the MariaDB monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.6.29-mycat-1.6.7.6-release-20210303094759 MyCat Server (OpenCloudDB) Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MySQL [TESTDB]> show databases; +----------+ | DATABASE | +----------+ | TESTDB | +----------+ 1 row in set (0.00 sec) MySQL [TESTDB]> show tables; +-------------------+ | Tables_in_hellodb | +-------------------+ | tbl1 | +-------------------+ 1 row in set (0.00 sec) MySQL [TESTDB]> select * from tbl1; +------+----------+ | id | name | +------+----------+ | 1 | tanliang | +------+----------+ 1 row in set (0.00 sec) MySQL [TESTDB]> select @@hostname; +--------------------+ | @@hostname | +--------------------+ | centos8.magedu.org | +--------------------+ 1 row in set (0.00 sec) MySQL [TESTDB]> select @@server_id; +-------------+ | @@server_id | +-------------+ | 2 | +-------------+ 1 row in set (0.00 sec) #7)通过日志确认实现读写分离 show variables like 'general_log'; #查看日志是否开启 set global general_log=on; #开启日志功能 show variables like 'general_log_file'; #查看日志文件保存位置 set global general_log_file='tmp/general.log'; #设置日志文件保存位置 #在主和从服务器分别开启通用日志,查看读写分离 #操作: MySQL [TESTDB]> show tables; +-------------------+ | Tables_in_hellodb | +-------------------+ | tbl1 | +-------------------+ 1 row in set (0.00 sec) MySQL [TESTDB]> select * from tbl1; +------+----------+ | id | name | +------+----------+ | 1 | tanliang | +------+----------+ 1 row in set (0.01 sec) MySQL [TESTDB]> insert tbl1 value(2,'liang'); Query OK, 1 row affected (0.01 sec) 主: [root@master mysql]#tail -f centos8.log 211109 23:03:41 20 Query select user() 24 Connect mycat@10.0.0.152 as anonymous on hellodb 211109 23:03:51 17 Query select user() 211109 23:04:01 16 Query SET names utf8;insert tbl1 value(2,'liang') 23 Query select user() ^C 从: 211109 23:02:04 16 Query SET names utf8;show tables 211109 23:02:11 15 Query select user() 211109 23:02:21 13 Query select user() 211109 23:02:31 12 Query select user() 211109 23:02:41 14 Query select user() 211109 23:02:51 11 Query select user() 211109 23:03:01 16 Query select user() 211109 23:03:11 15 Query select user() 211109 23:03:12 13 Query select * from tbl1 211109 23:03:21 12 Query select user() 211109 23:03:31 14 Query select user() 211109 23:03:41 11 Query select user() 18 Connect mycat@10.0.0.152 as anonymous on hellodb 211109 23:03:51 16 Query select user() 211109 23:04:01 10 Query BEGIN 10 Query insert tbl1 value(2,'liang') 10 Query COMMIT /* implicit, from Xid_log_event */ 15 Query select user() #MyCAT对后端服务器的健康性检查方法select user() #停止从节点,MyCAT自动调度读请求至主节点,#停止主节点,MyCAT不会自动调度写请求至从节点 slave上停止mariadb [root@slave1 mysql]#systemctl stop mariadb [root@localhost ~]# mysql -uroot -pmagedu -h10.0.0.152 TESTDB MySQL [TESTDB]> select @@server_id; +-------------+ | @@server_id | +-------------+ | 1 | +-------------+ 1 row in set (0.00 sec)
4、ansible常用模块介绍
s常用模块帮助文档参考: https://docs.ansible.com/ansible/2.9/modules/modules_by_category.html https://docs.ansible.com/ansible/2.9/modules/list_of_all_modules.html 1)Command模块 功能:在远程主机执行命令,此为默认模块,可忽略-m选项 说明:此命令不支持 $VARNAME < > | ; & 等,可用shell模块实现。 说明:此模块不具有幂等性(幂等性:多次执行不影响结果) #chdir切换工作目录 [root@master tasks]#ansible webservers -m command -a "chdir=/etc cat centos-release" 10.0.0.150 | CHANGED | rc=0 >> CentOS Linux release 8.3.2011 10.0.0.160 | CHANGED | rc=0 >> CentOS Linux release 8.3.2011 #不支持,所以输出了hello > /root/hello.log到屏幕,而没有把hello写入到文件中 [root@master ~]#ansible webservers -m command -a 'echo hello > /root/hello.log' 10.0.0.150 | CHANGED | rc=0 >> hello > /root/hello.log 10.0.0.160 | CHANGED | rc=0 >> hello > /root/hello.log #creates存在则不执行 [root@master ~]#ansible webservers -m command -a 'chdir=/etc creates=centos-release cat centos-release' 10.0.0.150 | SUCCESS | rc=0 >> skipped, since centos-release exists 10.0.0.160 | SUCCESS | rc=0 >> skipped, since centos-release exists #creates不存在则执行 [root@master ~]#ansible webservers -m command -a 'chdir=/etc creates=f1.txt cat centos-release' 10.0.0.150 | CHANGED | rc=0 >> CentOS Linux release 8.3.2011 10.0.0.160 | CHANGED | rc=0 >> CentOS Linux release 8.3.2011 #removes存在才执行 [root@master ~]#ansible webservers -m command -a 'chdir=/etc removes=centos-release cat centos-release' 10.0.0.150 | CHANGED | rc=0 >> CentOS Linux release 8.3.2011 10.0.0.160 | CHANGED | rc=0 >> CentOS Linux release 8.3.2011 #removes不存在不执行 [root@master ~]#ansible webservers -m command -a 'chdir=/etc removes=centos-rease cat centos-release' 10.0.0.150 | SUCCESS | rc=0 >> skipped, since centos-rease does not exist 10.0.0.160 | SUCCESS | rc=0 >> skipped, since centos-rease does not exist [root@master ~]#ansible webservers -m command -a 'service mariadb restart' [WARNING]: Consider using the service module rather than running 'service'. If you need to use command because service 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.160 | CHANGED | rc=0 >> Redirecting to /bin/systemctl restart mariadb.service 10.0.0.150 | CHANGED | rc=0 >> Redirecting to /bin/systemctl restart mariadb.service 2)shell模块 功能:和command相似,用shell执行命令,支持各种符号,比如:*,$, > 说明:此模块不具有幂等性 说明:调用bash执行命令 类似 cat /tmp/test.md | awk -F'|' '{print $1,$2}' &> /tmp/example.txt 这些 复杂命令,即使使用shell也可能会失败,解决办法:写到脚本时,copy到远程,执行,再把需要的结果 拉回执行命令的机器 [root@master ~]#ansible webservers -m shell -a 'echo $HOSTNAME' 10.0.0.150 | CHANGED | rc=0 >> master 10.0.0.160 | CHANGED | rc=0 >> slave1 [root@master ~]#ansible webservers -m shell -a 'echo hello > /root/hello.log' 10.0.0.150 | CHANGED | rc=0 >> 10.0.0.160 | CHANGED | rc=0 >> [root@master ~]#ansible webservers -m shell -a 'cat /root/hello.log' 10.0.0.150 | CHANGED | rc=0 >> hello 10.0.0.160 | CHANGED | rc=0 >> hello 可以把shell模块设置为默认模块 [root@master ~]#vim ansible.cfg module_name = shell Ansible 的配置文件可以放在多个不同地方,优先级从高到低顺序如下 ANSIBLE_CONFIG #环境变量,注意此项用 ansible --version 看不到,但可以生效 ./ansible.cfg #当前目录下的ansible.cfg ~/.ansible.cfg #当前用户家目录下的.ansible.cfg /etc/ansible/ansible.cfg #系统默认配置文件 3)script模块 功能:在远程主机上运行ansible服务器上的脚本(无需执行权限) 说明:此模块不具有幂等性 脚本在当前机器上 [root@master ~]#ansible webservers -m script -a '/root/test.sh' 4)copy模块 功能:从主控端复制文件到远程主机 说明: src=file 如果是没指明路径,则为当前目录或当前目录下的files目录下的file文件 #如目标存在,默认覆盖,此处指定先备份 [root@master ~]#ansible webservers -m copy -a "src=/root/test.sh dest=/tmp/test3.sh owner=mysql mode=600 backup=yes" #指定内容,直接生成目标文件 [root@master ~]#ansible webservers -m copy -a "content='test line1\ntest line2\n' dest=/tmp/test.txt" [root@master ~]#cat /tmp/test.txt test line1 test line2 #复制/etc目录自身,注意/etc/后面没有/ ansible webservers -m copy -a "src=/etc dest=/backup" #复制/etc/下的文件,不包括/etc/目录自身,注意/etc/后面有/ ansible webservers -m copy -a "src=/etc/ dest=/backup" 5)get_url模块 功能: 用于将文件从http、https或ftp下载到被管理机节点上 常用参数: url: 下载文件的URL,支持HTTP,HTTPS或FTP协议 dest: 下载到目标路径(绝对路径),如果目标是一个目录,就用服务器上面文件的名称,如果目标设置了名称就用目标设置的名称 owner:指定属主 group:指定属组 mode:指定权限 force: 如果yes,dest不是目录,将每次下载文件,如果内容改变,替换文件。如果否,则只有在目标不存在时才会下载该文件 checksum: 对目标文件在下载后计算摘要,以确保其完整性 示例: checksum="sha256:D98291AC[...]B6DC7B97", checksum="sha256:http://example.com/path/sha256sum.txt" url_username: 用于HTTP基本认证的用户名。 对于允许空密码的站点,此参数可以不使用`url_password' url_password: 用于HTTP基本认证的密码。 如果未指定`url_username'参数,则不会使用`url_password'参数validate_certs:如果“no”,SSL证书将不会被验证。 适用于自签名证书在私有网站上使用 timeout: URL请求的超时时间,秒为单位 [root@master ~]#ansible webservers -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"' [root@master ~]#ll /usr/local/src/nginx.tar.gz -rw-r--r-- 1 root root 1039530 Nov 11 17:57 /usr/local/src/nginx.tar.gz 6)fethch模块 功能:从远程主机提取文件至ansible的主控端,copy相反,目前不支持目录 [root@master ~]#ansible webservers -m fetch -a 'src=/root/test.sh dest=.' [root@master ~]#tree . ├── 10.0.0.150 │ └── root │ └── test.sh ├── 10.0.0.160 │ └── root │ └── test.sh 7)file模块 功能:设置文件属性,创建软链接等 #创建空文件 [root@master ~]#ansible all -m file -a 'path=/data/test.txt state=touch' #删除文件 [root@master ~]#ansible all -m file -a 'path=/data/test.txt state=absent' #设置文件权限 [root@master ~]#ansible all -m file -a "path=/root/test.sh owner=wang mode=755" #创建目录 [root@master ~]#ansible all -m file -a "path=/data/mysql state=directory owner=mysql group=mysql" #创建软链接,path|dest|name表示三个选一个都行 [root@master ~]#ansible all -m file -a 'src=/data/testfile path|dest|name=/data/testfile-link state=link' #创建目录 [root@master ~]#ansible all -m file -a 'path=/data/testdir state=directory' #递归修改目录属性,但不递归至子目录 [root@master ~]#ansible all -m file -a "path=/data/mysql state=directory owner=mysql group=mysql" #递归修改目录及子目录的属性 [root@master ~]#ansible all -m file -a "path=/data/mysql state=directory owner=mysql group=mysql recurse=yes" 8)stat模块 功能:检查文件或文件系统的状态 说明:对于Windows目标,请改用win_stat模块 选项:path:文件/对象的完整路径(必须) 常用的返回值判断: exists: 判断是否存在 isuid: 调用用户的ID与所有者ID是否匹配 [root@master data]#ansible 127.0.0.1 -m stat -a 'path=/etc/passwd' 127.0.0.1 | SUCCESS => { "changed": false, "stat": { "atime": 1636603261.996736, "attr_flags": "", "attributes": [], "block_size": 4096, "blocks": 8, "charset": "us-ascii", "checksum": "358721bb9ccdb8b1eaf3a9b97e51d1eee306dd50", "ctime": 1636429959.9657238, "dev": 2053, "device_type": 0, "executable": false, "exists": true, "gid": 0, "gr_name": "root", "inode": 135872481, "isblk": false, "ischr": false, "isdir": false, "isfifo": false, "isgid": false, "islnk": false, "isreg": true, "issock": false, "isuid": false, "mimetype": "text/plain", "mode": "0644", "mtime": 1636429959.9647236, "nlink": 1, "path": "/etc/passwd", "pw_name": "root", "readable": true, "rgrp": true, "roth": true, "rusr": true, "size": 2814, "uid": 0, "version": "4213724205", "wgrp": false, "woth": false, "writeable": true, "wusr": true, "xgrp": false, "xoth": false, "xusr": false } } 使用范例一: - name: install | Check if file is already configured. stat: path={{ nginx_file_path }} connection: local register: nginx_file_result - name: install | Download nginx file get_url: url={{ nginx_file_url }} dest={{ software_files_path }} validate_certs=no connection: local when:,not. nginx_file_result.stat.exists 使用范例二: [root@ansible ansible]#cat stat.yml --- - hosts: websrvs tasks: - name: check file stat: path=/data/mysql register: st - name: debug debug: msg: "/data/mysql is not exist" when: not st.stat.exists 9)unarchive模块 功能:解包解压缩 实现有两种用法: 1、将ansible主机上的压缩包传到远程主机后解压缩至特定目录,设置copy=yes,此为默认值,可省略 2、将远程主机上的某个压缩包解压缩到指定路径下,设置copy=no 常见参数: copy:默认为yes,当copy=yes,拷贝的文件是从ansible主机复制到远程主机上,如果设置为copy=no,会在远程主机上寻找src源文件 remote_src:和copy功能一样且互斥,yes表示在远程主机,不在ansible主机,no表示文件在ansible主机上 src:源路径,可以是ansible主机上的路径,也可以是远程主机(被管理端或者第三方主机)上的路径,如果是远程主机上的路径,则需要设置copy=no dest:远程主机上的目标路径 mode:设置解压缩后的文件权限 [root@master ~]#ansible all -m unarchive -a 'src=/data/foo.tgz dest=/var/lib/foo owner=wang group=bin' [root@master ~]#ansible all -m unarchive -a 'src=/tmp/foo.zip dest=/data copy=no mode=0777' [root@master ~]#ansible all -m unarchive -a 'src=https://releases.ansible.com/ansible/ansible-2.1.6.0-0.1.rc1.tar.gz dest=/data copy=no' [root@master ~]#ansible all -m unarchive -a 'src=https://releases.ansible.com/ansible/ansible-2.1.6.0-0.1.rc1.tar.gz dest=/data/ owner=root remote_src=yes' [root@master ~]#ansible all -m unarchive -a 'src=http://nginx.org/download/nginx-1.18.0.tar.gz dest=/usr/local/src/ copy=no' 10)archive模块 功能:打包压缩保存在被管理节点 [root@master ~]#ansible all -m archive -a 'path=/root/nginx-1.18.0 dest=/root/ngxin.tar.bz2 format=bz2 mode=0600' 11)hostname模块 功能:管理主机名 [root@master ~]#ansible 10.0.0.150 -m hostname -a 'name=master111' 12)cron模块 功能:计划任务 支持时间:minute,hour,day,month,weekday [root@master ~]#cat /root/mysql_backup.sh #!/bin/bash mysqldump -A -F --single-transaction --master-data=2 -q -uroot |gzip > /data/mysql_`date +%F_%T`.sql.gz #创建任务 [root@master ~]#ansible 10.0.0.150 -m cron -a 'hour=2 minute=30 weekday=1-5 name="backup mysql" job=/root/mysql_backup.sh' [root@master ~]#ansible websrvs -m cron -a "minute=*/5 job='/usr/sbin/ntpdate ntp.aliyun.com &>/dev/null' name=Synctime" #禁用计划任务 [root@master ~]#ansible websrvs -m cron -a "minute=*/5 job='/usr/sbin/ntpdate 172.20.0.1 &>/dev/null' name=Synctime disabled=yes" #启用计划任务 [root@master ~]#ansible websrvs -m cron -a "minute=*/5 job='/usr/sbin/ntpdate 172.20.0.1 &>/dev/null' name=Synctime disabled=no" #删除任务 [root@master ~]#ansible websrvs -m cron -a "name='backup mysql' state=absent" [root@master ~]#ansible websrvs -m cron -a 'state=absent name=Synctime' 13)yum和apt模块 功能: yum 管理软件包,只支持RHEL,CentOS,fedora,不支持Ubuntu其它版本 apt 模块管理 Debian 相关版本的软件包 [root@master ~]#ansible websrvs -m yum -a 'name=httpd state=present' #安装 [root@master ~]#ansible websrvs -m yum -a 'name=nginx state=present enablerepo=epel' #启用epel源进行安装 [root@master ~]#ansible websrvs -m yum -a 'name=* state=lastest exclude=kernel*,foo*' #升级除kernel和foo开头以外的所有包 [root@master ~]#ansible websrvs -m yum -a 'name=httpd state=absent' #删除 [root@master ~]#ansible websrvs -m yum -a 'name=sl,cowsay' [root@master ~]#ansible websrvs -m yum -a "name=https://mirror.tuna.tsinghua.edu.cn/zabbix/zabbix/5.2/rhel/7/x86_64/zabbixagent-5.2.5-1.el7.x86_64.rpm" [root@centos8 ~]#ansible websrvs -m apt -a 'name=rsync,psmisc state=absent' [root@master ~]#ansible localhost -m yum -a "list=tree" 14)yum_repository模块 功能:配置yum仓库 #创建yum仓库 [root@master ~]#ansible localhost -m yum_repository -a "name=test description='epel yum repo' file=external_repos baseurl=https://download.fedoraproject.org/pub/epel/$releasever/$basearch/ gpgcheck=no" localhost | CHANGED => { "changed": true, "repo": "test", "state": "present" } [root@master ~]#ls /etc/yum.repos.d/external_repos.repo /etc/yum.repos.d/external_repos.repo #删除仓库 [root@master ~]#ansible localhost -m yum_repository -a "name=test file=external_repos state=absent" localhost | CHANGED => { "changed": true, "repo": "test", "state": "absent" } [root@master ~]#ls /etc/yum.repos.d/external_repos.repo ls: cannot access '/etc/yum.repos.d/external_repos.repo': No such file or directory 15)service模块 功能:管理服务 [root@master ~]#ansible all -m service -a 'name=httpd state=started enabled=yes' [root@master ~]#ansible all -m service -a 'name=httpd state=stopped' [root@master ~]#ansible all -m service -a 'name=httpd state=reloaded' [root@master ~]#ansible all -m shell -a "sed -i 's/^Listen 80/Listen 8080/' /etc/httpd/conf/httpd.conf" [root@master ~]#ansible all -m service -a 'name=httpd state=restarted' 16)user模块 功能:管理用户 #创建用户 [root@master ~]#ansible all -m user -a 'name=nginx comment=nginx uid=88 groups="root,daemon" shell=/sbin/nologin system=yes create_home=no home=/data/nginx non_unique=yes' #remove=yes表示删除用户及家目录等数据,默认remove=no [root@master ~]#ansible all -m user -a 'name=nginx state=absent remove=yes' #生成123456加密的密码 [root@master ~]#ansible localhost -m debug -a "msg={{ '123456'| password_hash('sha512','salt')}}" localhost | SUCCESS => { "msg": "$6$salt$MktMKPZJ6t59GfxcJU20DwcwQzfMvOlHFVZiOVD71w." } #用上面创建的密码创建用户 [root@master ~]#ansible websrvs -m user -a 'name=test password="$6$salt$MktMKPZJ6t59GfxcJU20DwcwQzfMvOlHFVZiOVD71w."' #创建用户test,并生成4096bit的私钥 [root@master ~]#ansible websrvs -m user -a 'name=test generate_ssh_key=yes ssh_key_bits=4096 ssh_key_file=.ssh/id_rsa' 17)group模块 功能:管理组 #创建组 [root@master ~]#ansible websrvs -m group -a 'name=nginx gid=88 system=yes' #删除组 [root@master ~]#ansible websrvs -m group -a 'name=nginx state=absent' 18)lineinfile模块 ansible在使用sed进行替换时,经常会遇到需要转义的问题,而且ansible在遇到特殊符号进行替换时,存在问题,无法正常进行替换 。其实在ansible自身提供了两个模块:lineinfile模块和replace模块,可以方便的进行替换 一般在ansible当中去修改某个文件的单行进行替换的时候需要使用lineinfile模块 regexp参数 :使用正则表达式匹配对应的行,当替换文本时,如果有多行文本都能被匹配,则只有最后面被匹配到的那行文本才会被替换,当删除文本时,如果有多行文本都能被匹配,这么这些行都会被删除。 如果想进行多行匹配进行替换需要使用replace模块 功能:相当于sed,可以修改文件内容 [root@master ~]#ansible websrvs -m lineinfile -a "path=/etc/httpd/conf/httpd.conf regexp='^Listen' line='Listen 80'" [root@master ~]#ansible all -m lineinfile -a "path=/etc/selinux/config regexp='^SELINUX=' line='SELINUX=disabled'" [root@master ~]#ansible all -m lineinfile -a 'dest=/etc/fstab state=absent regexp="^#"' 19)replace模块 该模块有点类似于sed命令,主要也是基于正则进行匹配和替换,建议使用 [root@master ~]#ansible all -m replace -a "path=/etc/fstab regexp='^(UUID.*)' replace='#\1'" [root@master ~]#ansible all -m replace -a "path=/etc/fstab regexp='^#(UUID.*)' replace='\1'" 20)SELinux 模块 该模块管理 SELInux 策略 [root@master ~]#ansible all -m selinux -a 'state=disabled' 21)reboot模块 功能:重启服务器 [root@master ~]#ansible websrvs -m reboot 22)mount 挂载和卸载 功能: 挂载和卸载文件系统 #临时挂载 [root@master ~]#mount 10.0.0.150 -m mount -a 'src="UUID=b3e48f45-f933-4c8e-a700-22a159ec9077" path=/home fstype=xfs opts=noatime state=present' #临时取消挂载 [root@master ~]#mount all -m mount -a 'path=/home fstype=xfs opts=noatime state=unmounted' #永久挂载 [root@master ~]#ansible all -m mount -a 'src=10.0.0.150:/data/wordpress path=/var/www/html/wp-content/uploads opts="_netdev" state=mounted' #永久卸载 [root@master ~]#ansible all -m mount -a 'src=10.0.0.150:/data/wordpress path=/var/www/html/wp-content/uploads state=absent' 23)Setup 模块 功能: setup 模块来收集主机的系统信息,这些 facts 信息可以直接以变量的形式使用,但是如果主机较多,会影响执行速度 可以使用 gather_facts: no 来禁止 Ansible 收集 facts 信息 [root@master ~]#ansible all -m setup [root@master ~]#ansible all -m setup -a "filter=ansible_nodename" [root@master ~]#ansible all -m setup -a "filter=ansible_hostname" [root@master ~]#ansible all -m setup -a "filter=ansible_domain" [root@master ~]#ansible all -m setup -a "filter=ansible_memtotal_mb" [root@master ~]#ansible all -m setup -a "filter=ansible_memory_mb" [root@master ~]#ansible all -m setup -a "filter=ansible_memfree_mb" [root@master ~]#ansible all -m setup -a "filter=ansible_os_family" [root@master ~]#ansible all -m setup -a "filter=ansible_distribution_major_version" [root@master ~]#ansible all -m setup -a "filter=ansible_distribution_version" [root@master ~]#ansible all -m setup -a "filter=ansible_processor_vcpus" [root@master ~]#ansible all -m setup -a "filter=ansible_all_ipv4_addresses" [root@master ~]#ansible all -m setup -a "filter=ansible_architecture" [root@master ~]#ansible all -m setup -a "filter=ansible_uptime_seconds" [root@master ~]#ansible all -m setup -a "filter=ansible_processor*" [root@master ~]#ansible all -m setup -a 'filter=ansible_env' [root@master ~]#ansible all -m setup -a 'filter=ansible_all_ipv4_addresses' [root@master ~]#ansible all -m setup -a 'filter=ansible_python_version' [root@master ~]#ansible all -m setup -a 'filter="ansible_default_ipv4"' 24)debug 模块 此模块可以用于输出信息,并且通过 msg 定制输出的信息内容 注意: msg后面的变量有时需要加 " " 引起来 debug 模块默认输出Hello world [root@master ~]#ansible all -m debug 10.0.0.150 | SUCCESS => { "msg": "Hello world!" } [root@master ~]#ansible all -m debug -a 'msg="HOSTNAME"' 10.0.0.150 | SUCCESS => { "msg": "HOSTNAME" } #利用debug模块输出变量 [root@master ~]#cat debug.yaml --- - hosts: websrvs tasks: - name: output variables debug: msg: Host "{{ ansible_nodename }}" Ip "{{ ansible_default_ipv4.address }}" #显示特定字符 # cat debug.yml - hosts: all gather_facts: no vars: a: "12345" tasks: - debug: msg: "{{a[2]}}" #定义了一个字符串变量a,如果想要获取a字符串的第3个字符,则可以使用”a[2]”获取,索引从0开始,执行上例playbook,debug的输出信息如下: [root@master tasks]#ansible-playbook /root/debug.yml PLAY [all] ***************************************************************************** TASK [debug] *************************************************************************** ok: [10.0.0.150] => { "msg": "3" } ok: [10.0.0.160] => { "msg": "3" }