CentOS安裝MySQL-InnoDB-Cluster集群(適用MySQL5.7和MySQL8.0)


1、准備好3台CentOS系統機器

  1)修改hostname為mysql1、mysql2、mysql3,修改/etc/hostname文件,將里面內容改掉即可

vi /etc/hostname

  2)修改/etc/hosts文件,配置映射

vi /etc/hosts

  配置如下:

172.16.43.141     mysql1
172.16.43.142     mysql2
172.16.43.143     mysql3

  3)禁用防火牆或開放3306和33061端口

systemctl stop firewalld.service
systemctl disable firewalld.service

  4)禁用SELINUX,修改/etc,SELINUX=enforcing改為SELINUX=disabled(為啥這樣做不是很清楚,不操作的話后面無法創建集群)

vi /etc/selinux/config

  5)重啟服務器,讓配置生效。

2、安裝MySQL

  詳細安裝過程見:https://www.cnblogs.com/zhi-leaf/p/5993676.html

  mysql80-community-release-el7-3.noarch.rpm默認安裝的MySQL8,如果想安裝MySQL5.7,需要修改/etc/yum.repos.d/mysql-community.repo,將mysql57和mysql80的enabled值對調一下

vi /etc/yum.repos.d/mysql-community.repo

3、安裝mysql-shell和mysql-router

yum install mysql-community-server -y
yum install mysql-shell -y
yum install mysql-router -y

4、查看mysqld配置是否滿足集群

mysqlsh --log-level=DEBUG3  
dba.checkInstanceConfiguration('root@mysql1:3306')  # 檢測mysql服務是否滿足集群條件

5、根據上面的檢查,我們修改對應的配置(所有MySQL服務節點都需要修改)

  1)退出MySQL JS,重新登陸到具體服務

mysqlsh --uri root@localhost:3306

 

 

  2)執行dba.configureLocalInstance()自動修改配置(配置修改完成后一定要重啟mysqld服務)

dba.configureLocalInstance('root@localhost:3306')
dba.checkInstanceConfiguration()

  再次使用dba.checkInstanceConfiguration(),當時檢查結果status為ok時,說明配置已修改完成,MySQL服務符合集群條件,這時只需重啟mysqld服務就可以了

service mysqld restart

  說明一點:mysql8.0直接修改運行參數,而ysql5.7會將修改的配置寫入/etc/my.cnf文件,他在my.cnf中增加的配置信息如下圖(這些修改項正是 dba.checkInstanceConfiguration() 檢查的修改項,我們直接復制過去也是可以的):

binlog_checksum = NONE
enforce_gtid_consistency = ON
gtid_mode = ON
log_bin
log_slave_updates = ON
master_info_repository = TABLE
relay_log_info_repository = TABLE
server_id = 3293780721
transaction_write_set_extraction = XXHASH64

6、mysql服務重啟完成后,我們就可以創建集群,並添加2個節點(只需在一個節點操作)

mysqlsh --uri root@localhost:3306  # 進入mysqlsh客戶端
var cluster = dba.createCluster('dev') cluster.addInstance('root@mysql2:3306'); cluster.addInstance('root@mysql3:3306');

  再注意一點:mysql57在創建集群和添加節點的時候會出現警告:

WARNING: Instance 'mysql1:3306' cannot persist configuration since MySQL version 5.7.29 does not support the SET PERSIST command (MySQL version >= 8.0.11 required). Please use the <Dba>.configureLocalInstance() command locally to persist the changes.

  這個警告沒關系,他告訴我們做集群修改的配置沒有持久化,我們再次在每個節點都執行dba.configureLocalInstance()就可以了,他會一大堆group_replication配置信息寫入到/etc/my.cnf。

  新添加的配置項如下(我對比了一下,只有group_replication_group_seeds和group_replication_local_address2個參數不一致):

