差異處
這裏顯示兩個版本的差異處。
兩邊的前次修訂版 前次修改 下次修改 | 前次修改 | ||
tech:perconaxc [2020/07/06 14:47] – jonathan_tsai | tech:perconaxc [2021/03/13 11:37] (目前版本) – 增加自動清除資料紀錄檔設定 jonathan | ||
---|---|---|---|
行 1: | 行 1: | ||
+ | ====== CentOS 7 安裝 Percona XtraDB Cluster ====== | ||
+ | |||
+ | * 預計安裝在 CentOS Linux release 7.7.1908 (Core) (PVE6 Template upgrade) 環境下 | ||
+ | * 配置 node 如下: | ||
+ | * ct-percona1 192.168.11.252 (Master) | ||
+ | * ct-percona2 192.168.11.202 | ||
+ | |||
+ | ===== 安裝程序 ===== | ||
+ | <code sh> | ||
+ | yum install epel-release | ||
+ | yum install http:// | ||
+ | yum install percona-xtrabackup Percona-XtraDB-Cluster-server-56 | ||
+ | </ | ||
+ | |||
+ | |||
+ | < | ||
+ | * 要開啟 firewalld 允許 3306, 4444, 4567, 4568 | ||
+ | * 要關閉 selinux | ||
+ | * 第一台 mysql 要建立 sstuser 讓 my.cnf 內的 wsrep_sst_auth 可以正確設定 | ||
+ | </ | ||
+ | |||
+ | * 調整 firewalld <code sh> | ||
+ | firewall-cmd --add-service=mysql | ||
+ | firewall-cmd --add-port=4444/ | ||
+ | firewall-cmd --add-port=4567/ | ||
+ | firewall-cmd --add-port=4568/ | ||
+ | firewall-cmd --permanent --add-service=mysql | ||
+ | firewall-cmd --permanent --add-port=4444/ | ||
+ | firewall-cmd --permanent --add-port=4567/ | ||
+ | firewall-cmd --permanent --add-port=4568/ | ||
+ | * 關閉 SELinux <code sh> | ||
+ | vi / | ||
+ | : | ||
+ | SELINUX=disabled | ||
+ | : | ||
+ | </ | ||
+ | |||
+ | ==== 設定程序 ===== | ||
+ | * / | ||
+ | [mysqld] | ||
+ | server_id=1 | ||
+ | wsrep_provider=/ | ||
+ | wsrep_cluster_address=" | ||
+ | wsrep_sst_auth=sstuser: | ||
+ | wsrep_provider_options=" | ||
+ | wsrep_cluster_name=Percona | ||
+ | wsrep_sst_method=xtrabackup-v2 | ||
+ | wsrep_node_name=db_01 | ||
+ | wsrep_slave_threads=4 | ||
+ | log_slave_updates | ||
+ | innodb_locks_unsafe_for_binlog=1 | ||
+ | innodb_autoinc_lock_mode=2 | ||
+ | expire-logs-days = 30 | ||
+ | # | ||
+ | : | ||
+ | : | ||
+ | </ | ||
+ | * / | ||
+ | [mysqld] | ||
+ | server_id=2 | ||
+ | wsrep_provider=/ | ||
+ | wsrep_cluster_address=" | ||
+ | wsrep_sst_auth=sstuser: | ||
+ | wsrep_provider_options=" | ||
+ | wsrep_cluster_name=Percona | ||
+ | wsrep_sst_method=xtrabackup-v2 | ||
+ | wsrep_node_name=db_02 | ||
+ | wsrep_slave_threads=4 | ||
+ | log_slave_updates | ||
+ | innodb_locks_unsafe_for_binlog=1 | ||
+ | innodb_autoinc_lock_mode=2 | ||
+ | expire-logs-days = 30 | ||
+ | # | ||
+ | : | ||
+ | : | ||
+ | </ | ||
+ | |||
+ | * 更改 mysql 內 root 密碼與建立 sstuser @ct-percona1< | ||
+ | / | ||
+ | mysql -u root -p</ | ||
+ | CREATE USER ' | ||
+ | GRANT RELOAD, LOCK TABLES, REPLICATION CLIENT ON *.* TO ' | ||
+ | FLUSH PRIVILEGES; | ||
+ | </ | ||
+ | |||
+ | ===== 啟動 ===== | ||
+ | * 第一次啟動重新開機會比較快< | ||
+ | service mysql restart | ||
+ | </ | ||
+ | * 再來就可以查看 / | ||
+ | * 如果正常可以連入 mysql 命令列看到以下的狀況< | ||
+ | show global status like ' | ||
+ | +--------------------+-------+ | ||
+ | | Variable_name | ||
+ | +--------------------+-------+ | ||
+ | | wsrep_cluster_size | 2 | | ||
+ | +--------------------+-------+ | ||
+ | 1 row in set (0.00 sec) | ||
+ | </ | ||
+ | |||
+ | ===== 增加第三台的方式 ===== | ||
+ | * 依照前面步驟安裝與設定 | ||
+ | * /etc/my.cnf 更改, 可參考 / | ||
+ | * server_id=3 | ||
+ | * wsrep_node_name=db_03 | ||
+ | * / | ||
+ | [mysqld] | ||
+ | server_id=3 | ||
+ | wsrep_provider=/ | ||
+ | wsrep_cluster_address=" | ||
+ | wsrep_sst_auth=sstuser: | ||
+ | wsrep_provider_options=" | ||
+ | wsrep_cluster_name=Percona | ||
+ | wsrep_sst_method=xtrabackup-v2 | ||
+ | wsrep_node_name=db_03 | ||
+ | wsrep_slave_threads=4 | ||
+ | log_slave_updates | ||
+ | innodb_locks_unsafe_for_binlog=1 | ||
+ | innodb_autoinc_lock_mode=2 | ||
+ | expire-logs-days = 30 | ||
+ | # | ||
+ | datadir=/ | ||
+ | socket=/ | ||
+ | user=mysql | ||
+ | # Disabling symbolic-links is recommended to prevent assorted security risks | ||
+ | symbolic-links=0 | ||
+ | |||
+ | [mysqld_safe] | ||
+ | log-error=/ | ||
+ | pid-file=/ | ||
+ | </ | ||
+ | * 重新開機後, | ||
+ | * 連入 mysql 命令列看到以下的狀況< | ||
+ | show global status like ' | ||
+ | +--------------------+-------+ | ||
+ | | Variable_name | ||
+ | +--------------------+-------+ | ||
+ | | wsrep_cluster_size | 3 | | ||
+ | +--------------------+-------+ | ||
+ | 1 row in set (0.00 sec) | ||
+ | </ | ||
+ | |||
+ | ===== 效能測試 ===== | ||
+ | * 使用 sysbench 來進行效能測試< | ||
+ | yum install sysbench | ||
+ | </ | ||
+ | * 連入 mysql 建立 sbtest 測試資料庫 <code sql> | ||
+ | create database sbtest;</ | ||
+ | * 建立測試資料 <code sh> | ||
+ | sysbench --test=oltp --db-driver=mysql --mysql-engine-trx=yes --mysql-table-engine=innodb --mysql-host=127.0.0.1 --mysql-port=3306 --mysql-user=**username** --mysql-password=**password** --oltp-auto-inc=off --test=/ | ||
+ | </ | ||
+ | * 開始進行測試 <code sh> | ||
+ | sysbench --test=oltp --db-driver=mysql --mysql-engine-trx=yes --mysql-table-engine=innodb --mysql-host=127.0.0.1 --mysql-port=3306 --mysql-user=**username** --mysql-password=**password** --oltp-auto-inc=off --test=/ | ||
+ | </ | ||
+ | * 跑出來的結果 < | ||
+ | sysbench 0.5: multi-threaded system evaluation benchmark | ||
+ | |||
+ | Running the test with following options: | ||
+ | Number of threads: 100 | ||
+ | Random number generator seed is 0 and will be ignored | ||
+ | |||
+ | |||
+ | Threads started! | ||
+ | |||
+ | OLTP test statistics: | ||
+ | queries performed: | ||
+ | read: 140000 | ||
+ | write: | ||
+ | other: | ||
+ | total: | ||
+ | transactions: | ||
+ | read/write requests: | ||
+ | other operations: | ||
+ | ignored errors: | ||
+ | reconnects: | ||
+ | |||
+ | General statistics: | ||
+ | total time: 199.1551s | ||
+ | total number of events: | ||
+ | total time taken by event execution: 19819.8799s | ||
+ | response time: | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | |||
+ | Threads fairness: | ||
+ | events (avg/ | ||
+ | execution time (avg/ | ||
+ | </ | ||
+ | * 清除測試資料 <code sh> | ||
+ | sysbench --test=oltp --db-driver=mysql --mysql-engine-trx=yes --mysql-table-engine=innodb --mysql-host=127.0.0.1 --mysql-port=3306 --mysql-user=**username** --mysql-password=**password** --oltp-auto-inc=off --test=/ | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== 加上 HAProxy 來進行負載平衡 ===== | ||
+ | < | ||
+ | * 要開啟 iptables 允許 8080(haproxy stats), 9200(mysqlchk), | ||
+ | </ | ||
+ | |||
+ | * 調整 iptables <code sh> | ||
+ | vi / | ||
+ | : | ||
+ | : | ||
+ | -A INPUT -m state --state NEW -m tcp -p tcp --dport 4568 -j ACCEPT | ||
+ | -A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT | ||
+ | -A INPUT -m state --state NEW -m tcp -p tcp --dport 9200 -j ACCEPT | ||
+ | -A INPUT -m state --state NEW -m tcp -p tcp --dport 13306 -j ACCEPT | ||
+ | -A INPUT -j REJECT --reject-with icmp-host-prohibited | ||
+ | : | ||
+ | </ | ||
+ | service iptables reload | ||
+ | </ | ||
+ | * 安裝 haproxy< | ||
+ | yum -y install haproxy xinetd | ||
+ | </ | ||
+ | * 設定 mysqlchk Liston 9200 <code sh> | ||
+ | vi / | ||
+ | </ | ||
+ | : | ||
+ | sun-as-jpda | ||
+ | mysqlchk | ||
+ | wap-wsp | ||
+ | : | ||
+ | </ | ||
+ | * 啟動 xinetd <code sh> | ||
+ | service xinetd restart | ||
+ | chkconfig xinetd on | ||
+ | </ | ||
+ | * 建立檢核帳號 clustercheckuser 與設定 mysqlchk <code sh> | ||
+ | mysql -u root -p</ | ||
+ | GRANT PROCESS ON *.* TO ' | ||
+ | FLUSH PRIVILEGES; | ||
+ | </ | ||
+ | vi / | ||
+ | : | ||
+ | server | ||
+ | server_args | ||
+ | log_on_failure | ||
+ | : | ||
+ | </ | ||
+ | service xinetd reload | ||
+ | </ | ||
+ | |||
+ | * 設定 haproxy log<code sh> | ||
+ | vi / | ||
+ | </ | ||
+ | : | ||
+ | # Save boot messages also to boot.log | ||
+ | local7.* | ||
+ | |||
+ | # HAProxy log | ||
+ | local2.* | ||
+ | |||
+ | |||
+ | # ### begin forwarding rule ### | ||
+ | : | ||
+ | </ | ||
+ | service rsyslog restart | ||
+ | </ | ||
+ | * 設定 haproxy Liston 13306 指向三台 Percona XC<code sh> | ||
+ | vi / | ||
+ | </ | ||
+ | : | ||
+ | defaults | ||
+ | mode http | ||
+ | log | ||
+ | # | ||
+ | option | ||
+ | option | ||
+ | #option http-server-close | ||
+ | #option forwardfor | ||
+ | # | ||
+ | retries | ||
+ | #timeout http-request | ||
+ | timeout queue 1m | ||
+ | timeout connect | ||
+ | timeout client | ||
+ | timeout server | ||
+ | #timeout http-keep-alive 10s | ||
+ | timeout check 10s | ||
+ | maxconn | ||
+ | |||
+ | frontend stats-front | ||
+ | bind *:8080 | ||
+ | mode http | ||
+ | default_backend stats-back | ||
+ | |||
+ | frontend pxc-front | ||
+ | bind *:13306 | ||
+ | mode tcp | ||
+ | default_backend pxc-back | ||
+ | |||
+ | frontend pxc-onenode-front | ||
+ | bind *:3308 | ||
+ | mode tcp | ||
+ | default_backend pxc-onenode-back | ||
+ | |||
+ | backend stats-back | ||
+ | mode http | ||
+ | balance roundrobin | ||
+ | stats enable | ||
+ | stats hide-version | ||
+ | stats realm Haproxy\ Statistics | ||
+ | stats uri / | ||
+ | stats auth admin: | ||
+ | |||
+ | backend pxc-back | ||
+ | mode tcp | ||
+ | balance leastconn | ||
+ | option httpchk | ||
+ | server db_01 140.92.143.159: | ||
+ | server db_02 140.92.143.76: | ||
+ | server db_03 140.92.143.145: | ||
+ | |||
+ | backend pxc-onenode-back | ||
+ | mode tcp | ||
+ | balance leastconn | ||
+ | option httpchk | ||
+ | server db_01 140.92.143.159: | ||
+ | #server db_02 140.92.143.76: | ||
+ | #server db_03 140.92.143.145: | ||
+ | |||
+ | </ | ||
+ | * 啟動 HAProxy< | ||
+ | service haproxy start | ||
+ | chkconfig haproxy on | ||
+ | </ | ||
+ | * 查看 HAProxy 統計頁面 - http:// | ||
+ | * {{: | ||
+ | * 驗證 HAProxy + Percona XtraDB Cluster< | ||
+ | mysql -u root -p**password** -h 127.0.0.1 -P 13306 -e "show variables like ' | ||
+ | </ | ||
+ | +-----------------+-------+ | ||
+ | | Variable_name | ||
+ | +-----------------+-------+ | ||
+ | | wsrep_node_name | db_02 | | ||
+ | +-----------------+-------+ | ||
+ | </ | ||
+ | * 撰寫簡單驗證程式 <code sh> | ||
+ | vi t.pl</ | ||
+ | # | ||
+ | $n=10; | ||
+ | $user=' | ||
+ | $pass=' | ||
+ | $host=' | ||
+ | $port=13306; | ||
+ | $sql_cmd=" | ||
+ | |||
+ | for ($i=0; | ||
+ | $str=`mysql -u $user -p$pass -h $host -P $port -e " | ||
+ | print(" | ||
+ | } | ||
+ | </ | ||
+ | perl t.pl</ | ||
+ | [root@kvm-pxc2 ~]# perl t.pl | ||
+ | Warning: Using a password on the command line interface can be insecure. | ||
+ | Variable_name | ||
+ | wsrep_node_name db_02 | ||
+ | |||
+ | Warning: Using a password on the command line interface can be insecure. | ||
+ | Variable_name | ||
+ | wsrep_node_name db_03 | ||
+ | |||
+ | Warning: Using a password on the command line interface can be insecure. | ||
+ | Variable_name | ||
+ | wsrep_node_name db_01 | ||
+ | : | ||
+ | |||
+ | Warning: Using a password on the command line interface can be insecure. | ||
+ | Variable_name | ||
+ | wsrep_node_name db_03 | ||
+ | |||
+ | Warning: Using a password on the command line interface can be insecure. | ||
+ | Variable_name | ||
+ | wsrep_node_name db_01 | ||
+ | |||
+ | Warning: Using a password on the command line interface can be insecure. | ||
+ | Variable_name | ||
+ | wsrep_node_name db_02 | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== FAQ ===== | ||
+ | * 當發現 ct-percona1 關閉重起後, | ||
+ | ....gcs/ | ||
+ | </ | ||
+ | rm grastate.dat ib_log* | ||
+ | </ | ||
+ | * 加入一台全新的 node 時, 發現啟動後失敗無法同步原有的資料狀況時, | ||
+ | : | ||
+ | wsrep_sst_method=xtrabackup-v2 | ||
+ | : | ||
+ | </ | ||
+ | * 當發現 / | ||
+ | * 參考 - https:// | ||
+ | * 這是重要的系統資料紀錄, | ||
+ | * Exp. 保留可還原 20 天內的資料< | ||
+ | [mysqld] | ||
+ | : | ||
+ | expire-logs-days = 20 | ||
+ | : | ||
+ | </ | ||
+ | * Exp. 保留 10 個資料紀錄檔< | ||
+ | [mysqld] | ||
+ | : | ||
+ | max_binlog_files = 10 | ||
+ | : | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== 參考網址 ===== | ||
+ | * http:// | ||
+ | * http:// | ||
+ | * http:// | ||
+ | * http:// | ||
+ | * https:// | ||
+ | * http:// | ||
+ | |||
+ | {{tag> | ||