当前位置:   article > 正文

手把手教你安装 MySQL InnoDB Cluster 8.0.34

mysql cluster安装

f9fc7a184a339695e83db1d6a5f7e3e4.gif

作者 | JiekeXu

来源 |公众号 JiekeXu DBA之路(ID: JiekeXu_IT)

如需转载请联系授权 | (个人微信 ID:JiekeXu_DBA)

大家好,我是 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 架构简图:

27debeaab2c8e90739458f5c3002f762.png

下图是我三台 Linux7 部署架构图,IP 分别为 56、57、58,56 Node1 为主节点,并安装 MySQL 和 router,57 Node2、58 Node3 为从节点,也安装 MySQL 和 router,并在 Node3 安装了 shell。Node3 的 shell 用来安装 InnoDB Cluster,其他每个节点的 router 地址则作为高可用地址分配给 K8S 应用容器。当然官方建议将 router 和应用容器一起部署,我这么部署和使用也没有太大的问题。

3a9dd19bfa483b8c734756914c8042f4.png

下面开始使用最传统的方式从零开始安装 MySQL  InnoDB Cluster 8.0.34 单主模式,Linux X86 操作系统已安装好,并且 yum 源配置完成,1T /data 文件系统已挂载,所需软件包已下载并上传到服务器。也可参考去年的主从搭建文档----Linux 环境搭建 MySQL8.0.28 主从同步环境。

  1. --所需的软件包
  2. https://dev.mysql.com/downloads/mysql/8.0.html
  3. mysql-8.0.34-linux-glibc2.12-x86_64.tar.xz
  4. mysql-shell-8.0.34-1.el7.x86_64.rpm
  5. mysql-router-8.0.34-linux-glibc2.12-x86_64.tar.xz

一、系统准备

以下 6 小节操作需要在三台主机执行。

1、centos7 关闭防火墙
  1. systemctl stop firewalld.service 或者 systemctl stop firewalld
  2. systemctl disable firewalld.service 或者 systemctl disable firewalld
  3. systemctl status firewalld
2、关闭 selinux
  1. getenforce
  2. setenforce 0
  3. vim /etc/selinux/config
  4. SELINUX=disabled
3、绑定 /etc/hosts 解析(示例如下)
  1. vim /etc/hosts
  2. 192.168.27.56 jiekexu-my-56
  3. 192.168.27.57 jiekexu-my-57
  4. 192.168.27.58 jiekexu-my-58
4、安装 yum 包
  1. yum -y groupinstall "DeveLopment tools"
  2. yum -y install ncurses ncurses-devel openssl-devel bison gcc gcc-c++ make
5、清理系统自带 MariaDB 环境

CentOS 7 版本的系统默认自带安装了 MariaDB,需要先清理。

  1. ## 查询已安装的mariadb
  2. rpm -qa |grep mariadb
  3. yum list installed | grep mariadb
  4. ## 卸载 mariadb 包,文件名为上述命令查询出来的文件
  5. rpm -e --nodeps mariadb-libs-5.5.68-1.el7.x86_64
  6. yum -y remove mariadb-libs.x86_64
6、内核参数修改
  1. [root@jiekexu-my-56 ~]# cat>>/etc/sysctl.conf <<EOF
  2. fs.aio-max-nr = 1048576
  3. fs.file-max = 681574400
  4. kernel.shmmax = 137438953472
  5. kernel.shmmni = 4096
  6. kernel.sem = 250 32000 100 200
  7. net.ipv4.ip_local_port_range = 9000 65000
  8. net.core.rmem_default = 262144
  9. net.core.rmem_max = 4194304
  10. net.core.wmem_default = 262144
  11. net.core.wmem_max = 1048586
  12. EOF
  13. [root@jiekexu-my-56 ~]# sysctl -p
  14. [root@jiekexu-my-56 ~]# cat>>/etc/security/limits.conf <<EOF
  15. mysql soft nproc 65536
  16. mysql hard nproc 65536
  17. mysql soft nofile 65536
  18. mysql hard nofile 65536
  19. EOF
  20. [root@jiekexu-my-56 ~]# cat>>/etc/pam.d/login <<EOF
  21. session required /lib/security/pam_limits.so
  22. session required pam_limits.so
  23. EOF
  24. [root@jiekexu-my-56 ~]# cat>>/etc/profile<<EOF
  25. if [ $USER = "mysql" ]; then
  26. ulimit -u 16384 -n 65536
  27. fi
  28. EOF

二、安装 MySQL Server

1、三台主机创建数据库用户,创建实例所需目录

如果没有 /data 文件系统,可按照下面目录结构创建相对应的目录。
root 用户操作:

  1. mkdir -p /app/
  2. mkdir -p /data/mysqldb/conf/
  3. mkdir -p /data/mysqldb/data/
  4. mkdir -p /data/mysqldb/pid/
  5. mkdir -p /data/mysqldb/socket/
  6. mkdir -p /data/mysqldb/log/
  7. mkdir -p /data/mysqldb/binlog/
  8. mkdir -p /data/mysqldb/relaylog/
  9. mkdir -p /data/mysqldb/slowlog/
  10. mkdir -p /data/mysqldb/tmp/
  11. groupadd mysql
  12. useradd -g mysql mysql
  13. chown -R mysql:mysql /data
  14. chown -R mysql:mysql /app
  15. passwd mysql
  16. --MySQL
