时间:2021-07-01 10:21:17 帮助过:23人阅读
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 #清除中继日志
主机名 | IP地址(NAT) | 描述 |
---|---|---|
mysql-master | eth0:192.168.200.131 | 系统:CentOS6.5(6.x都可以) 安装:mysql5.6 |
mysql-slaveA | eth0:192.168.200.145 | 系统:CentOS6.5(6.x都可以) 安装:mysql5.6 |
mysql-slaveB | eth0:192.168.200.146 | 系统:CentOS6.5(6.x都可以) 安装:mysql5.6 |
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
[root@localhost etc]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.200.131 MySQL-Master
192.168.200.145 MySQL-SlaveA
192.168.200.146 MySQL-SlaveB
[root@localhost ~]# vim /etc/sysconfig/selinux
[root@localhost ~]# cat /etc/sysconfig/selinux | grep -v "#"
SELINUX=disabled
SELINUXTYPE=targeted
[root@localhost ~]# setenforce 0
[root@localhost ~]# service iptables stop
iptables: Setting chains to policy ACCEPT: filter [ OK ]
iptables: Flushing firewall rules: [ OK ]
iptables: Unloading modules: [ OK ]
[root@localhost ~]# chkconfig iptables off
mysql-master
#系统版本
[root@localhost bin]# cat /etc/redhat-release
CentOS release 6.5 (Final)
[root@localhost bin]# uname -r
2.6.32-431.el6.x86_64
[root@localhost bin]# hostname -I
192.168.0.51
mysql-slaveA
#系统版本
[root@localhost ~]# cat /etc/redhat-release
CentOS release 6.5 (Final)
[root@localhost ~]# uname -r
2.6.32-431.el6.x86_64
[root@localhost ~]# hostname -I
192.168.0.52
mysql-slaveB
#系统版本
[root@localhost ~]# cat /etc/redhat-release
CentOS release 6.5 (Final)
[root@localhost ~]# uname -r
2.6.32-431.el6.x86_64
[root@localhost ~]# hostname -I
192.168.0.53
[root@localhost ~]# yum -y install ncurses-devel libaio
[root@localhost ~]# tar xf mysql-5.6.17-linux-glibc2.5-x86_64.tar.gz -C /usr/local/
[root@localhost ~]# ln -s /usr/local/mysql-5.6.17-linux-glibc2.5-x86_64 /usr/local/mysql
[root@localhost ~]# useradd mysql -s /sbin/nologin -M
[root@localhost ~]# /usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data/
[root@localhost ~]# /bin/cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf
[root@localhost ~]# /bin/cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@localhost ~]# ln -s /usr/local/mysql/bin/* /usr/local/bin/
[root@localhost ~]# which mysqladmin
/usr/local/bin/mysqladmin
[root@localhost ~]# chkconfig mysqld on
[root@localhost ~]# chkconfig mysqld --list
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@localhost ~]# /etc/init.d/mysqld start
Starting MySQL. SUCCESS!
[root@chenguanzhou ~]# mysqladmin -uroot password ‘111111‘
#修改主库配置文件/etc/my.cnf
[root@localhost 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@localhost 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 |
| | localhost.localdomain |
| root | localhost.localdomain |
+------+-----------------------+
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 | localhost.localdomain |
+------+-----------------------+
2 rows in set (0.00 sec)
(2)创建主从复制用户
mysql> grant replication slave on *.* to rep@‘192.168.200.%‘ identified by ‘111111‘;
Query OK, 0 rows affected (0.00 sec)
mysql> select user,host from mysql.user;
+------+------------------------+
| user | host |
+------+------------------------+
| rep | 192.168.0.% |
| root | localhost |
| root | localhost.localdomain |
+------+------------------------+
3 rows in set (0.00 sec)
mysql> show grants for rep@‘192.168.0.%‘;
+----------------------------------------------------------------------------------------------------------------------------+
| Grants for rep@192.168.200.% |
+----------------------------------------------------------------------------------------------------------------------------+
| GRANT REPLICATION SLAVE ON *.* TO ‘rep‘@‘192.168.200.%‘ IDENTIFIED BY PASSWORD ‘*FD571203974BA9AFE270FE62151AE967ECA5E0AA‘ |
+----------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
#slaveA和slaveB 两个配置文件只有一处不一样,都是这个配置文件修改
[root@localhost ~]# 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 #仅需修改此项 A是5 B是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@localhost ~]# /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配置文件(主库从库都需要修改)
[root@localhost ~]# vim /etc/my.cnf
[client]
socket = /usr/local/mysql/data/mysql.sock
[mysqld]
gtid_mode =ON #这行修改
log_slave_updates #这行修改
enforce_gtid_consistency #这行修改
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
server_id = 1
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@localhost ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
再次查看GTID状态
[root@localhost ~]# vim /etc/my.cnf
[root@localhost ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
[root@localhost ~]# mysql -uroot -p111111
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>
#告诉从库主库是谁
mysql> change master to master_host=‘192.168.200.131‘,master_user=‘rep‘,master_password=‘111111‘,master_auto_position=1;
Query OK, 0 rows affected, 2 warnings (0.04 sec)
mysql>
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