下面部署採用InnoDB Cluster, 每臺服務器實例都運行MySQL Group Replication (即冗餘複製機制,內置failover), MGR有兩種模式,一種是Single-Primary,一種是Multi-Primary,即單主或者多主。 需求注意:模式Multi-Primary中,全部的節點都是主節點,均可以同時被讀寫,看上去這彷佛更好,可是由於多主的複雜性,在功能上若是設置了多主模式,則會有一些使用的限制,好比不支持Foreign Keys with Cascading Constraints。node
準備了4臺centos7版本的服務器用來部署innodb cluster多節點集羣環境 (至少也要須要3臺服務器), 其中:python
1) host-192-169-106-十一、host-192-169-106-十二、host-192-169-106-13 做爲 cluster 節點服務器, 三個節點都要安裝 mysql5.7.x 與 mysql-shell
2) host-192-169-106-11 做爲管理節點服務器,用來負責建立 cluster,並做爲 cluster 的路由, 該節點須要安裝mysql-shell、mysql-router
3) 全部節點的python版本要在2.7以上
複製代碼
ip地址 主機名 角色 安裝軟件
192.169.106.11 host-192-169-106-11 管理節點1+cluster節點1 Mysql5.7, mysql-shell,mysql-route
192.169.106.12 host-192-169-106-12 cluster節點2 Mysql5.7, mysql-shell
192.169.106.13 host-192-169-106-13 cluster節點3
複製代碼
[root@host-192-169-106-11 ~]# cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)
[root@host-192-169-106-11 ~]# python -V
Python 2.7.5
配置每一個節點的/etc/hosts主機映射, 方便經過節點的hostname進行鏈接
這一步很重要,不然可能會出現沒法同步的狀況,由於數據庫須要根據member_host同步,若是不配置,默認就是localhost,這樣時沒法通訊的!!!
..............
192.169.106.11 host-192-169-106-11
192.169.106.12 host-192-169-106-12
192.169.106.13 host-192-169-106-13
全部節點進行以下的相關優化配置
[root@host-192-169-106-11 ~]# cat>>/etc/sysctl.conf <<EOF
> fs.aio-max-nr = 1048576
> fs.file-max = 681574400
> kernel.shmmax = 137438953472
> kernel.shmmni = 4096
> kernel.sem = 250 32000 100 200
> net.ipv4.ip_local_port_range = 9000 65000
> net.core.rmem_default = 262144
> net.core.rmem_max = 4194304
> net.core.wmem_default = 262144
> net.core.wmem_max = 1048586
> EOF
[root@host-192-169-106-11 ~]# sysctl -p
[root@host-192-169-106-11 ~]# cat>>/etc/security/limits.conf <<EOF
> mysql soft nproc 65536
> mysql hard nproc 65536
> mysql soft nofile 65536
> mysql hard nofile 65536
> EOF
[root@host-192-169-106-11 ~]# cat>>/etc/pam.d/login <<EOF
> session required /lib/security/pam_limits.so
> session required pam_limits.so
> EOF
[root@host-192-169-106-11 ~]# cat>>/etc/profile<<EOF
> if [ $USER = "mysql" ]; then
> ulimit -u 16384 -n 65536
> fi
> EOF
[root@host-192-169-106-11 ~]# source /etc/profile
複製代碼
[root@host-192-169-106-11 src]# ll
總用量 21648
-rw-r--r--. 1 root root 15526654 8月 28 09:52 mysql-router-2.1.4-linux-glibc2.12-x86-64bit.tar.gz
-rw-r--r--. 1 root root 6635831 8月 28 09:52 mysql-shell-1.0.9-linux-glibc2.12-x86-64bit.tar.gz
[root@host-192-169-106-11 src]# tar -zvxf mysql-shell-1.0.9-linux-glibc2.12-x86-64bit.tar.gz
[root@host-192-169-106-11 src]# tar -zvxf mysql-router-2.1.4-linux-glibc2.12-x86-64bit.tar.gz
[root@host-192-169-106-11 src]# mv mysql-router-2.1.4-linux-glibc2.12-x86-64bit mysql-router
[root@host-192-169-106-11 src]# mv mysql-shell-1.0.9-linux-glibc2.12-x86-64bit mysql-shell
[root@host-192-169-106-11 src]# mv mysql-router /usr/local/
[root@host-192-169-106-11 src]# mv mysql-shell /usr/local/
[root@host-192-169-106-11 local]# vim /etc/profile
..............
export PATH=$PATH:/usr/local/mysql-shell/bin/:/usr/local/mysql-route/bin/
[root@host-192-169-106-11 local]# source /etc/profile
[root@host-192-169-106-11 ~]# mysqlprovision --version
mysqlprovision version 2.0.0
[root@host-192-169-106-11 ~]# mysqlsh --version
MySQL Shell Version 1.0.9
[root@host-192-169-106-11 ~]# mysqlrouter --version
MySQL Router v2.1.4 on Linux (64-bit) (GPL community edition)
複製代碼
[root@host-192-169-106-11 ~]# cd /usr/local/src/
[root@host-192-169-106-11 src]# ll mysql-shell-1.0.9-linux-glibc2.12-x86-64bit.tar.gz
-rw-r--r-- 1 root root 6635831 Mar 22 2017 mysql-shell-1.0.9-linux-glibc2.12-x86-64bit.tar.gz
[root@host-192-169-106-11 src]# tar -zvxf mysql-shell-1.0.9-linux-glibc2.12-x86-64bit.tar.gz
[root@host-192-169-106-11 src]# mv mysql-shell-1.0.9-linux-glibc2.12-x86-64bit mysql-shell
[root@host-192-169-106-11 src]# mv mysql-shell /usr/local/
[root@host-192-169-106-11 src]# echo "export PATH=$PATH:/usr/local/mysql-shell/bin/" >> /etc/profile
[root@host-192-169-106-11 src]# source /etc/profile
[root@host-192-169-106-11 ~]# mysqlprovision --version
mysqlprovision version 2.0.0
[root@host-192-169-106-11 ~]# mysqlsh --version
MySQL Shell Version 1.0.9
複製代碼
使用RPM包方式安裝
1)卸載系統自帶的 mysql和mariadb-lib
[root@host-192-169-106-11 src]# /bin/rpm -e $(/bin/rpm -qa | grep mysql|xargs) --nodeps
[root@host-192-169-106-11 src]# /bin/rpm -e $(/bin/rpm -qa | grep mariadb|xargs) --nodeps
2)下載mysql5.7.21 rpm安裝包
下載地址:http://ftp.ntu.edu.tw/MySQL/Downloads/MySQL-5.7/
[root@host-192-169-106-11 src]# tar -vxf mysql-5.7.27-1.el7.x86_64.rpm-bundle.tar
[root@host-192-169-106-11 src]# ll
總用量 1058872
-rw-r--r--. 1 root root 531056640 8月 28 12:05 mysql-5.7.27-1.el7.x86_64.rpm-bundle.tar
-rw-r--r--. 1 7155 31415 25365436 6月 12 14:42 mysql-community-client-5.7.27-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415 281248 6月 12 14:42 mysql-community-common-5.7.27-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415 3833396 6月 12 14:42 mysql-community-devel-5.7.27-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415 47074656 6月 12 14:42 mysql-community-embedded-5.7.27-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415 24079736 6月 12 14:42 mysql-community-embedded-compat-5.7.27-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415 129991352 6月 12 14:42 mysql-community-embedded-devel-5.7.27-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415 2272032 6月 12 14:42 mysql-community-libs-5.7.27-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415 2116432 6月 12 14:42 mysql-community-libs-compat-5.7.27-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415 173500088 6月 12 14:43 mysql-community-server-5.7.27-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415 122530756 6月 12 14:43 mysql-community-test-5.7.27-1.el7.x86_64.rpm
-rw-r--r--. 1 root root 15526654 8月 28 09:52 mysql-router-2.1.4-linux-glibc2.12-x86-64bit.tar.gz
-rw-r--r--. 1 root root 6635831 8月 28 09:52 mysql-shell-1.0.9-linux-glibc2.12-x86-64bit.tar.gz
依次執行(幾個包有依賴關係,因此執行有前後)下面命令安裝
[root@host-192-169-106-11 src]# rpm -ivh mysql-community-common-5.7.27-1.el7.x86_64.rpm --force
[root@host-192-169-106-11 src]# rpm -ivh mysql-community-libs-5.7.27-1.el7.x86_64.rpm --force
[root@host-192-169-106-11 src]# rpm -ivh mysql-community-client-5.7.27-1.el7.x86_64.rpm --force
[root@host-192-169-106-11 src]# rpm -ivh mysql-community-server-5.7.27-1.el7.x86_64.rpm --force
=============================================================================================================
可能在安裝mysql-community-server-5.7.27-1.el7.x86_64.rpm的時候會有以下報錯:
[root@kevin ~]# rpm -ivh mysql-community-server-5.7.21-1.el7.x86_64.rpm --force
warning: mysql-community-server-5.7.27-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
error: Failed dependencies:
libaio.so.1()(64bit) is needed by mysql-community-server-5.7.27-1.el7.x86_64
libaio.so.1(LIBAIO_0.1)(64bit) is needed by mysql-community-server-5.7.27-1.el7.x86_64
libaio.so.1(LIBAIO_0.4)(64bit) is needed by mysql-community-server-5.7.27-1.el7.x86_64
net-tools is needed by mysql-community-server-5.7.27-1.el7.x86_64
這個報錯的意思是須要安裝libaio包和net-tools包:
安裝libaio-0.3.107-10.el6.x86_64.rpm
[root@host-192-169-106-11 src]# wget http://mirror.centos.org/centos/6/os/x86_64/Packages/libaio-0.3.107-10.el6.x86_64.rpm
[root@host-192-169-106-11 src]# rpm -ivh libaio-0.3.107-10.el6.x86_64.rpm --force
安裝net-tools
[root@host-192-169-106-11 src]# yum install net-tools
=============================================================================================================
使用rpm安裝方式安裝mysql,安裝的路徑以下:
數據庫目錄
/var/lib/mysql/
配置文件
/usr/share/mysql(mysql.server命令及配置文件)
/etc/my.cnf
相關命令
/usr/bin(mysqladmin mysqldump等命令)
啓動腳本
/etc/rc.d/init.d/(啓動腳本文件mysql的目錄)
3)數據庫初始化
爲了保證數據庫目錄爲與文件的全部者爲 mysql 登錄用戶,若是你是以 root 身份運行 mysql 服務,須要執行下面的命令初始化
[root@host-192-169-106-11 src]# mysql_install_db --datadir=/var/lib/mysql //必須指定datadir,執行後會生成~/.mysql_secret密碼文件
[root@host-192-169-106-11 src]# mysqld --initialize --user=mysql //新版的推薦此方法,執行生會在/var/log/mysqld.log生成隨機密碼。若是是以mysql身份運行,則能夠去掉--user選項。
4)更改mysql數據庫目錄的所屬用戶及其所屬組,而後啓動mysql數據庫
[root@host-192-169-106-11 src]# chown mysql:mysql /var/lib/mysql -R
[root@host-192-169-106-11 src]# systemctl start mysqld.service //啓動mysql數據庫服務
[root@host-192-169-106-11 src]# systemctl enable mysqld.service
5)根據第3步中的密碼登陸到mysql,更改root用戶的密碼,新版的mysql在第一次登陸後更改密碼前是不能執行任何命令的
另外--initialize 選項默認以「安全」模式來初始化,則會爲 root 用戶生成一個密碼並將該密碼標記爲過時,登錄後你須要設置一個新的密碼,
而使用--initialize-insecure命令則不使用安全模式,則不會爲 root 用戶生成一個密碼。
這裏演示使用的--initialize初始化的,會生成一個 root 帳戶密碼,密碼在log文件裏,以下最後的"F;HNq*thK2hb"即爲隨即生成的root密碼
[root@host-192-169-106-11 src]# cat /var/log/mysqld.log|grep 'A temporary password'
.......
T05:57:00.021884Z 1 [Note] A temporary password is generated for root@localhost: F;HNq*thK2hb
[root@kevin ~]# mysql -uroot -p'F;HNq*thK2hb'
mysql> set password=password('sagis@123');
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'sagis@123' WITH GRANT OPTION;
mysql> flush privileges;
複製代碼
先配置host-192-169-106-11節點的my.cnf
[root@host-192-169-106-11 ~]# cp /etc/my.cnf /etc/my.cnf.bak
[root@host-192-169-106-11 ~]# >/etc/my.cnf
[root@host-192-169-106-11 ~]# vim /etc/my.cnf
[mysqld]
datadir = /var/lib/mysql
socket = /var/lib/mysql/mysql.sock
port=23306
symbolic-links = 0
log-error = /var/log/mysqld.log
pid-file = /var/run/mysqld/mysqld.pid
#複製框架
server_id=2
gtid_mode=ON
enforce_gtid_consistency=ON
binlog_checksum=NONE
log_bin=binlog
log_slave_updates=ON
binlog_format=ROW
master_info_repository=TABLE
relay_log_info_repository=TABLE
#組複製設置
#server必須爲每一個事務收集寫集合,並使用XXHASH64哈希算法將其編碼爲散列
transaction_write_set_extraction=XXHASH64
#告知插件加入或建立組命名,UUID
loose-group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
#server啓動時不自啓組複製,爲了不每次啓動自動引導具備相同名稱的第二個組,因此設置爲OFF。
loose-group_replication_start_on_boot=off
#告訴插件使用IP地址,端口24901用於接收組中其餘成員轉入鏈接
loose-group_replication_local_address="192.169.106.12:24902"
#啓動組server,種子server,加入組應該鏈接這些的ip和端口;其餘server要加入組得由組成員贊成
loose-group_replication_group_seeds="192.169.106.11:24901,192.169.106.12:24902,192.169.106.13:24903"
loose-group_replication_bootstrap_group=off
loose-group_replication_ip_whitelist="192.169.106.11,192.169.106.12,192.169.106.13"
# 使用MGR的單主模式
loose-group_replication_single_primary_mode = on
disabled_storage_engines = MyISAM,BLACKHOLE,FEDERATED,CSV,ARCHIVE
report_port=23306
如上配置完成後, 將db-node01節點的/etc/my.cnf文件拷貝到其餘兩個節點
[root@host-192-169-106-11 ~]# rsync -e "ssh -p60202" -avpgolr /etc/my.cnf root@192.169.106.12:/etc/
[root@host-192-169-106-11 ~]# rsync -e "ssh -p60203" -avpgolr /etc/my.cnf root@192.169.106.13:/etc/
3個cluster節點除了server_id、loose-group_replication_local_address 兩個參數不同外,其餘保持一致。
因此待拷貝完成後, 分別修改host-192-169-106-12和host-192-169-106-13節點/etc/my.cnf文件的server_id、loose-group_replication_local_address兩個參數
配置完成後, 要依次重啓三個節點的數據庫,安裝MGR插件,設置複製帳號(全部MGR節點都要執行)
[root@host-192-169-106-11 ~]# systemctl restart mysqld
複製代碼
[root@host-192-169-106-11 ~]# mysqlsh
Welcome to MySQL Shell 1.0.9
...................
# 執行配置命令,也須要密碼
# 而後須要輸入MySQL配置文件路徑,本示例中的路徑是 /usr/local/data/s1/s1.cnf
# 接下來須要建立供其餘主機訪問的用戶,這裏選擇第1項,爲root用戶受權
mysql-js> shell.connect('root@192.169.106.11:13306')
Creating a session to 'root@192.169.106.11:13306'
Please provide the password for 'root@192.169.106.11:13306': *********
Save password for 'root@192.169.106.11:13306'? [Y]es/[N]o/Ne[v]er (default No): yes
Fetching schema names for autocompletion... Press ^C to stop.
Your MySQL connection id is 1424
Server version: 5.7.27-log MySQL Community Server (GPL)
No default schema selected; type \use <schema> to set one.
<ClassicSession:root@192.169.106.11:13306>
MySQL 192.169.106.11:13306 JS > dba.configureLocalInstance();
Configuring local MySQL instance listening at port 13306 for use in an InnoDB cluster...
This instance reports its own address as host-192-169-106-11:13306
Clients and other cluster members will communicate with it through this address by default. If this is not correct, the report_host MySQL system variable should be changed.
The instance '192.169.106.11:13306' is valid for InnoDB cluster usage.
The instance '192.169.106.11:13306' is already ready for InnoDB cluster usage.
# 建立一個 cluster,命名爲 'myCluster'
MySQL 192.169.106.11:13306 JS > var cluster = dba.createCluster('myCluster');
A new InnoDB cluster will be created on instance '192.169.106.11:13306'.
Validating instance at 192.169.106.11:13306...
This instance reports its own address as host-192-169-106-11:13306
Adding Seed Instance...
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.
如上的信息, 若是建立成功, 則會輸出的信息中會有相似「Cluster successfully created.」的語句
#建立成功後,查看cluster狀態
mysql-js> cluster.status();
{
"clusterName": "myCluster",
"defaultReplicaSet": {
"name": "default",
"primary": "192.169.106.11:3306",
"ssl": "DISABLED",
"status": "OK",
"statusText": "Cluster is ONLINE and can tolerate up to ONE failure.",
"topology": {
"192.169.106.11:3306": {
"address": "192.169.106.11:3306",
"mode": "R/W",
"readReplicas": {},
"role": "HA",
"status": "ONLINE"
}
}
}
mysql-js> dba.getCluster();
<Cluster:myCluster>
複製代碼
經過host-192-169-106-12本機 mysql-shell 對 mysql 進行配置
[root@host-192-169-106-12 ~]# mysqlsh
................
mysql-js> shell.connect('root@192.169.106.12:3306')
Creating a session to 'root@192.169.106.12:3306'
Please provide the password for 'root@192.169.106.12:3306': *********
Save password for 'root@192.169.106.12:3306'? [Y]es/[N]o/Ne[v]er (default No): yes
Fetching schema names for autocompletion... Press ^C to stop.
Your MySQL connection id is 1424
Server version: 5.7.27-log MySQL Community Server (GPL)
No default schema selected; type \use <schema> to set one.
<ClassicSession:root@192.169.106.12:3306>
MySQL 192.169.106.12:3306 JS > dba.configureLocalInstance();
Configuring local MySQL instance listening at port 3306 for use in an InnoDB cluster...
This instance reports its own address as host-192-169-106-12:3306
Clients and other cluster members will communicate with it through this address by default. If this is not correct, the report_host MySQL system variable should be changed.
The instance '192.169.106.12:3306' is valid for InnoDB cluster usage.
The instance '192.169.106.12:3306' is already ready for InnoDB cluster usage.
接着修改 my.cnf,添加配置項:
[root@host-192-169-106-12 ~]# vim /etc/my.cnf
............
loose-group_replication_allow_local_disjoint_gtids_join=on
重啓mysql服務
[root@host-192-169-106-12 ~]# systemctl restart mysqld
而後經過 host-192-169-106-11 節點 的 mysql-shell 添加 192.169.106.12 到 "myCluster"集羣中
接着上面的host-192-169-106-11的mysql-shell終端窗口 (注意這個終端窗口是上面執行後, 沒有關閉一直開着的)
mysql-js> cluster.addInstance('root@192.169.106.12:3306');
A new instance will be added to the InnoDB cluster. Depending on the amount of
data on the cluster this might take from a few seconds to several hours.
Please provide the password for 'root@192.169.106.12:3306':
Adding instance to the cluster ...
The instance 'root@192.169.106.12:3306' was successfully added to the cluster.
上面信息表示192.169.106.12節點已經成功添加到"myCluster"集羣中了. 以下查看集羣狀態
mysql-js> cluster.status();
{
"clusterName": "myCluster",
"defaultReplicaSet": {
"name": "default",
"primary": "192.169.106.11:3306",
"ssl": "DISABLED",
"status": "OK",
"statusText": "Cluster is ONLINE and can tolerate up to ONE failure.",
"topology": {
"192.169.106.11:3306": {
"address": "192.169.106.11:3306",
"mode": "R/W",
"readReplicas": {},
"role": "HA",
"status": "ONLINE"
},
"192.169.106.12:3306": {
"address": "192.169.106.12:3306",
"mode": "R/O",
"readReplicas": {},
"role": "HA",
"status": "ONLINE"
}
}
}
}
一樣, 上面操做後, 這個192.169.106.11節點的mysql-shell當前終端窗口不要關閉,繼續保持在集羣狀態中, 下面添加192.169.106.13節點到集羣中會用到這裏.(後面經常使用命令中會提到)
複製代碼
經過host-192-169-106-13本機 mysql-shell 對 mysql 進行配置
[root@host-192-169-106-13 ~]# mysqlsh
................
mysql-js> shell.connect('root@192.169.106.13:3306')
Creating a session to 'root@192.169.106.13:3306'
Please provide the password for 'root@192.169.106.13:3306': *********
Save password for 'root@192.169.106.13:3306'? [Y]es/[N]o/Ne[v]er (default No): yes
Fetching schema names for autocompletion... Press ^C to stop.
Your MySQL connection id is 1424
Server version: 5.7.27-log MySQL Community Server (GPL)
No default schema selected; type \use <schema> to set one.
<ClassicSession:root@192.169.106.13:3306>
MySQL 192.169.106.13:3306 JS > dba.configureLocalInstance();
Configuring local MySQL instance listening at port 3306 for use in an InnoDB cluster...
This instance reports its own address as host-192-169-106-13:3306
Clients and other cluster members will communicate with it through this address by default. If this is not correct, the report_host MySQL system variable should be changed.
The instance '192.169.106.13:3306' is valid for InnoDB cluster usage.
The instance '192.169.106.13:3306' is already ready for InnoDB cluster usage.
接着修改 my.cnf,添加配置項:
[root@host-192-169-106-13 ~]# vim /etc/my.cnf
............
loose-group_replication_allow_local_disjoint_gtids_join=on
重啓mysql服務
[root@host-192-169-106-13 ~]# systemctl restart mysqld
而後經過 host-192-169-106-11 節點 的 mysql-shell 添加 192.169.106.13 到 "myCluster"集羣中
接着上面的host-192-169-106-11的mysql-shell終端窗口 (注意這個終端窗口是上面執行後, 沒有關閉一直開着的)
mysql-js> cluster.addInstance('root@192.169.106.13:3306');
A new instance will be added to the InnoDB cluster. Depending on the amount of
data on the cluster this might take from a few seconds to several hours.
Please provide the password for 'root@192.169.106.13:3306':
Adding instance to the cluster ...
The instance 'root@192.169.106.13:3306' was successfully added to the cluster.
上面信息表示192.169.106.13節點已經成功添加到"myCluster"集羣中了. 以下查看集羣狀態
mysql-js> cluster.status();
{
"clusterName": "myCluster",
"defaultReplicaSet": {
"name": "default",
"primary": "192.169.106.11:3306",
"ssl": "DISABLED",
"status": "OK",
"statusText": "Cluster is ONLINE and can tolerate up to ONE failure.",
"topology": {
"192.169.106.11:3306": {
"address": "192.169.106.11:3306",
"mode": "R/W",
"readReplicas": {},
"role": "HA",
"status": "ONLINE"
},
"192.169.106.12:3306": {
"address": "192.169.106.12:3306",
"mode": "R/O",
"readReplicas": {},
"role": "HA",
"status": "ONLINE"
},
"192.169.106.13:3306": {
"address": "192.169.106.13:3306",
"mode": "R/O",
"readReplicas": {},
"role": "HA",
"status": "ONLINE"
}
},
"topologyMode": "Single-Primary"
},
"groupInformationSourceMember": "192.169.106.11:3306"
}
經過上面cluster集羣信息可知, 192.169.106.11節點是主節點, 具備R/W讀寫權限, 其餘兩個節點是從節點, 具備R/O 只讀權限
複製代碼
進入 192.169.106.11管理節點中mysql-router 安裝目錄,配置並啓動 router
[root@host-192-169-106-11 ~]# ./bin/mysqlrouter --bootstrap root@192.169.106.11:3306 -d myrouter --user=root
Please enter MySQL password for root:
Bootstrapping MySQL Router instance at /root/myrouter...
MySQL Router has now been configured for the InnoDB cluster 'myCluster'.
The following connection information can be used to connect to the cluster.
Classic MySQL protocol connections to cluster 'myCluster':
- Read/Write Connections: localhost:6446 #讀寫端口
- Read/Only Connections: localhost:6447 #只讀端口
X protocol connections to cluster 'myCluster':
- Read/Write Connections: localhost:64460
- Read/Only Connections: localhost:64470
這裏會在當前目錄下產生mysql-router 目錄, 並生成router配置文件,接着把配置文件修改一下:
[root@host-192-169-106-11 ~]# ls /root/myrouter/
data log mysqlrouter.conf mysqlrouter.key run start.sh stop.sh
[root@host-192-169-106-11 ~]# cat /root/myrouter/mysqlrouter.conf #能夠修改配置文件, 也能夠默認不修改
默認經過route鏈接mysql後, 6446端口鏈接後能夠進行讀寫操做. 6447端口鏈接後只能進行只讀操做.
而後啓動mysqlroute
[root@host-192-169-106-11 ~]# /root/myrouter/start.sh
PID 28505 written to /root/myrouter/mysqlrouter.pid
[root@host-192-169-106-11 ~]# ps -ef|grep myroute
[root@host-192-169-106-11 ~]# ps -ef|grep myroute
root 7827 7755 0 10:49 pts/0 00:00:00 grep --color=auto myroute
root 28505 1 0 03:55 ? 00:00:55 /usr/local/mysql-router/bin/mysqlrouter -c /usr/local/mysql-router/myrouter/mysqlrouter.conf
[root@host-192-169-106-11 ~]# netstat -tunlp|grep 28505
tcp 0 0 0.0.0.0:64460 0.0.0.0:* LISTEN 28505/mysqlrouter
tcp 0 0 0.0.0.0:6446 0.0.0.0:* LISTEN 28505/mysqlrouter
tcp 0 0 0.0.0.0:6447 0.0.0.0:* LISTEN 28505/mysqlrouter
tcp 0 0 0.0.0.0:64470 0.0.0.0:* LISTEN 28505/mysqlrouter
這樣就可使用MySQL客戶端鏈接router了. 下面驗證下鏈接router:
a) 管理節點本機mysql-shell鏈接:
[root@host-192-169-106-11 ~]# mysqlsh --uri root@localhost:6446
b) 管理節點本機mysql鏈接:
[root@host-192-169-106-11 ~]# mysql -u root -h 127.0.0.1 -P 6446 -p
c) 遠程客戶機經過route鏈接mysql
[root@host-192-169-106-11 ~]# mysql -u root -h 192.169.106.11 -P 6446 -p
測試cluster節點數據同步. 這裏選擇host-192-169-106-13節點做爲遠程客戶端鏈接router
[root@host-192-169-106-13 ~]# mysql -u root -h 192.169.106.13 -P 6446 -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1054
Server version: 5.7.25-log MySQL Community Server (GPL)
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+-------------------------------+
| Database |
+-------------------------------+
| information_schema |
| mysql |
| mysql_innodb_cluster_metadata |
| performance_schema |
| sys |
+-------------------------------+
5 rows in set (0.00 sec)
測試測試庫kevin
mysql> CREATE DATABASE test CHARACTER SET utf8 COLLATE utf8_general_ci;
ERROR 1044 (42000): Access denied for user 'root'@'%' to database 'test'
這是由於'root@%'沒有建立庫的權限
mysql> select host,user from mysql.user;
+-----------+----------------------------------+
| host | user |
+-----------+----------------------------------+
| % | mysql_innodb_cluster_rp496261783 |
| % | mysql_innodb_cluster_rp496457975 |
| % | mysql_innodb_cluster_rp496569258 |
| % | mysql_innodb_cluster_rp496629685 |
| % | mysql_router1_olzau3ltjqzx |
| % | root |
| localhost | mysql.session |
| localhost | mysql.sys |
| localhost | root |
+-----------+----------------------------------+
9 rows in set (0.00 sec)
mysql> show grants for root@'%';
+-----------------------------------------------------------------------------------------------------------------------------------------+
| Grants for root@% |
+-----------------------------------------------------------------------------------------------------------------------------------------+
| GRANT RELOAD, SHUTDOWN, PROCESS, FILE, SUPER, REPLICATION SLAVE, REPLICATION CLIENT, CREATE USER ON *.* TO 'root'@'%' WITH GRANT OPTION |
| GRANT SELECT, INSERT, UPDATE, DELETE ON `mysql`.* TO 'root'@'%' WITH GRANT OPTION |
| GRANT SELECT ON `performance_schema`.* TO 'root'@'%' WITH GRANT OPTION |
| GRANT ALL PRIVILEGES ON `mysql_innodb_cluster_metadata`.* TO 'root'@'%' WITH GRANT OPTION |
+-----------------------------------------------------------------------------------------------------------------------------------------+
4 rows in set (0.00 sec)
登陸主庫, 建立一個具備管理權權限的用戶
[root@host-192-169-106-11 ~]# mysql -psagis@123
.............
mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)
mysql> set global validate_password_length=1;
Query OK, 0 rows affected (0.00 sec)
mysql> grant all on *.* to hjh@'%' identified by "hjh@123" with grant option;
Query OK, 0 rows affected, 1 warning (0.05 sec)
接着遠程使用上面建立的新帳號登陸router操做
[root@host-192-169-106-13 ~]# mysql -u bobo -h 192.169.106.13 -P 6446 -p
........
mysql> show grants for hjh@'%';
+-------------------------------------------------------------+
| Grants for hjh@% |
+-------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'hjh'@'%' WITH GRANT OPTION |
+-------------------------------------------------------------+
1 row in set (0.00 sec)
測試測試庫test
mysql> CREATE DATABASE test CHARACTER SET utf8 COLLATE utf8_general_ci;
Query OK, 1 row affected (0.06 sec)
mysql> use test;
Database changed
mysql> create table if not exists test (id int(10) PRIMARY KEY AUTO_INCREMENT,name varchar(50) NOT NULL);
Query OK, 0 rows affected (0.22 sec)
mysql> insert into kevin.haha values(1,"hejianhui");
Query OK, 1 rows affected (0.13 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> select * from test.test;
+----+-----------+
| id | name |
+----+-----------+
| 1 | hejianhui |
+----+-----------+
4 rows in set (0.00 sec)
分別登陸三個cluster節點的mysql, 發現測試庫test已經完成同步了, 其中:
寫操做的數據會先寫到192.169.106.11節點, 而後同步到192.169.106.12和192.169.106.13只讀節點上.
注意: 上面使用6446端口鏈接的route, 能夠進行讀寫操做. 可是使用6447端口鏈接後, 就只能進行只讀操做了. 登陸後能夠執行" select @@hostname" 查看登陸到哪一個節點上.
[root@host-192-169-106-13 ~]# mysql -u hjh -h 192.169.106.13 -P 6447 -p
.............
mysql> select * from kevin.haha;
+----+-----------+
| id | name |
+----+-----------+
| 1 | hejianhui |
+----+-----------+
1 rows in set (0.00 sec)
mysql> delete from test.test where id=1;
ERROR 1290 (HY000): The MySQL server is running with the --super-read-only option so it cannot execute this statement
複製代碼
此外, 還能夠利用keepalived實現InnoDB Cluster的高可用, 即兩臺db-route管理節點, 經過VIP資源實現故障無感知切換. 這樣須要準備5臺節點, 其中3個cluster節點(安裝mysql, mysql-shell), 2個route管理節點(安裝keepalived, mysql-shell, mysql-route, mysql-client)mysql
dba.checkInstanceConfiguration("root@192.169.106.11:3306");
複製代碼
mysql-js> shell.connect("root@192.169.106.11:3306");
Please provide the password for 'root@192.169.106.11:3306':
Creating a Session to 'root@192.169.106.11:3306'
Classic Session successfully established. No default schema selected.
查看集羣狀態
mysql-js> cluster.status();
ReferenceError: cluster is not defined
上面方式查看, 會報錯說集羣沒有定義, 這時須要先執行下面這條語句以後,纔看查看到集羣狀態!!!!!
mysql-js> cluster.status();
ReferenceError: cluster is not defined
而後就能夠查看集羣狀態了
mysql-js> cluster=dba.getCluster();
<Cluster:myCluster>
mysql-js> cluster.status();
查看已建立的集羣名稱
MySQL 172.16.60.214:6446 ssl JS > dba.getCluster();
<Cluster:myCluster>
=================================================
總結:
a) dba.getCluster(); #查看建立的集羣
b) cluster=dba.getCluster(); #獲取當前集羣
c) cluster.status(); #查看集羣狀態
複製代碼
dba.help();
複製代碼
dba.checkInstanceConfiguration("root@hostname:3306") #檢查節點配置實例,用於加入cluster以前
dba.rebootClusterFromCompleteOutage('myCluster'); #重啓
dba.dropMetadataSchema(); #刪除schema
var cluster = dba.getCluster('myCluster') #獲取當前集羣
cluster.checkInstanceState("root@hostname:3306") #檢查cluster裏節點狀態
cluster.rejoinInstance("root@hostname:3306") #從新加入節點,我本地測試的時候發現rejoin一直無效,每次是delete後
addcluster.dissolve({force:true}) #刪除集羣
cluster.addInstance("root@hostname:3306") #增長節點
cluster.removeInstance("root@hostname:3306") #刪除節點
cluster.removeInstance('root@host:3306',{force:true}) #強制刪除節點
cluster.dissolve({force:true}) #解散集羣
cluster.describe(); #集羣描述
集羣節點狀態
- ONLINE: The instance is online and participating in the cluster.
- OFFLINE: The instance has lost connection to the other instances.
- RECOVERING: The instance is attempting to synchronize with the cluster by retrieving transactions it needs before it can become an ONLINE member.
- UNREACHABLE: The instance has lost communication with the cluster.
- ERROR: The instance has encountered an error during the recovery phase or while applying a transaction
複製代碼