auto_increment_increment = 1
auto_increment_offset = 2
group_replication_allow_local_disjoint_gtids_join = OFF
group_replication_allow_local_lower_version_join = OFF
group_replication_auto_increment_increment = 7
group_replication_bootstrap_group = OFF
group_replication_components_stop_timeout = 31536000
group_replication_compression_threshold = 1000000
group_replication_enforce_update_everywhere_checks = OFF
group_replication_exit_state_action = READ_ONLY
group_replication_flow_control_applier_threshold = 25000
group_replication_flow_control_certifier_threshold = 25000
group_replication_flow_control_mode = QUOTA
group_replication_force_members =
group_replication_group_name = eb782af9-732a-11ea-9cee-000c290200c6
group_replication_group_seeds = mysql1:33061,mysql3:33061
group_replication_gtid_assignment_block_size = 1000000
group_replication_ip_whitelist = AUTOMATIC
group_replication_local_address = mysql2:33061
group_replication_member_weight = 50
group_replication_poll_spin_loops = 0
group_replication_recovery_complete_at = TRANSACTIONS_APPLIED
group_replication_recovery_reconnect_interval = 60
group_replication_recovery_retry_count = 10
group_replication_recovery_ssl_ca =
group_replication_recovery_ssl_capath =
group_replication_recovery_ssl_cert =
group_replication_recovery_ssl_cipher =
group_replication_recovery_ssl_crl =
group_replication_recovery_ssl_crlpath =
group_replication_recovery_ssl_key =
group_replication_recovery_ssl_verify_server_cert = OFF
group_replication_recovery_use_ssl = ON
group_replication_single_primary_mode = ON
group_replication_ssl_mode = REQUIRED
group_replication_start_on_boot = ON
group_replication_transaction_size_limit = 0
group_replication_unreachable_majority_timeout = 0
super_read_only = ON

7、集群安裝完成,我們可以查看集群的安裝狀態

var cluster = dba.getCluster()
cluster.status()

  可以看出,mysql1節點為讀寫模式,mysql2和mysql3節點為讀模式(只能查詢,不能執行建表、新增數據等操作)

8、集群驗證

 關閉mysql1節點,幾秒鍾后,集群primary節點從mysql1切換到了msql3,並且mysql3變成了讀寫模式

 

更多安裝說明:https://dev.mysql.com/doc/refman/8.0/en/mysql-innodb-cluster-creating.html

 

遇到的問題:

  問題1:Dba.createCluster: dba.createCluster: Unable to create cluster. The instance 'mysql1:3306' has a populated Metadata schema and belongs to that Metadata. Use either dba.dropMetadataSchema() to drop the schema, or dba.rebootClusterFromCompleteOutage() to reboot the cluster from complete outage. (RuntimeError)

  解決:執行dba.rebootClusterFromCompleteOutage()

  問題2:Cluster.addInstance: Cannot add an instance with the same server UUID (4245c0b6-7270-11ea-929d-000c292b8420) of an active member of the cluster 'mysql1:3306'. Please change the server UUID of the instance to add, all members must have a unique server UUID. (RuntimeError)

  解決:我是虛擬機復制的,所有MySQL服務的server_uuid和server_id是一樣的,我們必須修改,servier_uuid需要修改/var/lib/mysql/auto.cnf文件,server_id通過sql語句設置。資料參考:https://dev.mysql.com/doc/refman/8.0/en/replication-options.html

vi /var/lib/mysql/auto.cnf // 修改server_uuid
set global server_id = 549754138; // 修改server_id

  問題3:Unable to start Group Replication for instance 'mysql3:3306'. Please check the MySQL server error log for more information.
Cluster.addInstance: Group Replication failed to start: MySQL Error 3096 (HY000): mysql3:3306: The START GROUP_REPLICATION command failed as there was an error when initializing the group communication layer. (RuntimeError)
  解決:mysql3主機的hosts配置錯了。

  問題4:Dba.getCluster: This function is not available through a session to a standalone instance (metadata exists, instance belongs to that metadata, but GR is not active) (RuntimeError)

  解決:解決:執行dba.rebootClusterFromCompleteOutage()


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM