Asura&Angle

导航

配置 MySQL 主-主 高可用架构

安装环境:centos6.9

1、安装MySQL
	yum在线安装
	 1、  查看是否安装了mysql数据库:
		rpm -qa | grep mysql
	
	  2、  强制卸载mysql数据库:
		rpm -e nodeps mysql
	
	  3、  通过命令查看yum上提供的数据库可下载版本:
		yum list | grep mysql
	
	 4、安装数据库: 
		 yum -y install mysql-server mysql-devel
	
	  5、启动数据库:  service mysqld start
	
	  6、停止数据库  
		service mysqld stop                                                                                           
	
	  8、查看是否是开机启动(若2~5都是on则表明是开机启动):
	
	          chkconfig --list | grep mysqld
	
	  9、若2~5都是off则用以下命令设置:  
		chkconfig mysqld on 
	
	  10、  chkconfig --list | grep mysqld
	
	  11、启动mysql: service mysqld start
	
	  12、设置用户及密码(输入以下命令后两次回车,及第一次密码直接回车即可):
		 mysqladmin -u root -p password 'root'
	
	  13、测试登录mysql:  mysql -u root -p
	
	  14、如若远程需要进行授权操作:
	
	        GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
	        FLUSH PRIVILEGES;
	        
		   grant all privileges on *.* to root@"hadoop01" identified by "123456" with grant option;
	        FLUSH PRIVILEGES;
	
	  15、查看数据库的配置文件: cat /etc/my.cnf   
	
	(其中的datadir是MySQL数据库的存放路径,表示数据在CentOS里的/var/lib/mysql目录下)
	
	  16、进入mysql安装路径查看: cd /var/lib/mysql
	
	  17、停止mysql服务: service mysqld stop

2、配置防火墙
	
	service iptables stop
	iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT
    service iptables save
    service iptables start

