第十三周作业
Posted myworldkwd
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第十三周作业相关的知识,希望对你有一定的参考价值。
一、主从复制及主主复制的实现
环境,两台centos8
主172.16.0.151
备 172.16.0.152
A、主节点配置
1、配置
[mysqld]
server-id=8
#二进制日志
log-bin
log_bin=/data/logbin/mysql-bin
新生成的两个文件:
cd /data/logbin
-rw-rw---- 1 mysql mysql 342 Jul 23 21:06 mysql-bin.000001
-rw-rw---- 1 mysql mysql 30 Jul 23 21:06 mysql-bin.index
MariaDB [(none)]> show master logs;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000001 | 342 | 从这个时间点往后的往从服务器复制
+------------------+-----------+
1 row in set (0.000 sec)
MariaDB [(none)]>
但是之前的怎么办呢?从服务器不知道啊
需要对主服务做完全备份
[root@centos8 logbin]# mysqldump -uroot -pmagedu -A -F --single-transaction --master-data=2 --default-character-set=utf8 -q > /backup/all.sql
[root@centos8 logbin]#
[root@centos8 backup]# scp all.sql root@172.16.0.153:/data/
建账户(是在备份后):
MariaDB [(none)]> grant replication slave on . to repluser@\'172.16.0.%\' identified by \'magedu\';
Query OK, 0 rows affected (0.001 sec)
MariaDB [(none)]>
B、从节点
清空/var/lib/mysql
/etc/my.cnf.d/mariadb-ser.cn
清空:/data/logbin/*
yum install mariadb-server -y
[root@centos8 my.cnf.d]# vim mariadb-server.cn
[mysqld]
server-id=18
log-bin
#log_bin=/data/logbin/relay-log (经测试,这里写了下面要报错)
[root@centos8 my.cnf.d]# systemctl restart mariadb
[root@centos8 my.cnf.d]# mysql
MariaDB [(none)]>
查看帮助 信息:
help change master to; 将如下一段拷贝出来
CHANGE MASTER TO
MASTER_HOST=\'172.16.0.151\',
MASTER_USER=\'repluser\',
MASTER_PASSWORD=\'magedu\',
MASTER_PORT=3306,
MASTER_LOG_FILE=\'master2-bin.001\',这一行在前面主全量备份生成all.sql时有的
MASTER_LOG_POS=4, 这一行在前面主全量备份生成all.sql时有的
MASTER_CONNECT_RETRY=10; 这一行不管
修改all.sql
vim /data/all.sql
-- CHANGE MASTER TO MASTER_LOG_FILE=\'mysql-bin.000001\', MASTER_LOG_POS=342;
改为:
CHANGE MASTER TO
MASTER_HOST=\'172.16.0.151\',
MASTER_USER=\'repluser\',
MASTER_PASSWORD=\'magedu\',
MASTER_PORT=3306,
MASTER_LOG_FILE=\'mysql-bin.000001\',
MASTER_LOG_POS=342;
将all.sql导入 数据库(此步骤实现了数据和配置同时导入)
[root@centos8 data]# systemctl restart mariadb
[root@centos8 data]# mysql < all.sql
[root@centos8 data]# mysql
导入后
启动从前点
MariaDB [(none)]> START SLAVE;
Query OK, 0 rows affected (0.001 sec)
C、查看主结点进程(从节点启动前后)
启动前:
MariaDB [(none)]> show processlist;
+----+-------------+-----------+------+---------+------+--------------------------+------------------+----------+
| Id | User | Host | db | Command | Time | State | Info | Progress |
+----+-------------+-----------+------+---------+------+--------------------------+------------------+----------+
| 1 | system user | | NULL | Daemon | NULL | InnoDB purge coordinator | NULL | 0.000 |
| 3 | system user | | NULL | Daemon | NULL | InnoDB purge worker | NULL | 0.000 |
| 2 | system user | | NULL | Daemon | NULL | InnoDB purge worker | NULL | 0.000 |
| 5 | system user | | NULL | Daemon | NULL | InnoDB shutdown handler | NULL | 0.000 |
| 4 | system user | | NULL | Daemon | NULL | InnoDB purge worker | NULL | 0.000 |
| 10 | root | localhost | NULL | Query | 0 | Closing tables | show processlist | 0.000 |
+----+-------------+-----------+------+---------+------+--------------------------+------------------+----------+
6 rows in set (0.001 sec)
MariaDB [(none)]>
从节点启动后:
MariaDB [(none)]> show processlist;
+----+-------------+--------------------+------+-------------+------+-----------------------------------------------------------------------+------------------+----------+
| Id | User | Host | db | Command | Time | State | Info | Progress |
+----+-------------+--------------------+------+-------------+------+-----------------------------------------------------------------------+------------------+----------+
| 1 | system user | | NULL | Daemon | NULL | InnoDB purge coordinator | NULL | 0.000 |
| 3 | system user | | NULL | Daemon | NULL | InnoDB purge worker | NULL | 0.000 |
| 2 | system user | | NULL | Daemon | NULL | InnoDB purge worker | NULL | 0.000 |
| 5 | system user | | NULL | Daemon | NULL | InnoDB shutdown handler | NULL | 0.000 |
| 4 | system user | | NULL | Daemon | NULL | InnoDB purge worker | NULL | 0.000 |
| 10 | root | localhost | NULL | Query | 0 | Closing tables | show processlist | 0.000 |
| 13 | repluser | 172.16.0.153:54282 | NULL | Binlog Dump | 59 | Master has sent all binlog to slave; waiting for binlog to be updated | NULL | 0.000 |
+----+-------------+--------------------+------+-------------+------+-----------------------------------------------------------------------+------------------+----------+
7 rows in set (0.000 sec)
MariaDB [(none)]>
D、查看从节点进程
启动前
MariaDB [(none)]> show processlist;
+----+-------------+-----------+------+---------+------+--------------------------+------------------+----------+
| Id | User | Host | db | Command | Time | State | Info | Progress |
+----+-------------+-----------+------+---------+------+--------------------------+------------------+----------+
| 1 | system user | | NULL | Daemon | NULL | InnoDB purge coordinator | NULL | 0.000 |
| 3 | system user | | NULL | Daemon | NULL | InnoDB purge worker | NULL | 0.000 |
| 2 | system user | | NULL | Daemon | NULL | InnoDB purge worker | NULL | 0.000 |
| 4 | system user | | NULL | Daemon | NULL | InnoDB purge worker | NULL | 0.000 |
| 5 | system user | | NULL | Daemon | NULL | InnoDB shutdown handler | NULL | 0.000 |
| 10 | root | localhost | NULL | Query | 0 | Init | show processlist | 0.000 |
+----+-------------+-----------+------+---------+------+--------------------------+------------------+----------+
6 rows in set (0.000 sec)
MariaDB [(none)]>
MariaDB [(none)]> SHOW SLAVE STATUS\\G;
1. row
Slave_IO_State:
Master_Host: 172.16.0.152
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 342
Relay_Log_File: mariadb-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: No 两个线程还没有起来
Slave_SQL_Running: No
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: 342
Relay_Log_Space: 256
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: NULL 主从同步的时间
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: 0
Master_SSL_Crl:
Master_SSL_Crlpath:
Using_Gtid: No
Gtid_IO_Pos:
Replicate_Do_Domain_Ids:
Replicate_Ignore_Domain_Ids:
Parallel_Mode: conservative
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State:
Slave_DDL_Groups: 0
Slave_Non_Transactional_Groups: 0
Slave_Transactional_Groups: 0
1 row in set (0.000 sec)
ERROR: No query specified
MariaDB [(none)]>
从节点启动后:
MariaDB [(none)]> show processlist;
+----+-------------+-----------+------+-----------+------+-----------------------------------------------------------------------------+------------------+----------+
| Id | User | Host | db | Command | Time | State | Info | Progress |
+----+-------------+-----------+------+-----------+------+-----------------------------------------------------------------------------+------------------+----------+
| 1 | system user | | NULL | Daemon | NULL | InnoDB purge coordinator | NULL | 0.000 |
| 3 | system user | | NULL | Daemon | NULL | InnoDB purge worker | NULL | 0.000 |
| 2 | system user | | NULL | Daemon | NULL | InnoDB purge worker | NULL | 0.000 |
| 4 | system user | | NULL | Daemon | NULL | InnoDB purge worker | NULL | 0.000 |
| 5 | system user | | NULL | Daemon | NULL | InnoDB shutdown handler | NULL | 0.000 |
| 10 | root | localhost | NULL | Query | 0 | Init | show processlist | 0.000 |
| 11 | system user | | NULL | Slave_IO | 15 | Waiting for master to send event | NULL | 0.000 |
| 12 | system user | | NULL | Slave_SQL | 15 | Slave has read all relay log; waiting for the slave I/O thread to update it | NULL | 0.000 |
+----+-------------+-----------+------+-----------+------+-----------------------------------------------------------------------------+------------------+----------+
8 rows in set (0.000 sec)
MariaDB [(none)]> SHOW SLAVE STATUS\\G;
1. row
Slave_IO_State: Waiting for master to send event
Master_Host: 172.16.0.152
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 401
Relay_Log_File: mariadb-relay-bin.000005
Relay_Log_Pos: 700
Relay_Master_Log_File: mysql-bin.000003
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: 401
Relay_Log_Space: 1058
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: 8
Master_SSL_Crl:
Master_SSL_Crlpath:
Using_Gtid: No
Gtid_IO_Pos:
Replicate_Do_Domain_Ids:
Replicate_Ignore_Domain_Ids:
Parallel_Mode: conservative
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Slave_DDL_Groups: 1
Slave_Non_Transactional_Groups: 0
Slave_Transactional_Groups: 0
1 row in set (0.000 sec)
ERROR: No query specified
MariaDB [(none)]>
E、经测试,在主结点上建一个库,从节点马上就有了
另外,repluser 在从mysql服务器上也有了
二、xtrabackup实现全量+增量+binlog恢复库
三、MyCAT实现MySQL读写分离
四、MHA实现
五、Ansible常用模块总结
Ansible常用模块 3387个模块,只介绍常用的
1、 command
[root@centos8 ~]# ansible-doc -s command
[root@centos8 ~]# ansible all -m command -a \'hostname\'
172.16.0.31 | CHANGED | rc=0 >>
centos6
172.16.0.152 | CHANGED | rc=0 >>
centos8
172.16.0.151 | CHANGED | rc=0 >>
nodex
172.16.0.153 | CHANGED | rc=0 >>
centos8
[root@centos8 ~]#
因为是默认模块,所以可以简写成如下命令:
[root@centos8 ~]# ansible all -a \'hostname\'
2、
unarchive 模块
功能:解包解压缩
实现有两种用法:
1、将ansible主机上的压缩包传到远程主机后解压缩至特定目录,设置copy=yes
默认就是yes
2、将远程主机上的某个压缩包解压缩到指定路径下,设置copy=no,此时路径为远程主机的路径
将本地的文件拷贝到组中的/data中
[root@centos8 data]# ansible websrvs -m unarchive -a \'src=etc.tar.xz dest=/data owner=kuang group=bin\'
3、
Archive 模块
功能:打包压缩保存在被管理节点
Fetch 模块
功能:从远程主机提取文件至ansible的主控端
两个模块一起用,就可以被管理的文件压缩并拉到本地。
在远程主机打包
[root@centos8 data]# ansible websrvs -m archive -a \'path=/data/etc/ dest=/data/etc.tar.bz2 format=bz2 owner=kuang mode=0600\'
4、
Hostname 模块
功能:管理主机名
[root@centos8 data]# ansible 172.16.0.31 -m hostname -a \'name=centos66.magedu.org\'
是永久保存吗? 是的
5、Service 模块
功能:管理服务
[root@centos8 backup]# ansible websrvs -m yum -a \'name=vsftpd\'
查看没有启动
[root@centos8 backup]# ansible websrvs -a \'systemctl status vsftpd\'
172.16.0.153 | FAILED | rc=3 >>
● vsftpd.service - Vsftpd ftp daemon
Loaded: loaded (/usr/lib/systemd/system/vsftpd.service; disabled; vendor preset: disabled)
Active: inactive (dead)non-zero return code
172.16.0.151 | FAILED | rc=3 >>
● vsftpd.service - Vsftpd ftp daemon
Loaded: loaded (/usr/lib/systemd/system/vsftpd.service; disabled; vendor preset: disabled)
Active: inactive (dead)non-zero return code
[root@centos8 backup]#
启动服务
[root@centos8 backup]# ansible websrvs -m service -a \'name=vsftpd state=started\'
开机启动
[root@centos8 backup]# ansible websrvs -m service -a \'name=vsftpd state=started enabled=yes\'
6、
Group 模块
功能:管理组
#创建组
ansible websrvs -m group -a \'name=nginx gid=88 system=yes\'
cat /etc/group
#删除组
ansible websrvs -m group -a \'name=nginx state=absent\'
7、
Lineinfile 模块
ansible在使用sed进行替换时,经常会遇到需要转义的问题,而且ansible在遇到特殊符号进行替换时,
存在问题,无法正常进行替换 。其实在ansible自身提供了两个模块:lineinfile模块和replace模块,可
以方便的进行替换
替换实战:
[root@centos8 ~]# ansible 172.16.0.153 -m lineinfile -a "path=/data/selinux regexp=\'^SELINUX=\' line=\'SELINUX=kwd\'"
path=/data/selinux 对应的文件
regexp=\'^SELINUX=\' 找到那个行
line=\'SELINUX=kwd\' 整行替换
8、
replace
在UUID开头的行前面加#
[root@centos8 ~]# ansible 172.16.0.153 -m replace -a "path=/data/fstab regexp=\'^(UUID.*)\' replace=\'#\\1\'"
反向替换
[root@centos8 ~]# ansible 172.16.0.153 -m replace -a "path=/data/fstab regexp=\'^#(UUID.*)\' replace=\'\\1\'"
9、
setup
功能: setup 模块来收集主机的系统信息,这些 facts 信息可以直接以变量的形式使用,但是如果主机较多
[root@centos8 ~]# ansible 172.16.0.153 -m setup -a "filter=ansible_nodename"
172.16.0.153 | SUCCESS => {
"ansible_facts": {
"ansible_nodename": "centos8-153",
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false
}
[root@centos8 ~]#
以上是关于第十三周作业的主要内容,如果未能解决你的问题,请参考以下文章