2、三台主机解压 MySQL server 安装程序包
  1. mysql 用户操作
  2. cd /app
  3. tar xvf mysql-8.0.34-linux-glibc2.12-x86_64.tar.xz
  4. mv  mysql-8.0.34-linux-glibc2.12-x86_64 mysql8.0.34
3、三台主机配置 mysql 用户环境变量
  1. vim ~/.bash_profile
  2. MYSQL_HOME=/app/mysql8.0.34
  3. PATH=$PATH:$HOME/.local/bin:$HOME/bin:$MYSQL_HOME/bin
  4. source ~/.bash_profile
  5. which mysql
4、上传 mysql 初始化参数文件并初始化(参数文件名 mysql.conf)

Node1 参数文件如下,Node2 和 Node3 节点需要注意server_id 不能重复,innodb_buffer_pool_size 内存一般设置为 OS 的 50%,report_host 写本机地址,loose_group_replication_local_address 写本机地址和端口号。

  1. vim /data/mysqldb/conf/mysql.conf
  2. [mysqld]
  3. # basic settings #
  4. default-storage-engine= InnoDB
  5. server_id = 2756
  6. basedir = /app/mysql8.0.34
  7. datadir = /data/mysqldb/data/
  8. socket = /data/mysqldb/socket/mysql.sock
  9. pid_file = /data/mysqldb/pid/mysqld.pid
  10. port = 33066
  11. default-time_zone = '+8:00'
  12. character_set_server = utf8mb4
  13. explicit_defaults_for_timestamp = 1
  14. autocommit = 1
  15. transaction_isolation = READ-COMMITTED
  16. secure_file_priv = "/data/mysqldb/tmp/"
  17. max_allowed_packet = 64M
  18. lower_case_table_names = 1
  19. default_authentication_plugin = mysql_native_password
  20. sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'
  21. # connection #
  22. back_log = 500
  23. interactive_timeout = 300
  24. wait_timeout = 1800
  25. lock_wait_timeout = 300
  26. max_user_connections = 800
  27. skip_name_resolve = 1
  28. max_connections = 2500
  29. max_connect_errors = 1000
  30. #table cache performance settings
  31. #table_open_cache = 1024
  32. #table_definition_cache = 1024
  33. #table_open_cache_instances = 16
  34. #session memory settings #
  35. #read_buffer_size = 16M
  36. #read_rnd_buffer_size = 32M
  37. #sort_buffer_size = 32M
  38. #tmp_table_size = 64M
  39. #join_buffer_size = 128M
  40. #thread_cache_size = 256
  41. # log settings #
  42. slow_query_log = ON
  43. slow_query_log_file = /data/mysqldb/slowlog/slow.log
  44. log_error = /data/mysqldb/log/mysqld.log
  45. log_error_verbosity = 3
  46. log_bin = /data/mysqldb/binlog/mysql_bin
  47. log_bin_index = /data/mysqldb/binlog/mysql_binlog.index
  48. # general_log_file = /data/mysql/generallog/general.log
  49. log_queries_not_using_indexes = 1
  50. log_slow_admin_statements = 1
  51. #log_slow_slave_statements = 1
  52. #expire_logs_days = 15
  53. binlog_expire_logs_seconds = 2592000
  54. long_query_time = 5
  55. min_examined_row_limit = 100
  56. log_throttle_queries_not_using_indexes = 1000
  57. #log_bin_trust_function_creators = 1
  58. log_replica_updates = 1
  59. mysqlx_port = 33060
  60. mysqlx_socket = /data/mysqldb/socket/mysqlx.sock
  61. # innodb settings #
  62. innodb_buffer_pool_size = 16G
  63. innodb_buffer_pool_instances = 4
  64. innodb_log_buffer_size = 100M
  65. innodb_buffer_pool_load_at_startup = 1
  66. innodb_buffer_pool_dump_at_shutdown = 1
  67. innodb_lru_scan_depth = 4096
  68. innodb_lock_wait_timeout = 20
  69. innodb_io_capacity = 5000
  70. innodb_io_capacity_max = 10000
  71. innodb_flush_method = O_DIRECT
  72. innodb_log_file_size = 1G
  73. innodb_log_files_in_group = 3
  74. innodb_purge_threads = 4
  75. innodb_thread_concurrency = 200
  76. innodb_print_all_deadlocks = 1
  77. innodb_print_ddl_logs = 1
  78. innodb_strict_mode = 1
  79. innodb_sort_buffer_size = 32M
  80. innodb_write_io_threads = 16
  81. innodb_read_io_threads = 16
  82. innodb_file_per_table = 1
  83. innodb_stats_persistent_sample_pages = 64
  84. innodb_autoinc_lock_mode = 2
  85. innodb_online_alter_log_max_size = 1G
  86. innodb_open_files = 4096
  87. innodb_buffer_pool_dump_pct = 25
  88. innodb_page_cleaners = 16
  89. innodb_undo_log_truncate = 1
  90. innodb_max_undo_log_size = 2G
  91. innodb_purge_rseg_truncate_frequency = 128
  92. innodb_flush_log_at_trx_commit = 1
  93. log_timestamps = UTC
  94. # replication settings #
  95. sync_binlog = 1
  96. binlog_format = ROW
  97. gtid_mode = ON
  98. enforce_gtid_consistency = ON
  99. relay_log_recovery = 1
  100. relay_log = /data/mysqldb/relaylog/relay.log
  101. relay_log_index = /data/mysqldb/relaylog/mysql_relay.index
  102. replica_parallel_type = LOGICAL_CLOCK
  103. replica_parallel_workers = 16
  104. binlog_gtid_simple_recovery = 1
  105. replica_preserve_commit_order = 1
  106. binlog_rows_query_log_events = 1
  107. replica_transaction_retries = 10
  108. # group replication settings
  109. transaction_write_set_extraction = XXHASH64
  110. binlog_checksum = NONE
  111. loose_group_replication_group_name = "957e8af0-bc63-11ea-bb19-005056a52572"
  112. loose_group_replication_start_on_boot = off
  113. loose_group_replication_local_address = "192.168.27.56:24901"
  114. loose_group_replication_group_seeds = "192.168.27.56:24901,192.168.27.57:24901,192.168.27.58:24901"
  115. loose_group_replication_bootstrap_group = off
  116. loose_group_replication_ip_whitelist = "192.168.27.56/24"
  117. loose_group_replication_member_expel_timeout=30
  118. report_host = 192.168.27.56
  119. report_port = 33066
  120. #read_only=1
  121. #super_read_only=1
  122. binlog_transaction_dependency_tracking = WRITESET