3、配置MySQL 主-主同步
	主主同步事实上就是在主从的基础上,将原先的从机当主机,主机当从机再配置一遍主从同步
	
		1. 在数据库1的my.cnf的mysqld节点添加以下配置
	
		[root@localhost ~]# cat /etc/my.cnf
		[mysqld]
		datadir=/var/lib/mysql
		socket=/var/lib/mysql/mysql.sock
		user=mysql
		# Disabling symbolic-links is recommended to prevent assorted security risks
		symbolic-links=0
		
		wait_timeout = 343232
		
		max_connections=2000
		
		default-character-set=utf8
		
		default-storage-engine=INNODB
		
		sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'
		
		############################################
		######Active-Active Redundancy
		server_id=1
		
		log-bin=mysql-bin
		
		auto_increment_increment=2
		
		auto_increment_offset=1
		
		binlog-ignore=mysql
		
		binlog-ignore=information_schema
		
		binlog-ignore=performance_schema
		
		character_set_server=utf8
		
		log-bin-trust-function-creators=1
		###########################################
		
		skip_ssl
		# disable_ssl
		
		[mysqld_safe]
		log-error=/var/log/mysqld.log
		pid-file=/var/run/mysqld/mysqld.pid
		
	 2 、在数据库2的my.cnf的mysqld节点添加以下配置
	
		[root@localhost ~]# cat /etc/my.cnf
		[mysql]
		default-character-set=utf8
		
		[mysqld]
		datadir=/var/lib/mysql
		socket=/var/lib/mysql/mysql.sock
		user=mysql
		# Disabling symbolic-links is recommended to prevent assorted security risks
		symbolic-links=0
		
		wait_timeout = 343232
		
		max_connections=2000
		
		default-character-set=utf8
		
		default-storage-engine=INNODB
		
		sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'
		
		############################################
		######Active-Active Redundancy
		server_id=2
		
		log-bin=mysql-bin
		
		auto_increment_increment=2
		
		auto_increment_offset=2
		
		binlog-ignore=mysql
		
		relay-log=mysqld-relay-bin
		
		binlog-ignore=information_schema
		
		binlog-ignore=performance_schema
		
		character_set_server=utf8
		
		log-bin-trust-function-creators=1
		###########################################
		
		
		skip_ssl
		# disable_ssl
		
		[mysqld_safe]
		log-error=/var/log/mysqld.log
		pid-file=/var/run/mysqld/mysqld.pid
	
	3、参数介绍
		log_bin 启动mysql二进制日志,如果没有配置这个将无法远程链接
		
		binlog-ignore 指定不同步的数据库,如果有多个数据库不需要同步可以多个分别声明
		
		character_set_server=utf8 指定utf8为默认字符集
		
		server-id   可以为任意自然数,必须保证两台mysql主机不重复
		
		auto_increment_increment=2   步进值auto_imcrement。一般有n台主MySQL就填n
		
		auto_increment_offset   设定数据库中自动增长的起点,两台mysql的起点必须不同,这样才能避免两台服务器同步时出现主键冲突
		
		replicate-do-db  要同步的数据库,如果需要就填,指定数据库的名称即可,默认为所有库,声明了不同步就默认除了不同步数据库意外的所有库。这里我没写。
		
		log-bin-trust-function-creators=1 在默认情况下mysql会阻止主从同步的数据库function的创建,这会导致我们在导入sql文件时如果有创建function或者使用function的语句将会报错。
		
	4、数据库同步用户授权
		分别在两个数据库中,为对端配置授权:
		在两个服务器上创建用户:
		CREATE USER 'reply' IDENTIFIED BY '123456';  
		
		@'192.168.56.101'
		grant replication slave, replication client on *.* to 'reply'@'192.168.56.102' identified by '123456'
		flush privileges;
		
		
		@'192.168.56.102'
		grant replication slave, replication client on *.* to 'reply'@'192.168.56.101' identified by '123456'
		flush privileges;
		
		注意:推荐两个主机相互授权的账号密码相同,否则在接下来的操作中可能会比较乱
		
	5、查看两个数据库的日志节点:
		开始之前,先关闭同步,在两个服务器上执行:
		stop slave;
		
	
		@'192.168.56.101'
		mysql> show master status;
		+------------------+----------+--------------+---------------------------------------------+
		| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB                            |
		+------------------+----------+--------------+---------------------------------------------+
		| mysql-bin.000001 |      473 |              | mysql,information_schema,performance_schema |
		+------------------+----------+--------------+---------------------------------------------+
		1 row in set (0.00 sec)
		
		@'192.168.56.102'
		mysql> show master status;
		+------------------+----------+--------------+---------------------------------------------+
		| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB                            |
		+------------------+----------+--------------+---------------------------------------------+
		| mysql-bin.000001 |      473 |              | mysql,information_schema,performance_schema |
		+------------------+----------+--------------+---------------------------------------------+
		1 row in set (0.00 sec)
		
		
		根据以上信息,配置同步
		@'192.168.56.101'
		 change master to master_host='192.168.56.102', master_port=3306, master_user='reply',master_password='123456', master_log_file='mysql-bin.000001', master_log_pos=473;
		
		@'192.168.56.102'
		 change master to master_host='192.168.56.101', master_port=3306, master_user='reply',master_password='123456', master_log_file='mysql-bin.000001', master_log_pos=473;
		
		
		
	6、在两台主机上启动slave服务
		start slave;
		
	7、查看同步状态:
		mysql> show slave status\G;
		*************************** 1. row ***************************
		               Slave_IO_State: Waiting for master to send event
		                  Master_Host: 192.168.56.102
		                  Master_User: reply
		                  Master_Port: 3306
		                Connect_Retry: 60
		              Master_Log_File: mysql-bin.000001
		          Read_Master_Log_Pos: 553
		               Relay_Log_File: mysqld-relay-bin.000002
		                Relay_Log_Pos: 331
		        Relay_Master_Log_File: mysql-bin.000001
		             Slave_IO_Running: Yes
		            Slave_SQL_Running: Yes
		              Replicate_Do_DB:
		          Replicate_Ignore_DB:
		           Replicate_Do_Table:
		       Replicate_Ignore_Table:
		
		以上标志同时为yes时,表示进入同步状态
		然后去另外一台服务器,也应该是这总状态

Keepalived

	1. 安装
		Yum install keepalived
		Chkconfig keepalived on
		
		[root@Mysql2 checker]# chkconfig --list |grep keep
		keepalived      0:关闭  1:关闭  2:启用  3:启用  4:启用  5:启用  6:关闭
		
	2. 配置
		1. 工具脚本配置(两个服务器上一样)
			[root@Mysql1 checker]# cat mysql_check.sh
			#!/bin/bash
			count=1
			
			while true
			do
			    mysql -e "show status;" > /dev/null 2>&1
			    i=$?
			    ps aux | grep mysqld | grep -v grep > /dev/null 2>&1
			    j=$?
			    if [ $i = 0 ] && [ $j = 0 ]
			    then
exit 0
			    else
			        if [ $i = 1 ] && [ $j = 0 ]
			        then
exit 0
			        else
			            if [ $count -gt 5 ]
			            then
break
fi
let count++
			            continue
			        fi
			    fi
			done
