mysql集群PXC

centos7

三台服务器
	192.168.83.128
	192.168.83.129
	192.168.83.130

1、防火墙
	firewall-cmd --zone=public --add-port=3306/tcp --permanent
	firewall-cmd --zone=public --add-port=4444/tcp --permanent
	firewall-cmd --zone=public --add-port=4567/tcp --permanent
	firewall-cmd --zone=public --add-port=4568/tcp --permanent

	systemctl stop firewalld.service  
	systemctl start firewalld.service

2、系统设置
	vi /etc/selinux/config 
		SELINUX=disabled #修改该项为disabled

	或者:setenforce 0


4、安装Persona仓库
	yum install http://www.percona.com/downloads/percona-release/redhat/0.1-4/percona-release-0.1-4.noarch.rpm

5、安装PXC
	保证服务器没有装MySQL,卸载MySQL 参考链接:https://blog.csdn.net/tjcyjd/article/details/52189182

	yum install Percona-XtraDB-Cluster-57

6、开启PXC服务

	service mysql start

	查看:
	netstat -lntp

	多了两个进程
		tcp6       0      0 :::3306                 :::*                    LISTEN      2443/mysqld         
		tcp        0      0 0.0.0.0:4567            0.0.0.0:*               LISTEN      2443/mysqld   

7、查看安装数据库的临时密码并记住

	grep 'temporary password' /var/log/mysqld.log  

	2018-07-24T03:24:14.339493Z 1 [Note] A temporary password is generated for root@localhost: j*>0dtssdqVt
    
8、登录MySQL数据库

	mysql -u root -p
	j*>0dtssdqVt

	登录成功后修改密码

	ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';

9、停止mysql服务
	service mysqld stop

	netstat -lntp

10、配置节点

	vi  /etc/percona-xtradb-cluster.conf.d/wsrep.cnf

	详细:
	#集群中节点的IP地址,用逗号隔开,本机ip填最后
	wsrep_cluster_address=gcomm://192.168.83.129,192.168.83.130,192.168.83.128

	binlog_format=ROW

	default_storage_engine=InnoDB

	wsrep_slave_threads= 8
	wsrep_log_conflicts

	innodb_autoinc_lock_mode=2

	# Node IP address
	#当前节点IP
	wsrep_node_address = 192.168.83.128
	# Cluster name
	#集群名称
	wsrep_cluster_name=pxc-cluster
	#当前节点名称
	wsrep_node_name=pxc-cluster-node-1
	#pxc_strict_mode allowed values: DISABLED,PERMISSIVE,ENFORCING,MASTER
	#不使用实验功能
	pxc_strict_mode=ENFORCING
	# SST method
	#状态快照传输(sst)方法,官方建议
	wsrep_sst_method=xtrabackup-v2
	#Authentication for SST method
	#用户凭证(mysql的用户名和密码)
	wsrep_sst_auth="pxc123:123456"

	剩下的节点修改当前节点名、当前节点IP、集群中的节点IP,其他相同

11、初始化集群引导节点
	192.168.83.128服务器:
		启动mysql systemctl start [email protected]
		若是停止:systemctl stop [email protected]
		登录mysql  
	   		mysql -u root -p
		开启 wsrep_causal_reads
			set wsrep_causal_reads =1;

		创建配置文件中对应的用户,所有节点的IP都要创建
			-- 用于外部登录
			CREATE USER 'root'@'%' IDENTIFIED BY '123456';
			GRANT all privileges ON *.* TO 'root'@'%';
			FLUSH PRIVILEGES; 

			-- localhost
			CREATE USER 'pxc123'@'localhost' IDENTIFIED BY '123456';
			GRANT all privileges ON *.* TO 'pxc123'@'localhost';
			FLUSH PRIVILEGES;

			-- 192.168.83.129
			CREATE USER 'pxc123'@'192.168.83.129' IDENTIFIED BY '123456';
			GRANT all privileges ON *.* TO 'pxc123'@'192.168.83.129';
			FLUSH PRIVILEGES;

			-- 192.168.83.130
			CREATE USER 'pxc123'@'192.168.83.130' IDENTIFIED BY '123456';
			GRANT all privileges ON *.* TO 'pxc123'@'192.168.83.130';
			FLUSH PRIVILEGES;

