Percona XtraDB Cluster 如何在一臺服務器上安裝兩個集群節點
我認為在單個物理服務器上運行2個或多個Percona XtraDB Cluster(PXC)節點這樣沒有什么意義,除了教育和測試目的,但在這種情況下這樣做仍然是有用的。最受歡迎的實現方式似乎是服務器的虛擬化,比如利用流浪盒子。但是同樣的方式你可以運行多個MySQL實例在并行操作系統級別上,還有并發的mysqld的形成過程,因此你也可以有多個Percona XtraDB Cluster節點。而且實現這一目標的方法是恰恰相同的:使用專用的datadirs和為每個節點設置不同的端口。
哪個端口?
Pecona XtraDB Cluster 使用 4 個 TCP 端口:
常規的MySQL端口(默認3306)
(Galera)(默認4567)
狀態傳輸端口(默認4444)
增量狀態傳輸端口(默認是:組通信端口(4567)+ 1 = 4568)
</ul>
當然,當你在同一臺服務器上有多個實例的默認值,并不適用于所有人,所以我們需要為其他實例定義新的端口,確保本地防火墻對他們是開放的,如果有一個活動(iptables,selinux,…)。
安裝Percona XtraDB 集群,配置并啟動第一個節點
我的測試服務器用的是一個全新的CentOS(社區企業操作系統)6.5 版,系統安裝了Percona yum 工具,通過工具我安裝了最新的Percona XtraDB集群(5.6.20-25.7.888.el6版本);注意:你可能需要安裝EPEL(企業版Linux額外包)和socat(Socket CAT)工具,這兩個工具是獨立的(見bug)。 為了避免沖突,我已經停止了mysql服務的自啟動:
chkconfig --level 3 mysql off chkconfig --del mysql
我原本計劃從壓縮包中安裝PXC(Percona XtraDB Cluster),但是后來我決定通過yum工具進行安裝,這樣可以自動下載所有依賴包。 這是我最初的/etc/my.cnf 文件(注意默認值的使用):
[mysqld] datadir = /var/lib/mysql port=3306 socket=/var/lib/mysql/mysql-node1.sock pid-file=/var/lib/mysql/mysql-node1.pid log-error=/var/lib/mysql/mysql-node1.err binlog_format=ROW innodb_autoinc_lock_mode=2 wsrep_provider=/usr/lib64/libgalera_smm.so wsrep_cluster_name = singlebox wsrep_node_name = node1 wsrep_cluster_address=gcomm://
我使用下面的命令手動啟動了一個節點上的集群引導程序:
$ mysqld_safe --defaults-file=/etc/my.cnf --wsrep-new-cluster
啟動后,你應當可以通過本地接口訪問該節點:
$ mysql -S /var/lib/mysql/mysql-node1.sock
配置和啟動第二個節點
然后,我創建了一個類似的第二個實例配置文件的配置,我叫/etc/my2.cnf,有以下修改:
[mysqld] datadir = /var/lib/mysql2 port=3307 socket=/var/lib/mysql2/mysql-node2.sock pid-file=/var/lib/mysql2/mysql-node2.pid log-error=/var/lib/mysql2/mysql-node2.err binlog_format=ROW innodb_autoinc_lock_mode=2 wsrep_provider=/usr/lib64/libgalera_smm.so wsrep_cluster_name = singlebox wsrep_node_name = node2 wsrep_cluster_address=gcomm://127.0.0.1:4567,127.0.0.1:5020 wsrep_provider_options = "base_port=5020;"
注意使用base_port:通過它定義的,5020端口是用于組通信和5020(上面)為IST保留著(一樣簡單的使用gmcast.listen_addr =tcp:/ / 127.0.0.1:5021)。
您需要在這第二個實例中為datadir創建和設置正確的權限,否則MySQL無法創建一些文件(像.pid和.err),雖然你不需要運行mysql_install_db腳本:
$ chown -R mysql:mysql /var/lib/mysql2
然后,您可以用以下命令啟動第二個實例:
$ mysqld_safe --defaults-file=/etc/my2.cnf
當開始時,通過看日志來觀察這第二個節點開始,與主節點間的通信和加入集群。從一開始的實例在不同的終端上執行:
$ tail -f /var/log/mysql2/mysql-node2.err
記住,任何時候都可以使用mysqladmin停止節點,您只需要提供正確的套接字作為參數,如:
$ mysqladmin -S /var/lib/mysql/mysql-node1.sock shutdown
最后,一旦你有整個集群,你應該編輯my.cnf中的第一節點與一個完整的wsrep_cluster_addres,在/etc/my2.cnf上面顯示。
Using mysqld_multi
My last blog post was on running multiple instances of MySQL with myslqd_multi. It applies here as well, the only exception is that you need to make sure to use “wsrep_cluster_address=gcomm://” in the first node whenever you bootstrap the cluster – and pay attention to start it before the other nodes.
The only advantage I see in using mysqld_multi is facilitating the management (start/stop) of the nodes and concentrating all configuration in a single my.cnf file. In any case, you shouldn’t be running a PXC cluster in a single box for any purpose other than educational.
Adding a second Percona XtraDB Cluster node to a production server
What if you have a production cluster composed of multiple physical servers and you want to add a second node to one of them? It works the same way – you’ll just need to use the server’s IP address when configuring it instead of the loopback network interface. Here’s an example of a PXC cluster composed initially by three nodes: 192.168.70.1, 192.168.70.2, and 192.168.70.3. I’ve added a 4th node running on the server that is already hosting the 3rd – the wsrep_cluster_address line looks like as follows after the changes:
wsrep_cluster_address = gcomm://192.168.70.1,192.168.70.2,192.168.70.3:4567,192.168.70.3:5020
Additional ressources
We have a documentation page on “How to setup 3 node cluster on single box” that contains more details of what I’ve covered above with a slightly different approach.