Node 2 不同的部分参数

  1. server_id = 2757
  2. report_host = 192.168.27.57
  3. loose_group_replication_ip_whitelist = "192.168.27.57/24"

Node 3 不同的部分参数

  1. server_id = 2758
  2. report_host = 192.168.27.58
  3. loose_group_replication_ip_whitelist = "192.168.27.58/24"

mysql 用户分别初始化三台主机操作

mysqld  --defaults-file=/data/mysqldb/conf/mysql.conf --initialize --user=mysql --basedir=/app/mysql8.0.34  --datadir=/data/mysqldb/data
等待两分钟,查看每台主机初始化日志是否出现错误。
cat /data/mysqldb/log/mysqld.log
5、启动及停止命令
分别启动三个节点 mysql server
  1. mysqld_safe --defaults-file=/data/mysqldb/conf/mysql.conf --user=mysql &
  2. -----如下关闭命令,本次不需要执行
  3. mysqladmin -uroot -proot123  -S /data/mysqldb/socket/mysql.sock shutdown
6、修改 root@localhost 用户密码

root 密码可以根据需求自己修改,生产环境需使用 8 位以上的强密码。
mysql 用户查看日志中的密码分别登录进去修改 root@localhost 用户密码。

  1. --默认密码在错误日志里
  2. more /data/mysqldb/log/mysqld.log|grep password
  3. mysql -uroot -p -P 33066 -S /data/mysqldb/socket/mysql.sock
  4. alter user root@'localhost' identified by 'root@123';
  5. reset master;

三、配置 MGR 组复制

1、所有节点安装组复制插件

SQL 命令

mysql> INSTALL PLUGIN group_replication SONAME 'group_replication.so';
或者写入到配置文件 plugin-load = group_replication.so

查看所有插件,注意主复制插件是否为 active。

show plugins;
2、在 Node1 节点上创建复制用户和配置恢复通道
  1. SET SQL_LOG_BIN=0;
  2. CREATE USER rep@'%' IDENTIFIED BY 'Rep1Yh6t';
  3. GRANT REPLICATION SLAVE ON *.* TO rep@'%';
  4. SET SQL_LOG_BIN=1;
  5. CHANGE MASTER TO MASTER_USER='rep', MASTER_PASSWORD='Rep1Yh6t' FOR CHANNEL 'group_replication_recovery';

使用 SET SQL_LOG_BIN=0; 来保证创建用户的操作不记录到 binlog 中,这么做可以避免其他服务加入时报事务冲突错误。

3、在 Node1 节点启动 MGR(只需在第一个节点执行即可)
  1. SET GLOBAL group_replication_bootstrap_group=ON;
  2. START GROUP_REPLICATION;
  3. SET GLOBAL group_replication_bootstrap_group=OFF;

group_replication_bootstrap_group 参数设置为 ON,是为了表示以后加入集群的服务器都以这台服务器为基准。以后加入的就不需要再设置此参数。

  1. --查询当前组成员及状态
  2. mysql> select * from performance_schema.replication_group_members;
  3. +---------------------------+--------------------------------------+-------------+-------------+--------------+-------------+----------------+----------------------------+
  4. | CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE | MEMBER_ROLE | MEMBER_VERSION | MEMBER_COMMUNICATION_STACK |
  5. +---------------------------+--------------------------------------+-------------+-------------+--------------+-------------+----------------+----------------------------+
  6. | group_replication_applier | b9f52d68-52e2-11ee-953c-fa163e0e1ce8 |192.168.27.56 | 33066 | ONLINE | PRIMARY | 8.0.34 | XCom |
  7. +---------------------------+--------------------------------------+-------------+-------------+--------------+-------------+----------------+----------------------------+
  8. 1 rows in set (0.00 sec)