exit 10
			
			[root@Mysql1 checker]# cat master.sh
			#!/bin/bash
			
			Master_Log_File=$(mysql -e "show slave status\G" | grep -w Master_Log_File | awk -F": " '{print $2}')
			Relay_Master_Log_File=$(mysql -e "show slave status\G" | grep -w Relay_Master_Log_File | awk -F": " '{print $2}')
			Read_Master_Log_Pos=$(mysql -e "show slave status\G" | grep -w Read_Master_Log_Pos | awk -F": " '{print $2}')
			Exec_Master_Log_Pos=$(mysql -e "show slave status\G" | grep -w Exec_Master_Log_Pos | awk -F": " '{print $2}')
			
			i=1
			
			while true
do
			
			if [ $Master_Log_File = $Relay_Master_Log_File ] && [ $Read_Master_Log_Pos -eq $Exec_Master_Log_Pos ]
			then
echo "ok"
			   break
else
sleep 1
			
			   if [ $i -gt 60 ]
			   then
break
fi
			   continue
let i++
			fi
			done
			
			#mysql -e "stop slave;"
			#mysql -e "reset slave;"
			#mysql -e "reset master;"
			mysql -e "show master status;show slave status;" > /tmp/master_status_$(date "+%y%m%d-%H%M").txt
			
			[root@Mysql1 checker]# cat stop.sh
			#!/bin/bash
			
			M_File1=$(mysql  -e "show master status\G" | awk -F': ' '/File/{print $2}')
			M_Position1=$(mysql -e "show master status\G" | awk -F': ' '/Position/{print $2}')
			sleep 1
			M_File2=$(mysql  -e "show master status\G" | awk -F': ' '/File/{print $2}')
			M_Position2=$(mysql -e "show master status\G" | awk -F': ' '/Position/{print $2}')
			
			i=1
			
			while true
do
			
			if [ $M_File1 = $M_File1 ] && [ $M_Position1 -eq $M_Position2 ]
			then
echo "ok"
			   break
else
sleep 1
			
			   if [ $i -gt 30 ]
			   then
break
fi
			   continue
let i++
			fi
			done
		
		2. 主服务器配置
			i. Keepalived 配置
			! Configuration File for keepalived
			global_defs {
			   router_id MySQL-HA  #运行Keepalived的机器的一个标识
			}
			
			vrrp_script check_run {
			    script "/etc/keepalived/checker/mysql_check.sh"  #配置业务进程监控脚本
			    interval 10  #设置脚本执行的时间间隔,秒
			}
			
			vrrp_sync_group VG1 {
			    group {
			        VI_1
			    }
			}
			
			vrrp_instance VI_1 {
			    state BACKUP
			    interface eth3 #要改为当前服务器的值
			    virtual_router_id 51
			    priority 100  #权重
			    advert_int 1
			    nopreempt
			    authentication {
			        auth_type PASS
			        auth_pass 1234
			    }
			    track_script {
			        check_run
			    }
			    notify_master /etc/keepalived/checker/master.sh
			    notify_stop /etc/keepalived/checker/stop.sh
			
			    virtual_ipaddress {
			        192.168.56.99/24
			    }
			}
				
		3. 从服务器配置:
			[root@Mysql2 checker]# cat ../keepalived.conf
			! Configuration File for keepalived
			global_defs {
			   router_id MySQL-HA  #运行Keepalived的机器的一个标识
			}
			
			vrrp_script check_run {
			    script "/etc/keepalived/checker/mysql_check.sh"  #配置业务进程监控脚本
			    interval  10 #设置脚本执行的时间间隔,秒
			}
			
			vrrp_sync_group VG1 {
			    group {
			        VI_1
			    }
			}
			
			vrrp_instance VI_1 {
			    state BACKUP
			    interface eth3 #要改为当前服务器的值
			    virtual_router_id 51
			    priority 90  #权重
			    advert_int 1
			    nopreempt
			    authentication {
			        auth_type PASS
			        auth_pass 1234
			    }
			    track_script {
			        check_run
			    }
			    notify_master /etc/keepalived/checker/master.sh
			    notify_stop /etc/keepalived/checker/stop.sh
			
			    virtual_ipaddress {
			        192.168.56.99/24
			    }
			}
			
	3. 防火墙
		service iptables stop
		
		iptables -A INPUT -p vrrp -j ACCEPT                               #添加vrrp网络流量
		iptables -A INPUT -d 192.168.56.99 -j ACCEPT                      #添加对虚拟IP的支持
		
        service iptables save
        service iptables start
	4. 启动keepalived
		Service keepalived start
	5. 查看IP地址情况
		Ip addr

配置MySQL客户端登录用户名和密码
    [root@Mysql1 checker]# cat ~/.my.cnf
    [client]
    password=123456
    user=root

posted on 2021-01-29 14:48  Asura&Angle  阅读(54)  评论(0编辑  收藏  举报