mysql的叢集搭建

王林
發布: 2023-05-18 18:01:08
原創
3507 人瀏覽過

MySQL是一種常見的關聯式資料庫管理系統,常用於Web應用程式的資料儲存與管理。隨著資料量和並發存取量的不斷增加,單一MySQL實例的效能可能無法滿足應用程式的需求。因此,MySQL叢集成為了常見的解決方案。本文將介紹如何建構MySQL的叢集。

第一步:準備環境

在開始MySQL叢集的建置之前,我們需要準備一些環境和工具:

1.至少3台伺服器,其中一台作為管理節點(management node),其餘伺服器作為MySQL節點(MySQL node)。

2.確保伺服器之間的網路互通。

3.安裝MySQL伺服器和MySQL叢集軟體。

在本文中,我們將使用Ubuntu 18.04 LTS作業系統和MySQL Cluster 7.6.21版本進行示範。

第二步:建立管理節點

管理節點用於管理整個MySQL集群,包括建立和刪除MySQL節點、路由查詢要求等。在這個節點上,我們需要安裝兩個軟體包:MySQL Cluster Manager和MySQL Server。

1.安裝MySQL Cluster Manager

MySQL Cluster Manager是一個命令列工具,用於自動建立和設定MySQL叢集。透過以下指令安裝:

sudo apt-get update
sudo apt-get install mysql-cluster-manager

2.安裝MySQL Server

#在管理節點上,我們需要安裝MySQL Server,以便MySQL Cluster Manager可以管理MySQL叢集。透過以下命令安裝:

sudo apt-get install mysql-server

#3.建立MySQL Cluster設定

在管理節點上,建立MySQL Cluster設定文件,並指定節點位址和連接埠號。在這個範例中,我們假設管理節點的IP位址是192.168.1.100,連接埠號碼是1186。

建立一個名為mycluster.ini的文件,並指定節點位址和連接埠號碼:

[NDB_MGMD DEFAULT]

Port the management server listens on

PortNumber=1186

[NDB_MGMD]

IP address of the management server node

HostName=192.168.1.100

[MYSQLD DEFAULT]
[NDBD DEFAULT]

[NDBD]

IP address of the first ndb node

HostName=192.168.1.101

#[NDBD]

IP address of the second ndb node

HostName=192.168.1.102

4.啟動管理節點

透過下列指令啟動管理節點:

#ndb_mgmd -f mycluster.ini --configdir=/var/lib/mysql-cluster

此時,MySQL Cluster Manager會自動建立並設定MySQL叢集。您可以使用以下指令檢查MySQL叢集的狀態:

ndb_mgm
show

#第三個步驟:建立MySQL節點

MySQL節點是實際儲存和管理資料的伺服器.在這個範例中,我們將建立兩個MySQL節點。

1.安裝MySQL Cluster軟體

安裝MySQL Cluster軟體之前,請確保在您的伺服器上安裝了必要的依賴項和程式庫。可以使用以下指令安裝:

sudo apt-get install libaio1 libmecab2

然後下載MySQL Cluster二進位套件:

wget https://dev.mysql.com/get /Downloads/MySQL-Cluster-7.6/mysql-cluster_7.6.21-1ubuntu18.04_amd64.deb-bundle.tar

#解壓縮下載的套件:

##tar -xvf mysql-cluster_

# 21- 1ubuntu18.04_amd64.deb-bundle.tar

進入解壓後的目錄:

cd mysql-cluster_7.6.21-1ubuntu18.04_amd64.deb-bundle

執行以下命令安裝MySQL Cluster:

sudo dpkg -i mysql-common_7.6.21-1ubuntu18.04_amd64.deb
sudo dpkg -i mysql-cluster-client_7.6.21-1ubuntu18.04_amsql-cluster-client_7.6.21-1ubuntu18.04_am64. -i mysql-cluster-server_7.6.21-1ubuntu18.04_amd64.deb
sudo dpkg -i mysql-cluster-client-core_7.6.21-1ubuntu18.04_amd64.deb-#7.6.21-1ubuntu18.04_amd64.deb#a-##sudo-kg -i sql-uster-#my-#a-#-#i> 。 04_amd64.deb

2.設定MySQL節點

在MySQL節點上,我們需要修改MySQL設定檔my.cnf,並指定節點對應的管理節點和連接埠號碼。

開啟my.cnf檔案:

sudo nano /etc/mysql/my.cnf

在檔案結尾,新增以下設定:

[mysqld ]

ndbcluster

ndb-connectstring=192.168.1.100

3.啟動MySQL節點

透過下列指令啟動MySQL節點:

sudo systemctl start mysql

4.加入MySQL叢集

在MySQL節點啟動後,MySQL Cluster Manager會自動監控並管理MySQL節點。您可以使用下列指令檢查MySQL叢集的狀態:

ndb_mgm

show

#此時,您可以在MySQL節點上建立和管理資料庫。


第四步:測試MySQL叢集

為確保MySQL叢集正常運作,請在其中一個MySQL節點上建立資料庫,並將其資料表和資料擴展到其他節點。您可以使用下列指令建立一個testdb資料庫:

mysql -uroot -p

create database testdb;

然後,為該資料庫建立一個表格:


use testdb;

create table user(name VARCHAR(20), age INT);

最後,在一個節點上插入一些資料:


insert into user values('John', 18);

Then, use another MySQL node to check if the table exists:

use testdb;
select * from user;

If you can retrieve and read the data normally, then MySQL cluster has been successfully set up.

Summary

MySQL Cluster can greatly improve the performance and reliability of applications. Through the combination of management nodes and multiple MySQL nodes, automatic distribution and replication of data can be achieved, providing high availability and fault tolerance. In the above steps, we introduced how to build a MySQL cluster and conducted some tests and verifications. Depending on the specific application scenario, more configuration and optimization may be required. However, these steps can serve as a starting guide to help you get started learning and using MySQL Cluster.

以上是mysql的叢集搭建的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!