12、其他两个节点
	192.168.83.129, 192.168.83.130: 
		service mysql start
		登录mysql,
		set wsrep_causal_reads =1;
		-- 用于外部登录
			mysql启动成功后就会被同步,因此root / 123456 也可以在外部登录了

		其他节点启动成功后在引导节点验证集群:show status like 'wsrep%'; 
			看参数:
			wsrep_cluster_size
			wsrep_local_state_comment
			wsrep_connected
			wsrep_ready

13、节点数据同步验证
	在当前节点创建一个数据库


注意:服务的启动和停止要对应

	service mysql stop   ------>  service mysql start
	systemctl stop [email protected]   ----->  systemctl start [email protected]



Haproxy做pxc负载均衡--------未完

	1、192.168.83.131服务器:
		yum -y install haproxy

		cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.bak
		vi /etc/haproxy/haproxy.cfg

		内容:
			#---------------------------------------------------------------------
			# main frontend which proxys to the backends
			#---------------------------------------------------------------------
			frontend pxc-front
			        bind *:3306
			        mode tcp
			        default_backend pxc-back
			frontend statas-front
			        bind *:8088
			        mode http
			        default_backend stats-back
			#---------------------------------------------------------------------
			# static backend for serving up images, stylesheets and such
			#---------------------------------------------------------------------
			backend pxc-back
			        mode tcp
			        balance leastconn
			        option httpchk
			        server pxc-linux-28 192.168.83.128:3306  check port 9200 inter 12000 rise 3 fall 3
			        server pxc-linux-29 192.168.83.129:3306  check port 9200 inter 12000 rise 3 fall 3
			        server pxc-linux-30 192.168.83.130:3306  check port 9200 inter 12000 rise 3 fall 3
			#---------------------------------------------------------------------
			# round robin balancing between the various backends
			#---------------------------------------------------------------------
			backend stats-back
			        mode http
			        balance roundrobin
			        stats uri /haproxy/stats
			        stats auth pxcstats:xxwzopop

		防火墙
			firewall-cmd --zone=public --add-port=8088/tcp --permanent
			firewall-cmd --zone=public --add-port=3306/tcp --permanent
			firewall-cmd --zone=public --add-port=3307/tcp --permanent

			systemctl stop firewalld.service  
			systemctl start firewalld.service

		开启:service haproxy start

		http://192.168.83.131:8088/haproxy/stats
		pxcstats / xxwzopop
		
	2、在cluster的MySQL上创建用户
		监控用帐号:grant usage on *.* to 'pxc-monitor'@'%' identified by 'testpxc';
		服务测试帐号:grant all privileges on *.* to 'zxw'@'%' identified by 'xxwzopop';

	3、更改用户名和密码(三个节点都得修改)
		cp /usr/bin/clustercheck /usr/bin/clustercheck_bak
		vi /usr/bin/clustercheck
		MYSQL_USERNAME="pxc-monitor"
		MYSQL_PASSWORD="testpxc"
	
	4、测试
		三个节点分别执行命令:clustercheck
		输出:
			HTTP/1.1 200 OK
			Content-Type: text/plain
			Connection: close
			Content-Length: 40
			Percona XtraDB Cluster Node is synced.

	5、更改/etc/services添加mysqlchk的端口号:
		vi /etc/services
		mysqlchk  9200/tcp  #mysqlchk

	6、配置xinetd
		vi /etc/xinetd.d/mysqlchk
		该文件保持默认即可

	7、测试
		for i in `seq 1 1000`;do mysql -h 192.168.83.131 -P3306 -upxc-monitor -ptestpxc -e "select @@hostname;";done

你可能感兴趣的:(Mysql)