環境:
操做系統centos7.4,mysql 安裝在/data/mysql下,mysql屬於mysql用戶,pxc-5.7.19node
1.1 下載pxc-5.7.19mysql
mkdir -p /home/sunx/tools
wget -P /home/sunx/tools
https://www.percona.com/downloads/Percona-XtraDB-Cluster-57/Percona-XtraDB-Cluster-5.7.19-29.22/source/tarball/Percona-XtraDB-Cluster-5.7.19-29.22.tar.gzc++
1. 2 下載boostgit
mkdir -p /usr/local/boost
wget -P /usr/local/boost https://sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz/downloadsql
1.3 下載percona-xtrabackup-2.4.5數據庫
wget -P /home/sunx/tools https://www.percona.com/downloads/XtraBackup/Percona-XtraBackup-2.4.5/source/tarball/percona-xtrabackup-2.4.5.tar.gzvim
yum -y install libtool ncurses-devel libgcrypt-devel libev-devel \
git scons gcc gcc-c++ openssl check cmake bison boost-devel \
asio-devel libaio-devel ncurses-devel readline-devel pam-devel socat \
libaio automake autoconf vim redhat-lsb check-develcentosyum -y install libnl-devel openssl-devel libnfnetlink-devel ipvsadm \
popt-devel libnfnetlink kernel-devel popt-static iptrafapiyum -y install numactl libev perl-DBD-mysql perl-Time-HiRes服務器
配置epel庫
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
3.1 編譯安裝percona-xtrabackup
cd /usr/local/src/percona-xtrabackup-2.4.5/
cmake ./ -DBUILD_CONFIG=xtrabackup_release \
-DWITH_MAN_PAGES=OFF \
-DDOWNLOAD_BOOST=1 \
-DWITH_BOOST="/usr/local/boost"
make -j 4 && make install
3.2 編譯安裝Percona-XtraDB-Cluster-5.7.19-29.22
編譯安裝前最好要卸載掉本地的mysql數據庫
rpm-qa | grep mysql (注意大小寫,若是mysql不行就換MySQL)
yum -y remove mysql的包名
卸載後/var/lib/mysql中的數據及/etc/my.cnf不會刪除,若是肯定沒用後就手工刪除
rm -f /etc/my.cnf
rm -rf /var/lib/mysql
建立mysql用戶名及文件夾
mkdir /data/mysql
ln -s /data/mysql /usr/local/mysql
mkdir /usr/local/mysql/{binlog,slow} -p
useradd mysql
chown -R mysql:mysql /usr/local/mysql
編譯garbd,libgalera_smm.so
cd /usr/local/mysql
cd "percona-xtradb-cluster-galera"
獲取revno值
cat GALERA-REVISION
#revno="GALERA-REVISION"
scons -j4 psi=1 --config=force revno="8678538" boost_pool=0 libgalera_smm.so
scons -j4 --config=force revno="8678538" garb/garbd
建立pxc安裝目錄
mkdir -p /usr/local/mysql/{bin,lib}
拷貝編譯結果文件到pxc安裝目錄
cp garb/garbd /usr/local/mysql/bin
cp libgalera_smm.so /usr/local/mysql/lib
檢查複製的文件是否正常
ls -l /usr/local/mysql/bin/garbd
ls -l /usr/local/mysql/lib/libgalera_smm.so
-DMYSQL_SERVER_SUFFIX 值獲取
cd /usr/local/Percona-XtraDB-Cluster-5.7.19-29.22 WSREP_VERSION="$(grep WSREP_INTERFACE_VERSION wsrep/wsrep_api.h | cut -d '"' -f2).$(grep 'SET(WSREP_PATCH_VERSION' "cmake/wsrep.cmake" | cut -d '"' -f2)" echo $WSREP_VERSION
-DCOMPILATION_COMMENT 值獲取
cd /usr/local/Percona-XtraDB-Cluster-5.7.19-29.22 source VERSION MYSQL_VERSION="$MYSQL_VERSION_MAJOR.$MYSQL_VERSION_MINOR.$MYSQL_VERSION_PATCH" echo $MYSQL_VERSION REVISION="$(cd "$SOURCEDIR"; grep '^short: ' Docs/INFO_SRC |sed -e 's/short: //')" echo "Percona XtraDB Cluster binary (GPL) $MYSQL_VERSION-$WSREP_VERSION Revision $REVISION"
編譯Percona-XtraDB-Cluster-5.7.16-27.19
cd /usr/local/mysql cmake ./ -DBUILD_CONFIG=mysql_release \ -DCMAKE_BUILD_TYPE=RelWithDebInfo \ -DWITH_EMBEDDED_SERVER=OFF \ -DFEATURE_SET=community \ -DENABLE_DTRACE=OFF \ -DWITH_SSL=system -DWITH_ZLIB=system \ -DCMAKE_INSTALL_PREFIX="/usr/local/mysql" \ -DMYSQL_DATADIR="/usr/local/mysql/data" \ -DMYSQL_SERVER_SUFFIX="29.22" \ -DWITH_INNODB_DISALLOW_WRITES=ON \ -DWITH_WSREP=ON \ -DWITH_UNIT_TESTS=0 \ -DWITH_READLINE=system \ -DWITHOUT_TOKUDB=ON \ -DWITHOUT_ROCKSDB=ON \ -DCOMPILATION_COMMENT="Percona XtraDB Cluster binary (GPL) 5.7.19-29.22 Revision c10027a" \ -DWITH_PAM=ON \ -DWITH_INNODB_MEMCACHED=ON \ -DDOWNLOAD_BOOST=1 \ -DWITH_BOOST="/usr/local/boost" \ -DWITH_SCALABILITY_METRICS=ON make && make install
4.1 編譯配置文件
vim /usr/local/mysql/my.cnf [client] socket=/tmp/mysql.sock port = 3306 default-character-set = utf8 [mysqld] socket=/tmp/mysql.sock basedir = /usr/local/mysql datadir = /usr/local/mysql/data port = 3306 server_id = 10215 pid-file = /usr/local/mysql/mysql.pid key_buffer_size =128M max_allowed_packet = 16M table_open_cache = 512 max_connections = 200 sort_buffer_size = 20M read_buffer_size = 20M read_rnd_buffer_size = 8M myisam_sort_buffer_size = 64M thread_cache_size = 8 query_cache_size = 32M character-set-server=utf8 innodb_buffer_pool_size = 256M innodb_log_file_size = 48M innodb_log_buffer_size = 8M innodb_flush_log_at_trx_commit = 1 innodb_file_per_table=1 lower_case_table_names=1 innodb_temp_data_file_path = ibtmp1:12M:autoextend:max:5120M explicit_defaults_for_timestamp=TRUE sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,only_full_group_by skip-name-resolve log-bin = /usr/local/mysql/binlog/mysql-bin expire_logs_days = 7 #binlog-do-db=plateform_stable,finance_stable,location_log_stable #binlog-ignore-db=mysql,information_schema,test binlog_format=ROW log_slave_updates symbolic-links=0 default_storage_engine=InnoDB innodb_autoinc_lock_mode=2 log-error=/usr/local/mysql/data/mysqld-error.log wsrep_cluster_name = ycpxc ###指定您的羣集的邏輯名稱。 它必須對羣集中的全部節點都相同。 wsrep_provider=/usr/local/mysql/lib/libgalera_smm.so wsrep_cluster_address='gcomm://192.168.10.215,192.168.10.214,192.168.10.213'###指定羣集中節點的IP地址,節點加入羣集至少須要一個,但建議列出全部節點的地址。 wsrep_sst_receive_address=192.168.10.215###用於SST傳送的端口 wsrep_node_incoming_address=192.168.10.215###定義節點指望客戶端鏈接的IP地址和端口。 wsrep_provider_options = "gmcast.listen_addr=tcp://192.168.10.215;ist.recv_addr=192.168.10.215;" ###gmcast.listen ###來自其餘節點的地址Galera偵聽鏈接。能夠用來覆蓋默認端口傾聽,這是得到從鏈接地址。 ###ist.recv_addr ###地址監聽增量狀態傳輸。 wsrep_slave_threads= 4 wsrep_log_conflicts wsrep_node_address=192.168.10.215###集羣節點地址 wsrep_sst_method=xtrabackup-v2###默認狀況下,Percona XtraDB羣集使用Percona XtraBackup進行狀態快照傳輸(SST)。 wsrep_sst_auth=sstuser:passw0rd ###將SST的身份驗證憑據指定爲<sst_user>:<sst_pass>。 您必須建立此用戶 wsrep_log_conflicts wsrep_retry_autocommit=8 secure_file_priv="" innodb_page_cleaners=8 [mysqldump] quick max_allowed_packet = 16M [mysql] no-auto-rehash port = 3306 default-character-set = utf8 socket=/tmp/mysql.sock [myisamchk] key_buffer_size = 256M sort_buffer_size = 256M read_buffer = 2M write_buffer = 2M [mysqlhotcopy] interactive-timeout
注意:在[client]、[mysqld]、[mysql]最好都要指定sock的位置,避免初始化和啓動時報錯去找默認的配置文件。
4.2pxc環境所涉及的端口
#mysql實例端口:3306.
#pxc cluster相互通信的端口:4567
Port for group communication, default 4567. It can be changed by the option:
wsrep_provider_options ="gmcast.listen_addr=tcp://0.0.0.0:4567; "#用於SST傳送的端口:4444
Port for State Transfer, default 4444. It can be changed by the option:
wsrep_sst_receive_address=10.11.12.205:4444#用於IST傳送的端口:4568
Port for Incremental State Transfer, default port for group communication + 1 (4568). It can be changed by the option:
wsrep_provider_options = "ist.recv_addr=10.11.12.206:4568; "
4.1 爲wsrep_sst_auth傳輸建立用戶
當引導第一個節點併爲其提供必要的權限時:
mysql> CREATE USER 'sstuser'@'localhost' IDENTIFIED BY 'passw0rd';
mysql> GRANT RELOAD, LOCK TABLES, PROCESS, REPLICATION CLIENT ON . TO 'sstuser'@'localhost';
mysql> FLUSH PRIVILEGES;
5.1 初始化pxc
chown -R mysql:mysql /usr/local/mysql
/usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql --initialize
初始化錯誤能夠清空msyql/data/目錄下的文件
注意記錄密碼
5.2 啓動mysql
/usr/local/mysql/bin/mysqld --defaults-file=./my.cnf --wsrep-new-cluster &
注意主節點啓動要加上--wsrep-new-cluster,其餘節點不用。
用剛剛初始化的密碼進去MySQL
/usr/local/mysql/bin/mysql -uroot -p${mypasswd}
5.3 更改密碼
第一次進去會提示改密碼
ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
6.1 節點配置文件
192.168.10.214
安裝方式都是和主節點是同樣的,注意要卸載掉原來的mysql,更改配置文件的集羣信息和server_id 便可。
只列出差別部分:
server_id = 10214 wsrep_sst_receive_address=192.168.10.214:6615###用於SST傳送的端口 wsrep_node_incoming_address=192.168.10.214###定義節點指望客戶端鏈接的IP地址和端口。 wsrep_provider_options = "gmcast.listen_addr=tcp://192.168.10.214;ist.recv_addr=192.168.10.214:6618;" wsrep_node_address=192.168.10.214###集羣節點地址
192.168.10.213
server_id = 10213 wsrep_sst_receive_address=192.168.10.213###用於SST傳送的端口 wsrep_node_incoming_address=192.168.10.213###定義節點指望客戶端鏈接的IP地址和端口。 wsrep_provider_options = "gmcast.listen_addr=tcp://192.168.10.213;ist.recv_addr=192.168.10.213;" wsrep_node_address=192.168.10.213###集羣節點地址
6.2 啓動其餘節點
#其它節點直接啓動不用加--wsrep-new-cluster
/usr/local/mysql/bin/mysqld --defaults-file=../my.cnf
pxc集羣最少是要3臺,若是沒有,能夠使用仲裁者garbd,用來解決
能夠找在一個網段的服務器上。也能夠就在已有的節點上可是注意默認的端口不要衝突
7.1 #啓用garbd
cd /usr/local/mysql/bin/ ./garbd --group=pxc-cluster --address=gcomm://192.168.10.215,192.168.10.214 --option=gmcast.listen_addr=tcp://192.168.10.213 -d -l /tmp/garbd.log #所有起完了以後就能夠看到節點數和集羣
7.2若是要在一臺裝有pxc 的服務器上起garbd就要更改默認的pxc集羣的通訊端口
cd /usr/local/mysql/bin/ ./garbd --group=pxc-cluster --address=gcomm://192.168.10.215,192.168.10.214 --option=gmcast.listen_addr=tcp://192.168.10.214:5567 -d -l /tmp/garbd.log #所有起完了以後就能夠看到節點數和集羣
#查看MySQL 節點數 mysql> show global status like 'wsrep_cluster_size'; +--------------------+-------+ | Variable_name | Value | +--------------------+-------+ | wsrep_cluster_size | 3 | +--------------------+-------+ 1 row in set (0.00 sec)
是三個表示pxc集羣搭建成功,能夠在一個節點上建立一張表,其餘節點能夠查詢的到說明集羣運行正常。