mysql8.0帶來的新特性,結合MySQLshell,不需要第三方中間件,自動構建高可用集群。
mysql8.0作為一款新產品,其內置的mysq-innodb-cluster(MIC)高可用集群的技術確實驚艷,結合MySQLshell能夠實施集群的快速部署,MySQL-route能夠實現災備快速切換,內置讀寫分離技術,負載均衡技術。結合但實際效果如何,還需驗證。
一,集群部署
1.1 安裝環境;
操作系統:Linux,版本:CentOS-7-x86
介質准備:無
環境清理
釋放yum進程
[root@bug ~]# ps -ef|grep yum root 22481 1694 5 17:23 ? 00:00:03 /usr/bin/python /usr/share/PackageKit/helpers/yum/yumBackend.py get-updates none root 22591 22507 1 17:24 pts/0 00:00:00 grep --color=auto yum [root@bug ~]# kill -9 22481
查看是否有多余系統,有則卸載
[root@bug ~]# rpm -qa|grep mairadb [root@bug ~]# rpm -qa|grep mysql
關閉防火牆
[root@bug ~]# systemctl status firewalld.service ● firewalld.service - firewalld - dynamic firewall daemon Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled) Active: inactive (dead)
關閉selinux,重啟系統后生效
[root@bug selinux]# vi /etc/selinux/config SELINUX=disabled
[root@bug selinux]# reboot
1.2 安裝MySQL8.0
[root@bug ~]# yum install -y wget https://repo.mysql.com//mysql80-community-release-el7-1.noarch.rpm [root@bug ~]# yum list|grep mysql [root@bug ~]# yum install -y mysql-community-client.x86_64 mysql-router.x86_64 mysql-shell.x86_64
采用YUM源安裝方式,總下載量約400M,
1.3自動集群部署
部署節點1
[root@bug ~]# mysqlsh MySQL JS > dba.deploySandboxInstance(3310); A new MySQL sandbox instance will be created on this host in /root/mysql-sandboxes/3310 Warning: Sandbox instances are only suitable for deploying and running on your local machine for testing purposes and are not accessible from external networks. Please enter a MySQL root password for the new instance: ****** Deploying new MySQL instance... Instance localhost:3310 successfully deployed and started. Use shell.connect('root@localhost:3310'); to connect to the instance.
第一個節點部署完畢,端口設置為3310,登陸賬號為root@localhost,密碼 ******
使用本地認證的方式, 登陸數據庫實例,進行驗證。
[root@bug ~]# mysql -uroot -porange -S /root/mysql-sandboxes/3310/sandboxdata/mysqld.sock
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
同樣的方法,部署節點2,節點3。
MySQL JS > dba.deploySandboxInstance(3320); MySQL JS > dba.deploySandboxInstance(3330);
1.4創建集群
此實驗采用簡單的創建本地集群。
MySQL JS > \connect root@localhost:3310 Creating a session to 'root@localhost:3310' *****************************************************
MySQL localhost:3310 ssl JS > var cluster=dba.createCluster('test') A new InnoDB cluster will be created on instance 'root@localhost:3310'. ********************************************************* Cluster successfully created. Use Cluster.addInstance() to add MySQL instances. At least 3 instances are needed for the cluster to be able to withstand up to one server failure.
集群系統已經創建成功,最后一行:At least 3 instances are needed for the cluster to be able to withstand up to one server failure.提示需要至少三個實例,才能保證災備,所以接下來,將節點2,3添加進集群。
MySQL localhost:3310 ssl JS > cluster.addInstance( 'root@localhost:3320') The instance 'root@localhost:3320' was successfully added to the cluster. MySQL localhost:3310 ssl JS > cluster.addInstance( 'root@localhost:3330') The instance 'root@localhost:3330' was successfully added to the cluster.
查看集群狀態
MySQL localhost:3310 ssl JS > dba.getCluster().status()
{
"clusterName": "test",
"defaultReplicaSet": {
"name": "default",
"primary": "localhost:3310",
"ssl": "REQUIRED",
"status": "OK",
"statusText": "Cluster is ONLINE and can tolerate up to ONE failure.",
"topology": {
"localhost:3310": {
"address": "localhost:3310",
"mode": "R/W",
"readReplicas": {},
"role": "HA",
"status": "ONLINE"
},
"localhost:3320": {
"address": "localhost:3320",
"mode": "R/O",
"readReplicas": {},
"role": "HA",
"status": "ONLINE"
},
"localhost:3330": {
"address": "localhost:3330",
"mode": "R/O",
"readReplicas": {},
"role": "HA",
"status": "ONLINE"
}
}
},
"groupInformationSourceMember": "mysql://root@localhost:3310"
}
一個簡單的包含三個節點的集群已經創建完成。可以完成數據同步,讀寫分離等功能,比如此刻3310端口的狀態是"R/W",同時read與write,3320與3330的狀態是"R/O",只讀模式。
1.5配置中間件
此時的集群的高可用性還不完整,需要MySQL-router來完成集群與外部的對接,實現自動切換,故障轉移等功能。
MySQL-router的作用類似keepalived 類的中間件。當主機發生故障后,自動將應用切換到其他實例。
[root@bug ~]# mysqlrouter --bootstrap root@localhost:3310 --user=mysqlrouter Please enter MySQL password for root: ***** Bootstrapping system MySQL Router instance... Checking for old Router accounts Creating account mysql_router2_j05xzi45m81x@'%' MySQL Router has now been configured for the InnoDB cluster 'test'. The following connection information can be used to connect to the cluster. Classic MySQL protocol connections to cluster 'test': - Read/Write Connections: localhost:6446 - Read/Only Connections: localhost:6447 X protocol connections to cluster 'test': - Read/Write Connections: localhost:64460 - Read/Only Connections: localhost:64470 Existing configurations backed up to '/etc/mysqlrouter/mysqlrouter.conf.bak' [root@bug ~]# mysqlrouter& [1] 25602 [root@bug ~]# ps -ef|grep router mysqlro+ 25602 22507 8 19:35 pts/0 00:00:01 mysqlrouter root 25619 22507 0 19:36 pts/0 00:00:00 grep --color=auto router
驗證MySQL-router安裝效果
在MySQL-router默認配置下,
主機端口:6446
從庫端口:6447
[root@bug ~]# mysql -uroot -h 127.0.0.1 -P 6446 -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. ********************************************************8 mysql>
證明MySQL-router配置完成可用。
1.6驗證集群效果
1,通過router同時登陸三個節點,查看端口號。
2,在節點1構造數據,在節點2,3差看狀態,驗證數據同步性。
3,節點1(主機)離線,查看集群狀態與節點2,節點3狀態,驗證災備能力。