MySQL8.0 MIC高可用集羣搭建

 

mysql8.0帶來的新特性,結合MySQLshell,不須要第三方中間件,自動構建高可用集羣。python

mysql8.0做爲一款新產品,其內置的mysq-innodb-cluster(MIC)高可用集羣的技術確實驚豔,結合MySQLshell可以實施集羣的快速部署,MySQL-route可以實現災備快速切換,內置讀寫分離技術,負載均衡技術。結合但實際效果如何,還需驗證。mysql

一,集羣部署linux

1.1 安裝環境;sql

操做系統:Linux,版本:CentOS-7-x86shell

介質準備:無數據庫

環境清理bootstrap

釋放yum進程session

[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

查看是否有多餘系統,有則卸載app

[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狀態,驗證災備能力。

相關文章
相關標籤/搜索