4、其他节点加入组复制(其他节点执行)
  1. Node2 和 Node3 执行
  2. 先执行 reset master
  3. SET SQL_LOG_BIN=0;
  4. CREATE USER rep@'%' IDENTIFIED BY 'Rep1Yh6t';
  5. GRANT REPLICATION SLAVE ON *.* TO rep@'%';
  6. SET SQL_LOG_BIN=1;
  7. CHANGE MASTER TO MASTER_USER='rep', MASTER_PASSWORD='Rep1Yh6t' FOR CHANNEL 'group_replication_recovery';
  8. START GROUP_REPLICATION;
5、启动完成之后在看 Group 信息
  1. SELECT * FROM performance_schema.replication_group_members;
  2. mysql> SELECT * FROM performance_schema.replication_group_members;
  3. mysql> select * from performance_schema.replication_group_member_stats\G;
  4. mysql> select * from performance_schema.replication_group_members;
  5. +---------------------------+--------------------------------------+-------------+-------------+--------------+-------------+----------------+----------------------------+
  6. | CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE | MEMBER_ROLE | MEMBER_VERSION | MEMBER_COMMUNICATION_STACK |
  7. +---------------------------+--------------------------------------+-------------+-------------+--------------+-------------+----------------+----------------------------+
  8. | group_replication_applier | b9f52d68-52e2-11ee-953c-fa163e0e1ce8 |192.168.27.56 | 33066 | ONLINE | PRIMARY | 8.0.34 | XCom |
  9. | group_replication_applier | eaae5cec-52e2-11ee-a03c-fa163e0e1c84 |192.168.27.57 | 33066 | ONLINE | SECONDARY | 8.0.34 | XCom |
  10. | group_replication_applier | eea965a9-52e2-11ee-8027-fa163e0e1c05 |192.168.27.58 | 33066 | ONLINE | SECONDARY | 8.0.34 | XCom |
  11. +---------------------------+--------------------------------------+-------------+-------------+--------------+-------------+----------------+----------------------------+
  12. 3 rows in set (0.00 sec)
6、连接 Node1 主库创建 root@’%’ 用户

节点1

  1. mysql -uroot -p -P 33066 -S /data/mysqldb/socket/mysql.sock
  2. create user root@'%' identified by 'root@123';
  3. grant all privileges on *.* to root@'%' with grant option;
  4. flush privileges;

===================================
部署多实例需要添加如下两个参数(否则端口冲突)

  1. mysqlx_port
  2. mysqlx_socket

节点加入集群报错时,节点需要设置如下参数(使用默认密码插件时需要执行)

set global group_replication_recovery_get_public_key=on;

===========================

四、安装 MySQL Shell

初始化实例及创建纳管库,库名 mysql_innodb_cluster_metadata,只需要在一台中间件服务器安装即可,这里选择 Node3。

1、root 用户安装 MySQL Shell
  1. # rpm -ivh mysql-shell-8.0.34-1.el7.x86_64.rpm
  2. --这里下载的是 rpm 包,如果你下载的是二进制版本,可直接解压配置环境变量即可。
  3. # tar -zxvf mysql-shell-8.0.34-linux-glibc2.12-x86-64bit.tar.gz
  4. # ln -s mysql-shell-8.0.34-linux-glibc2.12-x86-64bit mysql-shell
  5. # export PATH=$PATH:/app/mysql-shell/bin
2、连接数据库,添加纳管

在第一次连接,创建纳管时,需要从 primary 连接进去。否则会破坏 read_only(root 用户会破坏 super_read_only)。

mysqlsh root@192.168.27.56:33066 --py

添加纳管

dba.create_cluster(‘mysqlrouter’)
  1. MySQL 192.168.27.56:33066 ssl Py > dba.create_cluster('mysqlrouter')
  2. A new InnoDB Cluster will be created on instance '192.168.27.56:33066'.
  3. You are connected to an instance that belongs to an unmanaged replication group.
  4. Do you want to setup an InnoDB Cluster based on this replication group? [Y/n]: Y
  5. Creating InnoDB Cluster 'mysqlrouter' on '192.168.27.56:33066'...
  6. Adding Seed Instance...
  7. Adding Instance '192.168.27.56:33066'...
  8. Adding Instance '192.168.27.57:33066'...
  9. Adding Instance '192.168.27.58:33066'...
  10. Resetting distributed recovery credentials across the cluster...
  11. Cluster successfully created based on existing replication group.
  12. <Cluster:mysqlrouter>
3、查看集群状态
  1. c=dba.get_cluster()
  2. c.status()
  1. MySQL 192.168.27.56:33066 ssl Py > c.status()
  2. {
  3. "clusterName": "mysqlrouter",
  4. "defaultReplicaSet": {
  5. "name": "default",
  6. "primary": "192.168.27.56:33066",
  7. "ssl": "DISABLED",
  8. "status": "OK",
  9. "statusText": "Cluster is ONLINE and can tolerate up to ONE failure.",
  10. "topology": {
  11. "192.168.27.56:33066": {
  12. "address": "192.168.27.56:33066",
  13. "memberRole": "PRIMARY",
  14. "mode": "R/W",
  15. "readReplicas": {},
  16. "replicationLag": "applier_queue_applied",
  17. "role": "HA",
  18. "status": "ONLINE",
  19. "version": "8.0.34"
  20. },
  21. "192.168.27.57:33066": {
  22. "address": "192.168.27.57:33066",
  23. "memberRole": "SECONDARY",
  24. "mode": "R/O",
  25. "readReplicas": {},
  26. "replicationLag": "applier_queue_applied",
  27. "role": "HA",
  28. "status": "ONLINE",
  29. "version": "8.0.34"
  30. },
  31. "192.168.27.58:33066": {
  32. "address": "192.168.27.58:33066",
  33. "memberRole": "SECONDARY",
  34. "mode": "R/O",
  35. "readReplicas": {},
  36. "replicationLag": "applier_queue_applied",
  37. "role": "HA",
  38. "status": "ONLINE",
  39. "version": "8.0.34"
  40. }
  41. },
  42. "topologyMode": "Single-Primary"
  43. },
  44. "groupInformationSourceMember": "192.168.27.56:33066"
  45. }
  46. MySQL 192.168.27.56:33066 ssl Py >
