时间:2021-07-01 10:21:17 帮助过:20人阅读
1 #系统版本 2 [root@mysql-db01 ~]# cat /etc/redhat-release 3 CentOS release 6.7 (Final) 4 #内核版本 5 [root@mysql-db01 ~]# uname -r 6 2.6.32-573.el6.x86_64 7 #IP地址 8 [root@mysql-db01 ~]# hostname -I 9 10.0.0.51
1 #系统版本 2 [root@mysql-db02 ~]# cat /etc/redhat-release 3 CentOS release 6.7 (Final) 4 #内核版本 5 [root@mysql-db02 ~]# uname -r 6 2.6.32-573.el6.x86_64 7 #IP地址 8 [root@mysql-db02 ~]# hostname -I 9 10.0.0.52
1 #系统版本 2 [root@mysql-db03 ~]# cat /etc/redhat-release 3 CentOS release 6.7 (Final) 4 #内核版本 5 [root@mysql-db03 ~]# uname -r 6 2.6.32-573.el6.x86_64 7 #IP地址 8 [root@mysql-db03 ~]# hostname -I 9 10.0.0.53
1 #创建安装包存放目录 2 [root@mysql-db01 ~]# mkdir /home/oldboy/tools -p 3 #进入目录 4 [root@mysql-db01 ~]# cd /home/oldboy/tools/ 5 #上传mysql安装包(mysql-5.6.16-linux-glibc2.5-x86_64.tar.gz) 6 [root@mysql-db01 tools]# rz -be
1 #创建安装目录 2 [root@mysql-db01 tools]# mkdir /application 3 #解压mysql二进制包 4 [root@mysql-db01 tools]# tar xf mysql-5.6.16-linux-glibc2.5-x86_64.tar.gz 5 #移动安装包 6 [root@mysql-db01 tools]# mv mysql-5.6.16-linux-glibc2.5-x86_64 /application/mysql-5.6.16 7 #做软链接 8 [root@mysql-db01 tools]# ln -s /application/mysql-5.6.16/ /application/mysql 9 #创建mysql用户 10 [root@mysql-db01 tools]# useradd mysql -s /sbin/nologin -M 11 #进入mysql初始化目录 12 [root@mysql-db01 tools]# cd /application/mysql/scripts/ 13 #初始化mysql 14 [root@mysql-db01 scripts]# ./mysql_install_db 15 --user=mysql 16 --datadir=/application/mysql/data/ 17 --basedir=/application/mysql/ 18 #注解 19 --user: 指定mysql用户 20 --datadir:指定mysql数据存放目录 21 --basedir:指定mysql base目录 22 #拷贝mysql配置文件 23 [root@mysql-db01 ~]# \cp /application/mysql/support-files/my-default.cnf /etc/my.cnf 24 #拷贝mysql启动脚本 25 [root@mysql-db01 ~]# cp /application/mysql/support-files/mysql.server /etc/init.d/mysqld 26 #修改mysql默认安装目录(否则无法启动) 27 [root@mysql-db01 ~]# sed -i ‘s#/usr/local#/application#g‘ /etc/init.d/mysqld 28 [root@mysql-db01 ~]# sed -i ‘s#/usr/local#/application#g‘ /application/mysql/bin/mysqld_safe 29 #配置mysql环境变量 30 [root@mysql-db01 ~]# echo ‘export PATH="/application/mysql/bin:$PATH"‘ >> /etc/profile.d/mysql.sh 31 #刷新环境变量 32 [root@mysql-db01 ~]# source /etc/profile
1 #加入开机自启 2 [root@mysql-db01 ~]# chkconfig mysqld on 3 #启动mysql 4 [root@mysql-db01 ~]# /etc/init.d/mysqld start 5 Starting MySQL........... SUCCESS! #启动成功
1 #配置mysql密码为oldboy123 2 [root@mysql-db01 ~]# mysqladmin -uroot password oldboy123
主库和从库都要开启binlog (因为从库有可能被提升为主库,所以必须开启binlog)
主库和从库server-id不同
要有主从复制用户
1 #编辑mysql配置文件 2 [root@mysql-db01 ~]# vim /etc/my.cnf 3 #在mysqld标签下配置 4 [mysqld] 5 #主库server-id为1,从库必须和主库不一样 6 server_id =1 7 #开启binlog日志 8 log_bin=mysql-bin
1 #登录数据库 2 [root@mysql-db01 ~]# mysql -uroot -poldboy123 3 #创建rep用户 4 mysql> grant replication slave on *.* to rep@‘10.0.0.%‘ identified by ‘oldboy123‘;
1 #修改mysql-db02配置文件 2 [root@mysql-db02 ~]# vim /etc/my.cnf 3 #在mysqld标签下配置 4 [mysqld] 5 #主库server-id为1,从库必须和主库不一样 6 server_id =5 7 #开启binlog日志 8 log_bin=mysql-bin 9 #重启mysql 10 [root@mysql-db02 ~]# /etc/init.d/mysqld restart 11 12 #修改mysql-db03配置文件 13 [root@mysql-db03 ~]# vim /etc/my.cnf 14 #在mysqld标签下配置 15 [mysqld] 16 #主库server-id为1,从库必须和主库不一样 17 server_id =10 18 #开启binlog日志 19 log_bin=mysql-bin 20 #重启mysql 21 [root@mysql-db03 ~]# /etc/init.d/mysqld restart
注:在以往如果是基于binlog日志的主从复制,则必须要记住主库的master状态信息。
1 mysql> show master status;
+------------------+----------+
| File | Position |
+------------------+----------+
| mysql-bin.000002 | 120 |
+------------------+----------+
1 #没开启之前先看一下GTID的状态 2 mysql> show global variables like ‘%gtid%‘;
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| enforce_gtid_consistency | OFF |
| gtid_executed | |
| gtid_mode | OFF |
| gtid_owned | |
| gtid_purged | |
+--------------------------+-------+ 3 #编辑mysql配置文件(主库从库都需要修改) 4 [root@mysql-db01 ~]# vim /etc/my.cnf 5 #在[mysqld]标签下添加 6 [mysqld] 7 gtid_mode=ON 8 log_slave_updates 9 enforce_gtid_consistency 10 #重启数据库 11 [root@mysql-db01 ~]# /etc/init.d/mysqld restart 12 #检查GTID状态 13 mysql> show global variables like ‘%gtid%‘;
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| enforce_gtid_consistency | ON | #执行GTID一致
| gtid_executed | |
| gtid_mode | ON | #开启GTID模块
| gtid_owned | |
| gtid_purged | |
+--------------------------+-------+
注:主库从库都需要开启GTID否则在做主从复制的时候就会报错
1 [root@mysql-db02 ~]# mysql -uroot -poldboy123 2 mysql> change master to 3 -> master_host=‘10.0.0.51‘, 4 -> master_user=‘rep‘, 5 -> master_password=‘oldboy123‘, 6 -> master_auto_position=1; 7 ERROR 1777 (HY000): CHANGE MASTER TO MASTER_AUTO_POSITION = 1 can only be executed when @@GLOBAL.GTID_MODE = ON.
1 #登录数据库 2 [root@mysql-db02 ~]# mysql -uroot -poldboy123 3 #配置复制主机信息 4 mysql> change master to 5 #主库IP 6 -> master_host=‘10.0.0.51‘, 7 #主库复制用户 8 -> master_user=‘rep‘, 9 #主库复制用户的密码 10 -> master_password=‘oldboy123‘, 11 #GTID位置点 12 -> master_auto_position=1; 13 #开启slave 14 mysql> start slave; 15 #查看slave状态 16 mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.0.0.51 #主库IP
Master_User: rep
Master_Port: 3306 #主库端口
Connect_Retry: 60
Master_Log_File: mysql-bin.000004 #binlog名
Read_Master_Log_Pos: 191
Relay_Log_File: mysql-db02-relay-bin.000005
Relay_Log_Pos: 401
Relay_Master_Log_File: mysql-bin.000004
Slave_IO_Running: Yes #IO线程状态
Slave_SQL_Running: Yes #SQL线程状态
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: 191
Relay_Log_Space: 1883
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_UUID: 722cdde9-0272-11e7-a825-000c2951d7ad
Master_Info_File: /application/mysql-5.6.16/data/master.info
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
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: 722cdde9-0272-11e7-a825-000c2951d7ad:1 #GTID号
Executed_Gtid_Set: 722cdde9-0272-11e7-a825-000c2951d7ad:1 #GTID号
Auto_Position: 1
1 #登录从库 2 [root@mysql-db02 ~]# mysql -uroot -poldboy123 3 #禁用自动删除relay log 功能 4 mysql> set global relay_log_purge = 0; 5 #设置只读 6 mysql> set global read_only=1; 7 #编辑配置文件 8 [root@mysql-db02 ~]# vim /etc/my.cnf 9 #在mysqld标签下添加 10 [mysqld] 11 #禁用自动删除relay log 永久生效 12 relay_log_purge = 0
1 #安装依赖包 2 [root@mysql-db01 ~]# yum install perl-DBD-MySQL -y 3 #进入安装包存放目录 4 [root@mysql-db01 ~]# cd /home/oldboy/tools/ 5 #上传mha安装包 6 [root@mysql-db01 tools]# rz -be 7 mha4mysql-manager-0.56-0.el6.noarch.rpm 8 mha4mysql-manager-0.56.tar.gz 9 mha4mysql-node-0.56-0.el6.noarch.rpm 10 mha4mysql-node-0.56.tar.gz 11 #安装node包 12 [root@mysql-db01 tools]# rpm -ivh mha4mysql-node-0.56-0.el6.noarch.rpm 13 #登录数据库 14 [root@mysql-db01 tools]# mysql -uroot -poldboy123 15 #添加mha管理账号 16 mysql> grant all privileges on *.* to mha@‘10.0.0.%‘ identified by ‘mha‘; 17 #查看是否添加成功 18 mysql> select user,host from mysql.user; 19 #主库上创建,从库会自动复制(在从库上查看) 20 mysql> select user,host from mysql.user;
1 #如果不创建命令软连接,检测mha复制情况的时候会报错 2 [root@mysql-db01 ~]# ln -s /application/mysql/bin/mysqlbinlog /usr/bin/mysqlbinlog 3 [root@mysql-db01 ~]# ln -s /application/mysql/bin/mysql /usr/bin/mysql
1 #使用epel源 2 [root@mysql-db03 ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo 3 #安装manager依赖包 4 [root@mysql-db03 ~]# yum install -y perl-Config-Tiny epel-release perl-Log-Dispatch perl-Parallel-ForkManager perl-Time-HiRes 5 #安装manager包 6 [root@mysql-db03 tools]# rpm -ivh mha4mysql-manager-0.56-0.el6.noarch.rpm 7 Preparing... ########################################### [100%] 8 1:mha4mysql-manager ########################################### [100%]
1 #创建配置文件目录 2 [root@mysql-db03 ~]# mkdir -p /etc/mha 3 #创建日志目录 4 [root@mysql-db03 ~]# mkdir -p /var/log/mha/app1 5 #编辑mha配置文件 6 [root@mysql-db03 ~]# vim /etc/mha/app1.cnf 7 [server default] 8 manager_log=/var/log/mha/app1/manager 9 manager_workdir=/var/log/mha/app1 10 master_binlog_dir=/application/mysql/data 11 user=mha 12 password=mha 13 ping_interval=2 14 repl_password=oldboy123 15 repl_user=rep 16 ssh_user=root 17 18 [server1] 19 hostname=10.0.0.51 20 port=3306 21 22 [server2] 23 candidate_master=1 24 check_repl_delay=0 25 hostname=10.0.0.52 26 port=3306 27 28 [server3] 29 hostname=10.0.0.53 30 port=3306
【配置文件详解】
1 [server default] 2 #设置manager的工作目录 3 manager_workdir=/var/log/masterha/app1 4 #设置manager的日志 5 manager_log=/var/log/masterha/app1/manager.log 6 #设置master 保存binlog的位置,以便MHA可以找到master的日志,我这里的也就是mysql的数据目录 7 master_binlog_dir=/data/mysql 8 #设置自动failover时候的切换脚本 9 master_ip_failover_script= /usr/local/bin/master_ip_failover 10 #设置手动切换时候的切换脚本 11 master_ip_online_change_script= /usr/local/bin/master_ip_online_change 12 #设置mysql中root用户的密码,这个密码是前文中创建监控用户的那个密码 13 password=123456 14 #设置监控用户root 15 user=root 16 #设置监控主库,发送ping包的时间间隔,尝试三次没有回应的时候自动进行failover 17 ping_interval=1 18 #设置远端mysql在发生切换时binlog的保存位置 19 remote_workdir=/tmp 20 #设置复制用户的密码 21 repl_password=123456 22 #设置复制环境中的复制用户名 23 repl_user=rep 24 #设置发生切换后发送的报警的脚本 25 report_script=/usr/local/send_report 26 #一旦MHA到server02的监控之间出现问题,MHA Manager将会尝试从server03登录到server02 27 secondary_check_script= /usr/local/bin/masterha_secondary_check -s server03 -s server02 --user=root --master_host=server02 --master_ip=192.168.0.50 --master_port=3306 28 #设置故障发生后关闭故障主机脚本(该脚本的主要作用是关闭主机放在发生脑裂,这里没有使用) 29 shutdown_script="" 30 #设置ssh的登录用户名 31 ssh_user=root 32 33 [server1] 34 hostname=10.0.0.51 35 port=3306 36 37 [server2] 38 hostname=10.0.0.52 39 port=3306 40 #设置为候选master,如果设置该参数以后,发生主从切换以后将会将此从库提升为主库,即使这个主库不是集群中事件最新的slave 41 candidate_master=1 42 #默认情况下如果一个slave落后master 100M的relay logs的话,MHA将不会选择该slave作为一个新的master,因为对于这个slave的恢复需要花费很长时间,通过设置check_repl_delay=0,MHA触发切换在选择一个新的master的时候将会忽略复制延时,这个参数对于设置了candidate_master=1的主机非常有用,因为这个候选主在切换的过程中一定是新的master 43 check_repl_delay=0
1 #创建秘钥对 2 [root@mysql-db01 ~]# ssh-keygen -t dsa -P ‘‘ -f ~/.ssh/id_dsa >/dev/null 2>&1 3 #发送公钥,包括自己 4 [root@mysql-db01 ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub root@10.0.0.51 5 [root@mysql-db01 ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub root@10.0.0.52 6 [root@mysql-db01 ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub root@10.0.0.53
1 #测试ssh 2 [root@mysql-db03 ~]# masterha_check_ssh --conf=/etc/mha/app1.cnf 3 #看到如下字样,则测试成功 4 Tue Mar 7 01:03:33 2017 - [info] All SSH connection tests passed successfully. 5 #测试复制 6 [root@mysql-db03 ~]# masterha_check_repl --conf=/etc/mha/app1.cnf 7 #看到如下字样,则测试成功 8 MySQL Replication Health is OK.
1 #启动 2 [root@mysql-db03 ~]# nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mha/app1/manager.log 2>&1 &
1 #登录数据库(db02) 2 [root@mysql-db02 ~]# mysql -uroot -poldboy123 3 #检查复制情况 4 mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.0.0.51
Master_User: rep
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 191
Relay_Log_File: mysql-db02-relay-bin.000005
Relay_Log_Pos: 401
Relay_Master_Log_File: mysql-bin.000004
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: 191
Relay_Log_Space: 1883
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_UUID: 722cdde9-0272-11e7-a825-000c2951d7ad
Master_Info_File: /application/mysql-5.6.16/data/master.info
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
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: 722cdde9-0272-11e7-a825-000c2951d7ad:1
Executed_Gtid_Set: 722cdde9-0272-11e7-a825-000c2951d7ad:1
Auto_Position: 1 5 #登录数据库(db03) 6 [root@mysql-db03 ~]# mysql -uroot -poldboy123 7 #检查复制情况 8 mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.0.0.51
Master_User: rep
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 191
Relay_Log_File: mysql-db02-relay-bin.000005
Relay_Log_Pos: 401
Relay_Master_Log_File: mysql-bin.000004
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: 191
Relay_Log_Space: 1883
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_UUID: 722cdde9-0272-11e7-a825-000c2951d7ad
Master_Info_File: /application/mysql-5.6.16/data/master.info
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
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: 722cdde9-0272-11e7-a825-000c2951d7ad:1
Executed_Gtid_Set: 722cdde9-0272-11e7-a825-000c2951d7ad:1 9 #停掉主库 10 [root@mysql-db01 ~]# /etc/init.d/mysqld stop 11 Shutting down MySQL..... SUCCESS! 12 #登录数据库(db02) 13 [root@mysql-db02 ~]# mysql -uroot -poldboy123 14 #查看slave状态 15 mysql> show slave status\G 16 #db02的slave已经为空 17 Empty set (0.00 sec) 18 #登录数据库(db03) 19 [root@mysql-db03 ~]# mysql -uroot -poldboy123 20 #查看slave状态 21 mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.0.0.52
Master_User: rep
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000006
Read_Master_Log_Pos: 191
Relay_Log_File: mysql-db03-relay-bin.000002
Relay_Log_Pos: 361
Relay_Master_Log_File: mysql-bin.000006
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: 191
Relay_Log_Space: 1883
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_UUID: 722cdde9-0272-11e7-a825-000c2951d7ad
Master_Info_File: /application/mysql-5.6.16/data/master.info
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
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: 722cdde9-0272-11e7-a825-000c2951d7ad:1
Executed_Gtid_