时间:2021-07-01 10:21:17 帮助过:22人阅读
mha4mysql-manager-0.56-0.el6.noarch.rpm
mha4mysql-manager-0.56.tar.gz
mha4mysql-node-0.56-0.el6.noarch.rpm
mha4mysql-node-0.56.tar.gz
Atlas-2.2.1.el6.x86_64.rpm
mysql-5.6.17-linux-glibc2.5-x86_64.tar
echo "192.168.200.69 sl-Mysql-Mater" >> /etc/hosts
echo "192.168.200.79 sl-Mysql-Slave1" >> /etc/hosts
echo "192.168.200.80 sl-Mysql-Slave2" >> /etc/hosts
scp /etc/hosts root@192.168.200.79:/etc/
scp /etc/hosts root@192.168.200.80:/etc/
[root@mysql-db01 ~]# vim /etc/sysconfig/selinux
[root@mysql-db01 ~]# cat /etc/sysconfig/selinux | grep -v "#"
SELINUX=disabled
SELINUXTYPE=targeted
[root@mysql-db01 ~]# setenforce 0
[root@mysql-db01 ~]# service iptables stop
iptables: Setting chains to policy ACCEPT: filter [ OK ]
iptables: Flushing firewall rules: [ OK ]
iptables: Unloading modules: [ OK ]
[root@mysql-db01 ~]# chkconfig iptables off
姓名:松信嘉范
MySQL/Linux专家
2001年索尼公司入职
2001年开始使用oracle
2004年开始使用MySQL
2006年9月-2010年8月MySQL从事顾问
2010年-2012年DeNA
2012年至今Facebook
- MHA(Master High Availability)目前在MySQL高可用方面是一个相对成熟的解决方案,是一套优秀的作为MySQL高可用性环境下故障切换和主从提升的高可用软件。在MySQL故障切换过程中,MHA能做到0~30秒之内自动完成数据库的故障切换操作,并且在进行故障切换过程中,MHA能最大程度上保证数据库的一致性,以达到真正意义上的高可用。
- MHA由两部分组成:MHA Manager(管理节点)和MHA Node(数据节点)。MHA Manager可以独立部署在一台独立的机器上管理多个Master-Slave集群,也可以部署在一台Slave上。当Master出现故障时,它可以自动将最新数据的Slave提升为新的Master,然后将所有其他的Slave重新指向新的Master。整个故障转移过程对应程序是完全透明的。
1,复制主库binlog日志出来(因为还有没来的及复制到从库的二进制日志呢)
2,找出relaylog日志最全的从库 (每个从库复制的速度是有差异的)
3,将最全的relaylog日志在所有从库中同步(第一次数据同步)
4,将之前最全的那个从库提升为主库
5,将复制出来的binlog日志,放到新提升的主库里
6,其他所有从库重新指向新提升的主库,继续主从复制。
MHA软件由两部分组成,Manager工具包和Node工具包,具体的说明如下
#Manager工具包主要包括以下几个工具
masterha_check_ssh #检查MHA的SSH配置状况
masterha_check_repl #检查MySQL复制状况
masterha_check_status #检测当前MHA运行状态
masterha_master_monitor #检测master是否宕机
masterha_manger #启动MHA
masterha_master_switch #控制故障转移(自动或者手动)
masterha_conf_host #添加或删除配置的server信息
masterha_secondary_check #试图建立TCP连接从远程服务器
masterha_stop #停止MHA
#Node工具包主要包括以下几个工具
save_binary_logs #保存和复制master的二进制日志
apply_diff_relay_logs #识别差异的中继日志事件
filter_mysqlbinlog #去除不必要的ROLLBACK事件
purge_relay_logs #清除中继日志
上方有所有软件包得下载链接
yum -y install ncurses-devel libaio
tar xf mysql-5.6.17-linux-glibc2.5-x86_64.tar.gz -C /usr/local/
ln -s /usr/local/mysql-5.6.17-linux-glibc2.5-x86_64 /usr/local/mysql
useradd mysql -s /sbin/nologin -M
/usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data/
/bin/cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf
/bin/cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
ln -s /usr/local/mysql/bin/* /usr/local/bin/
which mysqladmin
chkconfig mysqld on
chkconfig mysqld --list
/etc/init.d/mysqld start
mysqladmin -uroot password ‘linyaonie‘
#修改主库配置文件/etc/my.cnf
[root@mysql-db01 mysql]# cat /etc/my.cnf
[client]
socket = /usr/local/mysql/data/mysql.sock
[mysqld]
lower_case_table_names = 1
default-storage-engine = InnoDB
port = 3306
datadir = /usr/local/mysql/data
character-set-server = utf8
socket = /usr/local/mysql/data/mysql.sock
log_bin = mysql-bin #开启binlog日志
server_id = 1 #设置server_id
innodb_buffer_pool_size = 200M
slave-parallel-workers = 8
thread_cache_size = 600
back_log = 600
slave_net_timeout = 60
max_binlog_size = 512M
key_buffer_size = 8M
query_cache_size = 64M
join_buffer_size = 2M
sort_buffer_size = 2M
query_cache_type = 1
thread_stack = 192K
#重启动MySQL服务
[root@mysql-db01 mysql]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
1)删除不必要的用户
mysql> select user,host from mysql.user;
+------+------------+
| user | host |
+------+------------+
| root | 127.0.0.1 |
| root | ::1 |
| | localhost |
| root | localhost |
| | mysql-db01 |
| root | mysql-db01 |
+------+------------+
6 rows in set (0.00 sec)
mysql> drop user root@‘127.0.0.1‘;
Query OK, 0 rows affected (0.00 sec)
mysql> drop user root@‘::1‘;
Query OK, 0 rows affected (0.00 sec)
mysql> drop user ‘ ‘@‘localhost‘;
Query OK, 0 rows affected (0.00 sec)
mysql> drop user ‘ ‘@‘mysql-db01‘;
Query OK, 0 rows affected (0.00 sec)
mysql> select user,host from mysql.user;
+------+------------+
| user | host |
+------+------------+
| root | localhost |
| root | mysql-db01 |
+------+------------+
2 rows in set (0.00 sec)
2)创建主从复制用户
mysql> grant replication slave on *.* to rep@‘192.168.0.%‘ identified by ‘123123‘;
Query OK, 0 rows affected (0.00 sec)
mysql> select user,host from mysql.user;
+------+-------------+
| user | host |
+------+-------------+
| rep | 192.168.0.% |
| root | localhost |
| root | mysql-db01 |
+------+-------------+
3 rows in set (0.00 sec)
mysql> show grants for rep@‘192.168.0.%‘;
+--------------------------------------------------------------------------------------------------------------------------+
| Grants for rep@192.168.0.% |
+--------------------------------------------------------------------------------------------------------------------------+
| GRANT REPLICATION SLAVE ON *.* TO ‘rep‘@‘192.168.0.%‘ IDENTIFIED BY PASSWORD ‘*E56A114692FE0DE073F9A1DD68A00EEB9703F3F1‘ |
+--------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
#修改mysql-db02配置文件(和mysql-db01配置文件一致)
#只需要修改server-id = 5选项
[root@mysql-db02 ~]# cat /etc/my.cnf
[client]
socket = /usr/local/mysql/data/mysql.sock
[mysqld]
lower_case_table_names = 1
default-storage-engine = InnoDB
port = 3306
datadir = /usr/local/mysql/data
character-set-server = utf8
socket = /usr/local/mysql/data/mysql.sock
log_bin = mysql-bin #从binlog也要打开
server_id = 5 #仅需修改此项
innodb_buffer_pool_size = 200M
slave-parallel-workers = 8
thread_cache_size = 600
back_log = 600
slave_net_timeout = 60
max_binlog_size = 512M
key_buffer_size = 8M
query_cache_size = 64M
join_buffer_size = 2M
sort_buffer_size = 2M
query_cache_type = 1
thread_stack = 192K
[root@mysql-db02 ~]# /etc/init.d/mysqld restart #重启mysql
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
#修改mysql-db03配置文件(和mysql-db01配置文件一致)
#只需要修改server-id = 10选项
[root@mysql-db03 ~]# cat /etc/my.cnf
[client]
socket = /usr/local/mysql/data/mysql.sock
[mysqld]
lower_case_table_names = 1
default-storage-engine = InnoDB
port = 3306
datadir = /usr/local/mysql/data
character-set-server = utf8
socket = /usr/local/mysql/data/mysql.sock
log_bin = mysql-bin #从binlog也要打开
server_id = 10 #只需修改此项
innodb_buffer_pool_size = 200M
slave-parallel-workers = 8
thread_cache_size = 600
back_log = 600
slave_net_timeout = 60
max_binlog_size = 512M
key_buffer_size = 8M
query_cache_size = 64M
join_buffer_size = 2M
sort_buffer_size = 2M
query_cache_type = 1
thread_stack = 192K
[root@mysql-db03 ~]# /etc/init.d/mysqld restart #重启mysql
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
特别提示:
在以往如果是基于binlog日志的主从复制,则必须要记住主库的master状态信息。
但是在MySQL5.6版本里多了一个Gtid的功能,可以自动记录主从复制位置点的信息,并在日志中输出出来。
#没开启之前先看一下GTID状态
mysql> show global variables like ‘%gtid%‘;
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| enforce_gtid_consistency | OFF |
| gtid_executed | |
| gtid_mode | OFF |
| gtid_owned | |
| gtid_purged | |
+--------------------------+-------+
5 rows in set (0.00 sec)
编辑mysql配置文件(主库从库都需要修改)
mysql-db01,mysql-db02,mysql-db03都需要加入上图的上行代码
修改完配置文件以后重启动数据库
[root@mysql-db01 ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
[root@mysql-db02 ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
[root@mysql-db03 ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
再次查看GTID状态
[root@mysql-db01 ~]# mysql -uroot -p123123
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.17-log MySQL Community Server (GPL)
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.
mysql> show global variables like ‘%gtid%‘;
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| enforce_gtid_consistency | ON | #执行GTID一致
| gtid_executed | |
| gtid_mode | ON | #开启GTID模块
| gtid_owned | |
| gtid_purged | |
+--------------------------+-------+
5 rows in set (0.00 sec)
mysql>
再次提示:
主库从库都必须要开启GTID,否则在做主从复制的时候就会报错
mysql> change master to \
-> master_host=‘192.168.0.51‘,\ #主库IP
-> master_user=‘rep‘,\ #主库复制用户
-> master_password=‘123123‘,\ #主库复制用密码
-> master_auto_position=1; #GTID位置点(自动追踪需要同步的position)
Query OK, 0 rows affected, 2 warnings (0.00 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 master to send event
Master_Host: 192.168.0.51
Master_User: rep
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 151
Relay_Log_File: mysql-db02-relay-bin.000002
Relay_Log_Pos: 361
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: Yes #此项yes代表成功
Slave_SQL_Running: Yes #此项yes代表成功
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
#....以下省略若干行....
两个从库mysql-db02和mysql-db03都执行以上步骤。
- GTID(Global Transaction)全局事务标识符:是一个唯一的标识符,它创建并与源服务器(主)上提交的每个事务相关联。此标识符不仅对其发起的服务器是唯一的,而且在给定复制设置中的所有服务器上都是唯一的。所有交易和所有GTID之间都有1对1的映射。
- GTID实际上是由UUID+TID组成的。其中UUID是一个MySQL实例的唯一标识。TID代表了该实例上已经提交的事务数量,并且随着事务提交单调递增。
- 下面是一个GTID的具体形式:
3E11FA47-71CA-11E1-9E33-C80AA9429562:23
(1)支持多线程复制:事实上是针对每个database开启相应的独立线程,即每个库有一个单独的(sql thread)
(2)支持启用GTID,在配置主从复制,传统的方式里,你需要找到binlog和POS点,然后change master to 指向。在mysql5.6里,无须再知道binlog和POS点,只需要知道master的