4、MySQL Shell 的其他方法
  1. dba.help()
  2. \help dba
  3. 查看方法具体使用:
  4. dba.help('createCluster');
  5. MySQL Shell 工具集
  6. \help util
5、删除纳管

本次不需要执行,只要删除元数据数据库即可,从 primary 连接。

dba.drop_metadata_schema()
6、查看纳管数据库

mysql_innodb_cluster_metadata

  1. mysql> show databases;
  2. +-------------------------------+
  3. | Database |
  4. +-------------------------------+
  5. | information_schema |
  6. | mysql |
  7. | mysql_innodb_cluster_metadata |
  8. | performance_schema |
  9. | sys |
  10. +-------------------------------+
  11. 5 rows in set (0.00 sec)
  12. mysql> use mysql_innodb_cluster_metadata;
  13. Reading table information for completion of table and column names
  14. You can turn off this feature to get a quicker startup with -A
  15. Database changed
  16. mysql> show tables;
  17. +-----------------------------------------+
  18. | Tables_in_mysql_innodb_cluster_metadata |
  19. +-----------------------------------------+
  20. | async_cluster_members |
  21. | async_cluster_views |
  22. | clusters |
  23. | clusterset_members |
  24. | clusterset_views |
  25. | clustersets |
  26. | instances |
  27. | router_rest_accounts |
  28. | routers |
  29. | schema_version |
  30. | v2_ar_clusters |
  31. | v2_ar_members |
  32. | v2_clusters |
  33. | v2_cs_clustersets |
  34. | v2_cs_members |
  35. | v2_cs_router_options |
  36. | v2_gr_clusters |
  37. | v2_instances |
  38. | v2_router_rest_accounts |
  39. | v2_routers |
  40. | v2_this_instance |
  41. +-----------------------------------------+
  42. 21 rows in set (0.00 sec)

五、MySQL Router 安装配置

1、单独三节点安装 MySQL Router

在主节点服务器配置 mysql 用户执行

  1. $ tar -xvf mysql-router-8.0.34-linux-glibc2.12-x86_64.tar.xz
  2. $ mv mysql-router-8.0.34-linux-glibc2.12-x86_64 mysqlrouter
  3. $ mkdir -p /data/mysqlrouter/mysqlrouter6446
  4. /app/mysqlrouter/bin/mysqlrouter
