大家好,我是 JiekeXu,很高兴又和大家见面了,今天和大家一起来手把手教你安装 MySQL InnoDB Cluster 8.0.34,欢迎点击上方蓝字“JiekeXu DBA之路”关注我的公众号,标星或置顶,更多干货第一时间到达!
MySQL InnoDB Cluster 提供完整的高可用性 MySQL 解决方案。通过使用 MySQL Shell 附带的 AdminAPI,您可以轻松配置和管理至少三个 MySQL 服务器的组实例以充当 InnoDB 集群。
InnoDB Cluster 主要使用如下三大主件: ● MySQL Shell ● MySQL服务器和组复制 ● MySQL Router
InnoDB 集群中的每个 MySQL 服务器实例都运行 MySQL Group Replication,它提供了在 InnoDB 集群内复制数据的机制,并具有内置的故障切换。Admin API 消除了在 InnoDB 集群中直接使用 Group Replication 的需要,但有关更多信息,请参阅解释详细信息的 Group Replication。从 MySQL 8.0.27 开始,您还可以设置 InnoDB ClusterSet,通过将主 InnoDB 集群与其他位置(如不同的数据中心)的一个或多个副本链接,为 InnoDB 集群部署提供容灾能力。
MySQL Router 可以根据您部署的集群自动配置,将客户端应用程序透明地连接到服务器实例。在服务器实例发生意外故障的情况下,群集会自动重新配置。在默认的单主模式中,InnoDB 集群有一个读写服务器实例-主实例。多个辅助服务器实例是主服务器的副本。如果主服务器出现故障,辅助服务器将自动升级为主服务器。MySQL Router 检测到这一点,并将客户端应用程序转发到新的主应用程序。
如下是官方 MySQL InnoDB Cluster 架构简图:
下图是我三台 Linux7 部署架构图,IP 分别为 56、57、58,56 Node1 为主节点,并安装 MySQL 和 router,57 Node2、58 Node3 为从节点,也安装 MySQL 和 router,并在 Node3 安装了 shell。Node3 的 shell 用来安装 InnoDB Cluster,其他每个节点的 router 地址则作为高可用地址分配给 K8S 应用容器。当然官方建议将 router 和应用容器一起部署,我这么部署和使用也没有太大的问题。
下面开始使用最传统的方式从零开始安装 MySQL InnoDB Cluster 8.0.34 单主模式,Linux X86 操作系统已安装好,并且 yum 源配置完成,1T /data 文件系统已挂载,所需软件包已下载并上传到服务器。也可参考去年的主从搭建文档----Linux 环境搭建 MySQL8.0.28 主从同步环境。
代码语言:javascript复制--所需的软件包
https://dev.mysql.com/downloads/mysql/8.0.html
mysql-8.0.34-linux-glibc2.12-x86_64.tar.xz
mysql-shell-8.0.34-1.el7.x86_64.rpm
mysql-router-8.0.34-linux-glibc2.12-x86_64.tar.xz
代码语言:javascript复制
一、系统准备
以下 6 小节操作需要在三台主机执行。
1、centos7 关闭防火墙
代码语言:javascript复制systemctl stop firewalld.service 或者 systemctl stop firewalld
systemctl disable firewalld.service 或者 systemctl disable firewalld
systemctl status firewalld
代码语言:javascript复制
2、关闭 selinux
代码语言:javascript复制getenforce
setenforce 0
vim /etc/selinux/config
SELINUX=disabled
代码语言:javascript复制
3、绑定 /etc/hosts 解析(示例如下)
代码语言:javascript复制vim /etc/hosts
192.168.27.56 jiekexu-my-56
192.168.27.57 jiekexu-my-57
192.168.27.58 jiekexu-my-58
代码语言:javascript复制
4、安装 yum 包
代码语言:javascript复制
代码语言:javascript复制yum -y groupinstall "DeveLopment tools"
yum -y install ncurses ncurses-devel openssl-devel bison gcc gcc-c make
代码语言:javascript复制5、清理系统自带 MariaDB 环境
CentOS 7 版本的系统默认自带安装了 MariaDB,需要先清理。
代码语言:javascript复制## 查询已安装的mariadb
rpm -qa |grep mariadb
或
yum list installed | grep mariadb
## 卸载 mariadb 包,文件名为上述命令查询出来的文件
rpm -e --nodeps mariadb-libs-5.5.68-1.el7.x86_64
yum -y remove mariadb-libs.x86_64
代码语言:javascript复制
6、内核参数修改
代码语言:javascript复制[root@jiekexu-my-56 ~]# 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@jiekexu-my-56 ~]# sysctl -p
[root@jiekexu-my-56 ~]# cat>>/etc/security/limits.conf <<EOF
mysql soft nproc 65536
mysql hard nproc 65536
mysql soft nofile 65536
mysql hard nofile 65536
EOF
[root@jiekexu-my-56 ~]# cat>>/etc/pam.d/login <<EOF
session required /lib/security/pam_limits.so
session required pam_limits.so
EOF
[root@jiekexu-my-56 ~]# cat>>/etc/profile<<EOF
if [ $USER = "mysql" ]; then
ulimit -u 16384 -n 65536
fi
EOF
代码语言:javascript复制
二、安装 MySQL Server
1、三台主机创建数据库用户,创建实例所需目录
如果没有 /data 文件系统,可按照下面目录结构创建相对应的目录。 root 用户操作:
代码语言:javascript复制mkdir -p /app/
mkdir -p /data/mysqldb/conf/
mkdir -p /data/mysqldb/data/
mkdir -p /data/mysqldb/pid/
mkdir -p /data/mysqldb/socket/
mkdir -p /data/mysqldb/log/
mkdir -p /data/mysqldb/binlog/
mkdir -p /data/mysqldb/relaylog/
mkdir -p /data/mysqldb/slowlog/
mkdir -p /data/mysqldb/tmp/
groupadd mysql
useradd -g mysql mysql
chown -R mysql:mysql /data
chown -R mysql:mysql /app
passwd mysql
--MySQL
代码语言:javascript复制
2、三台主机解压 MySQL server 安装程序包
代码语言:javascript复制mysql 用户操作
cd /app
tar xvf mysql-8.0.34-linux-glibc2.12-x86_64.tar.xz
mv mysql-8.0.34-linux-glibc2.12-x86_64 mysql8.0.34
3、三台主机配置 mysql 用户环境变量
代码语言:javascript复制vim ~/.bash_profile
MYSQL_HOME=/app/mysql8.0.34
PATH=$PATH:$HOME/.local/bin:$HOME/bin:$MYSQL_HOME/bin
source ~/.bash_profile
which mysql
代码语言:javascript复制
4、上传 mysql 初始化参数文件并初始化(参数文件名 mysql.conf)
Node1 参数文件如下,Node2 和 Node3 节点需要注意server_id 不能重复,innodb_buffer_pool_size 内存一般设置为 OS 的 50%,report_host 写本机地址,loose_group_replication_local_address 写本机地址和端口号。
代码语言:javascript复制vim /data/mysqldb/conf/mysql.conf
[mysqld]
# basic settings #
default-storage-engine= InnoDB
server_id = 2756
basedir = /app/mysql8.0.34
datadir = /data/mysqldb/data/
socket = /data/mysqldb/socket/mysql.sock
pid_file = /data/mysqldb/pid/mysqld.pid
port = 33066
default-time_zone = ' 8:00'
character_set_server = utf8mb4
explicit_defaults_for_timestamp = 1
autocommit = 1
transaction_isolation = READ-COMMITTED
secure_file_priv = "/data/mysqldb/tmp/"
max_allowed_packet = 64M
lower_case_table_names = 1
default_authentication_plugin = mysql_native_password
sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'
# connection #
back_log = 500
interactive_timeout = 300
wait_timeout = 1800
lock_wait_timeout = 300
max_user_connections = 800
skip_name_resolve = 1
max_connections = 2500
max_connect_errors = 1000
#table cache performance settings
#table_open_cache = 1024
#table_definition_cache = 1024
#table_open_cache_instances = 16
#session memory settings #
#read_buffer_size = 16M
#read_rnd_buffer_size = 32M
#sort_buffer_size = 32M
#tmp_table_size = 64M
#join_buffer_size = 128M
#thread_cache_size = 256
# log settings #
slow_query_log = ON
slow_query_log_file = /data/mysqldb/slowlog/slow.log
log_error = /data/mysqldb/log/mysqld.log
log_error_verbosity = 3
log_bin = /data/mysqldb/binlog/mysql_bin
log_bin_index = /data/mysqldb/binlog/mysql_binlog.index
# general_log_file = /data/mysql/generallog/general.log
log_queries_not_using_indexes = 1
log_slow_admin_statements = 1
#log_slow_slave_statements = 1
#expire_logs_days = 15
binlog_expire_logs_seconds = 2592000
long_query_time = 5
min_examined_row_limit = 100
log_throttle_queries_not_using_indexes = 1000
#log_bin_trust_function_creators = 1
log_replica_updates = 1
mysqlx_port = 33060
mysqlx_socket = /data/mysqldb/socket/mysqlx.sock
# innodb settings #
innodb_buffer_pool_size = 16G
innodb_buffer_pool_instances = 4
innodb_log_buffer_size = 100M
innodb_buffer_pool_load_at_startup = 1
innodb_buffer_pool_dump_at_shutdown = 1
innodb_lru_scan_depth = 4096
innodb_lock_wait_timeout = 20
innodb_io_capacity = 5000
innodb_io_capacity_max = 10000
innodb_flush_method = O_DIRECT
innodb_log_file_size = 1G
innodb_log_files_in_group = 3
innodb_purge_threads = 4
innodb_thread_concurrency = 200
innodb_print_all_deadlocks = 1
innodb_print_ddl_logs = 1
innodb_strict_mode = 1
innodb_sort_buffer_size = 32M
innodb_write_io_threads = 16
innodb_read_io_threads = 16
innodb_file_per_table = 1
innodb_stats_persistent_sample_pages = 64
innodb_autoinc_lock_mode = 2
innodb_online_alter_log_max_size = 1G
innodb_open_files = 4096
innodb_buffer_pool_dump_pct = 25
innodb_page_cleaners = 16
innodb_undo_log_truncate = 1
innodb_max_undo_log_size = 2G
innodb_purge_rseg_truncate_frequency = 128
innodb_flush_log_at_trx_commit = 1
log_timestamps = UTC
# replication settings #
sync_binlog = 1
binlog_format = ROW
gtid_mode = ON
enforce_gtid_consistency = ON
relay_log_recovery = 1
relay_log = /data/mysqldb/relaylog/relay.log
relay_log_index = /data/mysqldb/relaylog/mysql_relay.index
replica_parallel_type = LOGICAL_CLOCK
replica_parallel_workers = 16
binlog_gtid_simple_recovery = 1
replica_preserve_commit_order = 1
binlog_rows_query_log_events = 1
replica_transaction_retries = 10
# group replication settings
transaction_write_set_extraction = XXHASH64
binlog_checksum = NONE
loose_group_replication_group_name = "957e8af0-bc63-11ea-bb19-005056a52572"
loose_group_replication_start_on_boot = off
loose_group_replication_local_address = "192.168.27.56:24901"
loose_group_replication_group_seeds = "192.168.27.56:24901,192.168.27.57:24901,192.168.27.58:24901"
loose_group_replication_bootstrap_group = off
loose_group_replication_ip_whitelist = "192.168.27.56/24"
loose_group_replication_member_expel_timeout=30
report_host = 192.168.27.56
report_port = 33066
#read_only=1
#super_read_only=1
binlog_transaction_dependency_tracking = WRITESET
代码语言:javascript复制
Node 2 不同的部分参数
代码语言:javascript复制server_id = 2757
report_host = 192.168.27.57
loose_group_replication_ip_whitelist = "192.168.27.57/24"
代码语言:javascript复制
Node 3 不同的部分参数
代码语言:javascript复制server_id = 2758
report_host = 192.168.27.58
loose_group_replication_ip_whitelist = "192.168.27.58/24"
代码语言:javascript复制
mysql 用户分别初始化三台主机操作
代码语言:javascript复制
代码语言:javascript复制mysqld --defaults-file=/data/mysqldb/conf/mysql.conf --initialize --user=mysql --basedir=/app/mysql8.0.34 --datadir=/data/mysqldb/data
代码语言:javascript复制等待两分钟,查看每台主机初始化日志是否出现错误。
代码语言:javascript复制cat /data/mysqldb/log/mysqld.log
代码语言:javascript复制
5、启动及停止命令
代码语言:javascript复制分别启动三个节点 mysql server
代码语言:javascript复制mysqld_safe --defaults-file=/data/mysqldb/conf/mysql.conf --user=mysql &
-----如下关闭命令,本次不需要执行
mysqladmin -uroot -proot123 -S /data/mysqldb/socket/mysql.sock shutdown
代码语言:javascript复制6、修改 root@localhost 用户密码
root 密码可以根据需求自己修改,生产环境需使用 8 位以上的强密码。 mysql 用户查看日志中的密码分别登录进去修改 root@localhost 用户密码。
代码语言:javascript复制--默认密码在错误日志里
more /data/mysqldb/log/mysqld.log|grep password
mysql -uroot -p -P 33066 -S /data/mysqldb/socket/mysql.sock
alter user root@'localhost' identified by 'root@123';
reset master;
代码语言:javascript复制
三、配置 MGR 组复制
1、所有节点安装组复制插件
SQL 命令
代码语言:javascript复制
代码语言:javascript复制mysql> INSTALL PLUGIN group_replication SONAME 'group_replication.so';
代码语言:javascript复制或者写入到配置文件 plugin-load = group_replication.so
查看所有插件,注意主复制插件是否为 active。
代码语言:javascript复制show plugins;
代码语言:javascript复制
2、在 Node1 节点上创建复制用户和配置恢复通道
代码语言:javascript复制
代码语言:javascript复制SET SQL_LOG_BIN=0;
CREATE USER rep@'%' IDENTIFIED BY 'Rep1Yh6t';
GRANT REPLICATION SLAVE ON *.* TO rep@'%';
SET SQL_LOG_BIN=1;
CHANGE MASTER TO MASTER_USER='rep', MASTER_PASSWORD='Rep1Yh6t' FOR CHANNEL 'group_replication_recovery';
使用 SET SQL_LOG_BIN=0; 来保证创建用户的操作不记录到 binlog 中,这么做可以避免其他服务加入时报事务冲突错误。
3、在 Node1 节点启动 MGR(只需在第一个节点执行即可)
代码语言:javascript复制SET GLOBAL group_replication_bootstrap_group=ON;
START GROUP_REPLICATION;
SET GLOBAL group_replication_bootstrap_group=OFF;
代码语言:javascript复制
group_replication_bootstrap_group 参数设置为 ON,是为了表示以后加入集群的服务器都以这台服务器为基准。以后加入的就不需要再设置此参数。
代码语言:javascript复制
代码语言:javascript复制--查询当前组成员及状态
mysql> select * from performance_schema.replication_group_members;
--------------------------- -------------------------------------- ------------- ------------- -------------- ------------- ---------------- ----------------------------
| CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE | MEMBER_ROLE | MEMBER_VERSION | MEMBER_COMMUNICATION_STACK |
--------------------------- -------------------------------------- ------------- ------------- -------------- ------------- ---------------- ----------------------------
| group_replication_applier | b9f52d68-52e2-11ee-953c-fa163e0e1ce8 |192.168.27.56 | 33066 | ONLINE | PRIMARY | 8.0.34 | XCom |
--------------------------- -------------------------------------- ------------- ------------- -------------- ------------- ---------------- ----------------------------
1 rows in set (0.00 sec)
代码语言:javascript复制4、其他节点加入组复制(其他节点执行)
代码语言:javascript复制
代码语言:javascript复制Node2 和 Node3 执行
先执行 reset master
SET SQL_LOG_BIN=0;
CREATE USER rep@'%' IDENTIFIED BY 'Rep1Yh6t';
GRANT REPLICATION SLAVE ON *.* TO rep@'%';
SET SQL_LOG_BIN=1;
CHANGE MASTER TO MASTER_USER='rep', MASTER_PASSWORD='Rep1Yh6t' FOR CHANNEL 'group_replication_recovery';
START GROUP_REPLICATION;
代码语言:javascript复制5、启动完成之后在看 Group 信息
代码语言:javascript复制SELECT * FROM performance_schema.replication_group_members;
mysql> SELECT * FROM performance_schema.replication_group_members;
mysql> select * from performance_schema.replication_group_member_statsG;
mysql> select * from performance_schema.replication_group_members;
--------------------------- -------------------------------------- ------------- ------------- -------------- ------------- ---------------- ----------------------------
| CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE | MEMBER_ROLE | MEMBER_VERSION | MEMBER_COMMUNICATION_STACK |
--------------------------- -------------------------------------- ------------- ------------- -------------- ------------- ---------------- ----------------------------
| group_replication_applier | b9f52d68-52e2-11ee-953c-fa163e0e1ce8 |192.168.27.56 | 33066 | ONLINE | PRIMARY | 8.0.34 | XCom |
| group_replication_applier | eaae5cec-52e2-11ee-a03c-fa163e0e1c84 |192.168.27.57 | 33066 | ONLINE | SECONDARY | 8.0.34 | XCom |
| group_replication_applier | eea965a9-52e2-11ee-8027-fa163e0e1c05 |192.168.27.58 | 33066 | ONLINE | SECONDARY | 8.0.34 | XCom |
--------------------------- -------------------------------------- ------------- ------------- -------------- ------------- ---------------- ----------------------------
3 rows in set (0.00 sec)
6、连接 Node1 主库创建 root@’%’ 用户
节点1
代码语言:javascript复制mysql -uroot -p -P 33066 -S /data/mysqldb/socket/mysql.sock
create user root@'%' identified by 'root@123';
grant all privileges on *.* to root@'%' with grant option;
flush privileges;
代码语言:javascript复制
=================================== 部署多实例需要添加如下两个参数(否则端口冲突)
代码语言:javascript复制mysqlx_port
mysqlx_socket
节点加入集群报错时,节点需要设置如下参数(使用默认密码插件时需要执行)
代码语言:javascript复制set global group_replication_recovery_get_public_key=on;
===========================
四、安装 MySQL Shell
初始化实例及创建纳管库,库名 mysql_innodb_cluster_metadata,只需要在一台中间件服务器安装即可,这里选择 Node3。
1、root 用户安装 MySQL Shell
代码语言:javascript复制# rpm -ivh mysql-shell-8.0.34-1.el7.x86_64.rpm
--这里下载的是 rpm 包,如果你下载的是二进制版本,可直接解压配置环境变量即可。
# tar -zxvf mysql-shell-8.0.34-linux-glibc2.12-x86-64bit.tar.gz
# ln -s mysql-shell-8.0.34-linux-glibc2.12-x86-64bit mysql-shell
# export PATH=$PATH:/app/mysql-shell/bin
代码语言:javascript复制
2、连接数据库,添加纳管
在第一次连接,创建纳管时,需要从 primary 连接进去。否则会破坏 read_only(root 用户会破坏 super_read_only)。
代码语言:javascript复制mysqlsh root@192.168.27.56:33066 --py
代码语言:javascript复制
添加纳管
代码语言:javascript复制dba.create_cluster(‘mysqlrouter’)
代码语言:javascript复制
代码语言:javascript复制MySQL 192.168.27.56:33066 ssl Py > dba.create_cluster('mysqlrouter')
A new InnoDB Cluster will be created on instance '192.168.27.56:33066'.
You are connected to an instance that belongs to an unmanaged replication group.
Do you want to setup an InnoDB Cluster based on this replication group? [Y/n]: Y
Creating InnoDB Cluster 'mysqlrouter' on '192.168.27.56:33066'...
Adding Seed Instance...
Adding Instance '192.168.27.56:33066'...
Adding Instance '192.168.27.57:33066'...
Adding Instance '192.168.27.58:33066'...
Resetting distributed recovery credentials across the cluster...
Cluster successfully created based on existing replication group.
<Cluster:mysqlrouter>
代码语言:javascript复制
3、查看集群状态
代码语言:javascript复制c=dba.get_cluster()
c.status()
代码语言:javascript复制
代码语言:javascript复制MySQL 192.168.27.56:33066 ssl Py > c.status()
{
"clusterName": "mysqlrouter",
"defaultReplicaSet": {
"name": "default",
"primary": "192.168.27.56:33066",
"ssl": "DISABLED",
"status": "OK",
"statusText": "Cluster is ONLINE and can tolerate up to ONE failure.",
"topology": {
"192.168.27.56:33066": {
"address": "192.168.27.56:33066",
"memberRole": "PRIMARY",
"mode": "R/W",
"readReplicas": {},
"replicationLag": "applier_queue_applied",
"role": "HA",
"status": "ONLINE",
"version": "8.0.34"
},
"192.168.27.57:33066": {
"address": "192.168.27.57:33066",
"memberRole": "SECONDARY",
"mode": "R/O",
"readReplicas": {},
"replicationLag": "applier_queue_applied",
"role": "HA",
"status": "ONLINE",
"version": "8.0.34"
},
"192.168.27.58:33066": {
"address": "192.168.27.58:33066",
"memberRole": "SECONDARY",
"mode": "R/O",
"readReplicas": {},
"replicationLag": "applier_queue_applied",
"role": "HA",
"status": "ONLINE",
"version": "8.0.34"
}
},
"topologyMode": "Single-Primary"
},
"groupInformationSourceMember": "192.168.27.56:33066"
}
MySQL 192.168.27.56:33066 ssl Py >
代码语言:javascript复制
4、MySQL Shell 的其他方法
代码语言:javascript复制dba.help()
help dba
查看方法具体使用:
dba.help('createCluster');
MySQL Shell 工具集
help util
代码语言:javascript复制
5、删除纳管
本次不需要执行,只要删除元数据数据库即可,从 primary 连接。
代码语言:javascript复制dba.drop_metadata_schema()
代码语言:javascript复制
6、查看纳管数据库
mysql_innodb_cluster_metadata
代码语言:javascript复制mysql> show databases;
-------------------------------
| Database |
-------------------------------
| information_schema |
| mysql |
| mysql_innodb_cluster_metadata |
| performance_schema |
| sys |
-------------------------------
5 rows in set (0.00 sec)
mysql> use mysql_innodb_cluster_metadata;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
-----------------------------------------
| Tables_in_mysql_innodb_cluster_metadata |
-----------------------------------------
| async_cluster_members |
| async_cluster_views |
| clusters |
| clusterset_members |
| clusterset_views |
| clustersets |
| instances |
| router_rest_accounts |
| routers |
| schema_version |
| v2_ar_clusters |
| v2_ar_members |
| v2_clusters |
| v2_cs_clustersets |
| v2_cs_members |
| v2_cs_router_options |
| v2_gr_clusters |
| v2_instances |
| v2_router_rest_accounts |
| v2_routers |
| v2_this_instance |
-----------------------------------------
21 rows in set (0.00 sec)
代码语言:javascript复制
五、MySQL Router 安装配置
1、单独三节点安装 MySQL Router
在主节点服务器配置 mysql 用户执行
代码语言:javascript复制
代码语言:javascript复制$ tar -xvf mysql-router-8.0.34-linux-glibc2.12-x86_64.tar.xz
$ mv mysql-router-8.0.34-linux-glibc2.12-x86_64 mysqlrouter
$ mkdir -p /data/mysqlrouter/mysqlrouter6446
/app/mysqlrouter/bin/mysqlrouter
2、初始化 MySQL Router
代码语言:javascript复制
代码语言:javascript复制/app/mysqlrouter/bin/mysqlrouter --bootstrap root@192.168.27.56:33066 --directory /data/mysqlrouter/mysqlrouter6446 --conf-base-port 6446 --conf-use-sockets --force
(注意:--conf-base-port 6446 这个参数是 mysqlrouter 参数 默认是 6446)
代码语言:javascript复制[mysql@jiekexu-my-56 data]$ /app/mysqlrouter/bin/mysqlrouter --bootstrap root@192.168.27.56:33066 --directory /data/mysqlrouter/mysqlrouter6446 --conf-base-port 6446 --conf-use-sockets --force
Please enter MySQL password for root:
# Bootstrapping MySQL Router instance at '/data/mysqlrouter/mysqlrouter6446'...
- Creating account(s) (only those that are needed, if any)
- Verifying account (using it to run SQL queries that would be run by Router)
- Storing account in keyring
- Adjusting permissions of generated files
- Creating configuration /data/mysqlrouter/mysqlrouter6446/mysqlrouter.conf
# MySQL Router configured for the InnoDB Cluster 'mysqlrouter'
After this MySQL Router has been started with the generated configuration
$ /app/mysqlrouter/bin/mysqlrouter -c /data/mysqlrouter/mysqlrouter6446/mysqlrouter.conf
InnoDB Cluster 'mysqlrouter' can be reached by connecting to:
## MySQL Classic protocol
- Read/Write Connections: jiekexu-my-56:6446, /data/mysqlrouter/mysqlrouter6446/mysql.sock
- Read/Only Connections: jiekexu-my-56:6447, /data/mysqlrouter/mysqlrouter6446/mysqlro.sock
## MySQL X protocol
- Read/Write Connections: jiekexu-my-56:6448, /data/mysqlrouter/mysqlrouter6446/mysqlx.sock
- Read/Only Connections: jiekexu-my-56:6449, /data/mysqlrouter/mysqlrouter6446/mysqlxro.sock
==========================================================
--./mysqlrouter_passwd set /app/percona/mysqlrouter3306/mysqlrouter.pwd rdsroot 输入 rest 认证密码(curl 命令查询 MGR 集群状态 非必须)
3、启动 MySQL Router
代码语言:javascript复制
代码语言:javascript复制[mysql@jiekexu-my-57 mysqlrouter6446]$ cd /data/mysqlrouter/mysqlrouter6446
[mysql@jiekexu-my-57 mysqlrouter6446]$ ./start.sh
[mysql@jiekexu-my-57 mysqlrouter6446]$ PID 26919 written to '/data/mysqlrouter/mysqlrouter6446/mysqlrouter.pid'
stopping to log to the console. Continuing to log to filelog
--除了上面脚本启动外,按提示中如下命令也可以启动:
/app/mysqlrouter/bin/mysqlrouter -c /data/mysqlrouter/mysqlrouter6446/mysqlrouter.conf
代码语言:javascript复制查看日志
cat mysqlrouter.log
代码语言:javascript复制
代码语言:javascript复制[mysql@jiekexu-my-56 log]$ cat mysqlrouter.log
2023-09-14 18:28:12 io INFO [7ff61678e780] starting 16 io-threads, using backend 'linux_epoll'
2023-09-14 18:28:12 http_server INFO [7ff61678e780] listening on 0.0.0.0:8443
2023-09-14 18:28:12 metadata_cache_plugin INFO [7ff5d8ff9700] Starting Metadata Cache
2023-09-14 18:28:12 metadata_cache INFO [7ff5d8ff9700] Connections using ssl_mode 'PREFERRED'
2023-09-14 18:28:12 metadata_cache INFO [7ff616702700] Starting metadata cache refresh thread
2023-09-14 18:28:12 routing INFO [7ff5937fe700] [routing:bootstrap_x_rw] started: routing strategy = first-available
2023-09-14 18:28:12 routing INFO [7ff5937fe700] [routing:bootstrap_x_rw] started: listening using /data/mysqlrouter/mysqlrouter6446/mysqlx.sock
2023-09-14 18:28:12 routing INFO [7ff5937fe700] Start accepting connections for routing routing:bootstrap_x_rw listening on 6448 and named socket /data/mysqlrouter/mysqlrouter6446/mysqlx.sock
2023-09-14 18:28:12 routing INFO [7ff598ff9700] [routing:bootstrap_rw] started: routing strategy = first-available
2023-09-14 18:28:12 routing INFO [7ff5997fa700] [routing:bootstrap_ro] started: routing strategy = round-robin-with-fallback
2023-09-14 18:28:12 routing INFO [7ff593fff700] [routing:bootstrap_x_ro] started: routing strategy = round-robin-with-fallback
2023-09-14 18:28:12 routing INFO [7ff598ff9700] [routing:bootstrap_rw] started: listening using /data/mysqlrouter/mysqlrouter6446/mysql.sock
2023-09-14 18:28:12 routing INFO [7ff5997fa700] [routing:bootstrap_ro] started: listening using /data/mysqlrouter/mysqlrouter6446/mysqlro.sock
2023-09-14 18:28:12 routing INFO [7ff598ff9700] Start accepting connections for routing routing:bootstrap_rw listening on 6446 and named socket /data/mysqlrouter/mysqlrouter6446/mysql.sock
2023-09-14 18:28:12 routing INFO [7ff5997fa700] Start accepting connections for routing routing:bootstrap_ro listening on 6447 and named socket /data/mysqlrouter/mysqlrouter6446/mysqlro.sock
2023-09-14 18:28:12 routing INFO [7ff593fff700] [routing:bootstrap_x_ro] started: listening using /data/mysqlrouter/mysqlrouter6446/mysqlxro.sock
2023-09-14 18:28:12 routing INFO [7ff593fff700] Start accepting connections for routing routing:bootstrap_x_ro listening on 6449 and named socket /data/mysqlrouter/mysqlrouter6446/mysqlxro.sock
2023-09-14 18:28:12 metadata_cache INFO [7ff616702700] Connected with metadata server running on 192.168.27.56:33066
2023-09-14 18:28:12 metadata_cache INFO [7ff616702700] Potential changes detected in cluster after metadata refresh (view_id=0)
2023-09-14 18:28:12 metadata_cache INFO [7ff616702700] Metadata for cluster 'mysqlrouter' has 3 member(s), single-primary:
2023-09-14 18:28:12 metadata_cache INFO [7ff616702700] 192.168.27.56:33066 / 33060 - mode=RW
2023-09-14 18:28:12 metadata_cache INFO [7ff616702700] 192.168.27.57:33066 / 33060 - mode=RO
2023-09-14 18:28:12 metadata_cache INFO [7ff616702700] 192.168.27.58:33066 / 33060 - mode=RO
代码语言:javascript复制4、使用 Router 登录 mysql 测试
代码语言:javascript复制
代码语言:javascript复制mysql -uroot -p -h192.168.27.56 -P 6446(6446是 mysqlrouer 读写端口)
mysql -uroot -p -h192.168.27.56 -P 6447(6447是 mysqlrouer 只读端口)
======================================
5、MySQL Router systemctl 启动配置
代码语言:javascript复制vim /etc/systemd/system/mysqlrouter@3306.service
[Unit]
Description= Mysql router
Documentation=man:mysqlrouter(8)
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=root
Group=root
Type=forking
CPUQuota=100%
TimeoutSec=30
PermissionsStartOnly=true
PIDFile=/data/mysqlrouter/mysqlrouter6446/mysqlrouter.pid
ExecStart=/usr/bin/bash -c "/data/mysqlrouter/mysqlrouter6446/start.sh &"
ExecStop=/usr/bin/bash -c "/data/mysqlrouter/mysqlrouter6446/stop.sh &"
LimitNOFILE = 102400
Restart=no
代码语言:javascript复制
6、只读参数设置
修改参数文件,将三个节点中的参数文件中关于只读的最后两行参数注释去掉。 注意:在《MySQL 实战》一书中说明单主模式下,组复制会自动将 Secondary 节点的 read_only 和 super_read_only 设置为 ON,感兴趣的小伙伴可以测试一下,我这里已经搭建完成了才看到没法测试了。
代码语言:javascript复制[mysql@jiekexu-my-56 log]$ ps -ef | grep mysql
root 11252 10549 0 17:37 pts/0 00:00:00 su - mysql
mysql 11253 11252 0 17:37 pts/0 00:00:00 -bash
mysql 11540 11253 0 17:43 pts/0 00:00:00 /bin/sh /app/mysql8.0.34/bin/mysqld_safe --defaults-file=/data/mysqldb/conf/mysql.conf --user=mysql
mysql 12786 11540 0 17:43 pts/0 00:00:26 /app/mysql8.0.34/bin/mysqld --defaults-file=/data/mysqldb/conf/mysql.conf --basedir=/app/mysql8.0.34 --datadir=/data/mysqldb/data --plugin-dir=/app/mysql8.0.34/lib/plugin --log-error=/data/mysqldb/log/mysqld.log --pid-file=/data/mysqldb/pid/mysqld.pid --socket=/data/mysqldb/socket/mysql.sock --port=33066
mysql 14279 1 0 18:28 pts/0 00:00:03 /app/mysqlrouter/bin/mysqlrouter -c /data/mysqlrouter/mysqlrouter6446/mysqlrouter.conf
mysql 14532 11253 0 18:35 pts/0 00:00:00 ps -ef
mysql 14533 11253 0 18:35 pts/0 00:00:00 grep --color=auto mysql
[mysql@jiekexu-my-56 log]$ vim /data/mysqldb/conf/mysql.conf
read_only=1
super_read_only=1
代码语言:javascript复制
然后登录两个从库,执行从库只读设置(注意只在从库执行,主库不用执行)。
代码语言:javascript复制mysql> set global read_only=on;
Query OK, 0 rows affected (0.00 sec)
mysql> set global super_read_only=on;
Query OK, 0 rows affected (0.00 sec)
mysql> show master status;
------------------ ---------- -------------- ------------------ --------------------------------------------
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
------------------ ---------- -------------- ------------------ --------------------------------------------
| mysql_bin.000001 | 397716 | | | 957e8af0-bc63-11ea-bb19-005056a52572:1-438 |
------------------ ---------- -------------- ------------------ --------------------------------------------
1 row in set (0.00 sec)
代码语言:javascript复制
7、可能遇到的问题
mysql 高版本可能存在 note 提示 可以看到,集群的状态都有一个问题:
代码语言:javascript复制
代码语言:javascript复制"instanceErrors": [
"NOTE: The required parallel-appliers settings are not enabled on the instance. Use dba.configureInstance() to fix it."
这个错误也提示了修复的方式,只要通过 mysqlshell 单独连上 MGR 中的每个实例,运行下即可修复:dba.configureInstance()
再根据提示,将 mysql 的参数修正下,或者将这个参数加到参数文件中,本文则是直接加到参数文件中了。
代码语言:javascript复制
代码语言:javascript复制set persist binlog_transaction_dependency_tracking='WRITESET';
下面是以前搭建的 MySQL 8.0.30 也是第一次遇到这个问题,特此记录一下。
代码语言:javascript复制[mysql@ts-mysql-101 ~]$ mysqlsh
MySQL Shell 8.0.30
Copyright (c) 2016, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates.
Other names may be trademarks of their respective owners.
Type 'help' or '?' for help; 'quit' to exit.
MySQL JS > shell.connect('root@192.168.221.101:33066')
Creating a session to 'root@192.168.221.101:33066'
Please provide the password for 'root@192.168.221.101:33066': *******
Save password for 'root@192.168.221.101:33066'? [Y]es/[N]o/Ne[v]er (default No): m^C
Shell.connect: Cancelled (RuntimeError)
MySQL JS > shell.connect('root@192.168.221.101:33066')
Creating a session to 'root@192.168.221.101:33066'
Please provide the password for 'root@192.168.221.101:33066': *******
Save password for 'root@192.168.221.101:33066'? [Y]es/[N]o/Ne[v]er (default No): n
Fetching schema names for autocompletion... Press ^C to stop.
Your MySQL connection id is 74
Server version: 8.0.30 MySQL Community Server - GPL
No default schema selected; type use <schema> to set one.
<ClassicSession:root@192.168.221.101:33066>
MySQL 192.168.221.101:33066 ssl JS > dba.getCluster()
<Cluster:mysqlrouter>
MySQL 192.168.221.101:33066 ssl JS > var cluster = dba.getCluster()
MySQL 192.168.221.101:33066 ssl JS > cluster.status()
{
"clusterName": "mysqlrouter",
"defaultReplicaSet": {
"name": "default",
"primary": "192.168.221.101:33066",
"ssl": "DISABLED",
"status": "OK",
"statusText": "Cluster is ONLINE and can tolerate up to ONE failure.",
"topology": {
"192.168.221.101:33066": {
"address": "192.168.221.101:33066",
"instanceErrors": [
"NOTE: The required parallel-appliers settings are not enabled on the instance. Use dba.configureInstance() to fix it."
],
"memberRole": "PRIMARY",
"mode": "R/W",
"readReplicas": {},
"replicationLag": "applier_queue_applied",
"role": "HA",
"status": "ONLINE",
"version": "8.0.30"
},
"192.168.221.102:33066": {
"address": "192.168.221.102:33066",
"instanceErrors": [
"NOTE: The required parallel-appliers settings are not enabled on the instance. Use dba.configureInstance() to fix it."
],
"memberRole": "SECONDARY",
"mode": "R/O",
"readReplicas": {},
"replicationLag": "applier_queue_applied",
"role": "HA",
"status": "ONLINE",
"version": "8.0.30"
},
"192.168.221.103:33066": {
"address": "192.168.221.103:33066",
"instanceErrors": [
"NOTE: The required parallel-appliers settings are not enabled on the instance. Use dba.configureInstance() to fix it."
],
"memberRole": "SECONDARY",
"mode": "R/O",
"readReplicas": {},
"replicationLag": "applier_queue_applied",
"role": "HA",
"status": "ONLINE",
"version": "8.0.30"
}
},
"topologyMode": "Single-Primary"
},
"groupInformationSourceMember": "192.168.221.101:33066"
}
MySQL 192.168.221.101:33066 ssl JS > dba.configureInstance()
The instance '192.168.221.101:33066' belongs to an InnoDB Cluster.
Configuring local MySQL instance listening at port 33066 for use in an InnoDB cluster...
This instance reports its own address as 192.168.221.101:33066
applierWorkerThreads will be set to the default value of 4.
NOTE: Some configuration options need to be fixed:
---------------------------------------- --------------- ---------------- ----------------------------
| Variable | Current Value | Required Value | Note |
---------------------------------------- --------------- ---------------- ----------------------------
| binlog_transaction_dependency_tracking | COMMIT_ORDER | WRITESET | Update the server variable |
---------------------------------------- --------------- ---------------- ----------------------------
Do you want to perform the required configuration changes? [y/n]: y
Configuring instance...
The instance '192.168.221.101:33066' was configured to be used in an InnoDB cluster.
MySQL 192.168.221.101:33066 ssl JS > cluster.status()
{
"clusterName": "mysqlrouter",
"defaultReplicaSet": {
"name": "default",
"primary": "192.168.221.101:33066",
"ssl": "DISABLED",
"status": "OK",
"statusText": "Cluster is ONLINE and can tolerate up to ONE failure.",
"topology": {
"192.168.221.101:33066": {
"address": "192.168.221.101:33066",
"memberRole": "PRIMARY",
"mode": "R/W",
"readReplicas": {},
"replicationLag": "applier_queue_applied",
"role": "HA",
"status": "ONLINE",
"version": "8.0.30"
},
"192.168.221.102:33066": {
"address": "192.168.221.102:33066",
"instanceErrors": [
"NOTE: The required parallel-appliers settings are not enabled on the instance. Use dba.configureInstance() to fix it."
],
"memberRole": "SECONDARY",
"mode": "R/O",
"readReplicas": {},
"replicationLag": "applier_queue_applied",
"role": "HA",
"status": "ONLINE",
"version": "8.0.30"
},
"192.168.221.103:33066": {
"address": "192.168.221.103:33066",
"instanceErrors": [
"NOTE: The required parallel-appliers settings are not enabled on the instance. Use dba.configureInstance() to fix it."
],
"memberRole": "SECONDARY",
"mode": "R/O",
"readReplicas": {},
"replicationLag": "applier_queue_applied",
"role": "HA",
"status": "ONLINE",
"version": "8.0.30"
}
},
"topologyMode": "Single-Primary"
},
"groupInformationSourceMember": "192.168.221.101:33066"
}
代码语言:javascript复制
参考链接
代码语言:javascript复制
代码语言:javascript复制https://dev.mysql.com/doc/mysql-shell/8.0/en/mysql-innodb-cluster.html
全文完,希望可以帮到正在阅读的你,如果觉得此文对你有帮助,可以分享给你身边的朋友,同事,你关心谁就分享给谁,一起学习共同进步~~~