安装环境介绍
mysql安装
mysql shell安装
mysql router安装
安装环境介绍
mysql官方下载地址:https://dev.mysql.com/downloads/

Centos7(CentOS-7-x86_64-Everything-1708.iso)
Mysql8.0.28(mysql-8.0.28-1.el7.x86_64.rpm-bundle.tar)
Mysql-shell8.0.28(mysql-shell-8.0.28-1.el7.x86_64.rpm)
Mysql-router8.0.28(mysql-router-8.0.28-1.el7.x86_64.rpm)

准备环境
centos7物理机或者虚拟机三台(python版本要在2.7以上)
10.4.3.66
10.4.3.67
10.4.3.68

由于mysql集群连接是通过本机的hostname进行连接所以要对集群内的节点进行映射
vim /etc/hosts
10.4.3.66 vm001
10.4.3.67 vm002
10.4.3.68 vm003

三台虚拟机分别设置hostname
在10.4.3.66输入以下命令:
hostnamectl set-hostname vm001
在10.4.3.67输入以下命令:
hostnamectl set-hostname vm002
在10.4.3.68输入以下命令:
hostnamectl set-hostname vm003

设置10.4.3.66到其他两台机器的免密登录。
在10.4.3.66机器执行如下命令:
1、输入:ssh-keygen -t rsa,然后一直回车
2、输入:ssh-copy-id -i ~/.ssh/id_rsa.pub 10.4.3.67
3、输入:ssh-copy-id -i ~/.ssh/id_rsa.pub 10.4.3.68

关闭三台机器防火墙和selinux
systemctl status firewalld
systemctl stop firewalld
systemctl disable firewalld

关闭 selinux
使用getenforce命令查看状态
修改文件
vim /etc/selinux/config
设置
SELINUX=disabled
需要重启

mysql安装
MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可 MariaDB的目的是完全兼容MySQL

命令查看mariadb的安装包:rpm -qa | grep mariadb
卸载上述查询mariabd安装包:rpm -e mariadb-libs-5.5.56-2.el7.x86_64 --nodeps
解压tar包:tar -xvf mysql-8.0.28-1.el7.x86_64.rpm-bundle.tar
命令安装 common:rpm -ivh mysql-community-common-8.0.28-1.el7.x86_64.rpm --nodeps --force
命令安装 libs:rpm -ivh mysql-community-libs-8.0.28-1.el7.x86_64.rpm --nodeps --force
命令安装 client:rpm -ivh mysql-community-client-8.0.28-1.el7.x86_64.rpm --nodeps --force
命令安装 server:rpm -ivh mysql-community-server-8.0.28-1.el7.x86_64.rpm --nodeps --force
初始化:mysqld --initialize;
赋访问权限:chown mysql:mysql /var/lib/mysql -R
启动mysql服务:systemctl start mysqld.service;
设置mysql开机自启:systemctl enable mysqld;
命令查看数据库的密码:cat /var/log/mysqld.log | grep password
进入mysql命令行:mysql -uroot -p (输入上述查看的数据库密码进入mysql命令行)
命令来修改密码: ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '设置的密码'; 
创建远程访问权限: create user 'root'@'%' identified with mysql_native_password by '设置的密码'; 
grant all privileges on . to 'root'@'%' with grant option; 
flush privileges; 
命令修改加密规则,MySql8.0 版本 和 5.0 的加密规则不一样,而现在的可视化工具只支持旧的加密方式:
ALTER USER 'root'@'localhost' IDENTIFIED BY '设置的密码' PASSWORD EXPIRE NEVER; 
flush privileges; 

mysql shell安装
分别在三台机器上安装mysql-shell,命令如下:
rpm -ivh mysql-shell-8.0.12-1.el7.x86_64.rpm --nodeps --force 
分别在三台机器执行mysqlshell, 以10.4.3.66为例: 
mysqlsh --uri root@vm001:3306 
执行:dba.configureLocalInstance(),
输入两次y 
退出mysqlshell:ctrl+z 
重新启动mysql服务:systemctl restart mysqld 
检测是否就绪:dba.checkInstanceConfiguration('root@vm001:3306') 
只需要一台上创建cluster:
#创建集群(默认)
var cluster = dba.createCluster('myCluster'); 
#创建集群(多主集群)
var cluster = dba.createCluster('myCluster', {'localAddress': '10.4.3.66', 'multiPrimary': true, 'force': true})
#获取集群对象
var cluster = dba.getCluster('myCluster')
将另外两台添加进入: 
cluster.addInstance('root@vm002:3306'); 
cluster.addInstance('root@vm003:3306'); 
查看状态 为ONLINE 说明集群安装成功:cluster.status()

mysql router安装 
只需要在第一台机器安装mysqlrouter:
rpm -ivh mysql-router-8.0.12-1.el7.x86_64.rpm --nodeps --force 
vim /etc/mysqlrouter/mysqlrouter.conf
设置连接数: max_connections=1024 

[DEFAULT] 
logging_folder = /var/log/mysqlrouter/ 
plugin_folder = /usr/lib64/mysqlrouter 
runtime_folder = /var/run/mysqlrouter 
config_folder = /etc/mysqlrouter 

[logger] 
level = INFO

 # 目前就支持两种 : read-write 和 read-only
 # read-write:用于高可用,用于可读可写
 # read-only:用于负载均衡,只读

 #当一台Master出现故障后另外一台Master或者Slave接管

[routing:read_write] 
bind_address = 10.4.3.66 
bind_port = 7001 
mode = read-write 
destinations = vm001:3306,vm002:3306,vm003:3306
protocol=classic 
max_connections=1024 

[routing:read_only] 
bind_address = 10.4.3.66
bind_port = 7002 
mode = read-only 
destinations = vm001:3306,vm002:3306,vm003:3306 
protocol=classic 
max_connections=1024 

[keepalive] 
interval = 60  

重启mysqlrouter:systemctl restart mysqlrouter 
设置mysqlrouter开机自启:systemctl enable mysqlrouter

测试集群   
1、宕掉主节点A后,会从另外两个中选择出一个作为主节点,且A恢复后,不会成为主节点。   
2、使用客户端工具Navicat可以连接路由节点,IP:192.168.7.121,端口7001,用户名密码与实例节点一致。   
3、JavaWeb应用程序,连接路由节点,且任意宕掉其中一个或两个实例节点后,应用程序不受影响。
4、在第一台机器创建一个数据库,其余两台会自动创建这个数据库

F&Q:

1、当集群的所有节点都offline,直接获取集群信息失败,如何重新恢复集群:

Dba.getCluster: This function is not available through a session to a standalone instance (RuntimeError)
mysql-js> var cluster=dba.getCluster()
Dba.getCluster: This function is not available through a session to a standalone instance (RuntimeError)

 执行rebootClusterFromCompleteOutage命令,可恢复集群

mysql-js> dba.rebootClusterFromCompleteOutage()
Reconfiguring the cluster 'mycluster' from complete outage...
The instance '10.186.23.96:3306' was part of the cluster configuration.
Would you like to rejoin it to the cluster? [y|N]: y
The instance '10.186.23.94:3306' was part of the cluster configuration.
Would you like to rejoin it to the cluster? [y|N]: y
The cluster was successfully rebooted.