2、初始化 MySQL Router
  1. /app/mysqlrouter/bin/mysqlrouter --bootstrap root@192.168.27.56:33066 --directory /data/mysqlrouter/mysqlrouter6446 --conf-base-port 6446 --conf-use-sockets --force
  2. (注意:--conf-base-port 6446 这个参数是 mysqlrouter 参数  默认是 6446)
  1. [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
  2. Please enter MySQL password for root:
  3. # Bootstrapping MySQL Router instance at '/data/mysqlrouter/mysqlrouter6446'...
  4. - Creating account(s) (only those that are needed, if any)
  5. - Verifying account (using it to run SQL queries that would be run by Router)
  6. - Storing account in keyring
  7. - Adjusting permissions of generated files
  8. - Creating configuration /data/mysqlrouter/mysqlrouter6446/mysqlrouter.conf
  9. # MySQL Router configured for the InnoDB Cluster 'mysqlrouter'
  10. After this MySQL Router has been started with the generated configuration
  11. $ /app/mysqlrouter/bin/mysqlrouter -c /data/mysqlrouter/mysqlrouter6446/mysqlrouter.conf
  12. InnoDB Cluster 'mysqlrouter' can be reached by connecting to:
  13. ## MySQL Classic protocol
  14. - Read/Write Connections: jiekexu-my-56:6446, /data/mysqlrouter/mysqlrouter6446/mysql.sock
  15. - Read/Only Connections: jiekexu-my-56:6447, /data/mysqlrouter/mysqlrouter6446/mysqlro.sock
  16. ## MySQL X protocol
  17. - Read/Write Connections: jiekexu-my-56:6448, /data/mysqlrouter/mysqlrouter6446/mysqlx.sock
  18. - Read/Only Connections:  jiekexu-my-56:6449/data/mysqlrouter/mysqlrouter6446/mysqlxro.sock
  19. ==========================================================
  20. --./mysqlrouter_passwd set /app/percona/mysqlrouter3306/mysqlrouter.pwd rdsroot 输入 rest 认证密码(curl 命令查询 MGR 集群状态  非必须)

3、启动 MySQL Router

  1. [mysql@jiekexu-my-57 mysqlrouter6446]$ cd /data/mysqlrouter/mysqlrouter6446
  2. [mysql@jiekexu-my-57 mysqlrouter6446]$ ./start.sh
  3. [mysql@jiekexu-my-57 mysqlrouter6446]$ PID 26919 written to '/data/mysqlrouter/mysqlrouter6446/mysqlrouter.pid'
  4. stopping to log to the console. Continuing to log to filelog
  5. --除了上面脚本启动外,按提示中如下命令也可以启动:
  6. /app/mysqlrouter/bin/mysqlrouter -/data/mysqlrouter/mysqlrouter6446/mysqlrouter.conf
  1. 查看日志
  2. cat mysqlrouter.log
  1. [mysql@jiekexu-my-56 log]$ cat mysqlrouter.log
  2. 2023-09-14 18:28:12 io INFO [7ff61678e780] starting 16 io-threads, using backend 'linux_epoll'
  3. 2023-09-14 18:28:12 http_server INFO [7ff61678e780] listening on 0.0.0.0:8443
  4. 2023-09-14 18:28:12 metadata_cache_plugin INFO [7ff5d8ff9700] Starting Metadata Cache
  5. 2023-09-14 18:28:12 metadata_cache INFO [7ff5d8ff9700] Connections using ssl_mode 'PREFERRED'
  6. 2023-09-14 18:28:12 metadata_cache INFO [7ff616702700] Starting metadata cache refresh thread
  7. 2023-09-14 18:28:12 routing INFO [7ff5937fe700] [routing:bootstrap_x_rw] started: routing strategy = first-available
  8. 2023-09-14 18:28:12 routing INFO [7ff5937fe700] [routing:bootstrap_x_rw] started: listening using /data/mysqlrouter/mysqlrouter6446/mysqlx.sock
  9. 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
  10. 2023-09-14 18:28:12 routing INFO [7ff598ff9700] [routing:bootstrap_rw] started: routing strategy = first-available
  11. 2023-09-14 18:28:12 routing INFO [7ff5997fa700] [routing:bootstrap_ro] started: routing strategy = round-robin-with-fallback
  12. 2023-09-14 18:28:12 routing INFO [7ff593fff700] [routing:bootstrap_x_ro] started: routing strategy = round-robin-with-fallback
  13. 2023-09-14 18:28:12 routing INFO [7ff598ff9700] [routing:bootstrap_rw] started: listening using /data/mysqlrouter/mysqlrouter6446/mysql.sock
  14. 2023-09-14 18:28:12 routing INFO [7ff5997fa700] [routing:bootstrap_ro] started: listening using /data/mysqlrouter/mysqlrouter6446/mysqlro.sock
  15. 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
  16. 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
  17. 2023-09-14 18:28:12 routing INFO [7ff593fff700] [routing:bootstrap_x_ro] started: listening using /data/mysqlrouter/mysqlrouter6446/mysqlxro.sock
  18. 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
  19. 2023-09-14 18:28:12 metadata_cache INFO [7ff616702700] Connected with metadata server running on 192.168.27.56:33066
  20. 2023-09-14 18:28:12 metadata_cache INFO [7ff616702700] Potential changes detected in cluster after metadata refresh (view_id=0)
  21. 2023-09-14 18:28:12 metadata_cache INFO [7ff616702700] Metadata for cluster 'mysqlrouter' has 3 member(s), single-primary:
  22. 2023-09-14 18:28:12 metadata_cache INFO [7ff616702700] 192.168.27.56:33066 / 33060 - mode=RW
  23. 2023-09-14 18:28:12 metadata_cache INFO [7ff616702700] 192.168.27.57:33066 / 33060 - mode=RO
  24. 2023-09-14 18:28:12 metadata_cache INFO [7ff616702700]     192.168.27.58:33066 / 33060 - mode=RO
4、使用 Router 登录 mysql 测试
  1. mysql -uroot -p -h192.168.27.56 -P 6446(6446是 mysqlrouer 读写端口)
  2. mysql -uroot -p -h192.168.27.56 -P 6447(6447是 mysqlrouer 只读端口)
  3. ======================================
5、MySQL Router systemctl 启动配置
  1. vim /etc/systemd/system/mysqlrouter@3306.service
  2. [Unit]
  3. Description= Mysql router
  4. Documentation=man:mysqlrouter(8)
  5. After=network.target
  6. After=syslog.target
  7. [Install]
  8. WantedBy=multi-user.target
  9. [Service]
  10. User=root
  11. Group=root
  12. Type=forking
  13. CPUQuota=100%
  14. TimeoutSec=30
  15. PermissionsStartOnly=true
  16. PIDFile=/data/mysqlrouter/mysqlrouter6446/mysqlrouter.pid
  17. ExecStart=/usr/bin/bash -c "/data/mysqlrouter/mysqlrouter6446/start.sh &"
  18. ExecStop=/usr/bin/bash -c "/data/mysqlrouter/mysqlrouter6446/stop.sh &"
  19. LimitNOFILE = 102400
  20. Restart=no
6、只读参数设置

修改参数文件,将三个节点中的参数文件中关于只读的最后两行参数注释去掉。
注意:在《MySQL 实战》一书中说明单主模式下,组复制会自动将 Secondary 节点的 read_only 和 super_read_only 设置为 ON,感兴趣的小伙伴可以测试一下,我这里已经搭建完成了才看到没法测试了。

  1. [mysql@jiekexu-my-56 log]$ ps -ef | grep mysql
  2. root 11252 10549 0 17:37 pts/0 00:00:00 su - mysql
  3. mysql 11253 11252 0 17:37 pts/0 00:00:00 -bash
  4. 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
  5. 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
  6. mysql 14279 1 0 18:28 pts/0 00:00:03 /app/mysqlrouter/bin/mysqlrouter -c /data/mysqlrouter/mysqlrouter6446/mysqlrouter.conf
  7. mysql 14532 11253 0 18:35 pts/0 00:00:00 ps -ef
  8. mysql 14533 11253 0 18:35 pts/0 00:00:00 grep --color=auto mysql
  9. [mysql@jiekexu-my-56 log]$ vim /data/mysqldb/conf/mysql.conf
  10. read_only=1
  11. super_read_only=1

然后登录两个从库,执行从库只读设置(注意只在从库执行,主库不用执行)。

  1. mysql> set global read_only=on;
  2. Query OK, 0 rows affected (0.00 sec)
  3. mysql> set global super_read_only=on;
  4. Query OK, 0 rows affected (0.00 sec)
  5. mysql> show master status;
  6. +------------------+----------+--------------+------------------+--------------------------------------------+
  7. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
  8. +------------------+----------+--------------+------------------+--------------------------------------------+
  9. | mysql_bin.000001 | 397716 | | | 957e8af0-bc63-11ea-bb19-005056a52572:1-438 |
  10. +------------------+----------+--------------+------------------+--------------------------------------------+
  11. 1 row in set (0.00 sec)
7、可能遇到的问题

mysql 高版本可能存在 note 提示
可以看到,集群的状态都有一个问题:

  1. "instanceErrors": [
  2.                     "NOTE: The required parallel-appliers settings are not enabled on the instance. Use dba.configureInstance() to fix it."

这个错误也提示了修复的方式,只要通过 mysqlshell 单独连上 MGR 中的每个实例,运行下即可修复:dba.configureInstance()

再根据提示,将 mysql 的参数修正下,或者将这个参数加到参数文件中,本文则是直接加到参数文件中了。

set persist binlog_transaction_dependency_tracking='WRITESET';

下面是以前搭建的 MySQL 8.0.30 也是第一次遇到这个问题,特此记录一下。

  1. [mysql@ts-mysql-101 ~]$ mysqlsh
  2. MySQL Shell 8.0.30
  3. Copyright (c) 2016, 2022, Oracle and/or its affiliates.
  4. Oracle is a registered trademark of Oracle Corporation and/or its affiliates.
  5. Other names may be trademarks of their respective owners.
  6. Type '\help' or '\?' for help; '\quit' to exit.
  7. MySQL JS > shell.connect('root@192.168.221.101:33066')
  8. Creating a session to 'root@192.168.221.101:33066'
  9. Please provide the password for 'root@192.168.221.101:33066': *******
  10. Save password for 'root@192.168.221.101:33066'? [Y]es/[N]o/Ne[v]er (default No): m^C
  11. Shell.connect: Cancelled (RuntimeError)
  12. MySQL JS > shell.connect('root@192.168.221.101:33066')
  13. Creating a session to 'root@192.168.221.101:33066'
  14. Please provide the password for 'root@192.168.221.101:33066': *******
  15. Save password for 'root@192.168.221.101:33066'? [Y]es/[N]o/Ne[v]er (default No): n
  16. Fetching schema names for autocompletion... Press ^C to stop.
  17. Your MySQL connection id is 74
  18. Server version: 8.0.30 MySQL Community Server - GPL
  19. No default schema selected; type \use <schema> to set one.
  20. <ClassicSession:root@192.168.221.101:33066>
  21. MySQL 192.168.221.101:33066 ssl JS > dba.getCluster()
  22. <Cluster:mysqlrouter>
  23. MySQL 192.168.221.101:33066 ssl JS > var cluster = dba.getCluster()
  24. MySQL 192.168.221.101:33066 ssl JS > cluster.status()
  25. {
  26. "clusterName": "mysqlrouter",
  27. "defaultReplicaSet": {
  28. "name": "default",
  29. "primary": "192.168.221.101:33066",
  30. "ssl": "DISABLED",
  31. "status": "OK",
  32. "statusText": "Cluster is ONLINE and can tolerate up to ONE failure.",
  33. "topology": {
  34. "192.168.221.101:33066": {
  35. "address": "192.168.221.101:33066",
  36. "instanceErrors": [
  37. "NOTE: The required parallel-appliers settings are not enabled on the instance. Use dba.configureInstance() to fix it."
  38. ],
  39. "memberRole": "PRIMARY",
  40. "mode": "R/W",
  41. "readReplicas": {},
  42. "replicationLag": "applier_queue_applied",
  43. "role": "HA",
  44. "status": "ONLINE",
  45. "version": "8.0.30"
  46. },
  47. "192.168.221.102:33066": {
  48. "address": "192.168.221.102:33066",
  49. "instanceErrors": [
  50. "NOTE: The required parallel-appliers settings are not enabled on the instance. Use dba.configureInstance() to fix it."
  51. ],
  52. "memberRole": "SECONDARY",
  53. "mode": "R/O",
  54. "readReplicas": {},
  55. "replicationLag": "applier_queue_applied",
  56. "role": "HA",
  57. "status": "ONLINE",
  58. "version": "8.0.30"
  59. },
  60. "192.168.221.103:33066": {
  61. "address": "192.168.221.103:33066",
  62. "instanceErrors": [
  63. "NOTE: The required parallel-appliers settings are not enabled on the instance. Use dba.configureInstance() to fix it."
  64. ],
  65. "memberRole": "SECONDARY",
  66. "mode": "R/O",
  67. "readReplicas": {},
  68. "replicationLag": "applier_queue_applied",
  69. "role": "HA",
  70. "status": "ONLINE",
  71. "version": "8.0.30"
  72. }
  73. },
  74. "topologyMode": "Single-Primary"
  75. },
  76. "groupInformationSourceMember": "192.168.221.101:33066"
  77. }
  78. MySQL 192.168.221.101:33066 ssl JS > dba.configureInstance()
  79. The instance '192.168.221.101:33066' belongs to an InnoDB Cluster.
  80. Configuring local MySQL instance listening at port 33066 for use in an InnoDB cluster...
  81. This instance reports its own address as 192.168.221.101:33066
  82. applierWorkerThreads will be set to the default value of 4.
  83. NOTE: Some configuration options need to be fixed:
  84. +----------------------------------------+---------------+----------------+----------------------------+
  85. | Variable | Current Value | Required Value | Note |
  86. +----------------------------------------+---------------+----------------+----------------------------+
  87. | binlog_transaction_dependency_tracking | COMMIT_ORDER | WRITESET | Update the server variable |
  88. +----------------------------------------+---------------+----------------+----------------------------+
  89. Do you want to perform the required configuration changes? [y/n]: y
  90. Configuring instance...
  91. The instance '192.168.221.101:33066' was configured to be used in an InnoDB cluster.
  92. MySQL 192.168.221.101:33066 ssl JS > cluster.status()
  93. {
  94. "clusterName": "mysqlrouter",
  95. "defaultReplicaSet": {
  96. "name": "default",
  97. "primary": "192.168.221.101:33066",
  98. "ssl": "DISABLED",
  99. "status": "OK",
  100. "statusText": "Cluster is ONLINE and can tolerate up to ONE failure.",
  101. "topology": {
  102. "192.168.221.101:33066": {
  103. "address": "192.168.221.101:33066",
  104. "memberRole": "PRIMARY",
  105. "mode": "R/W",
  106. "readReplicas": {},
  107. "replicationLag": "applier_queue_applied",
  108. "role": "HA",
  109. "status": "ONLINE",
  110. "version": "8.0.30"
  111. },
  112. "192.168.221.102:33066": {
  113. "address": "192.168.221.102:33066",
  114. "instanceErrors": [
  115. "NOTE: The required parallel-appliers settings are not enabled on the instance. Use dba.configureInstance() to fix it."
  116. ],
  117. "memberRole": "SECONDARY",
  118. "mode": "R/O",
  119. "readReplicas": {},
  120. "replicationLag": "applier_queue_applied",
  121. "role": "HA",
  122. "status": "ONLINE",
  123. "version": "8.0.30"
  124. },
  125. "192.168.221.103:33066": {
  126. "address": "192.168.221.103:33066",
  127. "instanceErrors": [
  128. "NOTE: The required parallel-appliers settings are not enabled on the instance. Use dba.configureInstance() to fix it."
  129. ],
  130. "memberRole": "SECONDARY",
  131. "mode": "R/O",
  132. "readReplicas": {},
  133. "replicationLag": "applier_queue_applied",
  134. "role": "HA",
  135. "status": "ONLINE",
  136. "version": "8.0.30"
  137. }
  138. },
  139. "topologyMode": "Single-Primary"
  140. },
  141. "groupInformationSourceMember": "192.168.221.101:33066"
  142. }

参考链接

https://dev.mysql.com/doc/mysql-shell/8.0/en/mysql-innodb-cluster.html

全文完,希望可以帮到正在阅读的你,如果觉得此文对你有帮助,可以分享给你身边的朋友,同事,你关心谁就分享给谁,一起学习共同进步~~~

欢迎关注我公众号【JiekeXu DBA之路】,第一时间一起学习新知识!以下三个地址可以找到我,其他地址均属于盗版侵权爬取我的文章,而且代码格式、图片等均有错乱,不方便阅读,欢迎来我公众号或者墨天轮地址关注我,第一时间收获最新消息。

————————————————————————————
公众号:JiekeXu DBA之路
CSDN :https://blog.csdn.net/JiekeXu
墨天轮:https://www.modb.pro/u/4347
————————————————————————————

051caf144a90d3a427184726da7b174f.gif

分享几个数据库备份脚本

Oracle 表碎片检查及整理方案

OGG|Oracle GoldenGate 基础2022 年公众号历史文章合集整理
 
 

Oracle 19c RAC 遇到的几个问题

OGG|Oracle 数据迁移后比对一致性

OGG|Oracle GoldenGate 微服务架构

Oracle 查询表空间使用率超慢问题一则

国产数据库|TiDB 5.4 单机快速安装初体验

Oracle ADG 备库停启维护流程及增量恢复

Linux 环境搭建 MySQL8.0.28 主从同步环境

从国产数据库调研报告中你都能了解哪些信息及我的总结建议

223020f9779046d411b21a75a2e982ab.png

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/1006232
推荐阅读
相关标签
  

闽ICP备14008679号