2020-10-18MySQL、Ansible作业(五)

1、如果主节点已经运行了一段时间,且有大量数据时,如何配置并启动slave节点(写出操作步骤)

[16:56:44 root@master ~]#mysqldump -A -F --single-transaction --master-data=1 > /data/full_`date +%F-%T`.sql[16:56:49 root@master ~]#ll /data/  #在主节点备份文件,同时刷新一下二进制日志
total 488
-rw-r--r--  1 root root 487707 Oct 17 16:56 full_2020-10-17-16:56:49.sql
-rw-r--r--. 1 root root   5706 Jun 16 19:42 reset_pro.sh
[16:56:54 root@master ~]#scp /data/full_2020-10-17-16\:56\:49.sql 10.0.0.38:/root/ #将备份拷贝至目标服务器
The authenticity of host '10.0.0.38 (10.0.0.38)' can't be established.
ECDSA key fingerprint is SHA256:B4GojTG9L1h2MUkoY950+2OuTNePsXfMQMtcFn6Z3YE.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '10.0.0.38' (ECDSA) to the list of known hosts.
[email protected]'s password: 
full_2020-10-17-16:56:49.sql                                              100%  476KB  35.3MB/s   00:00 
Welcome to the MariaDB monitor.  Commands end with ; or \g. 
Your MariaDB connection id is 12
Server version: 10.3.17-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 logs; --记录主节点二进制日志位置
+--------------------+-----------+
| Log_name           | File_size |
+--------------------+-----------+
| mariadb-bin.000001 |       379 |
| mariadb-bin.000002 |       424 |
| mariadb-bin.000003 |       375 |
+--------------------+-----------+

MariaDB [(none)]> set global innodb_flush_log_at_trx_commit=2; --优化主节点和从节点的性能
Query OK, 0 rows affected (0.000 sec)

MariaDB [(none)]> set global sync_binlog=0
    -> ;
Query OK, 0 rows affected (0.000 sec)

MariaDB [(none)]> grant replication slave on *.* to repluser@'10.0.0.%' identified by '123456'; --创制只具备复制权限的账号
Query OK, 0 rows affected (0.001 sec)
[16:58:03 root@slave ~]#vi /etc/my.cnf.d/mariadb-server.cnf  #修改从节点数据库配置文件

#
# These groups are read by MariaDB server.
# Use it for options that only the server (but not clients) should see
#
# See the examples of server my.cnf files in /usr/share/mysql/
#

# this is read by the standalone daemon and embedded servers
[server]

# this is only for the mysqld standalone daemon
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mysqld/mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld]
server-id=2
read-only                                                                                                   
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mariadb/mariadb.log
pid-file=/run/mariadb/mariadb.pid

[17:05:58 root@slave ~]#systemctl enable --now mariadb #启动从节点数据库
-- MySQL dump 10.17  Distrib 10.3.17-MariaDB, for Linux (x86_64)
--
-- Host: localhost    Database: 
-- ------------------------------------------------------
-- Server version   10.3.17-MariaDB-log

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

--
-- Position to start replication or point-in-time recovery from
--

CHANGE MASTER TO MASTER_HOST='10.0.0.28',
MASTER_USER='repluser',
MASTER_PASSWORD='123456',
MASTER_LOG_FILE='mariadb-bin.000003', MASTER_LOG_POS=375;

...
[17:17:11 root@slave ~]#mysql < full_2020-10-17-16\:56\:49.sql  #恢复数据库
[17:17:23 root@slave ~]#mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 16
Server version: 10.3.17-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)]> start slave; --启动从节点复制线程
Query OK, 0 rows affected (0.002 sec)

MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
                Slave_IO_State: Waiting for master to send event
                   Master_Host: 10.0.0.28
                   Master_User: repluser
                   Master_Port: 3306
                 Connect_Retry: 60
               Master_Log_File: mariadb-bin.000003
           Read_Master_Log_Pos: 572
                Relay_Log_File: mariadb-relay-bin.000002
                 Relay_Log_Pos: 754
         Relay_Master_Log_File: mariadb-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: 572
               Relay_Log_Space: 1065
               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: 1
                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)

2、当master服务器宕机,提升一个slave成为新的master(写出操作步骤)

#查看从节点同步记录,找到数据库最新的从节点当新主节点
[19:04:38 root@slave1 ~]#cat /var/lib/mysql/relay-log.info 
5
./mariadb-relay-bin.000002
754
mariadb-bin.000003
572
0

[19:07:55 root@slave1 ~]#vi /etc/my.cnf.d/mariadb-server.cnf  #修改从节点配置文件

#
# These groups are read by MariaDB server.
# Use it for options that only the server (but not clients) should see
#
# See the examples of server my.cnf files in /usr/share/mysql/
#

# this is read by the standalone daemon and embedded servers
[server]

# this is only for the mysqld standalone daemon
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mysqld/mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld]
server-id=2
read-only=OFF
log-bin                                                                                                     
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mariadb/mariadb.log
pid-file=/run/mariadb/mariadb.pid

[19:08:51 root@slave1 ~]#mysql

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 19
Server version: 10.3.17-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)]> set global read_only=off; --关闭只读
Query OK, 0 rows affected (0.000 sec)

MariaDB [(none)]> stop slave; --停止从复制线程
Query OK, 0 rows affected (0.002 sec)

MariaDB [(none)]> reset slave all; --清空slave信息
Query OK, 0 rows affected (0.001 sec)

[19:24:11 root@slave1 ~]#systemctl restart mariadb #重启数据库
[19:24:20 root@slave1 ~]#mysqldump -A --single-transaction --master-data=1 -F > backup.sql #对数据库进行全量备份
[19:26:22 root@slave1 ~]#vi backup.sql  #修改备份中的change master to

-- MySQL dump 10.17  Distrib 10.3.17-MariaDB, for Linux (x86_64)
--
-- Host: localhost    Database: 
-- ------------------------------------------------------
-- Server version   10.3.17-MariaDB-log

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

--
-- Position to start replication or point-in-time recovery from
--

CHANGE MASTER TO 
MASTER_HOST='10.0.0.38',
MASTER_USER='repluser',
MASTER_PASSWORD='123456',                                                                                   
MASTER_LOG_FILE='mariadb-bin.000002', MASTER_LOG_POS=375;
...

[19:37:04 root@slave1 ~]#scp backup.sql 10.0.0.48:/root #将备份拷贝至其他从节点
The authenticity of host '10.0.0.48 (10.0.0.48)' can't be established.
ECDSA key fingerprint is SHA256:B4GojTG9L1h2MUkoY950+2OuTNePsXfMQMtcFn6Z3YE.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '10.0.0.48' (ECDSA) to the list of known hosts.
[email protected]'s password: 
backup.sql                                                                100%  477KB  44.8MB/s   00:00  

[19:42:44 root@slave2 ~]#mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.3.17-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)]> stop slave; --停止slave
Query OK, 0 rows affected, 1 warning (0.000 sec)

MariaDB [(none)]> reset slave all; --清空slave信息
Query OK, 0 rows affected (0.000 sec)

MariaDB [(none)]> set sql_log_bin=off; --关闭二进制日志
Query OK, 0 rows affected (0.000 sec)

MariaDB [(none)]> source backup.sql; --恢复数据库
Query OK, 0 rows affected (0.000 sec)

Query OK, 0 rows affected (0.000 sec)

Query OK, 0 rows affected (0.000 sec)

Query OK, 0 rows affected (0.000 sec)

....

MariaDB [mysql]> set sql_log_bin=on; --开启二进
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值