1、编写脚本,支持让用户自主选择,使用mysqldump还是xtraback全量备份。
2、配置Mysql主从同步
主机:两台,一台当主服务器master(192.168.17.27),一台当从服务器(192.168.17.37)
(1)安装启动数据库,并设置密码
yum -y install mysql mysql-server mysql-devel
service mysqld restart #启动数据库
mysqladmin -u root password '123456' #设置root用户密码
(2)修改主库my.cnf主要设置个不一样的ID,以及同步的数据库的名字,我一般用vim 来完成,vim /etc/my.cnf 在[mysqld]中加入内容如下
server_id = 1 #主数据库端ID号
log-bin = mysql-bin #开启二进制日志
binlog-do-db = db #需要复制的数据库名,如果复制多个数据库,重复设置这个选项即可
log-slave-updates #将从服务器从主服务器收到的更新记入到从服务器自己的二进制日志文件中
sync_binlog = 1 #控制binlog的写入频率。每执行多少次事务写入一次(这个参数性能消耗很大,但可减小MySQL崩溃造成的损失)
auto_increment_offset = 1 #这个参数一般用在主主同步中,用来错开自增值, 防止键值冲突
auto_increment_increment = 1 #这个参数一般用在主主同步中,用来错开自增值, 防止键值冲突
expire_logs_days = 7 #二进制日志自动删除的天数,默认值为0,表示“没有自动删除”,启动时和二进制日志循环时可能删除
log_bin_trust_function_creators = 1 #将函数复制到slave
(3)登录主库赋予从库权限账号,允许用户在主库上读取日志(用户名:admin,密码:123456)
mysql -uroot -p123456 #创建slave账号admin,密码123456
grant replication slave on . to 'admin'@'192.168.100.20' identified by '123456'; #更新数据库权限
mysql>flush privileges;
(4)锁主库表,显示主库信息flush tables with read lock;
显示主库信息show master status;
mysql> show master status;
+---------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+---------------+----------+--------------+------------------+
| binlog.000002 | 106 | haocheng | |
+---------------+----------+--------------+------------------+
1 row in set (0.00 sec)
(记录file、position,从库设置将会用到)
(5)修改从库my.cnf
server_id = 2
log-bin = mysql-bin
log-slave-updates
sync_binlog = 0
innodb_flush_log_at_trx_commit = 0 #log buffer将每秒一次地写入log file中,并且log file的flush(刷到磁盘)操作同时进行。该模式下在事务提交的时候,不会主动触发写入磁盘的操作
replicate-do-db = db #指定slave要复制哪个库
#MySQL主从复制的时候,当Master和Slave之间的网络中断,但是Master和Slave无法察觉的情况下(比如防火墙或者路由问题)。Slave会等待slave_net_timeout设置的秒数后,才能认为网络出现故障,然后才会重连并且追赶这段时间主库的数据
slave-net-timeout = 60
log_bin_trust_function_creators = 1
#master-host=192.168.100.10
#master-user=admin
#master-password=123456
(6)在从库上设置同步
#执行同步命令,设置主服务器ip,同步账号密码,同步位置
mysql -u root -p123456
mysql>slave stop;
mysql>change master to master_host='10.10.20.111',master_user='account',master_password='123456',master_log_file='mysql-bin.000033',master_log_pos=337523;
#开启同步功能
mysql>start slave;
(7)查看从服务器状态
3、使用MHA实现Mysql高可用。
192.168.17.238 mha-manager
192.168.17.239 mysql-master
192.168.17.240 mysql-slave1
192.16817.241 mysql-slave2
1)mysql主服务器配置,编辑mysql配置文件/etc/my.cnf,在mysqld下添加如下选项
vim /etc/my.cnf
[mysqld]
serverid = 1 #主从唯一标识,主从架构中的mysql实例该选项值必须唯一
log-bin mysql-bin #开启二进制日志文件
skip_name_resolve=1
2)登录mysql master实例创建主从同步账号,在mysql主从集群所有节点执行此操作
create user 'repl'@'192.168.17.%' identified by 'repl';
grant replication slave on . to 'repl'@'192.168.17.%';
flush privileges;
重启mariadb服务 systemctl restart mysqld
登录数据库查看二进制文件及位置
MariaDB [(none)]> show master logs;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000001 | 375 |
| mysql-bin.000002 | 1968 |
+------------------+-----------+
3)mysql从节点配置 (多个从节点使用相同的方法进行配置)
vim /etc/my.cnf 增加如下选项
[mysqld]
server_id = 2
relay_log = relay-log #启用中继日志
read_only #mysql的从节点开启只读
relay_log_purge=0
skip_name_resolve=1
重启mysql服务
systemctl restart mysqld
使用有复制权限的用户账号连接至主服务器,并启动复制线程
CHANGE MASTER TO
MASTER_HOST='192.168.17.239',
MASTER_USER='repl',
MASTER_PASSWORD='repl',
MASTER_PORT=3306,
MASTER_LOG_FILE='mysql-bin.000002',
MASTER_LOG_POS=1968;
启动slave的sql和IO线程
MariaDB [(none)]> start slave;
查询slave的sql和IO线程是否正常
MariaDB [(none)]> show slave status \G
1. row
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.17.239
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 1968
Relay_Log_File: relay-log.000002
Relay_Log_Pos: 555
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
4)在主节点上导入数据验证主从数据是否同步
mysql-master上导入hellodb库
MariaDB [hellodb]> source /root/hellodb_innodb.sql
mysql-salve从节点检查数据同步正常
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| hellodb |
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
5)在MHA-Manager管理几点上安装依赖包和mha4mysql-manager mha4mysql-node包,在mha集群的其他节点上安装mha4mysql-node包。
yum install -y perl-Mail-Sender perl-Email-Date-Format perl-MIME-Types perl-MIME-Lite perl-Parallel-ForkManager perl-Mail-Sendmail perl-DBD-MySQL perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager perl-YAML-Tiny perl-PAR-Dist perl-Module-ScanDeps perl-Module-CoreList perl-Module-Build perl-CPAN perl-YAML perl-CPANPLUS perl-File-Remove perl-Module-Install
yum -y install mha4mysql-manager-0.58-0.el7.centos.noarch.rpm mha4mysql-node-0.58-0.el7.centos.noarch.rpm
软件包下载地址:
https://github.com/yoshinorim/mha4mysql-manager
https://github.com/yoshinorim/mha4mysql-node
6)在集群所有的节点上相互配置ssh基于key验证,这里配置集群所有的主机使用同一个公钥和私钥
ssh-keygen
ssh-copy-id 127.0.0.1
scp -r /root/.ssh root@remoteIp:/root/
7)在管理节点建立配置文件
创建存放配置文件的目录 mkdir -p /etc/mastermha
vim /etc/mastermha/app1.conf
[server default]
user=mhauser
password=mhauser
manager_workdir=/data/mastermha/app1/
manager_log=/data/mastermha/app1/manager.log
remote_workdir=/data/mastermha/app1/
ssh_user=root
repl_user=repl
repl_password=repl
ping_interval=1
[server1]
hostname=192.168.17.239
candidate_master=1
[server2]
hostname=192.168.17.240
candidate_master=1
[server3]
hostname=192.168.17.241
8)登录mysql master创建mha管理账号
create user 'mhauser'@'192.168.17.%' identified by 'mhauser';
grant all on . to 'mhauser'@'192.168.17.%';
9)使用的是二进制方式安装mysql,需要设置mysql命令的软链接,否则执行检查会提示命令找不到错误。
ln -s /usr/local/mysql/bin/mysqlbinlog /usr/local/bin/mysqlbinlog
ln -s /usr/local/mysql/bin/mysql /usr/local/bin/mysql
10)Mha验证和启动
验证节点之间ssh key相互登录
masterha_check_ssh --conf=/etc/mastermha/app1.cnf
显示如下结果为成功
Sun Feb 23 16:17:15 2020 - [info] All SSH connection tests passed successfully.
mha主从复制测试
masterha_check_repl --conf=/etc/mastermha/app1.cnf
Sat Feb 29 15:13:16 2020 - [info] Checking replication health on 192.168.17.240..
Sat Feb 29 15:13:16 2020 - [info] ok.
Sat Feb 29 15:13:16 2020 - [info] Checking replication health on 192.168.17.241..
Sat Feb 29 15:13:16 2020 - [info] ok.
Sat Feb 29 15:13:16 2020 - [warning] master_ip_failover_script is not defined.
Sat Feb 29 15:13:16 2020 - [warning] shutdown_script is not defined.
Sat Feb 29 15:13:16 2020 - [info] Got exit code 0 (Not master dead).
MySQL Replication Health is OK.
启动mha,使用后台运行
nohup masterha_manager --conf=/etc/mastermha/app1.cnf &> /dev/null &
ps -aux|grep mha
root 24329 2.6 0.4 207436 21948 pts/1 S 15:17 0:00 perl /usr/bin/masterha_manager --conf=/etc/mastermha/app1.cnf
模拟17.239主服务器故障,集群自动提升其他节点为主节点,停止17.239mysql服务,查看mha日志自动将配置文件中定义的候选节点240替身为mysql master。
Started automated(non-interactive) failover.
The latest slave 192.168.17.240(192.168.17.240:3306) has all relay logs for recovery.
Selected 192.168.17.240(192.168.17.240:3306) as a new master.
192.168.17.240(192.168.17.240:3306): OK: Applying all logs succeeded.
192.168.17.241(192.168.17.241:3306): This host has the latest relay log events.
Generating relay diff files from the latest slave succeeded.
192.168.17.241(192.168.17.241:3306): OK: Applying all logs succeeded. Slave started, replicating from 192.168.17.240(192.168.17.240:3306)
192.168.17.240(192.168.17.240:3306): Resetting slave info succeeded.
Master failover to 192.168.17.240(192.168.17.240:3306) completed successfully.