docker-compose搭建percona-xtradb-cluster8.0

fqydhk發表於2021-01-03

交流QQ號:555913397
有什麼問題可以加群大家一起交流

搭建percona-xtradb-cluster8.0

百度mysql-pxc叢集,都是5.7版本,對於8.0版本一個都沒搜到,照著5.7的教程搭建,完全搭建不了,於是跑到官網學習如何搭建,現將搭建成功案例分享給大家,不敢藏私.

1.建立ca證書

# 使用openssl建立CA證書
openssl genrsa 2048 > ca-key.pem
openssl req -new -x509 -nodes -days 3600 \
        -key ca-key.pem -out ca.pem
#這裡讓填寫一些基本資料
# Country Name (2 letter code) [AU]:CN
# State or Province Name (full name) [Some-State]:.
# Locality Name (eg, city) []:
# Organization Name (eg, company) [Internet Widgits Pty Ltd]:MySQL AB
# Organizational Unit Name (eg, section) []:
# Common Name (eg, YOUR name) []:MySQL admin
# Email Address []:

#建立伺服器證書
# server-cert.pem = 公鑰, server-key.pem = 私鑰
openssl req -newkey rsa:2048 -days 3600 \
        -nodes -keyout server-key.pem -out server-req.pem
#這裡讓填寫一些基本資料
# Country Name (2 letter code) [AU]:CN
# State or Province Name (full name) [Some-State]:.
# Locality Name (eg, city) []:
# Organization Name (eg, company) [Internet Widgits Pty Ltd]:MySQL AB
# Organizational Unit Name (eg, section) []:
# Common Name (eg, YOUR name) []:MySQL server
# Email Address []:
#
# Please enter the following 'extra' attributes
# to be sent with your certificate request
# A challenge password []:
# An optional company name []:

openssl rsa -in server-key.pem -out server-key.pem
openssl x509 -req -in server-req.pem -days 3600 \
        -CA ca.pem -CAkey ca-key.pem -set_serial 01 -out server-cert.pem

# 建立客戶端證書
# client-cert.pem = 公鑰, client-key.pem = 私鑰
openssl req -newkey rsa:2048 -days 3600 \
        -nodes -keyout client-key.pem -out client-req.pem
#這裡讓填寫一些基本資料
# Country Name (2 letter code) [AU]:CN
# State or Province Name (full name) [Some-State]:.
# Locality Name (eg, city) []:
# Organization Name (eg, company) [Internet Widgits Pty Ltd]:MySQL AB
# Organizational Unit Name (eg, section) []:
# Common Name (eg, YOUR name) []:MySQL server
# Email Address []:
#
# Please enter the following 'extra' attributes
# to be sent with your certificate request
# A challenge password []:
# An optional company name []:
openssl rsa -in client-key.pem -out client-key.pem
openssl x509 -req -in client-req.pem -days 3600 \
        -CA ca.pem -CAkey ca-key.pem -set_serial 01 -out client-cert.pem

2.建立完證書後可使用一下命令驗證

openssl verify -CAfile ca.pem server-cert.pem client-cert.pem
#如果顯示以下內容表示一切麼問題
server-cert.pem: OK
client-cert.pem: OK

3.建立cert.cnf檔案

ssl-ca = /cert/ca.pem
ssl-cert = /cert/server-cert.pem
ssl-key = /cert/server-key.pem

[client]
ssl-ca = /cert/ca.pem
ssl-cert = /cert/client-cert.pem
ssl-key = /cert/client-key.pem

[sst]
encrypt = 4
ssl-ca = /cert/ca.pem
ssl-cert = /cert/server-cert.pem
ssl-key = /cert/server-key.pem

4.一切準備就緒,下面開始寫docker-compose檔案

/home/StaticFile/mysql/cert 存放生成的證書檔案
/home/StaticFile/mysql/config 存放建立的cert.cnf檔案
/home/StaticFile/mysql/data mysql的資料檔案,此檔案需要chmod 777許可權否則無法執行容器
master節點
#這是master節點
version: '3.6'
services:
 mysql-pxc1:
  restart: always
  image: percona/percona-xtradb-cluster:8.0
  network_mode: host
  container_name: mysql-pxc1
  environment: # 環境變數
      - TZ=Asia/Shanghai
      - MYSQL_ROOT_PASSWORD=123456
      - CLUSTER_NAME=pxc
  ports:
    - "3306:3306"
    - "4444:4444"
    - "4567:4567"
    - "4568:4568"
  volumes:
   - /home/StaticFile/mysql/data:/var/lib/mysql/
   - /home/StaticFile/mysql/cert:/cert/
   - /home/StaticFile/mysql/config:/etc/percona-xtradb-cluster.conf.d
  tty: true
  privileged: true # 擁有容器內命令執行的許可權
slave1節點
version: '3.6'
services:
 mysql-pxc2:
  restart: always
  image: percona/percona-xtradb-cluster:8.0
  network_mode: host
  container_name: mysql-pxc2
  environment: # 環境變數
      - TZ=Asia/Shanghai
      - MYSQL_ROOT_PASSWORD=123456
      - CLUSTER_NAME=pxc
      - CLUSTER_JOIN=mysql-pxc1
  ports:
    - "3306:3306"
    - "4444:4444"
    - "4567:4567"
    - "4568:4568"
  volumes:
   - /home/StaticFile/mysql/data:/var/lib/mysql
   - /home/StaticFile/mysql/cert:/cert
   - /home/StaticFile/mysql/config:/etc/percona-xtradb-cluster.conf.d
  tty: true
  privileged: true # 擁有容器內命令執行的許可權
slave2節點
version: '3.6'
services:
 mysql-pxc3:
  restart: always
  image: percona/percona-xtradb-cluster:8.0
  network_mode: host
  container_name: mysql-pxc3
  environment: # 環境變數
      - TZ=Asia/Shanghai
      - MYSQL_ROOT_PASSWORD=123456
      - CLUSTER_NAME=pxc
      - CLUSTER_JOIN=mysql-pxc1
  ports:
    - "3306:3306"
    - "4444:4444"
    - "4567:4567"
    - "4568:4568"
  volumes:
   - /home/StaticFile/mysql/data:/var/lib/mysql
   - /home/StaticFile/mysql/cert:/cert
   - /home/StaticFile/mysql/config:/etc/percona-xtradb-cluster.conf.d
  tty: true
  privileged: true # 擁有容器內命令執行的許可權

相關文章